// Start of a heat per EFRA / IFMAR: the count-down before it ("prologue"), the transponder // check during that count-down, and when a car's own clock starts at the latest. // // Transponder check: every passing the main decoder sees is remembered (transponder -> when), // independent of whether a session is running, so the Race Day panel can show which drivers // in the selected heat have been seen since the count-down began and which unknown // transponders crossed the line. const MAX_SIGHTINGS = 500; export function recordTransponderSighting(sightings, transponder, at = Date.now()) { const key = String(transponder || "").trim(); if (!key) return sightings; const previous = sightings.get(key); sightings.delete(key); // re-insert so Map order stays oldest -> newest sightings.set(key, { transponder: key, lastSeenAt: at, count: (previous?.count || 0) + 1 }); while (sightings.size > MAX_SIGHTINGS) sightings.delete(sightings.keys().next().value); return sightings; } // entrants: drivers in the heat, in start order. drivers: every registered driver, used to // name the owner of a transponder that crossed the line but doesn't belong to this heat. export function buildWarmupCheck({ entrants = [], drivers = [], sightings = new Map(), since = 0 }) { const seenSince = (transponder) => { const sighting = transponder ? sightings.get(String(transponder).trim()) : null; return sighting && sighting.lastSeenAt >= since ? sighting : null; }; const rows = entrants.map((driver) => { const sighting = seenSince(driver.transponder); return { driverId: driver.id, name: driver.name, transponder: String(driver.transponder || ""), seen: Boolean(sighting), lastSeenAt: sighting?.lastSeenAt || null }; }); const entrantTransponders = new Set(rows.map((row) => row.transponder).filter(Boolean)); const unknownTransponders = [...sightings.values()] .filter((sighting) => sighting.lastSeenAt >= since && !entrantTransponders.has(sighting.transponder)) .map((sighting) => ({ ...sighting, ownerName: drivers.find((driver) => String(driver.transponder || "") === sighting.transponder)?.name || "" })) .sort((a, b) => b.lastSeenAt - a.lastSeenAt); return { rows, total: rows.length, seenCount: rows.filter((row) => row.seen).length, missing: rows.filter((row) => !row.seen), unknownTransponders, }; } // --- Prologue: the count-down before a heat -------------------------------------------- // Race Day's Start runs a prologue first (EVERLAPS' term): the track is open, the announcer // counts down (EFRA 9.2b: 2 min, 1 min, 30 s, 10 s) and the warm-up check above shows who // has crossed the line. The race director can hold it (cars being put on the grid), add time // or jump ahead. State is a plain object stored on the session so a reload keeps it. export const PROLOGUE_OPTIONS_SEC = Object.freeze([0, 30, 60, 90, 120, 180, 300]); export const DEFAULT_PROLOGUE_SEC = 120; // Grid starts (mass/position) and staggered releases hand over to the start procedure at // 3 s: IFMAR 4.2.8 - "from 3 seconds the counting stops and the start signal is given // between 0 and 5 seconds" (our random delay). A flying start ends at 0 with the horn. const GRID_HANDOVER_MS = 3000; export function normalizePrologueSec(value, fallback = DEFAULT_PROLOGUE_SEC) { const seconds = Number(value); return PROLOGUE_OPTIONS_SEC.includes(seconds) ? seconds : fallback; } // Staggered and flying starts give every car its own clock from its own first crossing. export function usesPersonalClock(startMode) { return startMode === "staggered" || startMode === "flying"; } export function getPrologueHandoverMs(startMode) { return startMode === "flying" ? 0 : GRID_HANDOVER_MS; } export function createPrologue(sessionId, totalMs, now = Date.now()) { return { sessionId, totalMs, startedAt: now, endsAt: now + totalMs, heldRemainingMs: null, announced: [] }; } export function getPrologueRemainingMs(prologue, now = Date.now()) { if (!prologue) return 0; if (prologue.heldRemainingMs !== null && prologue.heldRemainingMs !== undefined) return Math.max(0, prologue.heldRemainingMs); return Math.max(0, Number(prologue.endsAt || 0) - now); } export function holdPrologue(prologue, now = Date.now()) { if (!prologue || prologue.heldRemainingMs !== null) return prologue; return { ...prologue, heldRemainingMs: getPrologueRemainingMs(prologue, now) }; } export function resumePrologue(prologue, now = Date.now()) { if (!prologue || prologue.heldRemainingMs === null) return prologue; return { ...prologue, endsAt: now + prologue.heldRemainingMs, heldRemainingMs: null }; } // Sets a new remaining time (+1 min, "to 10 s", IFMAR 4.2.8 "re-start from 30 s"). Calls // for marks above the new time are forgotten so they are made again on the way down. export function setPrologueRemaining(prologue, remainingMs, now = Date.now()) { if (!prologue) return prologue; const value = Math.max(0, Math.round(remainingMs)); const announced = prologue.announced.filter((marker) => getMarkMs(marker) > value); const totalMs = Math.max(prologue.totalMs, value); return prologue.heldRemainingMs !== null ? { ...prologue, totalMs, announced, heldRemainingMs: value } : { ...prologue, totalMs, announced, endsAt: now + value }; } function getMarkMs(marker) { const [, unit, amount] = /^([ms])(\d+)$/.exec(marker) || []; return unit === "m" ? Number(amount) * 60000 : Number(amount) * 1000; } function getPrologueMarks(startMode) { const marks = [5, 4, 3, 2].map((minutes) => ({ marker: `m${minutes}`, event: "prologue_minutes", variables: { minutes } })); marks.push({ marker: "m1", event: "race_1_minute" }); // IFMAR 4.2.5: from 30 s the cars may be placed in the starting boxes. marks.push({ marker: "s30", event: startMode === "flying" ? "race_30_seconds" : "prologue_grid" }); marks.push({ marker: "s10", event: "race_10_seconds" }); // IFMAR 4.2.6: from 10 s to 3 s the time is counted down second by second. if (startMode !== "flying") [9, 8, 7, 6, 5, 4, 3].forEach((seconds) => marks.push({ marker: `s${seconds}`, event: "prologue_count", variables: { seconds } })); return marks.map((mark) => ({ ...mark, ms: getMarkMs(mark.marker) })); } // Which call is due now. Marks at or above the prologue's own length are skipped (the // opening call already says the time), and when several are passed at once (a jump to // 10 s) they are all marked done but only the latest one is spoken. export function takeDuePrologueCall(prologue, startMode, now = Date.now()) { if (!prologue) return { prologue, call: null }; const remaining = getPrologueRemainingMs(prologue, now); const due = getPrologueMarks(startMode) .filter((mark) => mark.ms < prologue.totalMs - 500 && remaining <= mark.ms && !prologue.announced.includes(mark.marker)); if (!due.length) return { prologue, call: null }; return { prologue: { ...prologue, announced: [...prologue.announced, ...due.map((mark) => mark.marker)] }, call: due[due.length - 1], }; } // Label for a length choice in the event form and on Race Day: "Go", "30 s", "1:30 min". export function formatPrologueOption(seconds, t) { if (!seconds) return t("prologue.option_go"); if (seconds < 60) return `${seconds} s`; return seconds % 60 ? `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")} min` : `${seconds / 60} min`; } export function formatPrologueTime(ms, language = "sv") { const total = Math.round(ms / 1000); const minutes = Math.floor(total / 60); const seconds = total % 60; const sv = language === "sv"; const minuteText = minutes ? (sv ? `${minutes} ${minutes === 1 ? "minut" : "minuter"}` : `${minutes} ${minutes === 1 ? "minute" : "minutes"}`) : ""; const secondText = seconds || !minutes ? (sv ? `${seconds} sekunder` : `${seconds} seconds`) : ""; return [minuteText, secondText].filter(Boolean).join(sv ? " och " : " "); } // --- Latest personal clock start ------------------------------------------------------- // A car that doesn't cross the line in time doesn't get a later clock of its own: // - staggered (EFRA 9.3, IFMAR 4.2.3): the clock starts at the latest when any car completes // a full lap, or 1 minute after the start, whichever comes first; // - flying (EFRA 9.3): at the latest 150 % of a standard lap after "clock is running". The // standard lap is the first lap anyone completes in the heat. // Returns null while that moment isn't known yet. export function getLatestClockStartTs(session, result) { const goTs = Number(session?.startedAt || 0); if (!goTs || !usesPersonalClock(session?.startMode)) return null; const firstLap = (result?.passings || []) .filter((passing) => passing.validLap !== false && Number(passing.lapMs) > 0) .sort((a, b) => a.timestamp - b.timestamp)[0] || null; if (session.startMode === "staggered") return Math.min(goTs + 60000, firstLap ? firstLap.timestamp : Number.POSITIVE_INFINITY); return firstLap ? goTs + Math.round(firstLap.lapMs * 1.5) : null; }