From 0aaf03839b601b90dbde58fc6ef95333a8688955 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Sat, 21 Dec 2024 11:07:28 +0100 Subject: [PATCH] fix : attempt to fix prepare with sub_effects Closes #17 --- src/games/services/effect.functions.ts | 19 ++++++------------- src/games/services/turn.service.ts | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/games/services/effect.functions.ts b/src/games/services/effect.functions.ts index 28303b2..593332f 100644 --- a/src/games/services/effect.functions.ts +++ b/src/games/services/effect.functions.ts @@ -178,13 +178,13 @@ export const effectMappings = { export const tokenMappings = { [CardEffects.PREPARE]: async ( game: Document & Game, - effect: Effect, - player: Player, + _effect: Effect, + _player: Player, card: Card, - gameService: GameService, + _gameService: GameService, turnService: TurnService, - eventEmitter: EventEmitter2, - _session: mongoose.mongo.ClientSession, + _eventEmitter: EventEmitter2, + session: mongoose.mongo.ClientSession, ) => { if (card.cardType !== CardType.CHAMPION) { throw new HttpException( @@ -208,14 +208,7 @@ export const tokenMappings = { } for (const e of targetEffects) { - const index = game.currentTurn.lockedEffects.findIndex((ee) => - ee._id.equals(e._id), - ); - game.currentTurn.lockedEffects.splice(index, 1); - eventEmitter.emit('sse.game.' + game._id.toHexString(), { - type: 'unlockEffect', - effect: e._id.toHexString(), - }); + await turnService.unlockEffect(game, e, session); } }, [CardEffects.STUN]: async ( diff --git a/src/games/services/turn.service.ts b/src/games/services/turn.service.ts index 228fe43..d161916 100644 --- a/src/games/services/turn.service.ts +++ b/src/games/services/turn.service.ts @@ -58,6 +58,28 @@ export class TurnService { await game.save({ session }); } + @WithTransaction + async unlockEffect( + game: Document & Game, + effect: Effect, + session: mongoose.mongo.ClientSession, + ) { + const index = game.currentTurn.lockedEffects.findIndex((ee) => + ee._id.equals(effect._id), + ); + game.currentTurn.lockedEffects.splice(index, 1); + + this.eventEmitter.emit('sse.game.' + game._id.toHexString(), { + type: 'unlockEffect', + effect: effect._id.toHexString(), + }); + + for (const sub of effect.subEffects) { + await this.unlockEffect(game, sub, session); + } + await game.save({ session }); + } + @WithTransaction async lockEffect( game: Document & Game,