Feat : kick player from game
This commit is contained in:
@@ -194,4 +194,19 @@ export class LobbyController {
|
|||||||
}
|
}
|
||||||
return await this.lobbyService.leaveGame(game, session.user);
|
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<string, any>,
|
||||||
|
@Param('id', GamePipe) game: Document<Game> & 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,6 +92,29 @@ export class LobbyService {
|
|||||||
HttpStatus.FORBIDDEN,
|
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> & 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 index = team.players.findIndex((p) => p.user._id.equals(user._id));
|
||||||
const player = team.players[index];
|
const player = team.players[index];
|
||||||
team.players.splice(index, 1);
|
team.players.splice(index, 1);
|
||||||
@@ -110,8 +133,6 @@ export class LobbyService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await game.save({ session });
|
await game.save({ session });
|
||||||
|
|
||||||
return 'true!';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findAll(): Promise<ReadGameDto[]> {
|
async findAll(): Promise<ReadGameDto[]> {
|
||||||
|
|||||||
Reference in New Issue
Block a user