// Suspiciously short laps in practice: new drivers and kids sometimes cut across the track, // and the lap lands just above the minimum lap time. Such a lap is far quicker than the // driver's own pace on the same layout, so it is flagged for a look. The flag is only a hint: // the lap counts like any other (best lap, stats) until someone removes it with one click; // Approve just drops the flag. It is never called out as a personal best or record. export const SUSPECT_LAP_RATIO = 0.85; export const MIN_REFERENCE_LAPS = 5; // Short enough that the reference follows a driver who gets faster during the day. const REFERENCE_WINDOW = 10; function median(values) { const sorted = [...values].sort((a, b) => a - b); const middle = Math.floor(sorted.length / 2); return sorted.length % 2 ? sorted[middle] : (sorted[middle - 1] + sorted[middle]) / 2; } // The driver's recent laps on this layout: counted (not removed) and not the first lap of a // stint (those span a break and say nothing about pace). Laps still flagged as suspect are // part of it on purpose: one shortcut hardly moves the median, but a driver who gets faster // during the day (slow warm-up laps, then a real pace) used to have every new lap flagged - // the flagged laps never entered the reference, so it stayed at the slow pace for good. export function getSuspectReferenceLaps(passings, competitorKey, trackLayoutId = "") { return (Array.isArray(passings) ? passings : []) .filter((passing) => passing.competitorKey === competitorKey && passing.validLap !== false && !passing.stintBoundary && String(passing.trackLayoutId ?? trackLayoutId) === String(trackLayoutId)) .slice(-REFERENCE_WINDOW) .map((passing) => Number(passing.lapMs || 0)) .filter((lapMs) => lapMs > 500); } export function isSuspectLap(lapMs, referenceLaps) { if (!Array.isArray(referenceLaps) || referenceLaps.length < MIN_REFERENCE_LAPS) return false; return Number(lapMs) > 0 && Number(lapMs) < median(referenceLaps) * SUSPECT_LAP_RATIO; } // Suspect laps still waiting for a decision, newest first. export function listPendingSuspectLaps(result) { return (Array.isArray(result?.passings) ? result.passings : []) .filter((passing) => passing.suspect && passing.validLap !== false) .sort((a, b) => Number(b.timestamp || 0) - Number(a.timestamp || 0)); } // Pending suspect laps of one driver that no longer stand out against the driver's pace now - // it was the driver getting faster, not a shortcut - lose their flag. A real shortcut stays // flagged. Returns the released laps. export function releaseSettledSuspectLaps(passings, competitorKey, trackLayoutId = "") { const reference = getSuspectReferenceLaps(passings, competitorKey, trackLayoutId); const released = []; (Array.isArray(passings) ? passings : []).forEach((passing) => { if (passing.competitorKey !== competitorKey || !passing.suspect || passing.validLap === false) return; if (String(passing.trackLayoutId ?? trackLayoutId) !== String(trackLayoutId)) return; if (isSuspectLap(Number(passing.lapMs || 0), reference)) return; passing.suspect = false; passing.suspectReleased = true; released.push(passing); }); return released; }