diff --git a/src/games/controllers/games.lobby.controller.ts b/src/games/controllers/games.lobby.controller.ts index cd8bf9f..95febb3 100644 --- a/src/games/controllers/games.lobby.controller.ts +++ b/src/games/controllers/games.lobby.controller.ts @@ -194,4 +194,19 @@ export class LobbyController { } return await this.lobbyService.leaveGame(game, session.user); } + + @Post(':id/kick:/:playerId') + @ApiOperation({ summary: "Leave a game that hasn't already started" }) + @UseGuards(AuthGuard) + async kick( + @Param('id') id: string, + @Session() session: Record, + @Param('id', GamePipe) game: Document & Game, + @Param('playerId') playerId: string, + ) { + if (game.started) { + throw new HttpException('Game already started', HttpStatus.BAD_REQUEST); + } + return await this.lobbyService.kickPlayer(game, session.user, playerId); + } } diff --git a/src/games/services/games.lobby.service.ts b/src/games/services/games.lobby.service.ts index 099bde0..32d1bc3 100644 --- a/src/games/services/games.lobby.service.ts +++ b/src/games/services/games.lobby.service.ts @@ -92,6 +92,29 @@ export class LobbyService { HttpStatus.FORBIDDEN, ); } + const player = team.players.find((p) => p.user._id.equals(user._id)); + await this.kickPlayer(game, game.owner, player._id.toHexString(), session); + } + + @WithTransaction + async kickPlayer( + game: Document & Game, + user: User, + kickedPlayer: string, + session?: mongoose.mongo.ClientSession, + ) { + if (!game.owner._id.equals(user._id)) { + throw new HttpException( + 'You are not the owner of this game', + HttpStatus.FORBIDDEN, + ); + } + const team = game.teams.find((t) => + t.players.find((p) => p._id.equals(kickedPlayer)), + ); + if (team == undefined) { + throw new HttpException('User not in game', HttpStatus.FORBIDDEN); + } const index = team.players.findIndex((p) => p.user._id.equals(user._id)); const player = team.players[index]; team.players.splice(index, 1); @@ -110,8 +133,6 @@ export class LobbyService { } await game.save({ session }); - - return 'true!'; } async findAll(): Promise {