From aa833736d32993b1656abeeb02a4f8b021ec6252 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Mon, 1 Mar 2021 16:58:34 +0100 Subject: [PATCH] Trackscans now have a laptime that get's calculated on creation ref #151 --- src/models/actions/create/CreateTrackScan.ts | 15 ++++++++------- src/models/entities/TrackScan.ts | 10 ++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/models/actions/create/CreateTrackScan.ts b/src/models/actions/create/CreateTrackScan.ts index 89ccb5b..578497f 100644 --- a/src/models/actions/create/CreateTrackScan.ts +++ b/src/models/actions/create/CreateTrackScan.ts @@ -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 { + public validateScan(scan: TrackScan): boolean { + return (scan.lapTime > scan.track.minimumLapTime); + } + + public async getLaptime(scan: TrackScan): Promise { 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); } } \ No newline at end of file diff --git a/src/models/entities/TrackScan.ts b/src/models/entities/TrackScan.ts index cd8049a..17983f3 100644 --- a/src/models/entities/TrackScan.ts +++ b/src/models/entities/TrackScan.ts @@ -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. */