(user: User)
| 50 | } |
| 51 | |
| 52 | private addHandler(user: User) { |
| 53 | user.socket.on('message', async (data) => { |
| 54 | const message = JSON.parse(data.toString()); |
| 55 | if (message.type === INIT_GAME) { |
| 56 | if (this.pendingGameId) { |
| 57 | const game = this.games.find((x) => x.gameId === this.pendingGameId); |
| 58 | if (!game) { |
| 59 | console.error('Pending game not found?'); |
| 60 | return; |
| 61 | } |
| 62 | if (user.userId === game.player1UserId) { |
| 63 | socketManager.broadcast( |
| 64 | game.gameId, |
| 65 | JSON.stringify({ |
| 66 | type: GAME_ALERT, |
| 67 | payload: { |
| 68 | message: 'Trying to Connect with yourself?', |
| 69 | }, |
| 70 | }), |
| 71 | ); |
| 72 | return; |
| 73 | } |
| 74 | socketManager.addUser(user, game.gameId); |
| 75 | await game?.updateSecondPlayer(user.userId); |
| 76 | this.pendingGameId = null; |
| 77 | } else { |
| 78 | const game = new Game(user.userId, null); |
| 79 | this.games.push(game); |
| 80 | this.pendingGameId = game.gameId; |
| 81 | socketManager.addUser(user, game.gameId); |
| 82 | socketManager.broadcast( |
| 83 | game.gameId, |
| 84 | JSON.stringify({ |
| 85 | type: GAME_ADDED, |
| 86 | gameId:game.gameId, |
| 87 | }), |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (message.type === MOVE) { |
| 93 | const gameId = message.payload.gameId; |
| 94 | const game = this.games.find((game) => game.gameId === gameId); |
| 95 | if (game) { |
| 96 | game.makeMove(user, message.payload.move); |
| 97 | if (game.result) { |
| 98 | this.removeGame(game.gameId); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if (message.type === EXIT_GAME){ |
| 104 | const gameId = message.payload.gameId; |
| 105 | const game = this.games.find((game) => game.gameId === gameId); |
| 106 | |
| 107 | if (game) { |
| 108 | game.exitGame(user); |
| 109 | this.removeGame(game.gameId) |
no test coverage detected