From b24975b1482be69b80ea77e34502022b2e09bac4 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Sun, 14 Jul 2024 00:19:40 +0200 Subject: [PATCH] Fix : various inconsistencies for effects --- src/games/controllers/turn.controller.ts | 6 ++++-- src/games/dto/damage-champion-dto.ts | 2 +- src/games/services/turn.service.ts | 26 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/games/controllers/turn.controller.ts b/src/games/controllers/turn.controller.ts index 6d5439f..d8d6693 100644 --- a/src/games/controllers/turn.controller.ts +++ b/src/games/controllers/turn.controller.ts @@ -192,7 +192,7 @@ export class TurnController { } const targetPlayer = findPlayer(_playerId, game); - if (!isInTeam(currentTeam, targetPlayer)) { + if (isInTeam(currentTeam, targetPlayer)) { throw new HttpException( 'You cannot target players in the same team as yours', HttpStatus.FORBIDDEN, @@ -324,7 +324,7 @@ export class TurnController { ); } - const targetPlayer = findPlayer(damageChampion.player, game); + const targetPlayer = findPlayer(damageChampion.playerId, game); if (!targetPlayer) { throw new HttpException('Player not found', HttpStatus.NOT_FOUND); } @@ -338,5 +338,7 @@ export class TurnController { HttpStatus.NOT_FOUND, ); } + + await this.turnService.damageChampion(game, targetPlayer, card); } } diff --git a/src/games/dto/damage-champion-dto.ts b/src/games/dto/damage-champion-dto.ts index fb2638d..56dd87a 100644 --- a/src/games/dto/damage-champion-dto.ts +++ b/src/games/dto/damage-champion-dto.ts @@ -2,5 +2,5 @@ import { ApiProperty } from '@nestjs/swagger'; export class DamageChampionDTO { @ApiProperty() - player: string; + playerId: string; } diff --git a/src/games/services/turn.service.ts b/src/games/services/turn.service.ts index 053e695..7874278 100644 --- a/src/games/services/turn.service.ts +++ b/src/games/services/turn.service.ts @@ -307,4 +307,30 @@ export class TurnService { await game.save({ session }); } + + @WithTransaction + async damageChampion( + game: Document & Game, + targetPlayer: Player, + targetCard: Card, + session?: mongoose.mongo.ClientSession, + ) { + game.currentTurn.damage -= targetCard.defense; + + this.eventEmitter.emit('sse.game.' + game._id.toHexString(), { + type: 'currentTurn.updateDamage', + damage: game.currentTurn.damage, + operation: -targetCard.defense, + }); + + await this.gamesService.distributeCards( + [targetCard], + targetPlayer.board, + targetPlayer.discard, + game, + session, + ); + + await game.save({ session }); + } }