Ajout d'un endpoint pour essayer de réparer un état éventuellement cassé
This commit is contained in:
parent
a1b5fccc98
commit
4c157ff67f
6
server/src/game/dto/repair-game.dto.ts
Normal file
6
server/src/game/dto/repair-game.dto.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { MoneyUpdate } from "@prisma/client"
|
||||
|
||||
export interface RepairGame {
|
||||
added: MoneyUpdate[]
|
||||
deleted: MoneyUpdate[]
|
||||
}
|
@ -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<GameEntity> {
|
||||
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<RepairGame> {
|
||||
return await this.gameService.repair()
|
||||
}
|
||||
}
|
||||
|
@ -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<Game> {
|
||||
return await this.prisma.game.findUnique({ where: { id: 1 } })
|
||||
}
|
||||
|
||||
async start() {
|
||||
async start(): Promise<Game> {
|
||||
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<Game> {
|
||||
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<Game> {
|
||||
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<RepairGame> {
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user