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 } } }