Ajout d'une structure de tentatives de courses (Run)
This commit is contained in:
@ -6,12 +6,18 @@ export class GameEntity implements Game {
|
||||
Object.assign(this, partial)
|
||||
}
|
||||
|
||||
@ApiProperty({ description: "Identifiant unique du jeu" })
|
||||
/**
|
||||
* Identifiant unique du jeu
|
||||
*/
|
||||
id: number
|
||||
|
||||
@ApiProperty({ description: "Est-ce que la partie est en cours" })
|
||||
/**
|
||||
* Est-ce que la partie est en cours
|
||||
*/
|
||||
started: boolean
|
||||
|
||||
@ApiProperty({ description: "Identifiant de læ joueur⋅se en course" })
|
||||
currentRunnerId: number
|
||||
/**
|
||||
* Identifiant de læ joueur⋅se en course
|
||||
*/
|
||||
currentRunId: number
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ import { RepairGame } from './dto/repair-game.dto'
|
||||
export class GameService {
|
||||
constructor (private prisma: PrismaService) {}
|
||||
|
||||
async find(): Promise<Game> {
|
||||
return await this.prisma.game.findUnique({ where: { id: 1 } })
|
||||
async find() {
|
||||
return await this.prisma.game.findUnique({ where: { id: 1 }, include: { currentRun: true } })
|
||||
}
|
||||
|
||||
async start(): Promise<Game> {
|
||||
@ -16,7 +16,8 @@ export class GameService {
|
||||
if (game.started)
|
||||
throw new ConflictException("La partie a déjà démarré.")
|
||||
const players = await this.prisma.player.findMany()
|
||||
const alreadyStarted = game.currentRunnerId !== null
|
||||
const alreadyStarted = game.currentRunId !== null
|
||||
let run
|
||||
if (!alreadyStarted) {
|
||||
for (const player of players) {
|
||||
await this.prisma.moneyUpdate.create({
|
||||
@ -27,30 +28,50 @@ export class GameService {
|
||||
}
|
||||
})
|
||||
}
|
||||
const runnerId = players[Math.floor(players.length * Math.random())].id
|
||||
run = await this.prisma.playerRun.create({
|
||||
data: {
|
||||
gameId: game.id,
|
||||
runnerId: runnerId,
|
||||
}
|
||||
})
|
||||
}
|
||||
else {
|
||||
run = game.currentRun
|
||||
}
|
||||
const runnerId = alreadyStarted ? game.currentRunnerId : players[Math.floor(players.length * Math.random())].id
|
||||
return await this.prisma.game.update({
|
||||
where: { id: 1 },
|
||||
data: {
|
||||
started: true,
|
||||
currentRunnerId: runnerId,
|
||||
currentRunId: run.id,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async switchRunningPlayer(): Promise<Game> {
|
||||
const game = await this.find()
|
||||
const newRunnerId = game.currentRunnerId == 1 ? 2 : 1
|
||||
await this.prisma.moneyUpdate.create({
|
||||
const newRunnerId = game.currentRun.runnerId == 1 ? 2 : 1
|
||||
const firstRun = await this.prisma.playerRun.findFirst({ where: { runnerId: newRunnerId } }) !== null
|
||||
const newRun = await this.prisma.playerRun.create({
|
||||
data: {
|
||||
playerId: newRunnerId,
|
||||
amount: 300,
|
||||
reason: MoneyUpdateType.START,
|
||||
gameId: game.id,
|
||||
runnerId: newRunnerId,
|
||||
}
|
||||
})
|
||||
if (!firstRun) {
|
||||
// Si c'est une nouvelle course (pas la première), on accorde un bonus de points de départ
|
||||
await this.prisma.moneyUpdate.create({
|
||||
data: {
|
||||
playerId: newRunnerId,
|
||||
amount: 300,
|
||||
reason: MoneyUpdateType.NEW_RUN,
|
||||
runId: newRun.id,
|
||||
}
|
||||
})
|
||||
}
|
||||
return await this.prisma.game.update({
|
||||
where: { id: game.id },
|
||||
data: { currentRunnerId: newRunnerId },
|
||||
data: { currentRunId: newRun.id },
|
||||
})
|
||||
}
|
||||
|
||||
@ -67,18 +88,19 @@ export class GameService {
|
||||
}
|
||||
|
||||
async reset(): Promise<Game> {
|
||||
await this.prisma.moneyUpdate.deleteMany()
|
||||
await this.prisma.challengeAction.deleteMany()
|
||||
await this.prisma.trainTrip.deleteMany()
|
||||
await this.prisma.geolocation.deleteMany()
|
||||
await this.prisma.player.updateMany({ data: { money: 0 } })
|
||||
await this.prisma.game.update({
|
||||
where: { id: 1 },
|
||||
data: {
|
||||
started: false,
|
||||
currentRunnerId: null,
|
||||
currentRunId: null,
|
||||
},
|
||||
})
|
||||
await this.prisma.player.updateMany({ data: { money: 0 } })
|
||||
await this.prisma.moneyUpdate.deleteMany()
|
||||
await this.prisma.challengeAction.deleteMany()
|
||||
await this.prisma.trainTrip.deleteMany()
|
||||
await this.prisma.playerRun.deleteMany()
|
||||
await this.prisma.geolocation.deleteMany()
|
||||
return await this.find()
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user