Paiement automatique d'une course
This commit is contained in:
parent
481400d404
commit
8681752888
@ -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({
|
return await this.prisma.game.update({
|
||||||
where: { id: 1 },
|
where: { id: 1 },
|
||||||
data: {
|
data: {
|
||||||
|
@ -95,6 +95,7 @@ export class TrainsController {
|
|||||||
* Importation d'un trajet en train à partir de Rail Planner
|
* Importation d'un trajet en train à partir de Rail Planner
|
||||||
*
|
*
|
||||||
* @throws {401} Non authentifié⋅e
|
* @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
|
* @throws {422} Le voyage Interrail à importer contient plusieurs trajets ou plusieurs trains
|
||||||
*/
|
*/
|
||||||
@Post("/import")
|
@Post("/import")
|
||||||
|
@ -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 { CreateTrainDto } from './dto/create-train.dto'
|
||||||
import { UpdateTrainDto } from './dto/update-train.dto'
|
import { UpdateTrainDto } from './dto/update-train.dto'
|
||||||
import { PrismaService } from 'src/prisma/prisma.service'
|
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 { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
|
||||||
import { paginate } from 'src/common/utils/pagination.utils'
|
import { paginate } from 'src/common/utils/pagination.utils'
|
||||||
import { ImportTrainDto } from './dto/import-train.dto'
|
import { ImportTrainDto } from './dto/import-train.dto'
|
||||||
@ -54,6 +54,9 @@ export class TrainsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async import(player: Player, { id: trainId }: ImportTrainDto): Promise<TrainTrip> {
|
async import(player: Player, { id: trainId }: ImportTrainDto): Promise<TrainTrip> {
|
||||||
|
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}`)
|
const interrailResult: InterrailJourney = await fetch(`https://3uiwjsimnh.execute-api.eu-central-1.amazonaws.com/Prod/journey-import?id=${trainId}`)
|
||||||
.then(data => data.json())
|
.then(data => data.json())
|
||||||
if (interrailResult.data.travels.length !== 1)
|
if (interrailResult.data.travels.length !== 1)
|
||||||
@ -80,12 +83,10 @@ export class TrainsService {
|
|||||||
const oldCoordinates = legInfoJson.trainStopStations.at(index - 1).coordinates
|
const oldCoordinates = legInfoJson.trainStopStations.at(index - 1).coordinates
|
||||||
return distance + distanceCoordinates({ from: oldCoordinates, to: coordinates })
|
return distance + distanceCoordinates({ from: oldCoordinates, to: coordinates })
|
||||||
}, 0)
|
}, 0)
|
||||||
|
const cost = 10 * 10 * Math.ceil(distance / 1000)
|
||||||
|
|
||||||
return this.prisma.trainTrip.upsert({
|
const train = await this.prisma.trainTrip.create({
|
||||||
where: {
|
data: {
|
||||||
id: trainId,
|
|
||||||
},
|
|
||||||
create: {
|
|
||||||
id: trainId,
|
id: trainId,
|
||||||
playerId: player.id,
|
playerId: player.id,
|
||||||
distance: distance,
|
distance: distance,
|
||||||
@ -95,15 +96,16 @@ export class TrainsService {
|
|||||||
arrivalTime: arrival,
|
arrivalTime: arrival,
|
||||||
infoJson: leg.infoJson,
|
infoJson: leg.infoJson,
|
||||||
},
|
},
|
||||||
update: {
|
})
|
||||||
|
// Ajout du trajet en points
|
||||||
|
await this.prisma.moneyUpdate.create({
|
||||||
|
data: {
|
||||||
playerId: player.id,
|
playerId: player.id,
|
||||||
distance: distance,
|
amount: -cost,
|
||||||
from: travel.from,
|
reason: MoneyUpdateType.BUY_TRAIN,
|
||||||
to: travel.to,
|
tripId: train.id,
|
||||||
departureTime: departure,
|
|
||||||
arrivalTime: arrival,
|
|
||||||
infoJson: leg.infoJson,
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
return train
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user