diff --git a/src/games/controllers/games.lobby.controller.ts b/src/games/controllers/games.lobby.controller.ts index a8abb3b..9b6c0c0 100644 --- a/src/games/controllers/games.lobby.controller.ts +++ b/src/games/controllers/games.lobby.controller.ts @@ -196,7 +196,7 @@ export class LobbyController { } @Post(':id/kick/:playerId') - @ApiOperation({ summary: "Leave a game that hasn't already started" }) + @ApiOperation({ summary: 'Kick a player' }) @UseGuards(AuthGuard) async kick( @Param('id') id: string, @@ -207,6 +207,12 @@ export class LobbyController { 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', + HttpStatus.FORBIDDEN, + ); + } 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 32d1bc3..e17b81c 100644 --- a/src/games/services/games.lobby.service.ts +++ b/src/games/services/games.lobby.service.ts @@ -100,32 +100,27 @@ export class LobbyService { async kickPlayer( game: Document & Game, user: User, - kickedPlayer: string, + kickedPlayerId: 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)), + t.players.find((p) => p._id.equals(kickedPlayerId)), ); 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); this.eventEmitter.emit('sse.lobby', { - type: 'leaveGame', - player: new ReadPlayerDto(player), + type: 'leaveTeam', + playerId: kickedPlayerId, + team: new ReadTeamDto(team), game: game._id.toHexString(), }); this.eventEmitter.emit('sse.game.' + game._id.toHexString(), { - type: 'leaveGame', - player: new ReadPlayerDto(player), + type: 'leaveTeam', + team: new ReadTeamDto(team), + playerId: kickedPlayerId, }); if (team.players.length == 0) { const teamIndex = game.teams.indexOf(team);