Files
Live_RC/src/race_overview_logic.js
T

74 lines
4.0 KiB
JavaScript

// Race overview (Race Day): the whole event in one place - podium, then practice, qualifying
// and finals, each with its standings and every heat's result. Also the rows for one CSV.
// Works for race events and sponsor events (their heats carry carAssignments).
const PHASES = ["practice", "qualification", "final"];
// Where each driver ended up in qualifying ("TQ" for the top qualifier), shown next to the
// final result so you can see who climbed or dropped - like House of RC's "Qual." column.
export function qualifyingPlaces(qualifyingRows = []) {
const places = new Map();
qualifyingRows.forEach((row) => {
if (row?.driverId && row.rank) places.set(row.driverId, String(row.rank) === "1" ? "TQ" : String(row.rank));
});
return places;
}
export function withQualifyingPlaces(rows = [], qualifyingRows = []) {
const places = qualifyingPlaces(qualifyingRows);
if (!places.size) return rows;
return rows.map((row) => ({ ...row, qual: places.get(row.driverId) || "" }));
}
export function buildRaceOverview(event, deps) {
const { sessions = [], cars = [], buildLeaderboard, buildPracticeStandings, buildQualifyingStandings, buildFinalStandings } = deps;
if (!event) return null;
const carName = (session, driverId) => {
const carId = session.carAssignments?.[driverId];
return carId ? cars.find((car) => car.id === carId)?.name || "" : "";
};
const standingsOf = { practice: buildPracticeStandings, qualification: buildQualifyingStandings, final: buildFinalStandings };
const qualPlaces = sessions.some((session) => session.type === "qualification" && session.status !== "ready")
? qualifyingPlaces(buildQualifyingStandings(event) || [])
: new Map();
const phases = PHASES.map((type) => {
const heats = sessions
.filter((session) => session.type === type && session.status !== "ready")
.map((session) => ({
session,
rows: buildLeaderboard(session).map((row, index) => ({
pos: index + 1,
driverId: row.driverId,
qual: type === "final" ? qualPlaces.get(row.driverId) || "" : "",
name: row.displayName || row.driverName || row.transponder || "-",
car: carName(session, row.driverId),
laps: Number(row.laps || 0),
result: row.resultDisplay || "-",
bestLapMs: Number(row.bestLapMs || 0) || null,
timeMs: Number(row.totalElapsedMs || 0) || null,
raceStatus: row.raceStatus || "",
})),
}));
const planned = sessions.filter((session) => session.type === type).length;
const standings = heats.length ? standingsOf[type](event) || [] : [];
return { type, planned, heats, standings: type === "final" && qualPlaces.size ? standings.map((row) => ({ ...row, qual: qualPlaces.get(row.driverId) || "" })) : standings };
}).filter((phase) => phase.planned);
const finals = phases.find((phase) => phase.type === "final");
const podium = finals ? finals.standings.filter((row) => /^A?[1-3]$/.test(String(row.rank))).slice(0, 3) : [];
return { event, phases, podium, finished: sessions.length > 0 && sessions.every((session) => ["finished", "stopped"].includes(session.status)) };
}
// One CSV for the whole race: the standings ("Total") and every heat, one row per driver.
export function buildRaceOverviewCsvRows(overview, { t, formatLap, phaseLabel }) {
const rows = [["event", "phase", "heat", "pos", "qual", "driver", "car", "laps", "result", "best_lap"]];
if (!overview) return rows;
const eventName = overview.event.name || "";
overview.phases.forEach((phase) => {
const label = phaseLabel(phase.type);
phase.standings.forEach((row) => rows.push([eventName, label, t("race_overview.total"), String(row.rank), row.qual || "", row.driverName || "-", "", "", row.score || "-", ""]));
phase.heats.forEach(({ session, rows: heatRows }) => heatRows.forEach((row) => rows.push([
eventName, label, session.name, String(row.pos), row.qual || "", row.name, row.car, String(row.laps), row.result, row.bestLapMs ? formatLap(row.bestLapMs) : "",
])));
});
return rows;
}