Files
Live_RC/src/timing_logic.js
T
larssandandClaude Opus 5.5 5bfb67ea02 feat(race-day): start heats with an EFRA/IFMAR count-down and flying start
Race Day Start now runs a count-down (Go, 30 s - 5 min, default per
event) with calls every minute, 30 s and 10 s, grid call and 9..3
count for grid starts, hold/resume/+1 min/to 30 s/to 10 s/Go now, and
starts the heat by itself when it runs out. The transponder check runs
during the count-down, replacing the separate warm-up.

Adds a flying start mode (horn + clock is running, own clock per car)
as the qualifying default, and the latest personal clock start rules
from EFRA 9.3 / IFMAR 4.2.3 for flying and staggered starts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 21:27:20 +02:00

1019 lines
38 KiB
JavaScript

export function getSessionTypeLabel(type, { t }) {
const key = `session.${String(type || "").toLowerCase()}`;
const translated = t(key);
return translated === key ? String(type || "") : translated;
}
export function getStatusLabel(status, { t }) {
const key = `status.${String(status || "").toLowerCase()}`;
const translated = t(key);
return translated === key ? String(status || "") : translated;
}
export function isUntimedSession(session) {
return String(session?.type || "").toLowerCase() === "open_practice";
}
export function getActiveSession({ state }) {
return state.sessions.find((s) => s.id === state.activeSessionId) || null;
}
export function getSessionTargetMs(session, { isUntimedSession }) {
if (isUntimedSession(session)) {
return null;
}
return Math.max(1, Number(session?.durationMin || 0)) * 60 * 1000;
}
export function getSessionLapWindow(session, { state }) {
const event = state.events.find((item) => item.id === session?.eventId);
if (event?.mode !== "race") {
return { minLapMs: 0, maxLapMs: Number.POSITIVE_INFINITY };
}
const minLapMs = Math.max(0, Number(event?.raceConfig?.minLapMs || 0) || 0);
const configuredMaxLapMs = Math.max(0, Number(event?.raceConfig?.maxLapMs || 60000) || 60000);
const maxLapMs = configuredMaxLapMs > 0 ? Math.max(configuredMaxLapMs, minLapMs || 0) : Number.POSITIVE_INFINITY;
return { minLapMs, maxLapMs };
}
export function isCountedPassing(passing) {
return passing?.validLap !== false;
}
export function getVisiblePassings(result, { isCountedPassing }) {
return Array.isArray(result?.passings) ? result.passings.filter((passing) => isCountedPassing(passing)) : [];
}
export function getPassingValidationLabel(passing, { t }) {
if (passing?.validationWarning === "above_max_stint") {
return t("timing.long_lap_stint");
}
if (passing?.validLap === false) {
if (passing.invalidReason === "below_min") {
return t("timing.invalid_short");
}
if (passing.invalidReason === "manual_invalid") {
return t("timing.invalid_manual");
}
return t("timing.invalid_long");
}
return t("timing.valid_passing");
}
export function getSessionTiming(session, nowTs = Date.now(), { getSessionTargetMs }) {
const baseTargetMs = getSessionTargetMs(session);
// A staggered heat doesn't release every car at once (see runStartSequence in
// race_audio.js) - the shared clock and auto-finish threshold need to cover the full
// release schedule too, or the last-released driver's own personal window could still
// be running when the session force-finishes and locks everyone out. Each competitor's
// OWN personal duration (used for buildLeaderboard's personalTimeUp) stays exactly
// durationMin - this only widens the session-level clock, not any individual's quota.
const gridCount = Array.isArray(session?.manualGridIds) && session.manualGridIds.length
? session.manualGridIds.length
: Array.isArray(session?.driverIds) ? session.driverIds.length : 0;
// A flying start's last car gets its clock at the latest ~1.5 laps after the horn (EFRA
// 9.3), so the shared clock gets one extra minute to cover that.
const staggerExtensionMs = baseTargetMs === null ? 0
: session?.startMode === "flying" ? 60000
: session?.startMode === "staggered" && gridCount > 1
? (gridCount - 1) * Math.max(0, Number(session?.staggerGapSec || 3) || 3) * 1000
: 0;
const targetMs = baseTargetMs === null ? null : baseTargetMs + staggerExtensionMs;
const startedAt = Number(session?.startedAt || 0);
const pausedAt = Number(session?.pausedAt || 0) || 0;
const endedAt = Number(session?.endedAt || 0) || 0;
// A finished session's elapsed time is a fixed historical fact - without this, reopening
// or simply leaving a finished session's timing view open would show its "elapsed" time
// still counting up against the real clock, using how long ago it finished rather than
// how long the session itself actually ran.
const clockNow = session?.status === "finished" && endedAt ? endedAt : (pausedAt || nowTs);
const totalPausedMs = Math.max(0, Number(session?.totalPausedMs || 0) || 0);
const elapsedMs = startedAt ? Math.max(0, clockNow - startedAt - totalPausedMs) : 0;
const followUpMs = Math.max(0, Number(session?.followUpSec || 0) || 0) * 1000;
const followUpStartedAt = Number(session?.followUpStartedAt || 0) || 0;
const followUpPausedMs = Math.max(0, Number(session?.followUpPausedMs || 0) || 0);
const followUpActive = Boolean(followUpStartedAt && followUpMs > 0);
const followUpRemainingMs = followUpActive ? Math.max(0, followUpMs - Math.max(0, clockNow - followUpStartedAt - followUpPausedMs)) : 0;
return {
targetMs,
elapsedMs,
remainingMs: targetMs === null ? null : Math.max(0, targetMs - elapsedMs),
untimed: targetMs === null,
followUpActive,
followUpRemainingMs,
};
}
export function ensureSessionResult(sessionId, { state }) {
if (!state.resultsBySession[sessionId]) {
state.resultsBySession[sessionId] = {
passings: [],
splits: [],
pitEvents: [],
pitStops: [],
competitors: {},
adjustments: [],
};
}
if (!Array.isArray(state.resultsBySession[sessionId].passings)) {
state.resultsBySession[sessionId].passings = [];
}
if (!Array.isArray(state.resultsBySession[sessionId].adjustments)) {
state.resultsBySession[sessionId].adjustments = [];
}
if (!Array.isArray(state.resultsBySession[sessionId].splits)) {
state.resultsBySession[sessionId].splits = [];
}
if (!Array.isArray(state.resultsBySession[sessionId].pitEvents)) {
state.resultsBySession[sessionId].pitEvents = [];
}
if (!Array.isArray(state.resultsBySession[sessionId].pitStops)) {
state.resultsBySession[sessionId].pitStops = [];
}
return state.resultsBySession[sessionId];
}
// The all-time fastest lap among every other session in the same class, used to decide
// whether a live lap deserves the "track_record" audio cue. Scoped to sessions run on the
// same track layout as `session` (see track_layouts_logic.js): once a club starts using
// layouts, a lap from a previous, different layout can no longer "beat" today's record.
// Sessions with no trackLayoutId (i.e. every session recorded before this feature existed,
// or an installation that has never configured a layout) compare only against each other -
// this keeps the feature fully inert until a club actually adds a layout.
export function getTrackRecordMs(session, { state }) {
const event = state.events.find((item) => item.id === session?.eventId);
if (!event?.classId) return null;
const candidates = state.sessions.filter((item) =>
item.id !== session?.id
&& String(item.trackLayoutId || "") === String(session?.trackLayoutId || "")
&& state.events.find((race) => race.id === item.eventId)?.classId === event.classId);
const values = candidates
.flatMap((item) => Object.values(state.resultsBySession[item.id]?.competitors || {}).map((row) => Number(row.bestLapMs || 0)))
.filter((value) => value > 500);
return values.length ? Math.min(...values) : null;
}
export function buildLeaderboard(session, { ensureSessionResult, getSessionTargetMs, getCompetitorPassings, isCountedPassing, getCompetitorElapsedMs, getCompetitorSeedMetric, getSessionLapWindow, getPassingValidationLabel, formatSeedMetric, formatRaceClock, formatLeaderboardGap, formatLapDelta, formatLap, t }) {
const result = ensureSessionResult(session.id);
const sessionType = String(session.type || "").toLowerCase();
const targetMs = getSessionTargetMs(session);
const useTargetTieBreak = session.status === "finished";
const useSeedRanking = ["practice", "qualification"].includes(sessionType) && Number(session.seedBestLapCount || 0) > 0;
const isFreePractice = sessionType === "free_practice";
const isOpenPractice = sessionType === "open_practice";
const isRollingPractice = isFreePractice || isOpenPractice;
const { maxLapMs } = getSessionLapWindow(session);
const nowTs = Date.now();
const rows = Object.values(result.competitors).map((row) => {
const allPassings = getCompetitorPassings(session, row, { includeInvalid: true });
const passings = allPassings.filter((passing) => isCountedPassing(passing));
const latestAnyPassing = allPassings.length ? allPassings[allPassings.length - 1] : null;
const latestPassing = passings.length ? passings[passings.length - 1] : null;
const lastPassingTs = latestPassing ? Number(latestPassing.timestamp || 0) : Number(row.lastTimestamp || 0) || 0;
// Excludes paused wall-clock time, the same way the session's own countdown already
// does (getSessionTiming) - otherwise a heat that had a track call isn't comparable to
// one that didn't, even though nobody actually lost race time to the stoppage. Correct
// as long as passings aren't recorded while paused (they aren't - the decoder ignores
// them), so a driver's last pass is always from before or after the full pause, never
// mid-pause.
const totalPausedMs = Math.max(0, Number(session?.totalPausedMs || 0) || 0);
const rawElapsedMs = lastPassingTs
? Math.max(0, lastPassingTs - Number(row.startTimestamp || session.startedAt || lastPassingTs) - totalPausedMs)
: getCompetitorElapsedMs(session, row);
const manualLapAdjustment = Number(row.manualLapAdjustment || 0) || 0;
const manualTimeAdjustmentMs = Number(row.manualTimeAdjustmentMs || 0) || 0;
const totalElapsedMs = Math.max(0, rawElapsedMs + manualTimeAdjustmentMs);
const distanceToTargetMs = Math.abs(targetMs - totalElapsedMs);
const seedMetric = getCompetitorSeedMetric(session, row);
const previousLapMs = passings.length >= 2 ? Number(passings[passings.length - 2].lapMs || 0) : null;
const lastLapMs = latestPassing ? Number(latestPassing.lapMs || 0) : Number(row.lastLapMs || 0) || 0;
const bestLapMs = Number(row.bestLapMs || 0) || 0;
const lapDeltaMs =
lastLapMs && previousLapMs && lastLapMs > 0 && previousLapMs > 0 ? lastLapMs - previousLapMs : null;
const predictionBaseMs =
lastLapMs > 0
? lastLapMs
: bestLapMs > 0
? bestLapMs
: null;
const currentLapElapsedMs = lastPassingTs ? Math.max(0, nowTs - lastPassingTs) : 0;
const predictionStaleMs = predictionBaseMs
? Math.max(
predictionBaseMs * 2,
Number.isFinite(maxLapMs) && maxLapMs > 0 ? maxLapMs : 0,
15000
)
: 0;
const predictionActive = Boolean(predictionBaseMs && lastPassingTs && currentLapElapsedMs <= predictionStaleMs);
const predictedRemainingMs = predictionActive ? predictionBaseMs - currentLapElapsedMs : null;
const predictedProgress = predictionActive ? currentLapElapsedMs / predictionBaseMs : 0;
const predictionTone =
!predictionActive || predictedProgress <= 0.85
? "good"
: predictedProgress <= 1
? "warn"
: "late";
const invalidPending = latestAnyPassing?.validLap === false;
// In staggered mode, each competitor's own window runs from their own first crossing
// (row.startTimestamp), independent of the session's shared clock - so one driver can
// be done for the day while the session itself is still "running" for others who
// started their own window later. Only meaningful in staggered mode: mass/position
// starts already share one clock, which the session-level countdown already covers.
const personalStartTs = Number(row.startTimestamp || 0) || 0;
const personalTimeUp = Boolean(
(session.startMode === "staggered" || session.startMode === "flying") && targetMs !== null && personalStartTs
&& (nowTs - personalStartTs - totalPausedMs) >= targetMs
);
return {
...row,
personalTimeUp,
laps: Math.max(0, Number(row.laps || 0) + manualLapAdjustment),
lastLapMs,
bestLapMs,
lastTimestamp: lastPassingTs || row.lastTimestamp,
totalElapsedMs,
manualLapAdjustment,
manualTimeAdjustmentMs,
distanceToTargetMs,
seedMetric,
previousLapMs,
lapDeltaMs,
predictedRemainingMs,
predictedProgress,
predictionTone,
invalidPending,
invalidLabel: invalidPending ? getPassingValidationLabel(latestAnyPassing) : "",
invalidLapMs: invalidPending ? Number(latestAnyPassing?.lapMs || 0) || 0 : 0,
comparisonMs:
isRollingPractice
? bestLapMs || lastLapMs || Number.MAX_SAFE_INTEGER
: useSeedRanking && seedMetric
? seedMetric.comparableMs
: totalElapsedMs,
resultDisplay:
isRollingPractice
? formatLap(bestLapMs || lastLapMs)
: useSeedRanking && seedMetric
? formatSeedMetric(seedMetric)
: `${Math.max(0, Number(row.laps || 0) + manualLapAdjustment)}/${formatRaceClock(totalElapsedMs)}`,
};
});
rows.sort((a, b) => {
if (isRollingPractice) {
if (a.comparisonMs !== b.comparisonMs) {
return a.comparisonMs - b.comparisonMs;
}
if (b.laps !== a.laps) {
return b.laps - a.laps;
}
return (b.lastTimestamp || 0) - (a.lastTimestamp || 0);
}
if (useSeedRanking) {
if (a.seedMetric && b.seedMetric && a.seedMetric.comparableMs !== b.seedMetric.comparableMs) {
return a.seedMetric.comparableMs - b.seedMetric.comparableMs;
}
if (a.seedMetric && !b.seedMetric) {
return -1;
}
if (!a.seedMetric && b.seedMetric) {
return 1;
}
if (b.laps !== a.laps) {
return b.laps - a.laps;
}
return a.totalElapsedMs - b.totalElapsedMs;
}
if (b.laps !== a.laps) {
return b.laps - a.laps;
}
if (useTargetTieBreak && a.distanceToTargetMs !== b.distanceToTargetMs) {
return a.distanceToTargetMs - b.distanceToTargetMs;
}
return a.totalElapsedMs - b.totalElapsedMs;
});
const leader = rows[0];
return rows.map((row, index) => {
if (!leader) {
return { ...row, gap: "-", leaderGap: "-", gapAhead: "-", lapDelta: "-" };
}
return {
...row,
gap: formatLeaderboardGap(row, leader, { useSeedRanking, useTargetTieBreak, isFreePractice: isRollingPractice }),
leaderGap: formatLeaderboardGap(row, leader, { useSeedRanking, useTargetTieBreak, isFreePractice: isRollingPractice }),
gapAhead: formatLeaderboardGap(row, rows[index - 1], {
useSeedRanking,
useTargetTieBreak,
isFreePractice: isRollingPractice,
selfLabel: t("status.leader"),
}),
lapDelta: formatLapDelta(row.lapDeltaMs),
};
});
}
export function formatLapDelta(deltaMs, { formatLap }) {
if (!deltaMs && deltaMs !== 0) {
return "-";
}
const sign = deltaMs <= 0 ? "-" : "+";
return `${sign}${(Math.abs(deltaMs) / 1000).toFixed(3)}s`;
}
export function formatLeaderboardGap(row, referenceRow, options = {}, { t }) {
if (!referenceRow) {
return "-";
}
if (row.key === referenceRow.key) {
if (options.isFreePractice) {
return t("status.free_practice");
}
return options.selfLabel || (options.useSeedRanking ? t("status.seeded") : t("status.leader"));
}
if (options.isFreePractice) {
if (Number.isFinite(row.comparisonMs) && Number.isFinite(referenceRow.comparisonMs)) {
return `+${((row.comparisonMs - referenceRow.comparisonMs) / 1000).toFixed(3)}s`;
}
return "-";
}
if (options.useSeedRanking) {
if (referenceRow.seedMetric && row.seedMetric) {
const seedGap = Math.max(0, row.seedMetric.comparableMs - referenceRow.seedMetric.comparableMs);
return `+${(seedGap / 1000).toFixed(3)}s`;
}
return `+${Math.max(0, (referenceRow.laps || 0) - (row.laps || 0))}L`;
}
const lapDiff = (referenceRow.laps || 0) - (row.laps || 0);
if (lapDiff > 0) {
return `+${lapDiff}L`;
}
const referenceValue = options.useTargetTieBreak ? referenceRow.distanceToTargetMs : referenceRow.totalElapsedMs;
const rowValue = options.useTargetTieBreak ? row.distanceToTargetMs : row.totalElapsedMs;
const timeGap = Math.max(0, rowValue - referenceValue);
return `+${(timeGap / 1000).toFixed(3)}s`;
}
export function getCompetitorElapsedMs(session, row) {
const startTs = Number(row?.startTimestamp || session?.startedAt || 0);
if (!startTs || !row?.lastTimestamp) {
return 0;
}
const totalPausedMs = Math.max(0, Number(session?.totalPausedMs || 0) || 0);
return Math.max(0, row.lastTimestamp - startTs - totalPausedMs);
}
export function getCompetitorPassings(session, row, options = {}, { ensureSessionResult, isCountedPassing }) {
const result = ensureSessionResult(session.id);
return result.passings
.filter((passing) => {
if (!options.includeInvalid && !isCountedPassing(passing)) {
return false;
}
if (passing.competitorKey) {
return passing.competitorKey === row.key;
}
return (
String(passing.transponder || "") === String(row.transponder || "") &&
String(passing.driverId || "") === String(row.driverId || "") &&
String(passing.carId || "") === String(row.carId || "")
);
})
.sort((a, b) => a.timestamp - b.timestamp);
}
export function getCompetitorSeedMetric(session, row, { getCompetitorPassings }) {
const lapCount = Math.max(0, Number(session?.seedBestLapCount || 0) || 0);
if (lapCount <= 0) {
return null;
}
const method = ["best_sum", "average", "consecutive"].includes(String(session?.seedMethod || "").toLowerCase())
? String(session.seedMethod).toLowerCase()
: "best_sum";
const laps = getCompetitorPassings(session, row)
.map((passing) => Number(passing.lapMs || 0))
.filter((lapMs) => lapMs > 500);
if (laps.length < lapCount) {
return null;
}
let selected = [];
if (method === "consecutive") {
let bestWindow = null;
for (let index = 0; index <= laps.length - lapCount; index += 1) {
const window = laps.slice(index, index + lapCount);
const totalMs = window.reduce((sum, lapMs) => sum + lapMs, 0);
if (!bestWindow || totalMs < bestWindow.totalMs) {
bestWindow = { laps: window, totalMs };
}
}
if (!bestWindow) {
return null;
}
selected = bestWindow.laps;
} else {
selected = [...laps].sort((a, b) => a - b).slice(0, lapCount);
}
const totalMs = selected.reduce((sum, lapMs) => sum + lapMs, 0);
const averageMs = totalMs / lapCount;
return {
lapCount,
method,
totalMs,
averageMs,
comparableMs: method === "average" ? averageMs : totalMs,
laps: selected,
};
}
export function getSessionEntrants(session, { state, getEventDrivers }) {
const event = state.events.find((item) => item.id === session.eventId);
const eventDrivers = event ? getEventDrivers(event) : state.drivers;
if (!Array.isArray(session.driverIds) || !session.driverIds.length) {
return eventDrivers;
}
return eventDrivers.filter((driver) => session.driverIds.includes(driver.id));
}
export function buildPracticeStandings(event, { getSessionsForEvent, buildLeaderboard, getCompetitorSeedMetric, formatSeedMetric, formatRaceClock }) {
const sessions = getSessionsForEvent(event.id).filter((session) => session.type === "practice");
const competitorMap = new Map();
sessions.forEach((session) => {
buildLeaderboard(session).forEach((row) => {
const key = row.driverId || row.key;
const seedMetric = getCompetitorSeedMetric(session, row);
const comparableMs = seedMetric?.comparableMs ?? row.bestLapMs ?? row.totalElapsedMs;
const current = competitorMap.get(key);
if (!current || comparableMs < current.comparableMs) {
competitorMap.set(key, {
key,
driverId: row.driverId,
driverName: row.driverName,
comparableMs,
resultDisplay: seedMetric ? formatSeedMetric(seedMetric) : `${row.laps}/${formatRaceClock(row.totalElapsedMs)}`,
sourceSessionName: session.name,
});
}
});
});
return [...competitorMap.values()].sort((a, b) => a.comparableMs - b.comparableMs).map((row, index) => ({
...row,
rank: index + 1,
score: row.resultDisplay,
}));
}
export function getQualifyingPointsValue(place, fieldSize, tableType) {
const normalized = ["rank_low", "field_desc", "ifmar"].includes(String(tableType || "").toLowerCase())
? String(tableType).toLowerCase()
: "rank_low";
if (normalized === "field_desc") {
return Math.max(1, Number(fieldSize || 0) - place + 1);
}
if (normalized === "ifmar") {
const scale = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
return scale[place - 1] ?? 0;
}
return place;
}
export function isHighPointsTable(tableType) {
return ["field_desc", "ifmar"].includes(String(tableType || "").toLowerCase());
}
export function compareNumberSet(left, right, highWins = false) {
for (let i = 0; i < Math.max(left.length, right.length); i += 1) {
const leftValue = left[i] ?? (highWins ? -Infinity : Infinity);
const rightValue = right[i] ?? (highWins ? -Infinity : Infinity);
if (leftValue !== rightValue) {
return highWins ? rightValue - leftValue : leftValue - rightValue;
}
}
return 0;
}
export function buildQualifyingTieBreakNote(row, tieBreak, { t, formatLap }) {
if (tieBreak === "best_lap") {
return `${t("events.tie_break_note")}: ${t("events.qual_tie_break_best_lap")} • ${formatLap(row.bestSingleLapMs)}`;
}
if (tieBreak === "best_round") {
return `${t("events.tie_break_note")}: ${t("events.qual_tie_break_best_round")} • ${row.bestRoundDisplay || formatLap(row.bestRoundMetric)}`;
}
return `${t("events.tie_break_note")}: ${t("events.counted_rounds_label")} • ${(row.ranks || []).join(" / ") || "-"}`;
}
export function hasQualifyingPrimaryTie(left, right, scoringMode) {
if (!left || !right) {
return false;
}
if (scoringMode === "points") {
return left.totalScore === right.totalScore;
}
return left.bestRank === right.bestRank;
}
export function buildQualifyingStandings(event, { getSessionsForEvent, buildLeaderboard, getSessionEntrants, isHighPointsTable, getQualifyingPointsValue, compareNumberSet, buildQualifyingTieBreakNote, formatLap, formatRaceClock, t, hasQualifyingPrimaryTie }) {
const sessions = getSessionsForEvent(event.id).filter((session) => session.type === "qualification");
const scoringMode = event.raceConfig?.qualifyingScoring || "points";
const countedRounds = Math.max(1, Number(event.raceConfig?.countedQualRounds || 1) || 1);
const pointsTable = event.raceConfig?.qualifyingPointsTable || "rank_low";
const highPointsWin = isHighPointsTable(pointsTable);
const tieBreak = event.raceConfig?.qualifyingTieBreak || "rounds";
const competitorMap = new Map();
sessions.forEach((session) => {
const rows = buildLeaderboard(session);
const entrants = getSessionEntrants(session);
const entrantCount = Math.max(rows.length, entrants.length || 0);
// A driver assigned to this heat who never recorded a single valid passing (dead
// battery, transponder issue, no-show) has no entry in buildLeaderboard's rows at all -
// without this they'd silently vanish not just from this round's standings, but from
// every future round they'd otherwise be reseeded into, and from the final grid
// entirely. Only backfilled once the heat has actually finished - a still-running or
// not-yet-started heat's assigned drivers simply haven't raced yet, which is different.
const rowsWithDns = session.status === "finished"
? [
...rows,
...entrants
.filter((driver) => !rows.some((row) => (row.driverId || row.key) === driver.id))
// bestLapMs is deliberately omitted (not 0) - Number.isFinite(0) is true, and a
// "0ms" lap would otherwise count as the fastest single lap of the round for
// the best_lap tie-break, ahead of every driver who actually raced.
.map((driver) => ({ driverId: driver.id, driverName: driver.name, comparisonMs: Number.MAX_SAFE_INTEGER, resultDisplay: t("events.dns") })),
]
: rows;
rowsWithDns.forEach((row, index) => {
const key = row.driverId || row.key;
if (!competitorMap.has(key)) {
competitorMap.set(key, {
key,
driverId: row.driverId,
driverName: row.driverName,
points: [],
ranks: [],
roundMetrics: [],
bestLaps: [],
});
}
const entry = competitorMap.get(key);
entry.points.push(getQualifyingPointsValue(index + 1, entrantCount, pointsTable));
entry.ranks.push(index + 1);
entry.roundMetrics.push(row.comparisonMs);
if (Number.isFinite(row.bestLapMs)) {
entry.bestLaps.push(row.bestLapMs);
}
if (!entry.bestRoundDisplay || row.comparisonMs < (entry.bestRoundMetricValue ?? Number.MAX_SAFE_INTEGER)) {
entry.bestRoundMetricValue = row.comparisonMs;
entry.bestRoundDisplay = row.resultDisplay;
}
entry.bestResultDisplay = row.resultDisplay;
entry.lastSessionName = session.name;
entry.sessionCount = (entry.sessionCount || 0) + 1;
entry.maxFieldSize = entrantCount;
});
});
const rows = [...competitorMap.values()].map((entry) => {
const sortedPoints = [...entry.points].sort((a, b) => (highPointsWin ? b - a : a - b));
const counted = sortedPoints.slice(0, countedRounds);
const totalScore = counted.reduce((sum, value) => sum + value, 0);
const bestRank = Math.min(...entry.ranks);
const bestRoundMetric = Math.min(...entry.roundMetrics);
const bestSingleLapMs = entry.bestLaps.length ? Math.min(...entry.bestLaps) : Number.MAX_SAFE_INTEGER;
return {
key: entry.key,
driverId: entry.driverId,
driverName: entry.driverName,
ranks: [...entry.ranks].sort((a, b) => a - b),
countedPoints: counted,
totalScore,
bestRank,
bestRoundMetric,
bestRoundDisplay: entry.bestRoundDisplay || formatLap(bestRoundMetric),
bestSingleLapMs,
score:
scoringMode === "points"
? `${totalScore} (${counted.join("+")})`
: `${bestRank} / ${formatRaceClock(bestRoundMetric)}`,
};
});
rows.sort((a, b) => {
if (scoringMode === "points") {
if (a.totalScore !== b.totalScore) {
return highPointsWin ? b.totalScore - a.totalScore : a.totalScore - b.totalScore;
}
if (tieBreak === "best_lap" && a.bestSingleLapMs !== b.bestSingleLapMs) {
return a.bestSingleLapMs - b.bestSingleLapMs;
}
if (tieBreak === "best_round" && a.bestRoundMetric !== b.bestRoundMetric) {
return a.bestRoundMetric - b.bestRoundMetric;
}
const pointDiff = compareNumberSet(a.countedPoints, b.countedPoints, highPointsWin);
if (pointDiff !== 0) {
return pointDiff;
}
return a.bestRoundMetric - b.bestRoundMetric;
}
if (a.bestRank !== b.bestRank) {
return a.bestRank - b.bestRank;
}
if (tieBreak === "rounds") {
const rankDiff = compareNumberSet(a.ranks, b.ranks, false);
if (rankDiff !== 0) {
return rankDiff;
}
}
if (tieBreak === "best_lap" && a.bestSingleLapMs !== b.bestSingleLapMs) {
return a.bestSingleLapMs - b.bestSingleLapMs;
}
return a.bestRoundMetric - b.bestRoundMetric;
});
rows.forEach((row, index) => {
row.tieBreakWonAgainst = "";
row.tieBreakLostAgainst = "";
if (index === 0) {
return;
}
const previous = rows[index - 1];
if (!hasQualifyingPrimaryTie(previous, row, scoringMode)) {
return;
}
previous.tieBreakWonAgainst = previous.tieBreakWonAgainst || row.driverName || t("common.unknown_driver");
row.tieBreakLostAgainst = row.tieBreakLostAgainst || previous.driverName || t("common.unknown_driver");
});
return rows.map((row, index) => ({
...row,
rank: index + 1,
scoreNote: [
buildQualifyingTieBreakNote(row, tieBreak),
row.tieBreakWonAgainst ? `${t("events.tie_break_won")}: ${row.tieBreakWonAgainst}` : "",
row.tieBreakLostAgainst ? `${t("events.tie_break_lost")}: ${row.tieBreakLostAgainst}` : "",
]
.filter(Boolean)
.join(" • "),
}));
}
export function formatTeamActiveMemberLabel(rowOrPassing) {
if (!rowOrPassing) {
return "-";
}
const parts = [rowOrPassing.driverName || "", rowOrPassing.carName || ""].filter(Boolean);
return parts.join(" • ") || rowOrPassing.subLabel || "-";
}
export function buildTeamRaceStandings(event, { getSessionsForEvent, buildLeaderboard, getSessionSortWeight }) {
return getSessionsForEvent(event.id)
.filter((session) => session.type === "team_race")
.sort((left, right) => getSessionSortWeight(left) - getSessionSortWeight(right) || String(left.name).localeCompare(String(right.name)))
.map((session) => ({
session,
rows: buildLeaderboard(session),
}));
}
export function buildSessionChannelStats(session, { ensureSessionResult }) {
const result = ensureSessionResult(session.id);
const summaries = new Map();
const ensureSummary = (competitorKey) => {
const key = String(competitorKey || "unknown");
if (!summaries.has(key)) {
const row = result.competitors?.[key] || {};
summaries.set(key, {
competitorKey: key,
displayName: row.displayName || row.teamName || row.driverName || row.transponder || key,
driverName: row.driverName || "",
carName: row.carName || "",
splitCount: 0,
totalSplitMs: 0,
bestSplitMs: 0,
lastSplitMs: 0,
pitStopCount: 0,
totalPitMs: 0,
bestPitStopMs: 0,
lastPitStopMs: 0,
});
}
return summaries.get(key);
};
(Array.isArray(result.splits) ? result.splits : []).forEach((split) => {
const splitMs = Math.max(0, Number(split?.splitMs || 0) || 0);
if (!splitMs) return;
const summary = ensureSummary(split.competitorKey);
summary.splitCount += 1;
summary.totalSplitMs += splitMs;
summary.bestSplitMs = summary.bestSplitMs ? Math.min(summary.bestSplitMs, splitMs) : splitMs;
summary.lastSplitMs = splitMs;
});
const pitStops = (Array.isArray(result.pitStops) ? result.pitStops : [])
.filter((stop) => Number(stop?.pitStopMs || 0) > 0)
.map((stop, index) => {
const summary = ensureSummary(stop.competitorKey);
const pitStopMs = Math.max(0, Number(stop.pitStopMs || 0) || 0);
summary.pitStopCount += 1;
summary.totalPitMs += pitStopMs;
summary.bestPitStopMs = summary.bestPitStopMs ? Math.min(summary.bestPitStopMs, pitStopMs) : pitStopMs;
summary.lastPitStopMs = pitStopMs;
return {
...stop,
index: index + 1,
pitStopMs,
displayName: summary.displayName,
driverName: stop.driverName || summary.driverName || "",
carName: stop.carName || summary.carName || "",
};
})
.sort((left, right) => Number(left.enteredAt || 0) - Number(right.enteredAt || 0));
const competitors = [...summaries.values()]
.map((summary) => ({
...summary,
averageSplitMs: summary.splitCount ? Math.round(summary.totalSplitMs / summary.splitCount) : 0,
averagePitStopMs: summary.pitStopCount ? Math.round(summary.totalPitMs / summary.pitStopCount) : 0,
}))
.sort((left, right) => right.pitStopCount - left.pitStopCount || right.splitCount - left.splitCount || left.displayName.localeCompare(right.displayName));
return {
splitCount: competitors.reduce((sum, row) => sum + row.splitCount, 0),
pitStopCount: pitStops.length,
totalPitMs: competitors.reduce((sum, row) => sum + row.totalPitMs, 0),
competitors,
pitStops,
};
}
export function buildTeamStintLog(session, row, { getCompetitorPassings, getSessionLapWindow, formatTeamActiveMemberLabel }) {
const passings = getCompetitorPassings(session, row);
if (!passings.length) {
return [];
}
const { maxLapMs } = getSessionLapWindow(session);
const stintGapMs = Number.isFinite(maxLapMs) ? maxLapMs : Number.POSITIVE_INFINITY;
const stints = [];
let current = null;
passings.forEach((passing) => {
const memberLabel = formatTeamActiveMemberLabel(passing);
const memberKey = `${passing.driverId || passing.driverName || "-"}|${passing.carId || passing.carName || "-"}`;
const gapBreak = current && Number.isFinite(stintGapMs) && Math.max(0, passing.timestamp - current.endTs) > stintGapMs;
if (!current || current.memberKey !== memberKey || gapBreak) {
current = {
memberKey,
memberLabel,
driverName: passing.driverName || "-",
carName: passing.carName || "-",
startTs: passing.timestamp,
endTs: passing.timestamp,
laps: 1,
};
stints.push(current);
return;
}
current.endTs = passing.timestamp;
current.laps += 1;
});
return stints.map((stint, index) => ({
...stint,
index: index + 1,
durationMs: Math.max(0, stint.endTs - stint.startTs),
}));
}
export function getSessionGridEntries(session, { state, t, getEventTeams, getDriverDisplayById, getSessionGridOrder }) {
if (!session) {
return [];
}
if (session.mode === "track") {
return (session.assignments || []).map((assignment, index) => {
const driver = state.drivers.find((item) => item.id === assignment.driverId);
const car = state.cars.find((item) => item.id === assignment.carId);
return {
slot: index + 1,
name: driver?.name || t("common.unknown_driver"),
meta: car ? `${car.name} (${car.transponder || "-"})` : t("common.unknown_car"),
};
});
}
if (session.type === "team_race") {
const event = state.events.find((item) => item.id === session.eventId);
return getEventTeams(event).map((team, index) => ({
slot: index + 1,
name: team.name,
meta:
team.driverIds.map((driverId) => getDriverDisplayById(driverId)).join(", ") ||
team.carIds
.map((carId) => {
const car = state.cars.find((item) => item.id === carId);
return car ? `${car.name} (${car.transponder || "-"})` : "";
})
.filter(Boolean)
.join(", ") ||
"-",
}));
}
return getSessionGridOrder(session).map((driverId, index) => {
const driver = state.drivers.find((item) => item.id === driverId);
return {
slot: index + 1,
name: driver?.name || t("common.unknown_driver"),
meta: driver?.transponder || "-",
};
});
}
export function getSessionGridOrder(session) {
if (!session) {
return [];
}
if (Array.isArray(session.manualGridIds) && session.manualGridIds.length) {
return session.manualGridIds;
}
return Array.isArray(session.driverIds) ? session.driverIds : [];
}
export function ensureSessionDriverOrder(session, { getSessionEntrants }) {
if (!session || session.mode !== "race") {
return [];
}
if (!Array.isArray(session.driverIds) || !session.driverIds.length) {
session.driverIds = getSessionEntrants(session)
.map((driver) => driver.id)
.filter(Boolean);
}
if (!Array.isArray(session.manualGridIds) || !session.manualGridIds.length) {
session.manualGridIds = [...session.driverIds];
}
return session.manualGridIds;
}
export function buildFinalStandings(event, { getSessionsForEvent, buildLeaderboard, getSessionEntrants, t }) {
const sessions = getSessionsForEvent(event.id).filter((session) => session.type === "final");
const groupedByMain = new Map();
const countedFinalLegs = Math.max(1, Number(event.raceConfig?.countedFinalLegs || 1) || 1);
sessions.forEach((session) => {
const rows = buildLeaderboard(session);
// See the identical note in buildQualifyingStandings above: a driver assigned to this
// final who never recorded a valid passing must not vanish from the results entirely.
const rowsWithDns = session.status === "finished"
? [
...rows,
...getSessionEntrants(session)
.filter((driver) => !rows.some((row) => (row.driverId || row.key) === driver.id))
.map((driver) => ({ driverId: driver.id, driverName: driver.name, totalElapsedMs: Number.MAX_SAFE_INTEGER, resultDisplay: t("events.dns") })),
]
: rows;
const mainMatch = String(session.name || "").match(/^([A-Z])/i);
const mainKey = mainMatch ? mainMatch[1].toUpperCase() : "A";
if (!groupedByMain.has(mainKey)) {
groupedByMain.set(mainKey, new Map());
}
const competitorMap = groupedByMain.get(mainKey);
rowsWithDns.forEach((row, index) => {
const key = row.driverId || row.key;
if (!competitorMap.has(key)) {
competitorMap.set(key, {
key,
mainKey,
driverId: row.driverId,
driverName: row.driverName,
legRanks: [],
bestElapsedMs: [],
});
}
const entry = competitorMap.get(key);
entry.legRanks.push(index + 1);
entry.bestElapsedMs.push(row.totalElapsedMs);
});
});
const mains = [...groupedByMain.entries()].sort((a, b) => a[0].localeCompare(b[0]));
const rows = [];
mains.forEach(([mainKey, competitorMap], mainIndex) => {
const mainRows = [...competitorMap.values()].map((entry) => {
const sortedRanks = [...entry.legRanks].sort((a, b) => a - b);
const countedRanks = sortedRanks.slice(0, countedFinalLegs);
return {
mainKey,
driverId: entry.driverId,
driverName: entry.driverName,
finalScore: countedRanks.reduce((sum, value) => sum + value, 0),
legRanks: sortedRanks,
bestElapsedMs: Math.min(...entry.bestElapsedMs),
};
});
mainRows.sort((a, b) => {
if (a.finalScore !== b.finalScore) {
return a.finalScore - b.finalScore;
}
for (let i = 0; i < Math.max(a.legRanks.length, b.legRanks.length); i += 1) {
const left = a.legRanks[i] ?? 999;
const right = b.legRanks[i] ?? 999;
if (left !== right) {
return left - right;
}
}
return a.bestElapsedMs - b.bestElapsedMs;
});
mainRows.forEach((row, rowIndex) => {
rows.push({
rank: `${mainKey}${rowIndex + 1}`,
driverId: row.driverId,
driverName: row.driverName,
score: `${row.finalScore} (${row.legRanks.join("+")})`,
orderingGroup: mainIndex,
orderingIndex: rowIndex,
});
});
});
return rows.sort((a, b) => {
if (a.orderingGroup !== b.orderingGroup) {
return a.orderingGroup - b.orderingGroup;
}
return a.orderingIndex - b.orderingIndex;
});
}