Skip to content

Commit

Permalink
fix(games): free players after a game is force-ended
Browse files Browse the repository at this point in the history
  • Loading branch information
garrappachc committed Aug 10, 2021
1 parent daff935 commit 6ffc97f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/games/services/game-runtime.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((resolve) => {
events.gameChanges.subscribe(({ game, adminId }) => {
Expand Down
10 changes: 10 additions & 0 deletions src/games/services/game-runtime.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 6ffc97f

Please sign in to comment.