This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user