From 7af91fce1fdbb409b0371b4e54ce2314d343b463 Mon Sep 17 00:00:00 2001 From: vvandenb Date: Mon, 6 Mar 2023 17:33:34 +0100 Subject: [PATCH] * Fixed pong backend error --- back/volume/src/pong/game/Games.ts | 32 +++++++++++++--------------- back/volume/src/pong/pong.gateway.ts | 8 +++---- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/back/volume/src/pong/game/Games.ts b/back/volume/src/pong/game/Games.ts index 5ac910b..da1458a 100644 --- a/back/volume/src/pong/game/Games.ts +++ b/back/volume/src/pong/game/Games.ts @@ -46,22 +46,22 @@ export class Games { } removePlayer (name: string): void { - const game: Game | null = this.playerGame(name) - if (game !== null) { + const game: Game | undefined = this.playerGame(name) + if (game !== undefined) { game.removePlayer(name) } } ready (name: string): void { - const game: Game | null = this.playerGame(name) - if (game !== null) { + const game: Game | undefined = this.playerGame(name) + if (game !== undefined) { game.ready(name) } } private gameStopped (name: string): void { - const game: Game | null = this.playerGame(name) - if (game !== null) { + const game: Game | undefined = this.playerGame(name) + if (game !== undefined) { this.games.splice(this.games.indexOf(game), 1) game.players.forEach((player) => { this.playerNameToGameIndex.delete(player.name) @@ -71,8 +71,8 @@ export class Games { } getGameInfo (name: string): GameInfo { - const game: Game | null = this.playerGame(name) - if (game !== null) { + const game: Game | undefined = this.playerGame(name) + if (game !== undefined) { return game.getGameInfo(name) } return { @@ -88,8 +88,8 @@ export class Games { } movePlayer (name: string | undefined, position: Point): void { - const game: Game | null = this.playerGame(name) - if (game !== null) { + const game: Game | undefined = this.playerGame(name) + if (game !== undefined) { game.movePaddle(name, position) } } @@ -99,13 +99,11 @@ export class Games { return this.playerNameToGameIndex.get(name) !== undefined } - playerGame (name: string | undefined): Game | null { - if (name === undefined) return null - const gameIndex: number | undefined = this.playerNameToGameIndex.get(name) - if (gameIndex !== undefined) { - return this.games[gameIndex] - } - return null + playerGame (name: string | undefined): Game | undefined { + const game: Game | undefined = this.games.find((game) => + game.players.some((player) => player.name === name) + ) + return game } spectateGame ( diff --git a/back/volume/src/pong/pong.gateway.ts b/back/volume/src/pong/pong.gateway.ts index 7b93299..5d2c92a 100644 --- a/back/volume/src/pong/pong.gateway.ts +++ b/back/volume/src/pong/pong.gateway.ts @@ -56,15 +56,13 @@ export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect { client: WebSocketWithId ): void { const name: string | undefined = this.socketToPlayerName.get(client) - const game: Game | null = this.games.playerGame(name) - console.log(game) - console.log(game?.isPlaying()) - if (game !== null && game.isPlaying()) { + const game: Game | undefined = this.games.playerGame(name) + if (game !== undefined && game.isPlaying()) { void game.stop() } if (name !== undefined) { - this.socketToPlayerName.delete(client) console.log('Disconnected ', this.socketToPlayerName.get(client)) + this.socketToPlayerName.delete(client) } }