Stockage des constants dans un fichier à part pour tout centraliser

This commit is contained in:
Emmy D'Anello 2024-12-08 23:32:57 +01:00
parent a0fd6ca6ab
commit 4a4233925d
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
4 changed files with 29 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import { PrismaService } from 'src/prisma/prisma.service'
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
import { paginate } from 'src/common/utils/pagination.utils'
import { FilterChallengeActionsDto } from './dto/filter-challenge-action.dto'
import { Constants } from 'src/common/utils/constants.utils'
@Injectable()
export class ChallengeActionsService {
@ -87,7 +88,7 @@ export class ChallengeActionsService {
active: false,
end: now,
penaltyStart: now,
penaltyEnd: new Date(now.getTime() + 30 * 60 * 1000),
penaltyEnd: new Date(now.getTime() + Constants.PENALTY_TIME * 60 * 1000),
}
}
return await this.prisma.challengeAction.update({

View File

@ -0,0 +1,21 @@
export const Constants = {
/**
* Nombre de points attribués au début de la partie
*/
INITIAL_MONEY: 2000,
/**
* Nombre de points attribués lors d'une nouvelle tentative
*/
NEW_RUN_MONEY: 300,
/**
* Nombre de points requis pour l'achat d'un train par kilomètre (arrondi au supérieur)
*/
PRICE_PER_KILOMETER: 10,
/**
* Temps de pénalité en minutes en cas d'échec d'un défi
*/
PENALTY_TIME: 30,
}

View File

@ -2,6 +2,7 @@ import { ConflictException, Injectable } from '@nestjs/common'
import { Game, MoneyUpdate, MoneyUpdateType } from '@prisma/client'
import { PrismaService } from 'src/prisma/prisma.service'
import { RepairGame } from './dto/repair-game.dto'
import { Constants } from 'src/common/utils/constants.utils'
@Injectable()
export class GameService {
@ -23,7 +24,7 @@ export class GameService {
await this.prisma.moneyUpdate.create({
data: {
playerId: player.id,
amount: 2000,
amount: Constants.INITIAL_MONEY,
reason: MoneyUpdateType.START,
}
})
@ -87,7 +88,7 @@ export class GameService {
await this.prisma.moneyUpdate.create({
data: {
playerId: newRunnerId,
amount: 300,
amount: Constants.NEW_RUN_MONEY,
reason: MoneyUpdateType.NEW_RUN,
runId: newRun.id,
}
@ -139,7 +140,7 @@ export class GameService {
const trainMoneyUpdate = await this.prisma.moneyUpdate.create({
data: {
playerId: train.playerId,
amount: -10 * Math.ceil(train.distance / 1000),
amount: -Constants.PRICE_PER_KILOMETER * Math.ceil(train.distance / 1000),
reason: MoneyUpdateType.BUY_TRAIN,
tripId: train.id,
}

View File

@ -9,6 +9,7 @@ import { ImportTrainDto } from './dto/import-train.dto'
import { InterrailJourney, InterrailLegInfo, InterrailTravelInfo } from './dto/interrail-api.dto'
import { distanceCoordinates } from 'src/common/utils/calculus.utils'
import { PlayerFilterDto } from 'src/common/dto/player_filter.dto'
import { Constants } from 'src/common/utils/constants.utils'
@Injectable()
export class TrainsService {
@ -87,7 +88,7 @@ export class TrainsService {
const oldCoordinates = legInfoJson.trainStopStations.at(index - 1).coordinates
return distance + distanceCoordinates({ from: oldCoordinates, to: coordinates })
}, 0)
const cost = 10 * 10 * Math.ceil(distance / 1000)
const cost = Constants.PRICE_PER_KILOMETER * Math.ceil(distance / 1000)
const train = await this.prisma.trainTrip.create({
data: {