La réparation du jeu met à jour les montants

This commit is contained in:
Emmy D'Anello 2024-12-14 12:42:37 +01:00
parent fd4b0e8cd1
commit 8b8453b276
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 21 additions and 1 deletions

View File

@ -2,5 +2,6 @@ import { MoneyUpdate } from "@prisma/client"
export interface RepairGame { export interface RepairGame {
added: MoneyUpdate[] added: MoneyUpdate[]
modified: MoneyUpdate[]
deleted: MoneyUpdate[] deleted: MoneyUpdate[]
} }

View File

@ -133,6 +133,7 @@ export class GameService {
async repair(): Promise<RepairGame> { async repair(): Promise<RepairGame> {
const added: MoneyUpdate[] = [] const added: MoneyUpdate[] = []
const modified: MoneyUpdate[] = []
const deleted: MoneyUpdate[] = [] const deleted: MoneyUpdate[] = []
const trains = await this.prisma.trainTrip.findMany({ include: { moneyUpdate: true } }) const trains = await this.prisma.trainTrip.findMany({ include: { moneyUpdate: true } })
for (const train of trains) { for (const train of trains) {
@ -147,6 +148,17 @@ export class GameService {
}) })
added.push(trainMoneyUpdate) 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 } }) 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 } }) await this.prisma.moneyUpdate.deleteMany({ where: { reason: MoneyUpdateType.BUY_TRAIN, tripId: null } })
@ -165,6 +177,13 @@ export class GameService {
}) })
added.push(challengeMoneyUpdate) 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) { else if (!challengeAction.success && challengeAction.moneyUpdate) {
deleted.push(challengeAction.moneyUpdate) deleted.push(challengeAction.moneyUpdate)
await this.prisma.moneyUpdate.delete({ where: { id: challengeAction.moneyUpdate.id } }) 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 } }) await this.prisma.moneyUpdate.deleteMany({ where: { reason: MoneyUpdateType.WIN_CHALLENGE, actionId: null } })
deleted.push(...orpanChallengeMoneyUpdates) deleted.push(...orpanChallengeMoneyUpdates)
return { added: added, deleted: deleted } return { added: added, modified: modified, deleted: deleted }
} }
} }