Browse Source

* Fixed race condition in back

master
vvandenb 2 years ago
parent
commit
41d75d764e
  1. 21
      back/volume/src/pong/game/Game.ts
  2. 2
      back/volume/src/pong/game/Games.ts
  3. 2
      back/volume/src/pong/pong.gateway.ts

21
back/volume/src/pong/game/Game.ts

@ -109,17 +109,18 @@ export class Game {
} }
} }
async stop (): Promise<void> { stop (): void {
if (this.timer !== null) { if (this.timer !== null && this.playing) {
await this.pongService.saveResult(this.players, this.ranked) this.playing = false
if (this.players.length !== 0) {
this.gameStoppedCallback(this.players[0].name)
}
clearInterval(this.timer) clearInterval(this.timer)
this.timer = null this.timer = null
this.players = [] this.pongService.saveResult(this.players, this.ranked).then(() => {
this.playing = false this.gameStoppedCallback(this.players[0].name)
this.players = []
}).catch(() => {
this.gameStoppedCallback(this.players[0].name)
this.players = []
})
} }
} }
@ -161,7 +162,7 @@ export class Game {
this.players[indexPlayerScored].score += 1 this.players[indexPlayerScored].score += 1
if (this.players[indexPlayerScored].score >= DEFAULT_WIN_SCORE) { if (this.players[indexPlayerScored].score >= DEFAULT_WIN_SCORE) {
console.log(`${this.players[indexPlayerScored].name} won`) console.log(`${this.players[indexPlayerScored].name} won`)
void this.stop() this.stop()
} }
} }

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

@ -109,7 +109,7 @@ export class Games {
async leaveGame (name: string): Promise<void> { async leaveGame (name: string): Promise<void> {
const game: Game | undefined = this.playerGame(name) const game: Game | undefined = this.playerGame(name)
if (game !== undefined && !game.ranked) { if (game !== undefined && !game.ranked) {
await game.stop() game.stop()
this.deleteGame(name) this.deleteGame(name)
} }
} }

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

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

Loading…
Cancel
Save