Each layout can carry its own lap window (set on creation, editable in Settings, saved from Race Day during practice). Open/Free practice on that layout uses it; races always keep the event's values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
4.2 KiB
JavaScript
83 lines
4.2 KiB
JavaScript
// A club that reconfigures its track (common indoors, in the same building) needs a way
|
|
// to say "everything from this point on is a different layout" so lap-time comparisons
|
|
// don't silently mix incomparable tracks. Layouts are assumed contiguous - a club changes
|
|
// layout roughly every few weeks and never switches back and forth - so only a start
|
|
// date/time is stored per layout; a layout's end is implicitly the next one's start (see
|
|
// ROADMAP.md "Track layout changes and stat continuity" for the fuller reasoning).
|
|
export function normalizeTrackLayout(layout, { uid } = {}) {
|
|
return {
|
|
id: String(layout?.id || (uid ? uid("layout") : "")),
|
|
label: String(layout?.label || "").trim(),
|
|
startedAt: Number.isFinite(Number(layout?.startedAt)) ? Number(layout.startedAt) : Date.now(),
|
|
// Optional lap window for practice on this layout (a rebuilt track is often shorter or
|
|
// longer); 0 = not set, the event's own min/max lap time applies.
|
|
minLapMs: Math.max(0, Math.round(Number(layout?.minLapMs) || 0)),
|
|
maxLapMs: Math.max(0, Math.round(Number(layout?.maxLapMs) || 0)),
|
|
};
|
|
}
|
|
|
|
// Practice sessions use their layout's min/max lap time where it is set; race sessions
|
|
// always keep the event's (set by race control, never changed mid-race by a layout).
|
|
export function getLayoutLapWindowOverride(layouts, session) {
|
|
if (!session || !isPracticeSessionType(session.type) || !session.trackLayoutId) return {};
|
|
const layout = (Array.isArray(layouts) ? layouts : []).find((item) => item?.id === session.trackLayoutId);
|
|
const override = {};
|
|
if (Number(layout?.minLapMs) > 0) override.minLapMs = Number(layout.minLapMs);
|
|
if (Number(layout?.maxLapMs) > 0) override.maxLapMs = Number(layout.maxLapMs);
|
|
return override;
|
|
}
|
|
|
|
export function normalizeTrackLayouts(layouts, { uid } = {}) {
|
|
const list = (Array.isArray(layouts) ? layouts : [])
|
|
.map((layout) => normalizeTrackLayout(layout, { uid }))
|
|
.filter((layout) => layout.id);
|
|
return list.slice().sort((a, b) => a.startedAt - b.startedAt);
|
|
}
|
|
|
|
// The layout active at `atMs` (default: now) - the latest one whose startedAt has already
|
|
// passed. Returns "" if no layout has been configured yet (the feature is simply unused,
|
|
// and every comparison that scopes by this id degrades back to today's unscoped behavior).
|
|
export function getTrackLayoutIdAt(layouts, atMs = Date.now()) {
|
|
let current = "";
|
|
for (const layout of normalizeTrackLayouts(layouts)) {
|
|
if (layout.startedAt > atMs) break;
|
|
current = layout.id;
|
|
}
|
|
return current;
|
|
}
|
|
|
|
export function getTrackLayoutLabel(layouts, layoutId) {
|
|
if (!layoutId) return "";
|
|
const layout = (Array.isArray(layouts) ? layouts : []).find((item) => item.id === layoutId);
|
|
return layout?.label || "";
|
|
}
|
|
|
|
// The default label offered when adding a layout without typing a custom name, numbered
|
|
// by how many layouts already exist (1-based, so the very first is "Layout 1").
|
|
export function nextTrackLayoutLabel(layouts) {
|
|
return `Layout ${(Array.isArray(layouts) ? layouts.length : 0) + 1}`;
|
|
}
|
|
|
|
// Layouts are a practice concept: a club re-lays the track between practice days (or mid
|
|
// Open Practice), never in the middle of a race format. These are the casual practice
|
|
// session types that may be running when a layout changes.
|
|
const PRACTICE_SESSION_TYPES = ["open_practice", "free_practice"];
|
|
|
|
export function isPracticeSessionType(type) {
|
|
return PRACTICE_SESSION_TYPES.includes(String(type || "").toLowerCase());
|
|
}
|
|
|
|
// A new layout can't start while a race-format session (qualifying, heat, final, ...) is
|
|
// running or paused - that race must finish on the track it started on.
|
|
export function findRaceSessionBlockingLayoutChange(sessions) {
|
|
return (Array.isArray(sessions) ? sessions : []).find((session) =>
|
|
["running", "paused"].includes(session?.status) && !isPracticeSessionType(session?.type)) || null;
|
|
}
|
|
|
|
// In use = any session started on it, or any training lap driven on it. Deleting a layout
|
|
// that practice laps belong to would silently move those laps onto the previous layout.
|
|
export function isTrackLayoutInUse(layoutId, { sessions = [], ledger = [] } = {}) {
|
|
return sessions.some((session) => session?.trackLayoutId === layoutId)
|
|
|| ledger.some((lap) => lap?.trackLayoutId === layoutId);
|
|
}
|