diff --git a/src/games/controllers/turn.controller.ts b/src/games/controllers/turn.controller.ts index f70a5f9..b498528 100644 --- a/src/games/controllers/turn.controller.ts +++ b/src/games/controllers/turn.controller.ts @@ -16,6 +16,8 @@ import { GameStartedPipe } from '../pipe/game.pipe'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { TurnService } from '../services/turn.service'; import { getPlayer, getCurrentTeam, isInTeam } from '../utils'; +import { CardPipe } from '../pipe/card.pipe'; +import { Card } from 'src/cards/schemas/cards.schema'; @ApiTags('turn') @Controller('games/:gameId/turn') @@ -32,6 +34,7 @@ export class TurnController { @Param('gameId') _gameId: string, @Param('cardId') cardId: string, @Param('gameId', GameStartedPipe) game: Document & Game, + @Param('gameId', CardPipe) card: Document & Card, @Session() session: Record, ) { const currentPlayer = getPlayer(session, game); @@ -42,11 +45,38 @@ export class TurnController { HttpStatus.FORBIDDEN, ); } - if ( - game.currentTurn.cardsLocked.some((c) => c._id.toHexString() == cardId) - ) { + if (game.currentTurn.cardsLocked.some((c) => c._id.equals(cardId))) { throw new HttpException('Card already locked', HttpStatus.BAD_REQUEST); } this.turnService.lockCard(game, currentPlayer, cardId); } + + @Post('buyCard/:cardId') + @ApiOperation({ summary: 'Buys a card from the market' }) + @UseGuards(AuthGuard) + async buyCard( + @Param('gameId') _gameId: string, + @Param('cardId') _cardId: string, + @Param('gameId', GameStartedPipe) game: Document & Game, + @Param('cardId', CardPipe) card: Document & Card, + @Session() session: Record, + ) { + const currentPlayer = getPlayer(session, game); + const currentTeam = getCurrentTeam(game); + if (!isInTeam(currentTeam, currentPlayer)) { + throw new HttpException( + 'It is not your turn to play', + HttpStatus.FORBIDDEN, + ); + } + if (!game.market.cards.some((c) => c._id.equals(card._id))) { + throw new HttpException( + 'Card not in current game market', + HttpStatus.NOT_FOUND, + ); + } + if (game.currentTurn.gold < card.cost) { + this.turnService.buyCard(game, currentPlayer, card); + } + } } diff --git a/src/games/services/turn.service.ts b/src/games/services/turn.service.ts index 40b21ac..3fe4d2a 100644 --- a/src/games/services/turn.service.ts +++ b/src/games/services/turn.service.ts @@ -85,4 +85,14 @@ export class TurnService { await effectRunner(game, effect, player, card, this.eventEmitter); await game.save({ session }); } + + @WithTransaction + async buyCard( + game: Document & Game, + player: Player, + card: Card, + session?: mongoose.mongo.ClientSession, + ) { + return; + } }