feat(series): add series/cup championships with points tables and standings
Group any number of races in freely chosen classes into a series with a points table (ÖRC/100-80-70/F1/custom), best-N or drop-X counting, a club filter that re-ranks members without guests, tie-breaks, rounds created from a date list, and per-class standings with print and PDF. Also fixes PDF export of wide tables: they are compacted and switched to landscape instead of truncating names and dropping columns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f65e526948
commit
9a9379aa83
18 files changed
+848
-20
No files matched your search
@@ -0,0 +1,148 @@
|
||||
// Series / cup: a set of races (events) scored together. Each event is one class, so a series
|
||||
// with several classes gives one standings table per class. Pure functions - app.js feeds
|
||||
// each event's day result (finishing order, from finals or else qualifying).
|
||||
//
|
||||
// Rules supported (see ROADMAP "Series / cup"):
|
||||
// - points per place from a table, then a fixed step down to a minimum for later places;
|
||||
// - only drivers from the series' club score (others still race and appear in the day
|
||||
// result); club drivers are re-ranked without them;
|
||||
// - a driver with no registered lap in the event gets 0 points and no place;
|
||||
// - count the best N results, or all completed events minus X (so a cancelled event
|
||||
// reduces the count automatically);
|
||||
// - ties: more wins among counted results, then 2nd places, 3rd..., then the latest common
|
||||
// event where both scored, going backwards; otherwise a shared position.
|
||||
|
||||
export const SERIES_POINT_PRESETS = Object.freeze({
|
||||
orc: { table: [100, 90, 82, 75, 69, 64, 60, 56, 52, 48], step: 4, min: 10 },
|
||||
hundred: { table: [100, 80, 70, 60, 50, 45, 40, 35, 30, 25], step: 5, min: 5 },
|
||||
f1: { table: [25, 18, 15, 12, 10, 8, 6, 4, 2, 1], step: 0, min: 0 },
|
||||
});
|
||||
|
||||
const clampInt = (value, fallback, min = 0) => {
|
||||
const number = Math.round(Number(value));
|
||||
return Number.isFinite(number) && number >= min ? number : fallback;
|
||||
};
|
||||
|
||||
export function parsePointsTable(text) {
|
||||
return String(text || "")
|
||||
.split(/[\s,;]+/)
|
||||
.map((value) => Math.round(Number(value)))
|
||||
.filter((value) => Number.isFinite(value) && value >= 0);
|
||||
}
|
||||
|
||||
export function normalizeSeries(series = {}, { uid } = {}) {
|
||||
const preset = SERIES_POINT_PRESETS.orc;
|
||||
const table = Array.isArray(series.pointsTable) ? series.pointsTable.map((value) => clampInt(value, 0)) : preset.table.slice();
|
||||
return {
|
||||
id: String(series.id || (uid ? uid("series") : "")),
|
||||
name: String(series.name || "").trim(),
|
||||
club: String(series.club || "").trim(),
|
||||
classIds: Array.isArray(series.classIds) ? [...new Set(series.classIds.map(String).filter(Boolean))] : [],
|
||||
eventIds: Array.isArray(series.eventIds) ? [...new Set(series.eventIds.map(String).filter(Boolean))] : [],
|
||||
pointsTable: table.length ? table : preset.table.slice(),
|
||||
pointsStep: clampInt(series.pointsStep, preset.step),
|
||||
pointsMin: clampInt(series.pointsMin, preset.min),
|
||||
countMode: series.countMode === "drop" ? "drop" : "best",
|
||||
countValue: clampInt(series.countValue, series.countMode === "drop" ? 0 : 9),
|
||||
createdAt: Number(series.createdAt) > 0 ? Number(series.createdAt) : Date.now(),
|
||||
};
|
||||
}
|
||||
|
||||
export function getSeriesPointsForPlace(series, place) {
|
||||
const table = series.pointsTable || [];
|
||||
if (!(place >= 1)) return 0;
|
||||
if (place <= table.length) return table[place - 1];
|
||||
// Step 0: every place after the table gets the minimum (F1: 0 from 11th).
|
||||
if (!Number(series.pointsStep)) return Number(series.pointsMin || 0);
|
||||
const last = table.length ? table[table.length - 1] : 0;
|
||||
return Math.max(Number(series.pointsMin || 0), last - Number(series.pointsStep) * (place - table.length));
|
||||
}
|
||||
|
||||
const sameClub = (club, seriesClub) => String(club || "").trim().toLowerCase() === String(seriesClub || "").trim().toLowerCase();
|
||||
|
||||
// dayResult: the event's finishing order, [{ driverId, driverName, hasLap }].
|
||||
// Returns per-driver { place, points } for the drivers who score in this series.
|
||||
export function scoreSeriesEvent(series, dayResult, { getDriverClub = () => "" } = {}) {
|
||||
const scored = new Map();
|
||||
let place = 0;
|
||||
(dayResult || []).forEach((row) => {
|
||||
if (!row?.driverId || scored.has(row.driverId)) return;
|
||||
if (series.club && !sameClub(getDriverClub(row.driverId), series.club)) return;
|
||||
if (!row.hasLap) {
|
||||
scored.set(row.driverId, { place: null, points: 0, driverName: row.driverName });
|
||||
return;
|
||||
}
|
||||
place += 1;
|
||||
scored.set(row.driverId, { place, points: getSeriesPointsForPlace(series, place), driverName: row.driverName });
|
||||
});
|
||||
return scored;
|
||||
}
|
||||
|
||||
export function getCountedResultsCount(series, completedEvents) {
|
||||
if (series.countMode === "drop") return Math.max(Math.min(1, completedEvents), completedEvents - series.countValue);
|
||||
return Math.min(series.countValue || completedEvents, completedEvents);
|
||||
}
|
||||
|
||||
function compareTie(a, b, completedEventIds) {
|
||||
const maxPlace = Math.max(0, ...a.countedPlaces, ...b.countedPlaces);
|
||||
for (let place = 1; place <= maxPlace; place += 1) {
|
||||
const diff = b.countedPlaces.filter((value) => value === place).length - a.countedPlaces.filter((value) => value === place).length;
|
||||
if (diff) return diff;
|
||||
}
|
||||
for (let index = completedEventIds.length - 1; index >= 0; index -= 1) {
|
||||
const left = a.byEvent.get(completedEventIds[index]);
|
||||
const right = b.byEvent.get(completedEventIds[index]);
|
||||
if (left?.place && right?.place && left.place !== right.place) return left.place - right.place;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// events: the series' events (each { id, name, date, classId }), dayResults: Map eventId ->
|
||||
// day result (empty/missing = not run yet). Returns one table per class.
|
||||
export function buildSeriesStandings(series, { events = [], dayResults = new Map(), getDriverClub, getDriverName = () => "", classNames = new Map() } = {}) {
|
||||
const inSeries = events
|
||||
.filter((event) => series.eventIds.includes(event.id))
|
||||
.sort((a, b) => String(a.date || "").localeCompare(String(b.date || "")) || String(a.name || "").localeCompare(String(b.name || "")));
|
||||
const classIds = series.classIds.length ? series.classIds : [...new Set(inSeries.map((event) => event.classId))];
|
||||
return classIds.map((classId) => {
|
||||
const classEvents = inSeries.filter((event) => event.classId === classId);
|
||||
const completed = classEvents.filter((event) => (dayResults.get(event.id) || []).length > 0);
|
||||
const completedIds = completed.map((event) => event.id);
|
||||
const counted = getCountedResultsCount(series, completed.length);
|
||||
const drivers = new Map();
|
||||
completed.forEach((event) => {
|
||||
scoreSeriesEvent(series, dayResults.get(event.id), { getDriverClub }).forEach((result, driverId) => {
|
||||
if (!drivers.has(driverId)) drivers.set(driverId, { driverId, driverName: getDriverName(driverId) || result.driverName || driverId, byEvent: new Map() });
|
||||
drivers.get(driverId).byEvent.set(event.id, result);
|
||||
});
|
||||
});
|
||||
const rows = [...drivers.values()].map((driver) => {
|
||||
// Every completed event is a result; absence counts as 0 and can be struck.
|
||||
const results = completed.map((event) => ({ eventId: event.id, ...(driver.byEvent.get(event.id) || { place: null, points: 0, absent: true }) }));
|
||||
const keep = new Set([...results.keys()].sort((a, b) => results[b].points - results[a].points || a - b).slice(0, counted));
|
||||
results.forEach((result, index) => { result.counted = keep.has(index); });
|
||||
return {
|
||||
driverId: driver.driverId,
|
||||
driverName: driver.driverName,
|
||||
results,
|
||||
total: results.filter((result) => result.counted).reduce((sum, result) => sum + result.points, 0),
|
||||
starts: results.filter((result) => !result.absent).length,
|
||||
countedPlaces: results.filter((result) => result.counted && result.place).map((result) => result.place),
|
||||
byEvent: driver.byEvent,
|
||||
};
|
||||
});
|
||||
rows.sort((a, b) => b.total - a.total || compareTie(a, b, completedIds) || a.driverName.localeCompare(b.driverName));
|
||||
rows.forEach((row, index) => {
|
||||
const previous = rows[index - 1];
|
||||
row.rank = previous && previous.total === row.total && compareTie(previous, row, completedIds) === 0 ? previous.rank : index + 1;
|
||||
});
|
||||
return {
|
||||
classId,
|
||||
className: classNames.get(classId) || classId,
|
||||
events: classEvents.map((event) => ({ id: event.id, name: event.name, date: event.date, completed: completedIds.includes(event.id) })),
|
||||
completedCount: completed.length,
|
||||
countedCount: counted,
|
||||
rows: rows.map(({ byEvent, countedPlaces, ...row }) => row),
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user