Flagged laps never entered the reference pace, so a driver who got faster had every lap flagged. They now count in the reference (last 10 laps), settled flags are released, and a flagged lap counts until it is removed; Approve only drops the flag.
622 lines
27 KiB
JavaScript
622 lines
27 KiB
JavaScript
import { decoderDiagnostics } from "./decoder_diagnostics.js";
|
|
import { getControlledLapStreak } from "./training_logic.js";
|
|
import { usesPersonalClock, getLatestClockStartTs } from "./prologue_logic.js";
|
|
import { isSuspectLap, getSuspectReferenceLaps, releaseSettledSuspectLaps } from "./suspect_laps_logic.js";
|
|
import { shouldCallPracticePersonalBest } from "./practice_callouts_logic.js";
|
|
import { normalizeRaceAudioSettings } from "./race_audio.js";
|
|
import { decoderClockCheck, describeDecoderClockSkew } from "./decoder_clock_check.js";
|
|
|
|
const PRACTICE_SESSION_TYPES = new Set(["open_practice", "free_practice", "practice"]);
|
|
// Last personal best *called* per driver, per practice day and layout (practice_callouts_logic.js).
|
|
const lastPracticePbCall = new Map();
|
|
|
|
function getCurrentPracticePaceLaps(result, competitorKey) {
|
|
const laps = [];
|
|
const passings = Array.isArray(result?.passings) ? result.passings : [];
|
|
for (let index = passings.length - 1; index >= 0; index -= 1) {
|
|
const passing = passings[index];
|
|
if (String(passing?.competitorKey || "") !== String(competitorKey || "")) continue;
|
|
if (passing.validLap === false || passing.excludedFromPace || passing.stintBoundary) break;
|
|
const lapMs = Number(passing.lapMs || 0);
|
|
if (lapMs <= 500) break;
|
|
laps.unshift(lapMs);
|
|
}
|
|
return laps;
|
|
}
|
|
|
|
function parseRtcTime(value) {
|
|
if (!value || typeof value !== "string") {
|
|
return null;
|
|
}
|
|
const ts = Date.parse(value);
|
|
return Number.isFinite(ts) ? ts : null;
|
|
}
|
|
|
|
const DECODER_ROLES = {
|
|
main: { urlKey: "wsUrl", connectedKey: "connected", messageKey: "lastMessageAt", errorKey: "lastError" },
|
|
split: { urlKey: "splitWsUrl", connectedKey: "splitConnected", messageKey: "splitLastMessageAt", errorKey: "splitLastError" },
|
|
pit_entry: { urlKey: "pitEntryWsUrl", connectedKey: "pitEntryConnected", messageKey: "pitEntryLastMessageAt", errorKey: "pitEntryLastError" },
|
|
pit_exit: { urlKey: "pitExitWsUrl", connectedKey: "pitExitConnected", messageKey: "pitExitLastMessageAt", errorKey: "pitExitLastError" },
|
|
};
|
|
|
|
function resolveCompetitor(session, transponder, deps) {
|
|
const { state, t, findEventTeamForPassing } = deps;
|
|
const sessionType = String(session?.type || "").toLowerCase();
|
|
const isOpenPractice = sessionType === "open_practice";
|
|
const isFreePractice = sessionType === "free_practice";
|
|
const isOpenMonitoringSession = isOpenPractice || isFreePractice;
|
|
const event = state.events.find((item) => item.id === session?.eventId) || null;
|
|
if (session.mode === "track") {
|
|
const matchingAssignments = (session.assignments || []).filter((a) => {
|
|
const car = state.cars.find((c) => c.id === a.carId);
|
|
return car?.transponder === transponder;
|
|
});
|
|
|
|
if (matchingAssignments.length > 1) {
|
|
return {
|
|
key: `track_ambiguous_${transponder}`,
|
|
driverId: null,
|
|
driverName: `Ambiguous TP ${transponder}`,
|
|
carId: null,
|
|
carName: t("common.unknown_car"),
|
|
};
|
|
}
|
|
|
|
const assignment = matchingAssignments[0];
|
|
if (assignment) {
|
|
const driver = state.drivers.find((d) => d.id === assignment.driverId);
|
|
const car = state.cars.find((c) => c.id === assignment.carId);
|
|
return {
|
|
key: `track_${assignment.id}`,
|
|
driverId: driver?.id || null,
|
|
driverName: driver?.name || t("common.unknown_driver"),
|
|
speechName: driver?.speechName || driver?.name || "",
|
|
carId: car?.id || null,
|
|
carName: car?.name || t("common.unknown_car"),
|
|
};
|
|
}
|
|
|
|
return {
|
|
key: `track_tp_${transponder}`,
|
|
driverId: null,
|
|
driverName: t("common.unassigned_driver"),
|
|
carId: null,
|
|
carName: t("common.unknown_car"),
|
|
};
|
|
}
|
|
|
|
if (session.mode === "race" && sessionType === "team_race") {
|
|
const driver = state.drivers.find((d) => d.transponder === transponder) || null;
|
|
const car = state.cars.find((c) => c.transponder === transponder) || null;
|
|
const team = event ? findEventTeamForPassing(event, driver?.id || null, car?.id || null) : null;
|
|
if (!team) {
|
|
return {
|
|
key: `ignore_team_${transponder}`,
|
|
ignore: true,
|
|
};
|
|
}
|
|
|
|
const memberBits = [driver?.name || "", car?.name || ""].filter(Boolean);
|
|
return {
|
|
key: `team_${team.id}`,
|
|
teamId: team.id,
|
|
teamName: team.name,
|
|
displayName: team.name,
|
|
subLabel: memberBits.join(" • ") || transponder,
|
|
driverId: driver?.id || null,
|
|
driverName: driver?.name || team.name,
|
|
speechName: driver?.speechName || driver?.name || team.name,
|
|
carId: car?.id || null,
|
|
carName: car?.name || t("common.unknown_car"),
|
|
};
|
|
}
|
|
|
|
const driver = state.drivers.find((d) => d.transponder === transponder);
|
|
if (driver) {
|
|
if (!isOpenMonitoringSession && Array.isArray(session.driverIds) && session.driverIds.length && !session.driverIds.includes(driver.id)) {
|
|
return {
|
|
key: `ignore_${driver.id}`,
|
|
ignore: true,
|
|
};
|
|
}
|
|
return {
|
|
key: `driver_${driver.id}`,
|
|
driverId: driver.id,
|
|
driverName: driver.name,
|
|
speechName: driver.speechName || driver.name,
|
|
displayName: driver.name,
|
|
subLabel: driver.transponder || "",
|
|
carId: null,
|
|
carName: t("common.driver_car"),
|
|
};
|
|
}
|
|
|
|
return {
|
|
key: `driver_tp_${transponder}`,
|
|
driverId: null,
|
|
driverName: isOpenPractice ? transponder : isFreePractice ? `TP ${transponder}` : t("common.unknown_driver"),
|
|
displayName: isOpenPractice ? transponder : isFreePractice ? `TP ${transponder}` : t("common.unknown_driver"),
|
|
subLabel: transponder,
|
|
carId: null,
|
|
carName: t("common.unknown_car"),
|
|
};
|
|
}
|
|
|
|
export function connectDecoderHelper(deps) {
|
|
deps.disconnectDecoder({ silent: true });
|
|
deps.state.decoder.lastError = "";
|
|
deps.state.decoder.splitLastError = "";
|
|
deps.state.decoder.pitEntryLastError = "";
|
|
deps.state.decoder.pitExitLastError = "";
|
|
deps.saveState();
|
|
connectDecoderRole("main", deps);
|
|
if (deps.state.settings.splitDecoderEnabled && deps.state.settings.splitWsUrl) {
|
|
connectDecoderRole("split", deps);
|
|
}
|
|
if (deps.state.settings.pitDecoderEnabled && deps.state.settings.pitEntryWsUrl) {
|
|
connectDecoderRole("pit_entry", deps);
|
|
}
|
|
if (deps.state.settings.pitDecoderEnabled && deps.state.settings.pitExitWsUrl) {
|
|
connectDecoderRole("pit_exit", deps);
|
|
}
|
|
}
|
|
|
|
function connectDecoderRole(role, deps) {
|
|
const {
|
|
state, t, saveState, getWsClient, setWsClient, getReconnectTimer, setReconnectTimer,
|
|
getCurrentView, renderView, updateConnectionBadge, processDecoderMessage,
|
|
} = deps;
|
|
const roleConfig = DECODER_ROLES[role] || DECODER_ROLES.main;
|
|
const { connectedKey, messageKey: lastMessageKey, errorKey: lastErrorKey } = roleConfig;
|
|
const url = state.settings[roleConfig.urlKey];
|
|
if (!url) return;
|
|
|
|
state.decoder[lastErrorKey] = "";
|
|
decoderDiagnostics.socket(role, "connecting", url);
|
|
saveState();
|
|
try {
|
|
setWsClient(role, new WebSocket(url));
|
|
} catch (error) {
|
|
decoderDiagnostics.socket(role, "error", url);
|
|
state.decoder[lastErrorKey] = t("error.ws_invalid", { msg: error instanceof Error ? error.message : String(error) });
|
|
saveState();
|
|
renderView();
|
|
return;
|
|
}
|
|
|
|
const wsClient = getWsClient(role);
|
|
wsClient.onopen = () => {
|
|
decoderDiagnostics.socket(role, "connected", url);
|
|
state.decoder[connectedKey] = true;
|
|
state.decoder[lastErrorKey] = "";
|
|
saveState();
|
|
updateConnectionBadge();
|
|
if (["timing", "overlay", "race_day"].includes(getCurrentView())) {
|
|
renderView();
|
|
}
|
|
};
|
|
|
|
wsClient.onmessage = (event) => {
|
|
state.decoder[lastMessageKey] = Date.now();
|
|
decoderDiagnostics.frame(role, event.data);
|
|
|
|
let parsed;
|
|
try {
|
|
parsed = JSON.parse(String(event.data));
|
|
} catch {
|
|
decoderDiagnostics.parseError(role);
|
|
return;
|
|
}
|
|
|
|
const messages = Array.isArray(parsed) ? parsed : [parsed];
|
|
const changed = messages.reduce((didChange, message) => processDecoderMessage(message, role) || didChange, false);
|
|
|
|
updateConnectionBadge();
|
|
if (changed && ["timing", "dashboard", "overlay", "training"].includes(getCurrentView())) {
|
|
renderView();
|
|
}
|
|
};
|
|
|
|
wsClient.onclose = () => {
|
|
decoderDiagnostics.socket(role, "disconnected", url);
|
|
state.decoder[connectedKey] = false;
|
|
saveState();
|
|
updateConnectionBadge();
|
|
|
|
if (state.settings.autoReconnect) {
|
|
clearTimeout(getReconnectTimer(role));
|
|
setReconnectTimer(role, setTimeout(() => {
|
|
const roleEnabled = role === "main"
|
|
|| (role === "split" && state.settings.splitDecoderEnabled)
|
|
|| (["pit_entry", "pit_exit"].includes(role) && state.settings.pitDecoderEnabled);
|
|
if (!state.decoder[connectedKey] && roleEnabled) {
|
|
connectDecoderRole(role, deps);
|
|
}
|
|
}, 2000));
|
|
}
|
|
|
|
if (["timing", "overlay", "race_day"].includes(getCurrentView())) {
|
|
renderView();
|
|
}
|
|
};
|
|
|
|
wsClient.onerror = () => {
|
|
decoderDiagnostics.socket(role, "error", url);
|
|
state.decoder[lastErrorKey] = t("error.decoder_connection");
|
|
saveState();
|
|
updateConnectionBadge();
|
|
if (["timing", "overlay", "race_day"].includes(getCurrentView())) {
|
|
renderView();
|
|
}
|
|
};
|
|
}
|
|
|
|
export function disconnectDecoderHelper(options = {}, deps) {
|
|
const { state, saveState, getWsClient, setWsClient, getReconnectTimer, updateConnectionBadge } = deps;
|
|
Object.keys(DECODER_ROLES).forEach((role) => {
|
|
decoderDiagnostics.socket(role, "disconnected");
|
|
clearTimeout(getReconnectTimer(role));
|
|
const wsClient = getWsClient(role);
|
|
if (wsClient) {
|
|
wsClient.onopen = null;
|
|
wsClient.onmessage = null;
|
|
wsClient.onclose = null;
|
|
wsClient.onerror = null;
|
|
wsClient.close();
|
|
setWsClient(role, null);
|
|
}
|
|
});
|
|
state.decoder.connected = false;
|
|
state.decoder.splitConnected = false;
|
|
state.decoder.pitEntryConnected = false;
|
|
state.decoder.pitExitConnected = false;
|
|
if (!options.silent) {
|
|
saveState();
|
|
}
|
|
updateConnectionBadge();
|
|
}
|
|
|
|
function getPracticePbRules(state) {
|
|
const audio = normalizeRaceAudioSettings(state.settings?.raceAudio);
|
|
return { warmupLaps: audio.practicePbWarmupLaps, cooldownSec: audio.practicePbCooldownSec, minGainMs: audio.practicePbMinGainMs };
|
|
}
|
|
|
|
export function processDecoderMessageHelper(msg, deps, decoderRole = "main") {
|
|
const {
|
|
state, t, getActiveSession, ensureSessionResult, normalizeStartMode, getSessionLapWindow,
|
|
findEventTeamForPassing, persistPassingToBackend, pushOverlayEvent, buildLeaderboard,
|
|
formatLap, announcePassing, saveState, lastOverlayLeaderKeyBySession,
|
|
lastOverlayTop3BySession, lastOverlayBestLapByKey, triggerRaceAudio = () => {}, getTrackRecordMs = () => null,
|
|
onTransponderSeen = () => {},
|
|
onDecoderClockChange = () => {},
|
|
} = deps;
|
|
|
|
const report = (reason, outcome) => decoderDiagnostics.message(decoderRole, msg, reason, outcome);
|
|
if (!msg || typeof msg !== "object") {
|
|
report("invalid_message");
|
|
return;
|
|
}
|
|
|
|
const type = String(msg.msg || msg.type || "").toUpperCase();
|
|
if (type !== "PASSING") {
|
|
report("not_passing");
|
|
return;
|
|
}
|
|
|
|
// Noted before the running-session check on purpose: the pre-heat warm-up / transponder
|
|
// check needs to see passings while the heat hasn't started yet. Main line only, the
|
|
// same line that counts laps.
|
|
if (decoderRole === "main") {
|
|
const seenTransponder = String(msg.transponder ?? msg.tran_code ?? "").replace("ID:", "").trim();
|
|
if (seenTransponder) onTransponderSeen(seenTransponder);
|
|
}
|
|
|
|
// The decoder's clock against the computer's - only measured, never changed: the decoder is
|
|
// the source of truth. Before the running-session check so Race Day can warn before a start.
|
|
const rtcTs = parseRtcTime(msg.rtc_time);
|
|
decoderClockCheck.record(decoderRole, rtcTs);
|
|
if (decoderRole === "main" && state.decoder) {
|
|
const before = JSON.stringify(describeDecoderClockSkew(state.decoder.clockSkewMs));
|
|
state.decoder.clockSkewMs = decoderClockCheck.skewMs("main");
|
|
if (JSON.stringify(describeDecoderClockSkew(state.decoder.clockSkewMs)) !== before) onDecoderClockChange();
|
|
}
|
|
|
|
const session = getActiveSession();
|
|
if (!session || session.status !== "running") {
|
|
report("no_running_session");
|
|
return;
|
|
}
|
|
|
|
const timestamp = rtcTs || Date.now();
|
|
const transponder = String(msg.transponder ?? msg.tran_code ?? "").replace("ID:", "").trim();
|
|
if (!transponder) {
|
|
report("missing_transponder");
|
|
return;
|
|
}
|
|
|
|
const result = ensureSessionResult(session.id);
|
|
const competitor = resolveCompetitor(session, transponder, { state, t, findEventTeamForPassing });
|
|
if (competitor.ignore) {
|
|
report("not_registered");
|
|
triggerRaceAudio("unknown_transponder", { variables: { transponder }, scope: transponder, logData: { raceId: session.eventId, sessionId: session.id } });
|
|
return;
|
|
}
|
|
const key = competitor.key;
|
|
|
|
if (!result.competitors[key]) {
|
|
result.competitors[key] = {
|
|
key,
|
|
teamId: competitor.teamId || null,
|
|
teamName: competitor.teamName || "",
|
|
driverId: competitor.driverId,
|
|
driverName: competitor.driverName,
|
|
speechName: competitor.speechName || competitor.driverName,
|
|
displayName: competitor.displayName || competitor.driverName,
|
|
subLabel: competitor.subLabel || competitor.carName || "",
|
|
carId: competitor.carId,
|
|
carName: competitor.carName,
|
|
transponder,
|
|
laps: 0,
|
|
lastLapMs: null,
|
|
bestLapMs: null,
|
|
bestLapLayoutId: String(session.trackLayoutId || ""),
|
|
startTimestamp: null,
|
|
lastTimestamp: null,
|
|
};
|
|
}
|
|
|
|
const entry = result.competitors[key];
|
|
entry.teamId = competitor.teamId || entry.teamId || null;
|
|
entry.teamName = competitor.teamName || entry.teamName || "";
|
|
entry.displayName = competitor.displayName || entry.displayName || competitor.driverName;
|
|
entry.subLabel = competitor.subLabel || entry.subLabel || competitor.carName || "";
|
|
entry.driverId = competitor.driverId ?? entry.driverId;
|
|
entry.driverName = competitor.driverName || entry.driverName;
|
|
entry.speechName = competitor.speechName || entry.speechName || entry.driverName;
|
|
entry.carId = competitor.carId ?? entry.carId;
|
|
entry.carName = competitor.carName || entry.carName;
|
|
entry.transponder = transponder;
|
|
const startMode = normalizeStartMode(session.startMode);
|
|
|
|
if (decoderRole === "pit_entry") {
|
|
if (entry.pitActive) { report("already_in_pit"); return; }
|
|
entry.pitEnteredAt = timestamp;
|
|
entry.pitActive = true;
|
|
entry.pitEntryDriverId = entry.driverId || null;
|
|
entry.pitEntryDriverName = entry.driverName || "";
|
|
entry.pitEntryCarId = entry.carId || null;
|
|
entry.pitEntryCarName = entry.carName || "";
|
|
entry.pitEntryTransponder = transponder;
|
|
if (!Array.isArray(result.pitEvents)) result.pitEvents = [];
|
|
result.pitEvents.push({ timestamp, type: "entry", transponder, competitorKey: key, driverId: entry.driverId, carId: entry.carId });
|
|
pushOverlayEvent("pit", `${entry.displayName || entry.driverName} • PIT IN`);
|
|
triggerRaceAudio("entering_pits", { variant: entry.carId || entry.driverId || transponder, driverId: entry.driverId, logData: { raceId: session.eventId, driverId: entry.driverId } });
|
|
saveState();
|
|
report("pit_entry", "handled");
|
|
return true;
|
|
}
|
|
|
|
if (decoderRole === "pit_exit") {
|
|
const enteredAt = Number(entry.pitEnteredAt || 0);
|
|
if (!enteredAt || timestamp <= enteredAt) { report("pit_exit_without_entry"); return; }
|
|
const pitStopMs = timestamp - enteredAt;
|
|
const pitDriverId = entry.pitEntryDriverId ?? entry.driverId ?? null;
|
|
const pitDriverName = entry.pitEntryDriverName || entry.driverName || "";
|
|
const pitCarId = entry.pitEntryCarId ?? entry.carId ?? null;
|
|
const pitCarName = entry.pitEntryCarName || entry.carName || "";
|
|
const pitTransponder = entry.pitEntryTransponder || transponder;
|
|
entry.pitActive = false;
|
|
entry.pitEnteredAt = null;
|
|
entry.pitStops = Math.max(0, Number(entry.pitStops || 0)) + 1;
|
|
entry.lastPitStopMs = pitStopMs;
|
|
entry.totalPitMs = Math.max(0, Number(entry.totalPitMs || 0)) + pitStopMs;
|
|
if (!entry.bestPitStopMs || pitStopMs < entry.bestPitStopMs) entry.bestPitStopMs = pitStopMs;
|
|
if (!Array.isArray(result.pitEvents)) result.pitEvents = [];
|
|
if (!Array.isArray(result.pitStops)) result.pitStops = [];
|
|
result.pitEvents.push({ timestamp, type: "exit", transponder, competitorKey: key, driverId: pitDriverId, driverName: pitDriverName, carId: pitCarId, carName: pitCarName, pitStopMs });
|
|
result.pitStops.push({ enteredAt, exitedAt: timestamp, pitStopMs, transponder: pitTransponder, exitTransponder: transponder, competitorKey: key, driverId: pitDriverId, driverName: pitDriverName, carId: pitCarId, carName: pitCarName });
|
|
entry.pitEntryDriverId = null;
|
|
entry.pitEntryDriverName = "";
|
|
entry.pitEntryCarId = null;
|
|
entry.pitEntryCarName = "";
|
|
entry.pitEntryTransponder = "";
|
|
pushOverlayEvent("pit", `${entry.displayName || entry.driverName} • PIT ${formatLap(pitStopMs)}`);
|
|
triggerRaceAudio("leaving_pits", { variant: entry.carId || entry.driverId || transponder, driverId: entry.driverId, logData: { raceId: session.eventId, driverId: entry.driverId } });
|
|
saveState();
|
|
report("pit_exit", "handled");
|
|
return true;
|
|
}
|
|
|
|
if (decoderRole === "split") {
|
|
const baseTimestamp = Number(entry.lastTimestamp || (startMode === "mass" ? session.startedAt : 0) || 0);
|
|
if (!baseTimestamp || timestamp <= baseTimestamp) { report("split_without_reference"); return; }
|
|
if (entry.lastSplitTimestamp && timestamp - Number(entry.lastSplitTimestamp) < 1000) { report("split_locktime"); return; }
|
|
const splitMs = timestamp - baseTimestamp;
|
|
const previousSplitMs = Number(entry.lastSplitMs || 0) || 0;
|
|
entry.previousSplitMs = previousSplitMs || null;
|
|
entry.lastSplitMs = splitMs;
|
|
entry.lastSplitDeltaMs = previousSplitMs ? splitMs - previousSplitMs : null;
|
|
entry.lastSplitTimestamp = timestamp;
|
|
entry.lastSplitLap = Math.max(1, Number(entry.laps || 0) + 1);
|
|
if (!entry.bestSplitMs || splitMs < entry.bestSplitMs) entry.bestSplitMs = splitMs;
|
|
if (!Array.isArray(result.splits)) result.splits = [];
|
|
result.splits.push({
|
|
timestamp,
|
|
transponder,
|
|
driverId: entry.driverId,
|
|
driverName: entry.driverName,
|
|
carId: entry.carId,
|
|
carName: entry.carName,
|
|
competitorKey: key,
|
|
lap: entry.lastSplitLap,
|
|
splitMs,
|
|
splitDeltaMs: entry.lastSplitDeltaMs,
|
|
loopId: String(msg.loop_id || ""),
|
|
decoderRole: "split",
|
|
});
|
|
pushOverlayEvent("split", `${entry.displayName || entry.driverName} • ${formatLap(splitMs)}`);
|
|
saveState();
|
|
report("split", "handled");
|
|
return true;
|
|
}
|
|
|
|
if (usesPersonalClock(startMode) && !entry.startTimestamp) {
|
|
// A late car's clock started without it (EFRA 9.3 / IFMAR 4.2.3): its first crossing
|
|
// after that moment is already a counted lap, measured from the latest clock start.
|
|
const latestStartTs = getLatestClockStartTs({ ...session, startMode }, result);
|
|
if (latestStartTs && timestamp > latestStartTs) {
|
|
entry.startTimestamp = latestStartTs;
|
|
entry.lastTimestamp = latestStartTs;
|
|
}
|
|
}
|
|
if (usesPersonalClock(startMode) && !entry.startTimestamp) {
|
|
entry.startTimestamp = timestamp;
|
|
entry.lastTimestamp = timestamp;
|
|
announcePassing(entry);
|
|
saveState();
|
|
report("start_reference", "handled");
|
|
return true;
|
|
}
|
|
|
|
if (!entry.startTimestamp) {
|
|
entry.startTimestamp = session.startedAt || timestamp;
|
|
}
|
|
|
|
const baseTs = entry.lastTimestamp || entry.startTimestamp || session.startedAt || timestamp;
|
|
const lapMs = Math.max(0, timestamp - baseTs);
|
|
const { minLapMs, maxLapMs } = getSessionLapWindow(session);
|
|
let validLap = true;
|
|
let invalidReason = "";
|
|
let validationWarning = "";
|
|
if (minLapMs > 0 && lapMs > 0 && lapMs < minLapMs) {
|
|
validLap = false;
|
|
invalidReason = "below_min";
|
|
} else if (Number.isFinite(maxLapMs) && maxLapMs > 0 && lapMs > maxLapMs) {
|
|
// A long lap remains part of the race result, but starts a new statistics stint.
|
|
validationWarning = "above_max_stint";
|
|
}
|
|
|
|
const passing = {
|
|
timestamp,
|
|
transponder,
|
|
teamId: entry.teamId,
|
|
teamName: entry.teamName,
|
|
driverId: entry.driverId,
|
|
driverName: entry.driverName,
|
|
speechName: entry.speechName,
|
|
displayName: entry.displayName,
|
|
subLabel: entry.subLabel,
|
|
carId: entry.carId,
|
|
carName: entry.carName,
|
|
competitorKey: key,
|
|
lapMs,
|
|
validLap,
|
|
invalidReason,
|
|
validationWarning,
|
|
excludedFromPace: validationWarning === "above_max_stint",
|
|
stintBoundary: validationWarning === "above_max_stint",
|
|
strength: msg.strength,
|
|
loopId: String(msg.loop_id || ""),
|
|
trackLayoutId: String(session.trackLayoutId || ""),
|
|
resend: Boolean(msg.resend),
|
|
};
|
|
|
|
if (!validLap) {
|
|
result.passings.push(passing);
|
|
persistPassingToBackend(session.id, passing);
|
|
saveState();
|
|
report("below_min", "invalid");
|
|
return true;
|
|
}
|
|
|
|
// Best laps are per track layout: an open practice that runs all season moves onto a new
|
|
// layout when one is added (addTrackLayout resets every best lap then), and this catches a
|
|
// layout change made elsewhere. Entries from before this field existed adopt the current one.
|
|
const layoutId = String(session.trackLayoutId || "");
|
|
if (entry.bestLapLayoutId === undefined) entry.bestLapLayoutId = layoutId;
|
|
else if (entry.bestLapLayoutId !== layoutId) { entry.bestLapMs = null; entry.bestLapLayoutId = layoutId; }
|
|
// Practice only: a lap far quicker than this driver's own pace (maybe a shortcut) is flagged
|
|
// for a look. It counts like any other lap until someone removes it (suspect_laps_logic.js).
|
|
const suspect = PRACTICE_SESSION_TYPES.has(String(session.type || "").toLowerCase())
|
|
&& !validationWarning
|
|
&& isSuspectLap(lapMs, getSuspectReferenceLaps(result.passings, key, layoutId));
|
|
if (suspect) passing.suspect = true;
|
|
const previousPersonalBest = Number(entry.bestLapMs || 0) || null;
|
|
const leaderboardBefore = buildLeaderboard(session);
|
|
const previousPosition = leaderboardBefore.findIndex((row) => row.key === key);
|
|
const previousLeaderKey = leaderboardBefore[0]?.key || null;
|
|
const previousSessionFastest = leaderboardBefore.map((row) => Number(row.bestLapMs || 0)).filter((value) => value > 500).sort((a, b) => a - b)[0] || null;
|
|
const previousTrackRecord = Number(getTrackRecordMs(session) || 0) || null;
|
|
|
|
entry.laps += 1;
|
|
entry.lastLapMs = lapMs;
|
|
entry.lastTimestamp = timestamp;
|
|
|
|
// A suspect lap counts like any other lap until someone removes it; it is only not called
|
|
// out as a personal best or record below.
|
|
if (lapMs > 500 && (!entry.bestLapMs || lapMs < entry.bestLapMs)) {
|
|
entry.bestLapMs = lapMs;
|
|
}
|
|
|
|
result.passings.push(passing);
|
|
// Earlier flagged laps of this driver that match the pace now were the driver getting
|
|
// faster, not shortcuts: drop their flag so the list only shows what stands out.
|
|
if (PRACTICE_SESSION_TYPES.has(String(session.type || "").toLowerCase())) releaseSettledSuspectLaps(result.passings, key, layoutId);
|
|
persistPassingToBackend(session.id, passing);
|
|
pushOverlayEvent("passing", `${entry.displayName || entry.driverName} • ${formatLap(entry.lastLapMs)}`);
|
|
const leaderboard = buildLeaderboard(session);
|
|
const currentPosition = leaderboard.findIndex((row) => row.key === key);
|
|
const audioVariables = { driverName: entry.speechName || entry.displayName || entry.driverName, lapTime: formatLap(lapMs) };
|
|
if (entry.driverId && PRACTICE_SESSION_TYPES.has(String(session.type || "").toLowerCase()) && !validationWarning) {
|
|
const controlTargetMs = entry.bestLapMs ? Math.round(entry.bestLapMs * 1.08) : 0;
|
|
const controlledLapStreak = getControlledLapStreak(getCurrentPracticePaceLaps(result, key), controlTargetMs);
|
|
if (controlledLapStreak >= 5 && controlledLapStreak % 5 === 0) {
|
|
triggerRaceAudio("training_stable_pace", {
|
|
variables: { ...audioVariables, laps: controlledLapStreak },
|
|
driverId: entry.driverId,
|
|
competitorKey: key,
|
|
scope: entry.driverId,
|
|
logData: { raceId: session.eventId, sessionId: session.id, driverId: entry.driverId, lapNumber: entry.laps, lapTime: lapMs },
|
|
});
|
|
}
|
|
}
|
|
if (suspect) pushOverlayEvent("suspect", `${entry.displayName || entry.driverName} • ${formatLap(lapMs)} ⚠`);
|
|
const isPracticeSession = PRACTICE_SESSION_TYPES.has(String(session.type || "").toLowerCase());
|
|
const pbCallKey = `${session.id}:${Number(session.resultsResetAt || 0)}:${layoutId}:${key}`;
|
|
const callPersonalBest = !suspect && (isPracticeSession
|
|
? shouldCallPracticePersonalBest({ lapNumber: entry.laps, lapMs, previousBestMs: previousPersonalBest, lastCall: lastPracticePbCall.get(pbCallKey), now: Date.now(), rules: getPracticePbRules(state) })
|
|
: Boolean(previousPersonalBest && lapMs < previousPersonalBest));
|
|
if (callPersonalBest && isPracticeSession) lastPracticePbCall.set(pbCallKey, { at: Date.now(), lapMs });
|
|
if (callPersonalBest) triggerRaceAudio("personal_best", { variables: audioVariables, driverId: entry.driverId, competitorKey: key, logData: { raceId: session.eventId, driverId: entry.driverId, lapNumber: entry.laps, lapTime: lapMs } });
|
|
if (!suspect && previousSessionFastest && lapMs < previousSessionFastest) triggerRaceAudio("fastest_lap", { variables: audioVariables, driverId: entry.driverId, competitorKey: key, logData: { raceId: session.eventId, driverId: entry.driverId, lapNumber: entry.laps, lapTime: lapMs } });
|
|
if (!suspect && previousTrackRecord && lapMs < previousTrackRecord) triggerRaceAudio("track_record", { variables: audioVariables, driverId: entry.driverId, competitorKey: key, logData: { raceId: session.eventId, driverId: entry.driverId, lapNumber: entry.laps, lapTime: lapMs } });
|
|
if (previousPosition >= 0 && currentPosition >= 0 && currentPosition < previousPosition) triggerRaceAudio("position_gain", { driverId: entry.driverId, competitorKey: key });
|
|
if (previousPosition >= 0 && currentPosition > previousPosition) triggerRaceAudio("position_loss", { driverId: entry.driverId, competitorKey: key });
|
|
const leader = leaderboard[0];
|
|
if (leader?.key && lastOverlayLeaderKeyBySession[session.id] !== leader.key) {
|
|
lastOverlayLeaderKeyBySession[session.id] = leader.key;
|
|
pushOverlayEvent("leader", `${leader.displayName || leader.driverName} • P1`);
|
|
if (previousLeaderKey && previousLeaderKey !== leader.key) triggerRaceAudio("new_leader", { variables: { driverName: leader.displayName || leader.driverName }, driverId: leader.driverId, competitorKey: leader.key, logData: { raceId: session.eventId, driverId: leader.driverId } });
|
|
}
|
|
if (entry.bestLapMs && Number.isFinite(entry.bestLapMs)) {
|
|
const bestKey = `${session.id}:${entry.key}`;
|
|
const previousBest = lastOverlayBestLapByKey[bestKey];
|
|
if (!previousBest || entry.bestLapMs < previousBest) {
|
|
lastOverlayBestLapByKey[bestKey] = entry.bestLapMs;
|
|
pushOverlayEvent("bestlap", `${entry.displayName || entry.driverName} • ${formatLap(entry.bestLapMs)}`);
|
|
}
|
|
}
|
|
const top3Keys = leaderboard.slice(0, 3).map((row) => row.key);
|
|
const previousTop3 = lastOverlayTop3BySession[session.id] || [];
|
|
if (top3Keys.join("|") !== previousTop3.join("|")) {
|
|
lastOverlayTop3BySession[session.id] = top3Keys;
|
|
if (previousTop3.length) {
|
|
pushOverlayEvent("top3", `${t("overlay.mode_leaderboard")} • Top 3 updated`);
|
|
}
|
|
}
|
|
announcePassing(entry);
|
|
saveState();
|
|
report(validationWarning || "lap_counted", "handled");
|
|
return true;
|
|
}
|