Feat : refactor buy function
This commit is contained in:
@@ -87,7 +87,35 @@ export class TurnController {
|
||||
if (game.currentTurn.gold < card.cost) {
|
||||
throw new HttpException('Not enough gold', HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
await this.turnService.buyCard(game, currentPlayer, card);
|
||||
await this.turnService.buyGeneric(game, currentPlayer, card, game.market);
|
||||
}
|
||||
|
||||
@Post('buyGem')
|
||||
@ApiOperation({ summary: 'Buys a gem from the market' })
|
||||
@UseGuards(AuthGuard)
|
||||
async buyGem(
|
||||
@Param('gameId') _gameId: string,
|
||||
@Param('gameId', GameStartedPipe) game: Document<Game> & Game,
|
||||
@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.fireGems.cards.length < 1) {
|
||||
throw new HttpException('No Gems left', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
const targetCard = game.fireGems.cards[0];
|
||||
await this.turnService.buyGeneric(
|
||||
game,
|
||||
currentPlayer,
|
||||
targetCard,
|
||||
game.fireGems,
|
||||
);
|
||||
}
|
||||
|
||||
@Post('lockEffect/:effectId')
|
||||
|
||||
@@ -35,6 +35,18 @@ async function addToken(
|
||||
});
|
||||
}
|
||||
|
||||
export async function removeToken(
|
||||
game: Document<Game> & Game,
|
||||
effect: Effect,
|
||||
eventEmitter: EventEmitter2,
|
||||
) {
|
||||
game.currentTurn.tokens.splice(game.currentTurn.tokens.indexOf(effect), 1);
|
||||
eventEmitter.emit('sse.game.' + game._id.toHexString(), {
|
||||
type: 'currentTurn.removeToken',
|
||||
token: effect._id,
|
||||
});
|
||||
}
|
||||
|
||||
export const effectMappings = {
|
||||
[CardEffects.GOLD]: async (
|
||||
game: Document<Game> & Game,
|
||||
|
||||
@@ -5,18 +5,24 @@ import {
|
||||
Inject,
|
||||
Injectable,
|
||||
} from '@nestjs/common';
|
||||
import { Game, Player } from '../schemas/game.schema';
|
||||
import { Container, Game, Player } from '../schemas/game.schema';
|
||||
import mongoose, { Document, Model } from 'mongoose';
|
||||
import { Card } from 'src/cards/schemas/cards.schema';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { Effect } from 'src/cards/schemas/effect.schema';
|
||||
import { CardEffects, EffectType } from 'src/cards/schemas/cards.types';
|
||||
import {
|
||||
CardEffects,
|
||||
CardRole,
|
||||
CardType,
|
||||
EffectType,
|
||||
} from 'src/cards/schemas/cards.types';
|
||||
import { WithTransaction } from '../utils';
|
||||
import {
|
||||
conditionsMappings,
|
||||
effectMappings,
|
||||
isAutoActivable,
|
||||
perMapping,
|
||||
removeToken,
|
||||
} from './effect.functions';
|
||||
import { InjectConnection, InjectModel } from '@nestjs/mongoose';
|
||||
import { GameService } from './games.service';
|
||||
@@ -140,30 +146,81 @@ export class TurnService {
|
||||
}
|
||||
|
||||
@WithTransaction
|
||||
async buyCard(
|
||||
async buyGeneric(
|
||||
game: Document<Game> & Game,
|
||||
player: Player,
|
||||
card: Card,
|
||||
fromContainer: Container,
|
||||
session?: mongoose.mongo.ClientSession,
|
||||
) {
|
||||
if (!card.cost) {
|
||||
throw new TypeError('Card cost is null or undefined');
|
||||
}
|
||||
game.currentTurn.gold -= card.cost;
|
||||
this.eventEmitter.emit('sse.game.' + game._id.toHexString(), {
|
||||
type: 'currentTurn.updateGold',
|
||||
gold: game.currentTurn.gold,
|
||||
operation: -card.cost,
|
||||
});
|
||||
// const targetChangingEffects = [CardEffects.STACK_NEXT_CARD_BOUGHT];
|
||||
// if (card.cardType == CardType.ACTION) {
|
||||
// targetChangingEffects.push(CardEffects.STACK_NEXT_ACTION_BOUGHT);
|
||||
// }
|
||||
// const targetChangingToken = game.currentTurn.tokens.find((e) =>
|
||||
// targetChangingEffects.includes(e.effect),
|
||||
// );
|
||||
|
||||
// const forFreeEffects = [CardEffects.BUY_FOR_FREE];
|
||||
// if (card.role == CardRole.FIRE_GEM) {
|
||||
// forFreeEffects.push(CardEffects.BUY_GEM_FOR_FREE);
|
||||
// }
|
||||
|
||||
// const forFreeToken = game.currentTurn.tokens.find((e) =>
|
||||
// forFreeEffects.includes(e.effect),
|
||||
// );
|
||||
|
||||
// const reducedPriceEffects = [];
|
||||
|
||||
// if (card.cardType == CardType.ACTION) {
|
||||
// reducedPriceEffects.push(CardEffects.CHEAPER_ACTION);
|
||||
// }
|
||||
// if (card.cardType == CardType.CHAMPION) {
|
||||
// reducedPriceEffects.push(CardEffects.CHEAPER_CHAMPION);
|
||||
// reducedPriceEffects.push(CardEffects.CHEAPER_CHAMPIONS_PASSIVE);
|
||||
// }
|
||||
// // if(card.cost)
|
||||
// CardEffects.CHEAPER_CARD_IF_HIGHER_PRICE
|
||||
|
||||
// const reducedPriceToken = game.currentTurn.tokens.find((e) =>
|
||||
// forFreeEffects.includes(e.effect),
|
||||
// );
|
||||
|
||||
const cost = card.cost;
|
||||
// if (forFreeToken) {
|
||||
// cost = 0;
|
||||
// removeToken(game, targetChangingToken, this.eventEmitter);
|
||||
// }
|
||||
|
||||
if (game.currentTurn.gold < cost) {
|
||||
throw new HttpException('Not enough gold', HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
const targetContainer = player.discard;
|
||||
// if (targetChangingToken) {
|
||||
// targetContainer = player.stack;
|
||||
// removeToken(game, targetChangingToken, this.eventEmitter);
|
||||
// }
|
||||
|
||||
if (cost != 0) {
|
||||
game.currentTurn.gold -= cost;
|
||||
this.eventEmitter.emit('sse.game.' + game._id.toHexString(), {
|
||||
type: 'currentTurn.updateGold',
|
||||
gold: game.currentTurn.gold,
|
||||
operation: -cost,
|
||||
});
|
||||
}
|
||||
|
||||
await this.gamesService.distributeCards(
|
||||
[card],
|
||||
game.market,
|
||||
player.discard,
|
||||
fromContainer,
|
||||
targetContainer,
|
||||
game,
|
||||
session,
|
||||
);
|
||||
await this.gamesService.fillMarket(game, session);
|
||||
await game.save({ session });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user