diff --git a/src/games/services/effect.functions.ts b/src/games/services/effect.functions.ts index a5b4243..e48d7a6 100644 --- a/src/games/services/effect.functions.ts +++ b/src/games/services/effect.functions.ts @@ -1,9 +1,11 @@ import { EventEmitter2 } from '@nestjs/event-emitter'; import { Game, Player } from '../schemas/game.schema'; -import { Document } from 'mongoose'; +import mongoose, { Document } from 'mongoose'; import { Card } from 'src/cards/schemas/cards.schema'; import { CardEffects, EffectType } from 'src/cards/schemas/cards.types'; import { Effect } from 'src/cards/schemas/effect.schema'; +import { TurnService } from './turn.service'; +import { GameService } from './games.service'; export function isAutoActivable(effect: Effect) { if ( @@ -17,12 +19,14 @@ export function isAutoActivable(effect: Effect) { } export const effectMappings = { - [CardEffects.GOLD]: ( + [CardEffects.GOLD]: async ( game: Document & Game, effect: Effect, _player: Player, _card: Card, + _gameService: GameService, eventEmitter: EventEmitter2, + _session: mongoose.mongo.ClientSession, ) => { game.currentTurn.gold += effect.amount; eventEmitter.emit('sse.game.' + game._id.toHexString(), { @@ -32,12 +36,14 @@ export const effectMappings = { }); }, - [CardEffects.DAMAGE]: ( + [CardEffects.DAMAGE]: async ( game: Document & Game, effect: Effect, _player: Player, _card: Card, + _gameService: GameService, eventEmitter: EventEmitter2, + _session: mongoose.mongo.ClientSession, ) => { game.currentTurn.damage += effect.amount; eventEmitter.emit('sse.game.' + game._id.toHexString(), { @@ -47,12 +53,14 @@ export const effectMappings = { }); }, - [CardEffects.HEAL]: ( + [CardEffects.HEAL]: async ( game: Document & Game, effect: Effect, player: Player, _card: Card, + _gameService: GameService, eventEmitter: EventEmitter2, + _session: mongoose.mongo.ClientSession, ) => { const team = game.teams.find((t) => t.players.some((p) => p._id.equals(player._id)), @@ -65,6 +73,18 @@ export const effectMappings = { operation: effect.amount, }); }, + + [CardEffects.DRAW]: async ( + game: Document & Game, + effect: Effect, + player: Player, + _card: Card, + gameService: GameService, + _eventEmitter: EventEmitter2, + session: mongoose.mongo.ClientSession, + ) => { + await gameService.drawToBoard(effect.amount, player, game, session); + }, } as Record< CardEffects, ( @@ -72,6 +92,8 @@ export const effectMappings = { effect: Effect, player: Player, card: Card, + gameService: GameService, eventEmitter: EventEmitter2, - ) => void + session: mongoose.mongo.ClientSession, + ) => Promise >; diff --git a/src/games/services/games.service.ts b/src/games/services/games.service.ts index 6f66392..7a8653a 100644 --- a/src/games/services/games.service.ts +++ b/src/games/services/games.service.ts @@ -1,18 +1,26 @@ -import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; +import { + forwardRef, + HttpException, + HttpStatus, + Inject, + Injectable, +} from '@nestjs/common'; import { InjectModel, InjectConnection } from '@nestjs/mongoose'; 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 { WithTransaction, getCurrentTeam, shuffle } from '../utils'; +import { TurnService } from './turn.service'; @Injectable() export class GameService { constructor( + @Inject(forwardRef(() => TurnService)) + private readonly turnService: TurnService, @InjectModel(Game.name) private gameModel: Model, @InjectModel(Card.name) private cardModel: Model, @InjectConnection() private readonly connection: mongoose.Connection, - private eventEmitter: EventEmitter2, ) {} @@ -35,6 +43,7 @@ export class GameService { } const index = game.teams.indexOf(currentTeam); + game.currentTurn.currentTeam = game.teams[(index + 1) % game.teams.length]._id.toHexString(); @@ -43,6 +52,7 @@ export class GameService { game.currentTurn.damage = 0; game.currentTurn.gold = 0; + await game.save({ session }); this.eventEmitter.emit('sse.game.' + game._id.toHexString(), { type: 'endTurn', currentTurn: game.currentTurn, @@ -50,6 +60,14 @@ export class GameService { currentTeam = getCurrentTeam(game); + for (const player of currentTeam.players) { + for (const card of player.board.cards) { + if (card.defense) { + await this.turnService.lockCard(game, player, card, session); + } + } + } + for (const player of currentTeam.players) { await this.distributeCards( player.hand.cards, @@ -193,7 +211,7 @@ export class GameService { } const shallow = [...cards]; for (const c of shallow) { - const index = from.cards.indexOf(c); + const index = from.cards.findIndex((cc) => cc?._id.equals(c._id)); from.cards.splice(index, 1); to.cards.push(c); } diff --git a/src/games/services/turn.service.ts b/src/games/services/turn.service.ts index a51e75d..55ddb2f 100644 --- a/src/games/services/turn.service.ts +++ b/src/games/services/turn.service.ts @@ -1,4 +1,10 @@ -import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; +import { + forwardRef, + HttpException, + HttpStatus, + Inject, + Injectable, +} from '@nestjs/common'; import { Game, Player } from '../schemas/game.schema'; import mongoose, { Document, Model } from 'mongoose'; import { Card } from 'src/cards/schemas/cards.schema'; @@ -6,7 +12,7 @@ import { EventEmitter2 } from '@nestjs/event-emitter'; import { Effect } from 'src/cards/schemas/effect.schema'; import { CardEffects, EffectType } from 'src/cards/schemas/cards.types'; import { WithTransaction } from '../utils'; -import { effectMappings } from './effect.functions'; +import { effectMappings, isAutoActivable } from './effect.functions'; import { InjectConnection, InjectModel } from '@nestjs/mongoose'; import { GameService } from './games.service'; @@ -14,6 +20,7 @@ import { GameService } from './games.service'; export class TurnService { constructor( private eventEmitter: EventEmitter2, + @Inject(forwardRef(() => GameService)) private readonly gamesService: GameService, @InjectModel(Game.name) private gameModel: Model, @InjectConnection() private readonly connection: mongoose.Connection, @@ -32,15 +39,7 @@ export class TurnService { card: card._id.toHexString(), }); for (const effect of card.effects) { - if ( - [CardEffects.HEAL, CardEffects.GOLD, CardEffects.DAMAGE].includes( - effect.effect, - ) && - !effect.mutex && - !effect.per && - (!effect.condition || - effect.condition.effectId == EffectType.CHAMPION_ACTION) - ) { + if (isAutoActivable(effect)) { await this.useEffect(game, player, effect, card, session); } } @@ -77,7 +76,15 @@ export class TurnService { effect: effectUUID, }); game.currentTurn.effectsUsed.push(effectUUID); - await effectRunner(game, effect, player, card, this.eventEmitter); + await effectRunner( + game, + effect, + player, + card, + this.gamesService, + this.eventEmitter, + session, + ); await game.save({ session }); }