From 6ffc97f8e8f1e7f01afb423e0859aa3969cd823e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Garapich?= Date: Wed, 11 Aug 2021 01:22:26 +0200 Subject: [PATCH] fix(games): free players after a game is force-ended --- src/games/services/game-runtime.service.spec.ts | 12 ++++++++++++ src/games/services/game-runtime.service.ts | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/games/services/game-runtime.service.spec.ts b/src/games/services/game-runtime.service.spec.ts index c540d58f5..af6c5a29c 100644 --- a/src/games/services/game-runtime.service.spec.ts +++ b/src/games/services/game-runtime.service.spec.ts @@ -181,6 +181,18 @@ describe('GameRuntimeService', () => { expect(releaseSpy).toHaveBeenCalledWith(mockGameServer.id); }); + it('should free the players', async () => { + await service.forceEnd(mockGame.id); + const players = await Promise.all( + mockGame.slots + .map((s) => s.player) + .map((playerId) => playersService.getById(playerId)), + ); + expect(players.every((player) => player.activeGame === undefined)).toBe( + true, + ); + }); + it('should emit the gameChanges event', async () => new Promise((resolve) => { events.gameChanges.subscribe(({ game, adminId }) => { diff --git a/src/games/services/game-runtime.service.ts b/src/games/services/game-runtime.service.ts index ee169a678..59479dd5c 100644 --- a/src/games/services/game-runtime.service.ts +++ b/src/games/services/game-runtime.service.ts @@ -74,6 +74,16 @@ export class GameRuntimeService { this.events.gameChanges.next({ game, adminId }); this.events.substituteRequestsChange.next(); + await Promise.all( + game.slots + .map((slot) => slot.player) + .map((playerId) => + this.playersService.updatePlayer(playerId.toString(), { + $unset: { activeGame: 1 }, + }), + ), + ); + if (game.gameServer) { await this.cleanupServer(game.gameServer.toString()); }