Trackscans now have a laptime that get's calculated on creation

ref #151
This commit is contained in:
Nicolai Ort 2021-03-01 16:58:34 +01:00
parent 771a205fe6
commit aa833736d3
2 changed files with 18 additions and 7 deletions

View File

@ -44,6 +44,7 @@ export class CreateTrackScan {
}
newScan.timestamp = Math.round(new Date().getTime() / 1000);
newScan.lapTime = await this.getLaptime(newScan)
newScan.valid = await this.validateScan(newScan);
return newScan;
@ -65,15 +66,15 @@ export class CreateTrackScan {
return station;
}
public async validateScan(scan: TrackScan): Promise<boolean> {
public validateScan(scan: TrackScan): boolean {
return (scan.lapTime > scan.track.minimumLapTime);
}
public async getLaptime(scan: TrackScan): Promise<number> {
const scans = await getConnection().getRepository(TrackScan).find({ where: { runner: scan.runner, valid: true }, relations: ["track"] });
if (scans.length == 0) { return true; }
if (scans.length == 0) { return 0; }
const newestScan = scans[scans.length - 1];
if ((scan.timestamp - newestScan.timestamp) > scan.track.minimumLapTime) {
return true;
}
return false;
return (scan.timestamp - newestScan.timestamp);
}
}

View File

@ -2,6 +2,8 @@ import {
IsInt,
IsNotEmpty,
IsNumber,
IsPositive
} from "class-validator";
import { ChildEntity, Column, ManyToOne } from "typeorm";
@ -59,6 +61,14 @@ export class TrackScan extends Scan {
@IsInt()
timestamp: number;
/**
* The scan's lap time.
* This simply get's calculated from the last lap time;
*/
@Column()
@IsNumber()
lapTime: number;
/**
* Turns this entity into it's response class.
*/