Files
Live_RC/server/session_guard.js
T

65 lines
3.9 KiB
JavaScript

// Every open browser saves its whole copy of the state. A second computer that is not the
// Race Controller Master (or a tab left in the background) can hold a copy from before a heat
// was run, and its next save put that heat back to "ready" with no laps - the heat had to be
// run again. A heat that has started only goes back through Reset (which stamps a newer
// resultsResetAt), so a save that turns a started heat back without one comes from an older
// copy: the server keeps its own version of that heat and its results.
const STAGE = { ready: 0, running: 1, paused: 1, finished: 2, stopped: 2 };
const stageOf = (session) => STAGE[String(session?.status || "ready")] ?? 0;
function keepStartedSessions(incoming, stored) {
const kept = [];
if (!incoming || !Array.isArray(incoming.sessions) || !Array.isArray(stored?.sessions)) return { state: incoming, kept };
const storedById = new Map(stored.sessions.filter((session) => session?.id).map((session) => [session.id, session]));
incoming.sessions = incoming.sessions.map((session) => {
const before = storedById.get(session?.id);
if (!before) return session;
const resetBefore = Number(before.resultsResetAt || 0);
const resetNow = Number(session.resultsResetAt || 0);
if (resetNow > resetBefore) return session; // a deliberate Reset
// Same reset (or none): only a started heat can be turned back. An older reset: a copy
// from before the latest Reset, which must not bring the old run back.
if (resetNow === resetBefore && (!before.startedAt || stageOf(session) >= stageOf(before))) return session;
kept.push(session.id);
return before;
});
kept.forEach((sessionId) => {
const result = stored.resultsBySession?.[sessionId];
if (result === undefined) return;
if (!incoming.resultsBySession || typeof incoming.resultsBySession !== "object") incoming.resultsBySession = {};
incoming.resultsBySession[sessionId] = result;
});
return { state: incoming, kept };
}
// A save from a browser that is not the Race Controller Master (a second computer that only
// shows the race): it may change drivers, classes, heat grids and so on, but the race control
// stays the master's - which heat is active, each heat's status and clock, and the laps. Judging
// corrections from that browser go through /api/session-results, not through this save.
const RACE_CONTROL_FIELDS = ["status", "startedAt", "endedAt", "finishedByTimer", "pausedAt", "totalPausedMs", "followUpStartedAt", "followUpPausedMs", "finishWaitUntil", "resultsResetAt", "audioAnnouncements", "trackLayoutId", "calledAt"];
function keepRaceControl(incoming, stored) {
if (!incoming || typeof incoming !== "object" || !stored || typeof stored !== "object") return incoming;
incoming.activeSessionId = stored.activeSessionId ?? null;
if (stored.resultsBySession && typeof stored.resultsBySession === "object") incoming.resultsBySession = stored.resultsBySession;
const storedById = new Map((Array.isArray(stored.sessions) ? stored.sessions : []).filter((session) => session?.id).map((session) => [session.id, session]));
if (Array.isArray(incoming.sessions)) {
incoming.sessions = incoming.sessions.map((session) => {
const before = storedById.get(session?.id);
if (!before) return session;
const merged = { ...session };
RACE_CONTROL_FIELDS.forEach((field) => { if (field in before) merged[field] = before[field]; else delete merged[field]; });
return merged;
});
// A heat that has run and is missing from the copy: the copy is older (or a heat with laps
// was deleted from the wrong computer) - it stays.
const present = new Set(incoming.sessions.map((session) => session?.id));
storedById.forEach((session, id) => {
if (!present.has(id) && (session.startedAt || Object.keys(stored.resultsBySession?.[id]?.competitors || {}).length)) incoming.sessions.push(session);
});
}
return incoming;
}
module.exports = { keepStartedSessions, keepRaceControl };