|
| 1 | +import type { |
| 2 | + LapLogSnapshot, |
| 3 | + Session, |
| 4 | + SessionLifecycleEvent, |
| 5 | + Telemetry, |
| 6 | +} from '@irdashies/types'; |
| 7 | +import type { TelemetryProcessor } from './TelemetryProcessor'; |
| 8 | + |
| 9 | +const numberValue = (frame: Telemetry, key: keyof Telemetry): number => { |
| 10 | + const value = frame[key]?.value?.[0]; |
| 11 | + return typeof value === 'number' && Number.isFinite(value) ? value : 0; |
| 12 | +}; |
| 13 | + |
| 14 | +const booleanValue = (frame: Telemetry, key: keyof Telemetry): boolean => { |
| 15 | + const value = frame[key]?.value?.[0]; |
| 16 | + return value === true || value === 1; |
| 17 | +}; |
| 18 | + |
| 19 | +export class LapLogProcessor implements TelemetryProcessor<LapLogSnapshot> { |
| 20 | + readonly channel = 'lap-log.snapshot'; |
| 21 | + readonly tickRateHz = 25; |
| 22 | + private readonly bestLaps: number[] = []; |
| 23 | + private readonly latest = this.empty(); |
| 24 | + |
| 25 | + init(session: Session): void { |
| 26 | + void session; |
| 27 | + } |
| 28 | + |
| 29 | + onFrame(frame: Telemetry): void { |
| 30 | + const source = frame.CarIdxBestLapTime?.value; |
| 31 | + this.bestLaps.length = Array.isArray(source) ? source.length : 0; |
| 32 | + for (let index = 0; index < this.bestLaps.length; index += 1) { |
| 33 | + const value = source?.[index]; |
| 34 | + this.bestLaps[index] = |
| 35 | + typeof value === 'number' && Number.isFinite(value) ? value : 0; |
| 36 | + } |
| 37 | + Object.assign(this.latest, { |
| 38 | + lapCompleted: numberValue(frame, 'LapCompleted'), |
| 39 | + currentLapTime: numberValue(frame, 'LapCurrentLapTime'), |
| 40 | + lastLapTime: numberValue(frame, 'LapLastLapTime'), |
| 41 | + bestLapTime: numberValue(frame, 'LapBestLapTime'), |
| 42 | + sessionNum: numberValue(frame, 'SessionNum'), |
| 43 | + sessionTime: numberValue(frame, 'SessionTime'), |
| 44 | + playerTrackSurface: numberValue(frame, 'PlayerTrackSurface'), |
| 45 | + incidentCount: numberValue(frame, 'PlayerCarMyIncidentCount'), |
| 46 | + lapDistPct: numberValue(frame, 'LapDistPct'), |
| 47 | + deltaToSessionLastLap: numberValue(frame, 'LapDeltaToSessionLastlLap'), |
| 48 | + deltaToSessionLastLapOk: booleanValue( |
| 49 | + frame, |
| 50 | + 'LapDeltaToSessionLastlLap_OK' |
| 51 | + ), |
| 52 | + deltaToSessionBestLap: numberValue(frame, 'LapDeltaToSessionBestLap'), |
| 53 | + deltaToSessionBestLapOk: booleanValue( |
| 54 | + frame, |
| 55 | + 'LapDeltaToSessionBestLap_OK' |
| 56 | + ), |
| 57 | + version: this.latest.version + 1, |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + onLifecycle(event: SessionLifecycleEvent): void { |
| 62 | + if (event.type === 'enter') return; |
| 63 | + Object.assign(this.latest, this.empty(), { |
| 64 | + version: this.latest.version + 1, |
| 65 | + }); |
| 66 | + this.bestLaps.length = 0; |
| 67 | + } |
| 68 | + |
| 69 | + snapshot(): LapLogSnapshot { |
| 70 | + return this.latest; |
| 71 | + } |
| 72 | + |
| 73 | + private empty(): LapLogSnapshot { |
| 74 | + return { |
| 75 | + lapCompleted: 0, |
| 76 | + currentLapTime: 0, |
| 77 | + lastLapTime: 0, |
| 78 | + bestLapTime: 0, |
| 79 | + carIdxBestLapTime: this.bestLaps, |
| 80 | + sessionNum: null, |
| 81 | + sessionTime: 0, |
| 82 | + playerTrackSurface: 0, |
| 83 | + incidentCount: 0, |
| 84 | + lapDistPct: 0, |
| 85 | + deltaToSessionLastLap: 0, |
| 86 | + deltaToSessionLastLapOk: false, |
| 87 | + deltaToSessionBestLap: 0, |
| 88 | + deltaToSessionBestLapOk: false, |
| 89 | + version: 0, |
| 90 | + }; |
| 91 | + } |
| 92 | +} |
0 commit comments