feat(day): breaks between rounds, before the finals and before any heat (1.89.9)

This commit is contained in:
larssand committed 2026-11-12 21:36:28 +01:00
1 parent 5e4e77ca7b
commit daed2ff790
10 files changed
+114 -16

No files matched your search

+32 -5
View File
@@ -6,13 +6,23 @@ import { applyManualOrder, orderSessionsForRunning, parseQualifyingHeatName } fr
export const DEFAULT_DAY_GAP_MIN = 3;
export const DEFAULT_CALL_MIN = 10;
// One plan per date: { date, startAt, gapMin, callMin, sessionOrder, autopilot, pausedAt }.
// callMin = how long before its start a heat is called (drivers get ready).
// One plan per date: { date, startAt, gapMin, roundGapMin, finalsGapMin, breaks, callMin,
// sessionOrder, autopilot, pausedAt }.
// gapMin = break between heats of one round; roundGapMin = between rounds (practice -> qualifying,
// round 1 -> round 2, final leg 1 -> leg 2); finalsGapMin = before the finals (lunch). Empty
// round / finals gaps fall back to the one before. breaks = { sessionId: minutes } of extra
// break before that heat. callMin = how long before its start a heat is called.
const optionalMin = (value) => (value === null || value === undefined || value === "" || !Number.isFinite(Number(value)) ? null : Math.max(0, Number(value)));
export function normalizeRaceDay(day = {}) {
const breaks = Object.fromEntries(Object.entries(day.breaks && typeof day.breaks === "object" ? day.breaks : {})
.map(([id, min]) => [String(id), Math.max(0, Math.min(600, Number(min) || 0))]).filter(([id, min]) => id && min > 0));
return {
date: String(day.date || ""),
startAt: Number(day.startAt) > 0 ? Number(day.startAt) : null,
gapMin: Number.isFinite(Number(day.gapMin)) && day.gapMin !== null && day.gapMin !== "" ? Math.max(0, Number(day.gapMin)) : DEFAULT_DAY_GAP_MIN,
roundGapMin: optionalMin(day.roundGapMin),
finalsGapMin: optionalMin(day.finalsGapMin),
breaks,
callMin: Number(day.callMin) > 0 ? Math.min(60, Number(day.callMin)) : DEFAULT_CALL_MIN,
sessionOrder: [...new Set((Array.isArray(day.sessionOrder) ? day.sessionOrder : []).map(String).filter(Boolean))],
autopilot: day.autopilot === true,
@@ -50,6 +60,19 @@ function dayStepKey(session) {
}
return [1, parseQualifyingHeatName(session.name)?.round || 0, 0];
}
// The break before `session` when `previous` ran just before it (minutes).
export function breakBeforeMin(previous, session, day) {
const plan = normalizeRaceDay(day || {});
const extra = plan.breaks[session?.id] || 0;
if (!previous) return extra;
const [prevType, prevRound] = dayStepKey(previous);
const [type, round] = dayStepKey(session);
const roundGap = plan.roundGapMin ?? plan.gapMin;
const gap = type === 2 && prevType !== 2 ? plan.finalsGapMin ?? roundGap
: type !== prevType || round !== prevRound ? roundGap
: plan.gapMin;
return gap + extra;
}
const compareKeys = (a, b) => a.reduce((diff, value, i) => diff || value - b[i], 0);
// The race events of a date (one per class) and their race sessions, in the day's running order.
@@ -84,15 +107,18 @@ export function moveInDayOrder(ordered, sessionId, step) {
// late heat pushes the rest of the day on its own. Without a start time the day runs from
// its first real start, else from now.
export function buildDaySchedule(ordered, day, now = Date.now()) {
const gapMs = Math.max(0, Number(day?.gapMin ?? DEFAULT_DAY_GAP_MIN)) * 60000;
const firstStart = ordered.map((session) => Number(session.startedAt || 0)).filter(Boolean).sort((a, b) => a - b)[0];
let cursor = Number(day?.startAt || 0) || firstStart || now;
let upcomingIndex = 0;
let previous = null;
return ordered.map((session) => {
const durationMs = sessionDurationMs(session);
const actualStart = Number(session.startedAt || 0) || null;
const actualEnd = isDone(session) ? Number(session.endedAt || 0) || null : null;
const planned = cursor;
// The first heat starts at the day's start time; an extra break before it still counts.
const breakMin = breakBeforeMin(previous, session, day);
const planned = cursor + breakMin * 60000;
previous = session;
let state = "upcoming";
let start = planned;
let end;
@@ -109,9 +135,10 @@ export function buildDaySchedule(ordered, day, now = Date.now()) {
start = Math.max(planned, now);
end = start + durationMs;
}
cursor = end + gapMs;
cursor = end;
return {
session,
breakMin,
state,
plannedStart: planned,
start,