From 979362d012b688d763eb5d0b49c9d7511adc740b Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 14 Dec 2024 13:30:26 +0100 Subject: [PATCH] =?UTF-8?q?Les=20=C3=A9checs=20de=20d=C3=A9fis=20donnent?= =?UTF-8?q?=20lieu=20=C3=A0=20un=20mouvement=20de=20points=20nul?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/app/(tabs)/history.tsx | 6 ++-- .../moneyUpdates/moneyUpdatesSlice.ts | 4 +-- .../migration.sql | 14 ++++++++++ server/prisma/schema.prisma | 2 +- .../challenge-actions.service.ts | 10 ++++++- server/src/game/game.service.ts | 28 +++++++++++++++---- 6 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 server/prisma/migrations/20241214122135_money_update_challenge/migration.sql diff --git a/client/app/(tabs)/history.tsx b/client/app/(tabs)/history.tsx index 639863d..40ad072 100644 --- a/client/app/(tabs)/history.tsx +++ b/client/app/(tabs)/history.tsx @@ -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 {verb} {moneyUpdate.amount} le {earnDate} à {earnTime} diff --git a/client/utils/features/moneyUpdates/moneyUpdatesSlice.ts b/client/utils/features/moneyUpdates/moneyUpdatesSlice.ts index b7b89d2..f67ac85 100644 --- a/client/utils/features/moneyUpdates/moneyUpdatesSlice.ts +++ b/client/utils/features/moneyUpdates/moneyUpdatesSlice.ts @@ -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 diff --git a/server/prisma/migrations/20241214122135_money_update_challenge/migration.sql b/server/prisma/migrations/20241214122135_money_update_challenge/migration.sql new file mode 100644 index 0000000..b0ee7a1 --- /dev/null +++ b/server/prisma/migrations/20241214122135_money_update_challenge/migration.sql @@ -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; diff --git a/server/prisma/schema.prisma b/server/prisma/schema.prisma index d0f2d9a..bfc4e07 100644 --- a/server/prisma/schema.prisma +++ b/server/prisma/schema.prisma @@ -114,6 +114,6 @@ model MoneyUpdate { enum MoneyUpdateType { START NEW_RUN - WIN_CHALLENGE + CHALLENGE BUY_TRAIN } diff --git a/server/src/challenge-actions/challenge-actions.service.ts b/server/src/challenge-actions/challenge-actions.service.ts index 3a2c85e..c033947 100644 --- a/server/src/challenge-actions/challenge-actions.service.ts +++ b/server/src/challenge-actions/challenge-actions.service.ts @@ -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 }, diff --git a/server/src/game/game.service.ts b/server/src/game/game.service.ts index 3b0dfd6..c588d66 100644 --- a/server/src/game/game.service.ts +++ b/server/src/game/game.service.ts @@ -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 }