You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

83 lines
2.6 KiB

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Sequelize } from 'sequelize-typescript';
import { Race } from './race.model';
import { RaceResult } from 'src/race-results/race-result.model';
import { Season } from 'src/seasons/season.model';
//https://github.com/GreepTheSheep/node-trackmania.io
import * as TMIO from 'trackmania.io';
import { from } from 'rxjs';
import { Racer } from 'src/racers/racer.model';
@Injectable()
export class RacesService {
TMIO: any;
constructor(
@InjectModel(Race) private raceModel: typeof Race,
private sequelize: Sequelize
)
{
this.TMIO = new TMIO.Client();
}
async findOne(id: number) {
return this.raceModel.findOne({
where: {id: id},
include:[{model: RaceResult, include:[Racer]}, Season]
});
}
async findManyBySeason( seasonId: number) {
return this.raceModel.findAll({
where: { seasonId : seasonId },
include:[RaceResult]
})
}
async findOneBySeasonAndMapUID(seasonId: number, mapUID: string) {
return this.raceModel.findOne({ where: {seasonId: seasonId, mapUID: mapUID} })
}
async getMapDetails(mapUId: string) {
let promise = new Promise((resolve, reject) => {
try{
this.TMIO.maps.get(mapUId).then(async mapInfo => {
console.log(mapInfo);
resolve(mapInfo._data);
});
} catch (error) {
reject(error);
}
});
return promise;
}
async create(mapUID: string, startDate: Date, endDate: Date, seasonId: number) {
try {
let mapDetails = await this.getMapDetails(mapUID) as any;
await this.sequelize.transaction( async t => {
const transactionHost = { transaction: t };
await this.raceModel.create({
mapName: mapDetails.name,
mapURL: mapDetails.fileUrl,
mapUID: mapUID,
mapImgUrl: mapDetails.thumbnailUrl,
authorTime: mapDetails.authorScore,
goldTime: mapDetails.goldScore,
silverTime: mapDetails.silverScore,
bronzeTime: mapDetails.bronzeScore,
startDate: startDate,
endDate: endDate,
seasonId: seasonId
},
transactionHost
);
});
} catch (error) {
}
}
}