fix(sync): a browser that is not the race master can't change heats, laps or the active heat (1.89.38)

This commit is contained in:
larssand committed 2026-11-15 16:55:58 +01:00
1 parent cea278518e
commit 5928ce847b
8 files changed
+76 -10

No files matched your search

+30 -1
View File
@@ -32,4 +32,33 @@ function keepStartedSessions(incoming, stored) {
return { state: incoming, kept };
}
module.exports = { keepStartedSessions };
// 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 };