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 {
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 (

8
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)
}
}

Loading…
Cancel
Save