Chore : start buyCard login
This commit is contained in:
@@ -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> & Game,
|
||||
@Param('gameId', CardPipe) card: Document<Card> & Card,
|
||||
@Session() session: Record<string, string>,
|
||||
) {
|
||||
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> & Game,
|
||||
@Param('cardId', CardPipe) card: Document<Card> & Card,
|
||||
@Session() session: Record<string, string>,
|
||||
) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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> & Game,
|
||||
player: Player,
|
||||
card: Card,
|
||||
session?: mongoose.mongo.ClientSession,
|
||||
) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user