feat(race-day): day schedule for all classes, autopilot, pause the day, printed running order and drivers' board (1.50.0)
This commit is contained in:
1 parent
0ba858d638
commit
fdc6c179dc
18 files changed
+735
-20
No files matched your search
@@ -0,0 +1,144 @@
|
||||
// The race day as one schedule: every race session of every class on one date, in one running
|
||||
// order with planned times, plus what the autopilot should do next. Pure logic - the panel is
|
||||
// in day_schedule_view.js and the runner in app.js.
|
||||
import { applyManualOrder, orderSessionsForRunning, parseQualifyingHeatName } from "./event_race_logic.js";
|
||||
|
||||
export const DEFAULT_DAY_GAP_MIN = 3;
|
||||
export const HEADS_UP_MS = 10 * 60000;
|
||||
|
||||
// One plan per date: { date, startAt, gapMin, sessionOrder, autopilot, pausedAt }.
|
||||
export function normalizeRaceDay(day = {}) {
|
||||
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,
|
||||
sessionOrder: [...new Set((Array.isArray(day.sessionOrder) ? day.sessionOrder : []).map(String).filter(Boolean))],
|
||||
autopilot: day.autopilot === true,
|
||||
pausedAt: Number(day.pausedAt) > 0 ? Number(day.pausedAt) : null,
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeRaceDays(list) {
|
||||
const byDate = new Map();
|
||||
(Array.isArray(list) ? list : []).map(normalizeRaceDay).filter((day) => /^\d{4}-\d{2}-\d{2}$/.test(day.date)).forEach((day) => byDate.set(day.date, day));
|
||||
return [...byDate.values()];
|
||||
}
|
||||
|
||||
export function getRaceDay(raceDays, date) {
|
||||
return (raceDays || []).find((day) => day.date === date) || normalizeRaceDay({ date });
|
||||
}
|
||||
|
||||
// Stores a changed plan for its date; returns the new list.
|
||||
export function upsertRaceDay(raceDays, day) {
|
||||
const next = normalizeRaceDay(day);
|
||||
return [...(raceDays || []).filter((item) => item.date !== next.date), next];
|
||||
}
|
||||
|
||||
const sessionDurationMs = (session) => Math.max(1, Number(session.durationMin || 0)) * 60000 + Math.max(0, Number(session.followUpSec || 0)) * 1000;
|
||||
const isDone = (session) => ["finished", "stopped"].includes(session.status);
|
||||
const isLive = (session) => ["running", "paused"].includes(session.status);
|
||||
|
||||
// Where a session sits in the day: practice, then qualifying round by round, then finals leg
|
||||
// by leg (lowest final first). Classes take turns within each step.
|
||||
function dayStepKey(session) {
|
||||
if (["practice", "free_practice", "open_practice"].includes(session.type)) return [0, 0, 0];
|
||||
if (session.type === "final") {
|
||||
const match = String(session.name || "").match(/^([A-Z])\b.*?(\d+)\s*$/);
|
||||
return match ? [2, Number(match[2]), -match[1].charCodeAt(0)] : [2, 0, 0];
|
||||
}
|
||||
return [1, parseQualifyingHeatName(session.name)?.round || 0, 0];
|
||||
}
|
||||
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.
|
||||
export function orderDaySessions({ events = [], sessions = [], date, manualOrder = [] }) {
|
||||
const dayEvents = events.filter((event) => event.mode === "race" && event.date === date)
|
||||
.sort((a, b) => String(a.name || "").localeCompare(String(b.name || ""), undefined, { numeric: true }));
|
||||
const items = dayEvents.flatMap((event, eventIndex) => {
|
||||
const own = orderSessionsForRunning(sessions.filter((session) => session.eventId === event.id && session.mode === "race"), event.sessionOrder);
|
||||
// A step never goes back within a class, so a class's own ▲▼ order is kept.
|
||||
let step = [0, 0, 0];
|
||||
return own.map((session, position) => {
|
||||
const key = dayStepKey(session);
|
||||
if (compareKeys(key, step) > 0) step = key;
|
||||
return { session, event, key: [...step, eventIndex, position] };
|
||||
});
|
||||
});
|
||||
const auto = items.sort((a, b) => compareKeys(a.key, b.key)).map((item) => item.session);
|
||||
return applyManualOrder(auto, manualOrder);
|
||||
}
|
||||
|
||||
export function moveInDayOrder(ordered, sessionId, step) {
|
||||
const ids = ordered.map((session) => session.id);
|
||||
const index = ids.indexOf(sessionId);
|
||||
const other = index + step;
|
||||
if (index < 0 || other < 0 || other >= ids.length) return ids;
|
||||
[ids[index], ids[other]] = [ids[other], ids[index]];
|
||||
return ids;
|
||||
}
|
||||
|
||||
// Planned time for each session. A session that started or finished re-anchors everything
|
||||
// after it; one that should have started but hasn't is expected now, so a paused day or a
|
||||
// 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;
|
||||
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;
|
||||
let state = "upcoming";
|
||||
let start = planned;
|
||||
let end;
|
||||
if (isDone(session)) {
|
||||
state = "done";
|
||||
start = actualStart || planned;
|
||||
end = actualEnd || start + durationMs;
|
||||
} else if (isLive(session) && actualStart) {
|
||||
state = "live";
|
||||
start = actualStart;
|
||||
const pausedMs = Number(session.totalPausedMs || 0) + (session.status === "paused" && session.pausedAt ? Math.max(0, now - Number(session.pausedAt)) : 0);
|
||||
end = Math.max(now, actualStart + durationMs + pausedMs);
|
||||
} else {
|
||||
start = Math.max(planned, now);
|
||||
end = start + durationMs;
|
||||
}
|
||||
cursor = end + gapMs;
|
||||
return {
|
||||
session,
|
||||
state,
|
||||
plannedStart: planned,
|
||||
start,
|
||||
end,
|
||||
upcomingIndex: state === "upcoming" ? upcomingIndex++ : null,
|
||||
delayed: Boolean((actualStart || (state === "upcoming" ? start : 0)) > planned + 60000),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// What the autopilot does now: { type: "start", row, launchAt } or { type: "wait", reason, row?, launchAt? }.
|
||||
// prologueMs(session) = how long before the planned start its count-down begins.
|
||||
export function getAutopilotAction(rows, { day, now = Date.now(), busy = false, prologueMs = () => 0 }) {
|
||||
if (day?.pausedAt) return { type: "wait", reason: "paused" };
|
||||
if (busy || rows.some((row) => row.state === "live")) return { type: "wait", reason: "running" };
|
||||
const row = rows.find((item) => item.state === "upcoming");
|
||||
if (!row) return { type: "wait", reason: "done" };
|
||||
const launchAt = row.start - Math.max(0, Number(prologueMs(row.session) || 0));
|
||||
return now >= launchAt ? { type: "start", row, launchAt } : { type: "wait", reason: "until", row, launchAt };
|
||||
}
|
||||
|
||||
// The date the drivers' board shows: today if anything is raced today, else the next date with
|
||||
// a session still to run, else today.
|
||||
export function pickBoardDate({ events = [], sessions = [], today }) {
|
||||
const raceDates = new Set(events.filter((event) => event.mode === "race" && event.date).map((event) => event.date));
|
||||
if (raceDates.has(today)) return today;
|
||||
const pending = events
|
||||
.filter((event) => event.mode === "race" && event.date >= today && sessions.some((session) => session.eventId === event.id && !isDone(session)))
|
||||
.map((event) => event.date)
|
||||
.sort();
|
||||
return pending[0] || today;
|
||||
}
|
||||
Reference in new issue
Block a user