From 36a9a2e4430f96c7e5b2591eb95b03f9adc4a77c Mon Sep 17 00:00:00 2001 From: legonzaur Date: Tue, 27 Aug 2024 13:08:33 +0200 Subject: [PATCH] Fix : surrender and kick --- src/games/controllers/games.controller.ts | 21 ++++++++++++++++----- src/games/services/games.service.ts | 16 ++++++++++++---- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/games/controllers/games.controller.ts b/src/games/controllers/games.controller.ts index 01fee7c..e111aaa 100644 --- a/src/games/controllers/games.controller.ts +++ b/src/games/controllers/games.controller.ts @@ -77,7 +77,7 @@ export class GamesController { await this.gameService.endTurn(game); } - @Post(':id/leave') + @Post('leave') @ApiOperation({ summary: "Leave a game that hasn't already started" }) @UseGuards(AuthGuard) async leave( @@ -91,7 +91,21 @@ export class GamesController { return await this.gameService.leaveGame(game, session.user); } - @Post(':id/kick/:playerId') + @Post('surrender') + @ApiOperation({ summary: "Leave a game that hasn't already started" }) + @UseGuards(AuthGuard) + async surrender( + @Param('id') id: string, + @Session() session: Record, + @Param('id', GamePipe) game: Document & Game, + ) { + if (!game.started) { + throw new HttpException('Game has not started', HttpStatus.BAD_REQUEST); + } + return await this.gameService.leaveGame(game, session.user); + } + + @Post('kick/:playerId') @ApiOperation({ summary: 'Kick a player' }) @UseGuards(AuthGuard) async kick( @@ -100,9 +114,6 @@ export class GamesController { @Param('id', GamePipe) game: Document & Game, @Param('playerId') playerId: string, ) { - if (game.started) { - throw new HttpException('Game already started', HttpStatus.BAD_REQUEST); - } if (session.user.userId != game.owner.userId) { throw new HttpException( 'You do do not own this game', diff --git a/src/games/services/games.service.ts b/src/games/services/games.service.ts index 4be07a6..6a644c6 100644 --- a/src/games/services/games.service.ts +++ b/src/games/services/games.service.ts @@ -289,7 +289,9 @@ export class GameService { await game.save({ session }); if (game.teams.length <= 1) { await this.endGame(game, session); + return false; } + return true; } @WithTransaction @@ -340,12 +342,18 @@ export class GameService { team: new ReadTeamDto(team), playerId: kickedPlayerId, }); + let gameExists = true; if (team.players.length == 0) { - const teamIndex = game.teams.indexOf(team); - game.teams.splice(teamIndex, 1); + if (!game.started) { + const teamIndex = game.teams.indexOf(team); + game.teams.splice(teamIndex, 1); + } else { + gameExists = await this.killTeam(game, team, session); + } + } + if (gameExists) { + await game.save({ session }); } - - await game.save({ session }); } @WithTransaction