This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,32 +100,27 @@ export class LobbyService {
|
||||
async kickPlayer(
|
||||
game: Document<Game> & 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);
|
||||
|
||||
Reference in New Issue
Block a user