From 8b8453b276f0a0aa15238fd6e32e429012491ead Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 14 Dec 2024 12:42:37 +0100 Subject: [PATCH] =?UTF-8?q?La=20r=C3=A9paration=20du=20jeu=20met=20=C3=A0?= =?UTF-8?q?=20jour=20les=20montants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/game/dto/repair-game.dto.ts | 1 + server/src/game/game.service.ts | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/server/src/game/dto/repair-game.dto.ts b/server/src/game/dto/repair-game.dto.ts index fa40517..fc8f23c 100644 --- a/server/src/game/dto/repair-game.dto.ts +++ b/server/src/game/dto/repair-game.dto.ts @@ -2,5 +2,6 @@ import { MoneyUpdate } from "@prisma/client" export interface RepairGame { added: MoneyUpdate[] + modified: MoneyUpdate[] deleted: MoneyUpdate[] } diff --git a/server/src/game/game.service.ts b/server/src/game/game.service.ts index 9a4661c..3b0dfd6 100644 --- a/server/src/game/game.service.ts +++ b/server/src/game/game.service.ts @@ -133,6 +133,7 @@ export class GameService { async repair(): Promise { const added: MoneyUpdate[] = [] + const modified: MoneyUpdate[] = [] const deleted: MoneyUpdate[] = [] const trains = await this.prisma.trainTrip.findMany({ include: { moneyUpdate: true } }) for (const train of trains) { @@ -147,6 +148,17 @@ export class GameService { }) added.push(trainMoneyUpdate) } + else { + const moneyUpdate = train.moneyUpdate + const cost = Constants.PRICE_PER_KILOMETER * Math.ceil(train.distance / 1000) + if (moneyUpdate.amount != -cost) { + const modifiedMoneyUpdate = await this.prisma.moneyUpdate.update({ + where: { id: moneyUpdate.id }, + data: { amount: -cost }, + }) + modified.push(modifiedMoneyUpdate) + } + } } const orpanTrainMoneyUpdates = await this.prisma.moneyUpdate.findMany({ where: { reason: MoneyUpdateType.BUY_TRAIN, tripId: null } }) await this.prisma.moneyUpdate.deleteMany({ where: { reason: MoneyUpdateType.BUY_TRAIN, tripId: null } }) @@ -165,6 +177,13 @@ export class GameService { }) added.push(challengeMoneyUpdate) } + else if (challengeAction.success && challengeAction.moneyUpdate.amount !== challengeAction.challenge.reward) { + const modifiedMoneyUpdate = await this.prisma.moneyUpdate.update({ + where: { id: challengeAction.moneyUpdate.id }, + data: { amount: challengeAction.challenge.reward }, + }) + modified.push(modifiedMoneyUpdate) + } else if (!challengeAction.success && challengeAction.moneyUpdate) { deleted.push(challengeAction.moneyUpdate) await this.prisma.moneyUpdate.delete({ where: { id: challengeAction.moneyUpdate.id } }) @@ -175,6 +194,6 @@ export class GameService { await this.prisma.moneyUpdate.deleteMany({ where: { reason: MoneyUpdateType.WIN_CHALLENGE, actionId: null } }) deleted.push(...orpanChallengeMoneyUpdates) - return { added: added, deleted: deleted } + return { added: added, modified: modified, deleted: deleted } } }