Les échecs de défis donnent lieu à un mouvement de points nul

This commit is contained in:
Emmy D'Anello 2024-12-14 13:30:26 +01:00
parent 9dfb2ba15d
commit 979362d012
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
6 changed files with 52 additions and 12 deletions

View File

@ -32,7 +32,7 @@ function MoneyUpdateListItem({ moneyUpdate }: { moneyUpdate: MoneyUpdate }) {
case 'START': return 'star'
case 'NEW_RUN': return 'run'
case 'BUY_TRAIN': return 'train'
case 'WIN_CHALLENGE': return 'cards'
case 'CHALLENGE': return 'cards'
}
}, [moneyUpdate.reason])
@ -50,7 +50,7 @@ function MoneyUpdateListItem({ moneyUpdate }: { moneyUpdate: MoneyUpdate }) {
const arrDateTime = new Date(train.arrivalTime)
const arrTime = arrDateTime.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' })
return `${train.from} ${depTime} => ${train.to} ${arrTime}`
case 'WIN_CHALLENGE':
case 'CHALLENGE':
const challengeAction = challengeActions.find((challengeAction) => challengeAction.id === moneyUpdate.actionId)
if (!challengeAction) return "Défi"
const challenge = challenges.find((challenge) => challenge.id === challengeAction.challengeId)
@ -62,7 +62,7 @@ function MoneyUpdateListItem({ moneyUpdate }: { moneyUpdate: MoneyUpdate }) {
const description = useMemo(() => {
const earnDate = new Date(moneyUpdate.timestamp).toLocaleDateString(undefined, { day: '2-digit', month: '2-digit' })
const earnTime = new Date(moneyUpdate.timestamp).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' })
const verb = moneyUpdate.amount > 0 ? "Gagné" : "Dépensé"
const verb = moneyUpdate.amount >= 0 ? "Gagné" : "Dépensé"
return <Text>
{verb} {moneyUpdate.amount} <FontAwesome6 name='coins' /> le {earnDate} à {earnTime}
</Text>

View File

@ -5,7 +5,7 @@ export interface MoneyUpdate {
id: number
playerId: number
amount: number
reason: 'START' | 'NEW_RUN' | 'BUY_TRAIN' | 'WIN_CHALLENGE'
reason: 'START' | 'NEW_RUN' | 'BUY_TRAIN' | 'CHALLENGE'
timestamp: number
runId: number | null
actionId: number | null
@ -24,7 +24,7 @@ export interface MoneyUpdatePayload {
id: number
playerId: number
amount: number
reason: 'START' | 'NEW_RUN' | 'BUY_TRAIN' | 'WIN_CHALLENGE'
reason: 'START' | 'NEW_RUN' | 'BUY_TRAIN' | 'CHALLENGE'
timestamp: string
runId: number | null
actionId: number | null

View File

@ -0,0 +1,14 @@
/*
Warnings:
- The values [WIN_CHALLENGE] on the enum `MoneyUpdateType` will be removed. If these variants are still used in the database, this will fail.
*/
-- AlterEnum
BEGIN;
CREATE TYPE "MoneyUpdateType_new" AS ENUM ('START', 'NEW_RUN', 'CHALLENGE', 'BUY_TRAIN');
ALTER TABLE "MoneyUpdate" ALTER COLUMN "reason" TYPE "MoneyUpdateType_new" USING ("reason"::text::"MoneyUpdateType_new");
ALTER TYPE "MoneyUpdateType" RENAME TO "MoneyUpdateType_old";
ALTER TYPE "MoneyUpdateType_new" RENAME TO "MoneyUpdateType";
DROP TYPE "MoneyUpdateType_old";
COMMIT;

View File

@ -114,6 +114,6 @@ model MoneyUpdate {
enum MoneyUpdateType {
START
NEW_RUN
WIN_CHALLENGE
CHALLENGE
BUY_TRAIN
}

View File

@ -75,7 +75,7 @@ export class ChallengeActionsService {
data: {
playerId: player.id,
amount: challenge.reward,
reason: MoneyUpdateType.WIN_CHALLENGE,
reason: MoneyUpdateType.CHALLENGE,
actionId: challengeAction.id,
}
})
@ -87,6 +87,14 @@ export class ChallengeActionsService {
penaltyStart: now,
penaltyEnd: new Date(now.getTime() + Constants.PENALTY_TIME * 60 * 1000),
}
await this.prisma.moneyUpdate.create({
data: {
playerId: player.id,
amount: 0,
reason: MoneyUpdateType.CHALLENGE,
actionId: challengeAction.id,
}
})
}
await this.prisma.player.update({
where: { id: player.id },

View File

@ -164,14 +164,14 @@ export class GameService {
await this.prisma.moneyUpdate.deleteMany({ where: { reason: MoneyUpdateType.BUY_TRAIN, tripId: null } })
deleted.push(...orpanTrainMoneyUpdates)
const challengeActions = await this.prisma.challengeAction.findMany({ include: { moneyUpdate: true, challenge: true } })
const challengeActions = await this.prisma.challengeAction.findMany({ include: { moneyUpdate: true, challenge: true, activePlayer: true } })
for (const challengeAction of challengeActions) {
if (challengeAction.success && !challengeAction.moneyUpdate) {
const challengeMoneyUpdate = await this.prisma.moneyUpdate.create({
data: {
playerId: challengeAction.playerId,
amount: challengeAction.challenge.reward,
reason: MoneyUpdateType.WIN_CHALLENGE,
reason: MoneyUpdateType.CHALLENGE,
actionId: challengeAction.id,
}
})
@ -184,14 +184,32 @@ export class GameService {
})
modified.push(modifiedMoneyUpdate)
}
else if (!challengeAction.success && challengeAction.moneyUpdate) {
else if (!challengeAction.success && !challengeAction.activePlayer && !challengeAction.moneyUpdate) {
const challengeMoneyUpdate = await this.prisma.moneyUpdate.create({
data: {
playerId: challengeAction.playerId,
amount: 0,
reason: MoneyUpdateType.CHALLENGE,
actionId: challengeAction.id,
}
})
added.push(challengeMoneyUpdate)
}
else if (!challengeAction.success && !challengeAction.activePlayer && challengeAction.moneyUpdate.amount !== 0) {
const modifiedMoneyUpdate = await this.prisma.moneyUpdate.update({
where: { id: challengeAction.moneyUpdate.id },
data: { amount: 0 },
})
modified.push(modifiedMoneyUpdate)
}
else if (!challengeAction.success && challengeAction.activePlayer && challengeAction.moneyUpdate) {
deleted.push(challengeAction.moneyUpdate)
await this.prisma.moneyUpdate.delete({ where: { id: challengeAction.moneyUpdate.id } })
}
}
const orpanChallengeMoneyUpdates = await this.prisma.moneyUpdate.findMany({ where: { reason: MoneyUpdateType.WIN_CHALLENGE, actionId: null } })
await this.prisma.moneyUpdate.deleteMany({ where: { reason: MoneyUpdateType.WIN_CHALLENGE, actionId: null } })
const orpanChallengeMoneyUpdates = await this.prisma.moneyUpdate.findMany({ where: { reason: MoneyUpdateType.CHALLENGE, actionId: null } })
await this.prisma.moneyUpdate.deleteMany({ where: { reason: MoneyUpdateType.CHALLENGE, actionId: null } })
deleted.push(...orpanChallengeMoneyUpdates)
return { added: added, modified: modified, deleted: deleted }