fix(race): finish the lap you are on, one clock owner, edits survive refresh, participants editable (1.59.0)
This commit is contained in:
1 parent
afb8829b16
commit
db6a9f19e4
16 files changed
+313
-39
No files matched your search
+44
-11
@@ -103,19 +103,32 @@ export async function stopManagedAmmcHelper({ ammc, t, getBackendUrl }) {
|
||||
}
|
||||
}
|
||||
|
||||
// The background refresh updates items with the same id in place instead of replacing them,
|
||||
// so a form or button that already holds a session/event/driver (an open edit dialog, Run
|
||||
// again) still changes the live one - not a copy that is thrown away on the next save.
|
||||
export function keepIdentityById(previous, next) {
|
||||
const byId = new Map((Array.isArray(previous) ? previous : []).filter((item) => item && item.id).map((item) => [item.id, item]));
|
||||
return next.map((item) => {
|
||||
const existing = item && item.id ? byId.get(item.id) : null;
|
||||
if (!existing || existing === item) return item;
|
||||
Object.keys(existing).forEach((key) => { if (!(key in item)) delete existing[key]; });
|
||||
return Object.assign(existing, item);
|
||||
});
|
||||
}
|
||||
|
||||
export function applyPersistedStateHelper(persisted, deps) {
|
||||
const { state, getDefaultBackendUrl, DEFAULT_LANGUAGE, DEFAULT_THEME, AVAILABLE_THEMES, normalizeDriver, normalizeCar, normalizeEvent, normalizeSession, normalizeStoredRacePreset, normalizeObsOverlaySettings, normalizeTrainingState, normalizeRaceAudioSettings = (value = {}) => value || {}, normalizeTrackLayouts = (value) => (Array.isArray(value) ? value : []), applyTheme } = deps;
|
||||
state.classes = persisted.classes || [];
|
||||
state.drivers = (persisted.drivers || []).map((driver) => normalizeDriver(driver)).filter((driver) => driver.name);
|
||||
state.cars = (persisted.cars || []).map((car) => normalizeCar(car)).filter((car) => car.name);
|
||||
state.events = (persisted.events || []).map((event) => normalizeEvent(event));
|
||||
state.sessions = (persisted.sessions || []).map((session) => normalizeSession(session));
|
||||
state.drivers = keepIdentityById(state.drivers, (persisted.drivers || []).map((driver) => normalizeDriver(driver)).filter((driver) => driver.name));
|
||||
state.cars = keepIdentityById(state.cars, (persisted.cars || []).map((car) => normalizeCar(car)).filter((car) => car.name));
|
||||
state.events = keepIdentityById(state.events, (persisted.events || []).map((event) => normalizeEvent(event)));
|
||||
state.sessions = keepIdentityById(state.sessions, (persisted.sessions || []).map((session) => normalizeSession(session)));
|
||||
state.resultsBySession = persisted.resultsBySession || {};
|
||||
state.audioEventLog = Array.isArray(persisted.audioEventLog) ? persisted.audioEventLog.slice(0, 500) : [];
|
||||
state.training = normalizeTrainingState(persisted.training);
|
||||
state.activeSessionId = persisted.activeSessionId || null;
|
||||
state.trackLayouts = normalizeTrackLayouts(persisted.trackLayouts);
|
||||
state.series = (Array.isArray(persisted.series) ? persisted.series : []).map((series) => normalizeSeries(series)).filter((series) => series.id);
|
||||
state.series = keepIdentityById(state.series, (Array.isArray(persisted.series) ? persisted.series : []).map((series) => normalizeSeries(series)).filter((series) => series.id));
|
||||
state.raceDays = normalizeRaceDays(persisted.raceDays);
|
||||
state.settings = {
|
||||
wsUrl: persisted.settings?.wsUrl || state.settings.wsUrl || "ws://127.0.0.1:9000",
|
||||
@@ -420,11 +433,26 @@ export function announceRaceFinishedHelper({ t, getActiveSession, pushOverlayEve
|
||||
if (session) pushOverlayEvent("finish", `${session.name} • ${t("timing.race_finished")}`, { silentCue: true });
|
||||
}
|
||||
|
||||
export function handleSessionTimerTickHelper({ getActiveSession, isUntimedSession, getSessionTiming, saveState, getLastFinishAnnouncementSessionId, setLastFinishAnnouncementSessionId, announceRaceFinished, announceRaceClockEvent = () => {} }) {
|
||||
// isClockOwner: only the browser that runs the race (connected to the decoder) calls the
|
||||
// clock and closes the heat - every other open window (Timing on a second PC, the drivers'
|
||||
// board) would otherwise call "time has expired" too. Those only close a heat left running
|
||||
// a minute past its limit, in case the race browser was closed.
|
||||
export function handleSessionTimerTickHelper({ getActiveSession, isUntimedSession, getSessionTiming, saveState, getLastFinishAnnouncementSessionId, setLastFinishAnnouncementSessionId, announceRaceFinished, announceRaceClockEvent = () => {}, isClockOwner = () => true, getFinishWait = () => ({ allFinished: true, closeAt: null }), now = () => Date.now() }) {
|
||||
const active = getActiveSession();
|
||||
if (!active || active.status !== "running") return { changed: false };
|
||||
if (isUntimedSession(active)) return { changed: false };
|
||||
const timing = getSessionTiming(active);
|
||||
if (!isClockOwner(active)) {
|
||||
const limit = Number(active.finishWaitUntil || 0) || (Number(active.startedAt || 0) + Number(timing.targetMs || 0) + Math.max(0, Number(active.totalPausedMs || 0)) + 120000);
|
||||
if (timing.remainingMs > 0 || now() < limit + 60000) return { changed: false };
|
||||
active.status = "finished";
|
||||
active.endedAt = now();
|
||||
active.finishedByTimer = true;
|
||||
active.followUpStartedAt = null;
|
||||
active.finishWaitUntil = null;
|
||||
saveState();
|
||||
return { changed: true };
|
||||
}
|
||||
if (!Array.isArray(active.audioAnnouncements)) active.audioAnnouncements = [];
|
||||
let changed = false;
|
||||
const announceOnce = (marker, eventKey) => {
|
||||
@@ -440,16 +468,21 @@ export function handleSessionTimerTickHelper({ getActiveSession, isUntimedSessio
|
||||
if (changed) saveState();
|
||||
return { changed };
|
||||
}
|
||||
// "Time has expired. Finish the lap you are on." - then each car finishes at its next
|
||||
// crossing, and the heat closes when all have (or the wait runs out).
|
||||
announceOnce("time_expired", "time_expired");
|
||||
if (!["open_practice", "free_practice", "practice"].includes(String(active.type || ""))) announceOnce("final_lap", "final_lap");
|
||||
if (Number(active.followUpSec || 0) > 0) {
|
||||
if (!active.followUpStartedAt) { active.followUpStartedAt = Date.now(); saveState(); return { changed: true }; }
|
||||
if (timing.followUpRemainingMs > 0) { if (changed) saveState(); return { changed }; }
|
||||
const wait = getFinishWait(active);
|
||||
if (!active.followUpStartedAt) { active.followUpStartedAt = now(); changed = true; }
|
||||
if (!wait.allFinished && wait.closeAt && now() < wait.closeAt) {
|
||||
if (active.finishWaitUntil !== wait.closeAt) { active.finishWaitUntil = wait.closeAt; changed = true; }
|
||||
if (changed) saveState();
|
||||
return { changed };
|
||||
}
|
||||
active.status = "finished";
|
||||
active.endedAt = Date.now();
|
||||
active.endedAt = now();
|
||||
active.finishedByTimer = true;
|
||||
active.followUpStartedAt = null;
|
||||
active.finishWaitUntil = null;
|
||||
if (getLastFinishAnnouncementSessionId() !== active.id) { announceRaceFinished(active); setLastFinishAnnouncementSessionId(active.id); }
|
||||
saveState();
|
||||
return { changed: true };
|
||||
|
||||
Reference in new issue
Block a user