|
| 1 | +import type { |
| 2 | + Session, |
| 3 | + SessionBarSnapshot, |
| 4 | + SessionLifecycleEvent, |
| 5 | + Telemetry, |
| 6 | +} from '@irdashies/types'; |
| 7 | +import type { TelemetryProcessor } from './TelemetryProcessor'; |
| 8 | + |
| 9 | +const n = (f: Telemetry, k: keyof Telemetry): number | undefined => { |
| 10 | + const v = f[k]?.value?.[0]; |
| 11 | + return typeof v === 'number' && Number.isFinite(v) ? v : undefined; |
| 12 | +}; |
| 13 | +const a = (f: Telemetry, k: keyof Telemetry): readonly unknown[] => { |
| 14 | + const value = f[k]?.value; |
| 15 | + return Array.isArray(value) ? value : []; |
| 16 | +}; |
| 17 | + |
| 18 | +export class SessionBarProcessor implements TelemetryProcessor<SessionBarSnapshot> { |
| 19 | + readonly channel = 'session-bar.snapshot'; |
| 20 | + readonly tickRateHz = 5; |
| 21 | + private session?: Session; |
| 22 | + private lastTime = -Infinity; |
| 23 | + private lap = -1; |
| 24 | + private lapTop = 0; |
| 25 | + private enabled = true; |
| 26 | + private readonly latest = this.empty(); |
| 27 | + |
| 28 | + init(session: Session): void { |
| 29 | + this.session = session; |
| 30 | + } |
| 31 | + onFrame(frame: Telemetry): void { |
| 32 | + if (!this.enabled) return; |
| 33 | + const time = n(frame, 'SessionTime'); |
| 34 | + if (time === undefined) return; |
| 35 | + const sessionNum = n(frame, 'SessionNum') ?? null; |
| 36 | + if ( |
| 37 | + this.latest.sessionNum !== null && |
| 38 | + sessionNum !== this.latest.sessionNum |
| 39 | + ) |
| 40 | + this.reset(sessionNum); |
| 41 | + const currentLap = n(frame, 'Lap') ?? 0; |
| 42 | + const speed = n(frame, 'Speed') ?? 0; |
| 43 | + if (this.lap >= 0 && currentLap !== this.lap) { |
| 44 | + this.latest.lastLapTopSpeed = this.lapTop || null; |
| 45 | + this.lapTop = 0; |
| 46 | + } |
| 47 | + this.lap = currentLap; |
| 48 | + this.lapTop = Math.max(this.lapTop, speed); |
| 49 | + this.latest.sessionBestTopSpeed = |
| 50 | + Math.max(this.latest.sessionBestTopSpeed ?? 0, speed) || null; |
| 51 | + if (time < this.lastTime || time - this.lastTime < 0.2 - 1e-6) return; |
| 52 | + this.lastTime = time; |
| 53 | + const info = this.session?.SessionInfo?.Sessions?.find( |
| 54 | + (s) => s.SessionNum === sessionNum |
| 55 | + ); |
| 56 | + const drivers = this.session?.DriverInfo?.Drivers ?? []; |
| 57 | + const playerCarIdx = this.session?.DriverInfo?.DriverCarIdx ?? null; |
| 58 | + const player = drivers.find((d) => d?.CarIdx === playerCarIdx); |
| 59 | + const positions = a(frame, 'CarIdxPosition'); |
| 60 | + const classPositions = a(frame, 'CarIdxClassPosition'); |
| 61 | + this.latest.competitorCarIds.length = 0; |
| 62 | + this.latest.competitorPositions.length = 0; |
| 63 | + let classSize = 0; |
| 64 | + for (const driver of drivers) { |
| 65 | + if (!driver || driver.CarIsPaceCar || driver.IsSpectator) continue; |
| 66 | + if (driver.CarClassID === player?.CarClassID) classSize += 1; |
| 67 | + this.latest.competitorCarIds.push(driver.CarID ?? 0); |
| 68 | + const position = positions[driver.CarIdx]; |
| 69 | + this.latest.competitorPositions.push( |
| 70 | + typeof position === 'number' ? position : 0 |
| 71 | + ); |
| 72 | + } |
| 73 | + const bestLaps = a(frame, 'CarIdxBestLapTime'); |
| 74 | + let sessionBest: number | undefined; |
| 75 | + for (const value of bestLaps) |
| 76 | + if ( |
| 77 | + typeof value === 'number' && |
| 78 | + value > 0 && |
| 79 | + (sessionBest === undefined || value < sessionBest) |
| 80 | + ) |
| 81 | + sessionBest = value; |
| 82 | + const isClio = player?.CarID === 162; |
| 83 | + Object.assign(this.latest, { |
| 84 | + sessionName: info?.SessionType, |
| 85 | + trackDisplayName: this.session?.WeekendInfo?.TrackDisplayName, |
| 86 | + displayUnits: n(frame, 'DisplayUnits') ?? 0, |
| 87 | + brakeBias: n(frame, isClio ? 'dcPeakBrakeBias' : 'dcBrakeBias'), |
| 88 | + brakeBiasIsClio: isClio, |
| 89 | + incidents: n(frame, 'PlayerCarTeamIncidentCount') ?? 0, |
| 90 | + incidentLimit: this.session?.WeekendInfo?.WeekendOptions?.IncidentLimit, |
| 91 | + incidentWarningInitialLimit: |
| 92 | + this.session?.WeekendInfo?.WeekendOptions?.IncidentWarningInitialLimit, |
| 93 | + incidentWarningSubsequentLimit: |
| 94 | + this.session?.WeekendInfo?.WeekendOptions |
| 95 | + ?.IncidentWarningSubsequentLimit, |
| 96 | + trackWetness: n(frame, 'TrackWetness') ?? 0, |
| 97 | + precipitation: n(frame, 'Precipitation'), |
| 98 | + airTemp: n(frame, 'AirTemp'), |
| 99 | + trackTemp: n(frame, 'TrackTempCrew'), |
| 100 | + windDirection: n(frame, 'WindDir'), |
| 101 | + windVelocity: n(frame, 'WindVel'), |
| 102 | + windYaw: n(frame, 'YawNorth'), |
| 103 | + fuelLevel: n(frame, 'FuelLevel'), |
| 104 | + lastLapTime: n(frame, 'LapLastLapTime'), |
| 105 | + bestLapTime: n(frame, 'LapBestLapTime'), |
| 106 | + sessionBestLap: sessionBest, |
| 107 | + sessionTimeOfDay: n(frame, 'SessionTimeOfDay'), |
| 108 | + playerCarIdx, |
| 109 | + playerCarId: player?.CarID, |
| 110 | + playerOverallPosition: |
| 111 | + typeof positions[playerCarIdx ?? -1] === 'number' |
| 112 | + ? (positions[playerCarIdx ?? -1] as number) |
| 113 | + : 0, |
| 114 | + playerClassPosition: |
| 115 | + typeof classPositions[playerCarIdx ?? -1] === 'number' |
| 116 | + ? (classPositions[playerCarIdx ?? -1] as number) |
| 117 | + : 0, |
| 118 | + playerClassSize: classSize, |
| 119 | + sessionNum, |
| 120 | + version: this.latest.version + 1, |
| 121 | + }); |
| 122 | + } |
| 123 | + onLifecycle(event: SessionLifecycleEvent): void { |
| 124 | + if (event.type === 'enter') { |
| 125 | + this.enabled = !event.replay; |
| 126 | + if (event.replay) this.reset(null); |
| 127 | + } else this.reset(null); |
| 128 | + } |
| 129 | + snapshot(): SessionBarSnapshot { |
| 130 | + return this.latest; |
| 131 | + } |
| 132 | + private reset(sessionNum: number | null): void { |
| 133 | + const version = this.latest.version + 1; |
| 134 | + Object.assign(this.latest, this.empty(), { sessionNum, version }); |
| 135 | + this.lastTime = -Infinity; |
| 136 | + this.lap = -1; |
| 137 | + this.lapTop = 0; |
| 138 | + } |
| 139 | + private empty(): SessionBarSnapshot { |
| 140 | + return { |
| 141 | + displayUnits: 0, |
| 142 | + brakeBiasIsClio: false, |
| 143 | + incidents: 0, |
| 144 | + trackWetness: 0, |
| 145 | + playerCarIdx: null, |
| 146 | + playerOverallPosition: 0, |
| 147 | + playerClassPosition: 0, |
| 148 | + playerClassSize: 0, |
| 149 | + competitorCarIds: [], |
| 150 | + competitorPositions: [], |
| 151 | + lastLapTopSpeed: null, |
| 152 | + sessionBestTopSpeed: null, |
| 153 | + sessionNum: null, |
| 154 | + version: 0, |
| 155 | + }; |
| 156 | + } |
| 157 | +} |
0 commit comments