La réparation du jeu met à jour les montants
This commit is contained in:
parent
fd4b0e8cd1
commit
8b8453b276
@ -2,5 +2,6 @@ import { MoneyUpdate } from "@prisma/client"
|
||||
|
||||
export interface RepairGame {
|
||||
added: MoneyUpdate[]
|
||||
modified: MoneyUpdate[]
|
||||
deleted: MoneyUpdate[]
|
||||
}
|
||||
|
@ -133,6 +133,7 @@ export class GameService {
|
||||
|
||||
async repair(): Promise<RepairGame> {
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user