diff --git a/server/src/game/game.service.ts b/server/src/game/game.service.ts index 61c6b18..9158f10 100644 --- a/server/src/game/game.service.ts +++ b/server/src/game/game.service.ts @@ -27,7 +27,7 @@ export class GameService { }) } } - const runnerId = alreadyStarted ? game.currentRunnerId : players[Math.trunc(players.length * Math.random())].id + const runnerId = alreadyStarted ? game.currentRunnerId : players[Math.floor(players.length * Math.random())].id return await this.prisma.game.update({ where: { id: 1 }, data: { diff --git a/server/src/trains/trains.controller.ts b/server/src/trains/trains.controller.ts index 28ad57a..1b09c61 100644 --- a/server/src/trains/trains.controller.ts +++ b/server/src/trains/trains.controller.ts @@ -95,6 +95,7 @@ export class TrainsController { * Importation d'un trajet en train à partir de Rail Planner * * @throws {401} Non authentifié⋅e + * @throws {409} Le voyage Interrail est déjà importé * @throws {422} Le voyage Interrail à importer contient plusieurs trajets ou plusieurs trains */ @Post("/import") diff --git a/server/src/trains/trains.service.ts b/server/src/trains/trains.service.ts index d7a0ef3..283b26b 100644 --- a/server/src/trains/trains.service.ts +++ b/server/src/trains/trains.service.ts @@ -1,8 +1,8 @@ -import { Injectable, NotFoundException, UnprocessableEntityException } from '@nestjs/common' +import { ConflictException, HttpException, Injectable, NotFoundException, UnprocessableEntityException } from '@nestjs/common' import { CreateTrainDto } from './dto/create-train.dto' import { UpdateTrainDto } from './dto/update-train.dto' import { PrismaService } from 'src/prisma/prisma.service' -import { Player, TrainTrip } from '@prisma/client' +import { MoneyUpdateType, Player, TrainTrip } from '@prisma/client' import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto' import { paginate } from 'src/common/utils/pagination.utils' import { ImportTrainDto } from './dto/import-train.dto' @@ -54,6 +54,9 @@ export class TrainsService { } async import(player: Player, { id: trainId }: ImportTrainDto): Promise { + if (this.findOne(trainId)) + throw new ConflictException(`Le train avec l'identifiant ${trainId} est déjà importé`) + const interrailResult: InterrailJourney = await fetch(`https://3uiwjsimnh.execute-api.eu-central-1.amazonaws.com/Prod/journey-import?id=${trainId}`) .then(data => data.json()) if (interrailResult.data.travels.length !== 1) @@ -80,12 +83,10 @@ 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) - return this.prisma.trainTrip.upsert({ - where: { - id: trainId, - }, - create: { + const train = await this.prisma.trainTrip.create({ + data: { id: trainId, playerId: player.id, distance: distance, @@ -95,15 +96,16 @@ export class TrainsService { arrivalTime: arrival, infoJson: leg.infoJson, }, - update: { + }) + // Ajout du trajet en points + await this.prisma.moneyUpdate.create({ + data: { playerId: player.id, - distance: distance, - from: travel.from, - to: travel.to, - departureTime: departure, - arrivalTime: arrival, - infoJson: leg.infoJson, + amount: -cost, + reason: MoneyUpdateType.BUY_TRAIN, + tripId: train.id, } }) + return train } }