feat: card lock

This commit is contained in:
2024-07-03 00:04:31 +02:00
parent ebbb07829f
commit dc9b00284b
12 changed files with 393 additions and 108 deletions
+13 -30
View File
@@ -13,32 +13,14 @@ import { LobbyService } from '../services/games.lobby.service';
import { AuthGuard } from '../guards/auth.guard';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { StartingDeckDto } from '../dto/update-game.dto';
import { Game, Player } from '../schemas/game.schema';
import { Game } from '../schemas/game.schema';
import { Document } from 'mongoose';
import { GamePipe } from '../pipe/game.pipe';
import { GamePipe, GameStartedPipe } from '../pipe/game.pipe';
import { ReadUserDto } from '../dto/read-games.dto';
import { Observable, fromEvent, map } from 'rxjs';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { GameService } from '../services/games.service';
function getPlayer(session: Record<string, any>, game: Game) {
const user = session.user as ReadUserDto;
if (!user) {
throw new HttpException('Please log in', HttpStatus.UNAUTHORIZED);
}
for (const team of game.teams) {
for (const player of team.players) {
if (player.user._id.toHexString() == user._id) {
return player as Document<Player> & Player;
}
}
}
throw new HttpException(
'You are not part of this game',
HttpStatus.FORBIDDEN,
);
}
import { getCurrentTeam, getPlayer, isInTeam } from '../utils';
@ApiTags('game')
@Controller('games/:id')
@@ -72,18 +54,19 @@ export class GamesController {
@UseGuards(AuthGuard)
async endTurn(
@Param('id') id: string,
@Param('id', GamePipe) game: Document<Game> & Game,
@Param('id', GameStartedPipe) game: Document<Game> & Game,
@Session() session: Record<string, string>,
) {
if (!game.started || !game.currentTurn) {
throw new HttpException('Game not started', HttpStatus.BAD_REQUEST);
}
const player = getPlayer(session, game);
const currentTurn = game.teams.find(
(t) => t._id.toHexString() == game.currentTurn,
);
const currentTeam = getCurrentTeam(game);
const currentPlayer = getPlayer(session, game);
await this.gameService.endTurn(game, player._id.toHexString());
if (!isInTeam(currentTeam, currentPlayer)) {
throw new HttpException(
'It is not your turn to play',
HttpStatus.FORBIDDEN,
);
}
await this.gameService.endTurn(game);
}
@Sse('/subscribe')