From 4c157ff67ff72484ec30753a3f58a11ccd8f0704 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sun, 8 Dec 2024 19:55:07 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20d'un=20endpoint=20pour=20essayer=20de?= =?UTF-8?q?=20r=C3=A9parer=20un=20=C3=A9tat=20=C3=A9ventuellement=20cass?= =?UTF-8?q?=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/game/dto/repair-game.dto.ts | 6 +++ server/src/game/game.controller.ts | 18 +++++++- server/src/game/game.service.ts | 58 +++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 server/src/game/dto/repair-game.dto.ts diff --git a/server/src/game/dto/repair-game.dto.ts b/server/src/game/dto/repair-game.dto.ts new file mode 100644 index 0000000..fa40517 --- /dev/null +++ b/server/src/game/dto/repair-game.dto.ts @@ -0,0 +1,6 @@ +import { MoneyUpdate } from "@prisma/client" + +export interface RepairGame { + added: MoneyUpdate[] + deleted: MoneyUpdate[] +} diff --git a/server/src/game/game.controller.ts b/server/src/game/game.controller.ts index 3e3856c..e4e2ad7 100644 --- a/server/src/game/game.controller.ts +++ b/server/src/game/game.controller.ts @@ -1,8 +1,9 @@ -import { Controller, Delete, Get, HttpCode, Post, UseGuards } from '@nestjs/common' +import { Controller, Delete, Get, Post, Put, UseGuards } from '@nestjs/common' import { GameService } from './game.service' import { JwtAuthGuard } from 'src/auth/jwt-auth.guard' -import { ApiBearerAuth, ApiNoContentResponse, ApiOkResponse, ApiOperation, ApiUnauthorizedResponse } from '@nestjs/swagger' +import { ApiBearerAuth } from '@nestjs/swagger' import { GameEntity } from './entities/game.entity' +import { RepairGame } from './dto/repair-game.dto' @Controller('game') export class GameController { @@ -60,4 +61,17 @@ export class GameController { async reset(): Promise { return new GameEntity(await this.gameService.reset()) } + + /** + * Tente de réparer l'état du jeu + * @remarks , Vérifie que tous les trains ont bien été payés et que tous les défis réussis sont bien accompagnés d'un crédit de points. + * + * @throws {401} Non authentifié⋅e + */ + @Put('/repair') + @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + async repair(): Promise { + return await this.gameService.repair() + } } diff --git a/server/src/game/game.service.ts b/server/src/game/game.service.ts index 9158f10..f23fae7 100644 --- a/server/src/game/game.service.ts +++ b/server/src/game/game.service.ts @@ -1,16 +1,17 @@ import { ConflictException, Injectable } from '@nestjs/common' -import { MoneyUpdateType } from '@prisma/client' +import { Game, MoneyUpdate, MoneyUpdateType } from '@prisma/client' import { PrismaService } from 'src/prisma/prisma.service' +import { RepairGame } from './dto/repair-game.dto' @Injectable() export class GameService { constructor (private prisma: PrismaService) {} - async find() { + async find(): Promise { return await this.prisma.game.findUnique({ where: { id: 1 } }) } - async start() { + async start(): Promise { const game = await this.find() if (game.started) throw new ConflictException("La partie a déjà démarré.") @@ -37,7 +38,7 @@ export class GameService { }) } - async stop() { + async stop(): Promise { const game = await this.find() if (!game.started) throw new ConflictException("La partie n'a pas encore démarré.") @@ -49,7 +50,7 @@ export class GameService { }) } - async reset() { + async reset(): Promise { await this.prisma.moneyUpdate.deleteMany() await this.prisma.challengeAction.deleteMany() await this.prisma.trainTrip.deleteMany() @@ -64,4 +65,51 @@ export class GameService { }) return await this.find() } + + async repair(): Promise { + const added: MoneyUpdate[] = [] + const deleted: MoneyUpdate[] = [] + const trains = await this.prisma.trainTrip.findMany({ include: { moneyUpdate: true } }) + for (const train of trains) { + if (!train.moneyUpdate) { + const trainMoneyUpdate = await this.prisma.moneyUpdate.create({ + data: { + playerId: train.playerId, + amount: -10 * Math.ceil(train.distance / 1000), + reason: MoneyUpdateType.BUY_TRAIN, + tripId: train.id, + } + }) + added.push(trainMoneyUpdate) + } + } + 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 } }) + deleted.push(...orpanTrainMoneyUpdates) + + const challengeActions = await this.prisma.challengeAction.findMany({ include: { moneyUpdate: true, challenge: 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, + actionId: challengeAction.id, + } + }) + added.push(challengeMoneyUpdate) + } + else if (!challengeAction.success && 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 } }) + deleted.push(...orpanTrainMoneyUpdates) + + return { added: added, deleted: deleted } + } }