Fix : surrender and kick
release-tag / release-image (push) Successful in 32s

This commit is contained in:
2024-08-27 13:08:33 +02:00
parent ad48f25a7d
commit 36a9a2e443
2 changed files with 28 additions and 9 deletions
+16 -5
View File
@@ -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<string, any>,
@Param('id', GamePipe) game: Document<Game> & 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> & 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',
+12 -4
View File
@@ -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