Feat : add leave and kick
release-tag / release-image (push) Successful in 40s

This commit is contained in:
2024-08-27 11:51:40 +02:00
parent 3a2a808fc6
commit ead4f39627
2 changed files with 15 additions and 14 deletions
@@ -196,7 +196,7 @@ export class LobbyController {
} }
@Post(':id/kick/:playerId') @Post(':id/kick/:playerId')
@ApiOperation({ summary: "Leave a game that hasn't already started" }) @ApiOperation({ summary: 'Kick a player' })
@UseGuards(AuthGuard) @UseGuards(AuthGuard)
async kick( async kick(
@Param('id') id: string, @Param('id') id: string,
@@ -207,6 +207,12 @@ export class LobbyController {
if (game.started) { if (game.started) {
throw new HttpException('Game already started', HttpStatus.BAD_REQUEST); 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); return await this.lobbyService.kickPlayer(game, session.user, playerId);
} }
} }
+8 -13
View File
@@ -100,32 +100,27 @@ export class LobbyService {
async kickPlayer( async kickPlayer(
game: Document<Game> & Game, game: Document<Game> & Game,
user: User, user: User,
kickedPlayer: string, kickedPlayerId: string,
session?: mongoose.mongo.ClientSession, 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) => 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) { if (team == undefined) {
throw new HttpException('User not in game', HttpStatus.FORBIDDEN); throw new HttpException('User not in game', HttpStatus.FORBIDDEN);
} }
const index = team.players.findIndex((p) => p.user._id.equals(user._id)); const index = team.players.findIndex((p) => p.user._id.equals(user._id));
const player = team.players[index];
team.players.splice(index, 1); team.players.splice(index, 1);
this.eventEmitter.emit('sse.lobby', { this.eventEmitter.emit('sse.lobby', {
type: 'leaveGame', type: 'leaveTeam',
player: new ReadPlayerDto(player), playerId: kickedPlayerId,
team: new ReadTeamDto(team),
game: game._id.toHexString(), game: game._id.toHexString(),
}); });
this.eventEmitter.emit('sse.game.' + game._id.toHexString(), { this.eventEmitter.emit('sse.game.' + game._id.toHexString(), {
type: 'leaveGame', type: 'leaveTeam',
player: new ReadPlayerDto(player), team: new ReadTeamDto(team),
playerId: kickedPlayerId,
}); });
if (team.players.length == 0) { if (team.players.length == 0) {
const teamIndex = game.teams.indexOf(team); const teamIndex = game.teams.indexOf(team);