Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. const io = require('socket.io');
  2.  
  3. export class WebSocketServer {
  4. public io: any;
  5.  
  6. public games: Mesa[] = [];
  7.  
  8. public init = (server: any) => {
  9.  
  10. this.io = io.listen(server);
  11. this.io.sockets.on('connection', (client: any) => {
  12.  
  13. client.player = new Player();
  14.  
  15. client.emit('players', Date.now() + ': Welcome to Sueca Online');
  16.  
  17. client.broadcast.emit('players', Date.now() + ': A new player has arrived');
  18.  
  19. client.on('lobby-chat', (data) => this.io.emit('lobby-chat', data));
  20. client.on('room-chat', (data) => this.io.to(server.user.room).emit('room-chat', data));
  21.  
  22.  
  23.  
  24. client.on('room', (data) => {
  25. console.log("One room was created" + data.room);
  26. client.join(data.room); // o utilizador junta-se ao room que criou
  27.  
  28. client.player.username = data.username;
  29. client.player.id = data.userId;
  30. client.player.gameRoom = data.room;
  31. client.player.socketId = client.id;
  32.  
  33. this.games[data.room] = new Mesa();
  34. this.games[data.room].gameRoom = data.room;
  35. this.games[data.room].gamers.push(data.username);
  36. })
  37.  
  38. client.on('join', (data) => {
  39. console.log("One player joined the room " + data.room);
  40. client.player.gameRoom = data.room;
  41. client.player.socketId = data.id;
  42. client.player.username = data.username;
  43. client.join(client.player.gameRoom);
  44.  
  45. this.games[data.room].gamers.push(data.username);
  46. })
  47.  
  48. client.on('start-game', (data) => {
  49. console.log("Game will start" + data.room + " sdad " + client.player.gameRoom);
  50. this.io.to(client.player.gameRoom).emit('game-start', client.player.gameRoom);
  51. console.log('GAME WILL START ->' + client.player.gameRoom)
  52. this.io.emit(client.player.gameRoom).emit('game-start', client.player.gameRoom);
  53. this.games[data.room].gamers.forEach((player: any) => {
  54. console.log(player);
  55. });
  56. });
  57.  
  58. client.on('players-on-game', (data) => {
  59. this.games[data.room].gamers.forEach((player: any) => {
  60. this.io.to(client.player.gameRoom).emit('players-on-game', player);
  61. });
  62. })
  63.  
  64. client.on('my-cards',(data) => {
  65. this.games[data.room].cards
  66. })
  67.  
  68.  
  69. });
  70. };
  71.  
  72. public notifyAll = (channel: string, message: any) => {
  73. this.io.sockets.emit(channel, message);
  74. };
  75. };
  76.  
  77. export class Player {
  78. public username: string;
  79. public id: number;
  80. public gameRoom: string;
  81. public socketId: string;
  82.  
  83. }
  84. export class Mesa {
  85.  
  86. public gameRoom: string;
  87.  
  88. public gamers: string[] = [];
  89.  
  90. public cards: Card[];
  91.  
  92. constructor() {
  93. this.gameRoom = '';
  94. this.gamers = [];
  95. this.cards = [];
  96.  
  97. Mesa.todosOsNaipes().forEach(naipe => {
  98. Mesa.todosOsSimbolos().forEach(simbolo => {
  99. let c: Card = null;
  100. let img = '../../cards-1/' + naipe + simbolo + ".png"
  101. switch (simbolo) {
  102. case 1: c = new Card(naipe, simbolo, 11, img);
  103. break;
  104. case 7: c = new Card(naipe, simbolo, 10, img);
  105. break;
  106. case 13: c = new Card(naipe, simbolo, 4, img);
  107. break;
  108. case 11: c = new Card(naipe, simbolo, 3, img);
  109. break;
  110. case 12: c = new Card(naipe, simbolo, 2, img);
  111. break;
  112. default: c = new Card(naipe, simbolo, 0, img);
  113. }
  114. this.cards.push(c);
  115. });
  116. });
  117. }
  118.  
  119. public getCard(naipe: string, simbolo: number): Card {
  120.  
  121. for (let i = 0; i < this.cards.length; i++) {
  122. if (this.cards[i].tipoCard == naipe && this.cards[i].simbolo == simbolo) {
  123. return this.cards[i];
  124. }
  125. }
  126.  
  127. return;
  128. }
  129.  
  130. public static todosOsNaipes(): string[] {
  131. return ['o', 'e', 'p', 'c'];
  132. }
  133.  
  134. public static todosOsSimbolos(): number[] {
  135. return [1, 2, 3, 4, 5, 6, 7, 11, 12, 13];
  136. }
  137. }
  138.  
  139. export class Card {
  140. private _tipoCard: string;
  141. private _simbolo: number;
  142. private _isAval: boolean;
  143. private _ponto: number;
  144. private _img: string;
  145.  
  146. public constructor(tipo: string, id: number, pontos: number, img: string) {
  147. this._tipoCard = tipo;
  148. this._simbolo = id;
  149. this._isAval = true;
  150. this._ponto = pontos;
  151. this._img = img;
  152. }
  153.  
  154. public toString() {
  155. return "Naipe: " + this._tipoCard + " Simbolo: " + this._simbolo + " Pontos: " + this._ponto + "\n";
  156. }
  157.  
  158.  
  159. get tipoCard(): string {
  160. return this._tipoCard;
  161. }
  162.  
  163. set tipoCard(value: string) {
  164. this._tipoCard = value;
  165. }
  166.  
  167. get simbolo(): number {
  168. return this._simbolo;
  169. }
  170.  
  171. set simbolo(value: number) {
  172. this._simbolo = value;
  173. }
  174.  
  175. get isAval(): boolean {
  176. return this._isAval;
  177. }
  178.  
  179. set isAval(value: boolean) {
  180. this._isAval = value;
  181. }
  182.  
  183. get ponto(): number {
  184. return this._ponto;
  185. }
  186.  
  187. set ponto(value: number) {
  188. this._ponto = value;
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement