Feat : allow buying cards
This commit is contained in:
@@ -8,11 +8,13 @@ import { CardEffects, EffectType } from 'src/cards/schemas/cards.types';
|
||||
import { WithTransaction } from '../utils';
|
||||
import { effectMappings } from './effect.functions';
|
||||
import { InjectConnection, InjectModel } from '@nestjs/mongoose';
|
||||
import { GameService } from './games.service';
|
||||
|
||||
@Injectable()
|
||||
export class TurnService {
|
||||
constructor(
|
||||
private eventEmitter: EventEmitter2,
|
||||
private readonly gamesService: GameService,
|
||||
@InjectModel(Game.name) private gameModel: Model<Game>,
|
||||
@InjectConnection() private readonly connection: mongoose.Connection,
|
||||
) {}
|
||||
@@ -21,16 +23,9 @@ export class TurnService {
|
||||
async lockCard(
|
||||
game: Document<Game> & Game,
|
||||
player: Player,
|
||||
cardId: string,
|
||||
card: Card,
|
||||
session?: mongoose.mongo.ClientSession,
|
||||
) {
|
||||
const card = player.board.cards.find((c) => c._id.equals(cardId));
|
||||
if (!card) {
|
||||
throw new HttpException(
|
||||
'Card not found in player board',
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
game.currentTurn.cardsLocked.push(card);
|
||||
this.eventEmitter.emit('sse.game.' + game._id.toHexString(), {
|
||||
type: 'lockCard',
|
||||
@@ -38,7 +33,7 @@ export class TurnService {
|
||||
});
|
||||
for (const effect of card.effects) {
|
||||
if (
|
||||
[, CardEffects.HEAL, CardEffects.GOLD, CardEffects.DAMAGE].includes(
|
||||
[CardEffects.HEAL, CardEffects.GOLD, CardEffects.DAMAGE].includes(
|
||||
effect.effect,
|
||||
) &&
|
||||
!effect.mutex &&
|
||||
@@ -93,6 +88,24 @@ export class TurnService {
|
||||
card: Card,
|
||||
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,
|
||||
});
|
||||
await this.gamesService.distributeCards(
|
||||
[card],
|
||||
game.market,
|
||||
player.discard,
|
||||
game,
|
||||
session,
|
||||
);
|
||||
await this.gamesService.fillMarket(game, session);
|
||||
await game.save({ session });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user