Browse Source

* Fixed pong backend error

master
vvandenb 2 years ago
parent
commit
7af91fce1f
  1. 32
      back/volume/src/pong/game/Games.ts
  2. 8
      back/volume/src/pong/pong.gateway.ts

32
back/volume/src/pong/game/Games.ts

@ -46,22 +46,22 @@ export class Games {
} }
removePlayer (name: string): void { removePlayer (name: string): void {
const game: Game | null = this.playerGame(name) const game: Game | undefined = this.playerGame(name)
if (game !== null) { if (game !== undefined) {
game.removePlayer(name) game.removePlayer(name)
} }
} }
ready (name: string): void { ready (name: string): void {
const game: Game | null = this.playerGame(name) const game: Game | undefined = this.playerGame(name)
if (game !== null) { if (game !== undefined) {
game.ready(name) game.ready(name)
} }
} }
private gameStopped (name: string): void { private gameStopped (name: string): void {
const game: Game | null = this.playerGame(name) const game: Game | undefined = this.playerGame(name)
if (game !== null) { if (game !== undefined) {
this.games.splice(this.games.indexOf(game), 1) this.games.splice(this.games.indexOf(game), 1)
game.players.forEach((player) => { game.players.forEach((player) => {
this.playerNameToGameIndex.delete(player.name) this.playerNameToGameIndex.delete(player.name)
@ -71,8 +71,8 @@ export class Games {
} }
getGameInfo (name: string): GameInfo { getGameInfo (name: string): GameInfo {
const game: Game | null = this.playerGame(name) const game: Game | undefined = this.playerGame(name)
if (game !== null) { if (game !== undefined) {
return game.getGameInfo(name) return game.getGameInfo(name)
} }
return { return {
@ -88,8 +88,8 @@ export class Games {
} }
movePlayer (name: string | undefined, position: Point): void { movePlayer (name: string | undefined, position: Point): void {
const game: Game | null = this.playerGame(name) const game: Game | undefined = this.playerGame(name)
if (game !== null) { if (game !== undefined) {
game.movePaddle(name, position) game.movePaddle(name, position)
} }
} }
@ -99,13 +99,11 @@ export class Games {
return this.playerNameToGameIndex.get(name) !== undefined return this.playerNameToGameIndex.get(name) !== undefined
} }
playerGame (name: string | undefined): Game | null { playerGame (name: string | undefined): Game | undefined {
if (name === undefined) return null const game: Game | undefined = this.games.find((game) =>
const gameIndex: number | undefined = this.playerNameToGameIndex.get(name) game.players.some((player) => player.name === name)
if (gameIndex !== undefined) { )
return this.games[gameIndex] return game
}
return null
} }
spectateGame ( spectateGame (

8
back/volume/src/pong/pong.gateway.ts

@ -56,15 +56,13 @@ export class PongGateway implements OnGatewayConnection, OnGatewayDisconnect {
client: WebSocketWithId client: WebSocketWithId
): void { ): void {
const name: string | undefined = this.socketToPlayerName.get(client) const name: string | undefined = this.socketToPlayerName.get(client)
const game: Game | null = this.games.playerGame(name) const game: Game | undefined = this.games.playerGame(name)
console.log(game) if (game !== undefined && game.isPlaying()) {
console.log(game?.isPlaying())
if (game !== null && game.isPlaying()) {
void game.stop() void game.stop()
} }
if (name !== undefined) { if (name !== undefined) {
this.socketToPlayerName.delete(client)
console.log('Disconnected ', this.socketToPlayerName.get(client)) console.log('Disconnected ', this.socketToPlayerName.get(client))
this.socketToPlayerName.delete(client)
} }
} }

Loading…
Cancel
Save