feat(profile): 'My race today' on the driver's mobile profile - next heats, grid place, marshal duties and today's results (1.89.31)

This commit is contained in:
larssand committed 2026-11-14 16:38:08 +01:00
1 parent 163e82224c
commit 01998952a7
14 files changed
+222 -3

No files matched your search

+54
View File
@@ -866,7 +866,61 @@ function getPublicTrainingRecommendation(stats) {
return { level: "progression", targetLapMs: Math.round(stats.bestLapMs * 1.05) };
}
// "My race today": one driver's part of the race master's day snapshot (src/my_race_logic.js):
// upcoming heats with time and grid place, marshal duties, and results of heats already run.
// Only for today's date; the driver's other class entries (same profile) count too.
function publicProfileDriverIds(state, shareToken) {
const share = findPublicTrainingShare(state, shareToken);
if (!share) return [];
const profile = getPublicDriverProfile(Array.isArray(state?.drivers) ? state.drivers : [], String(share.driverId || ""));
return profile ? profile.drivers.map((driver) => String(driver.id || "")).filter(Boolean) : [];
}
function localDateKey(ms) {
const d = new Date(ms);
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
}
function pickDriverRaceDay(snapshot, driverIds, now = Date.now()) {
const ids = new Set((driverIds || []).map(String));
if (!snapshot || !ids.size || snapshot.date !== localDateKey(now)) return null;
const sessions = Array.isArray(snapshot.sessions) ? snapshot.sessions : [];
const heats = [];
const marshal = [];
const results = [];
sessions.forEach((session) => {
const entryIndex = (session.entries || []).findIndex((id) => ids.has(String(id)));
if (session.state !== "done" && entryIndex >= 0) heats.push({ label: String(session.label || ""), start: Number(session.start) || 0, live: session.state === "live", gridPosition: session.gridStart ? entryIndex + 1 : null });
if (session.state !== "done" && (session.marshals || []).some((id) => ids.has(String(id)))) marshal.push({ label: String(session.label || ""), start: Number(session.start) || 0, live: session.state === "live" });
const result = (session.results || []).find((item) => ids.has(String(item.driverId)));
if (session.state === "done" && result) results.push({ label: String(session.label || ""), position: Number(result.position) || 0, laps: Number(result.laps) || 0, result: String(result.result || "") });
});
if (!heats.length && !marshal.length && !results.length) return null;
return { heats, marshal, results };
}
// Keeps what the race master posts to sane sizes and types.
function sanitizePublicRaceDaySnapshot(input) {
const date = String(input?.date || "");
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) return null;
const ids = (list) => (Array.isArray(list) ? list : []).slice(0, 60).map((id) => String(id).slice(0, 80));
const sessions = (Array.isArray(input?.sessions) ? input.sessions : []).slice(0, 200).map((session) => ({
id: String(session?.id || "").slice(0, 80),
label: String(session?.label || "").slice(0, 120),
start: Math.max(0, Number(session?.start) || 0),
state: ["upcoming", "live", "done"].includes(session?.state) ? session.state : "upcoming",
gridStart: session?.gridStart === true,
entries: ids(session?.entries),
marshals: ids(session?.marshals),
results: (Array.isArray(session?.results) ? session.results : []).slice(0, 60).map((item) => ({ driverId: String(item?.driverId || "").slice(0, 80), position: Math.max(0, Number(item?.position) || 0), laps: Math.max(0, Number(item?.laps) || 0), result: String(item?.result || "").slice(0, 40) })),
}));
return { date, sessions };
}
module.exports = {
publicProfileDriverIds,
pickDriverRaceDay,
sanitizePublicRaceDaySnapshot,
normalizePublicTrainingLayout,
buildPublicTrainingProfile,
findPublicTrainingShare,