feat(sponsor): quick setup with guest drivers, shared rental cars, rotating heats and A/B finals (1.63.0)
This commit is contained in:
1 parent
6142b14e47
commit
d0a5df6992
19 files changed
+511
-54
No files matched your search
@@ -0,0 +1,132 @@
|
||||
// Sponsor events (rental-kart style): guests drive the club's cars, several drivers share one
|
||||
// car (and its transponder) across the day. The event stays a "track" event (its own tab), but
|
||||
// its heats are ordinary race sessions plus carAssignments { driverId: carId }, so Race Day,
|
||||
// qualifying standings, A/B finals and Start / Pause / Stop work as in a race.
|
||||
// Guests are drivers with guestEventId: they are only shown inside their event.
|
||||
import { splitIntoBalancedGroups } from "./event_race_logic.js";
|
||||
|
||||
export const isGuestDriver = (driver) => Boolean(driver?.guestEventId);
|
||||
export const isSponsorEvent = (event) => event?.mode === "track" && Boolean(event?.sponsor);
|
||||
|
||||
// One name per line (or comma separated), duplicates and blanks dropped.
|
||||
export function parseGuestNames(text) {
|
||||
const seen = new Set();
|
||||
return String(text || "").split(/[\n,;]+/).map((name) => name.trim().replace(/\s+/g, " ")).filter((name) => {
|
||||
const key = name.toLowerCase();
|
||||
if (!name || seen.has(key)) return false;
|
||||
seen.add(key);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
// 8 drivers, 4 cars -> 2 groups, 1 practice per group, 2 qualifying rounds, A and B finals.
|
||||
export function defaultSponsorFormat(driverCount = 0, carCount = 0) {
|
||||
const cars = Math.max(1, carCount);
|
||||
return {
|
||||
groups: Math.max(1, Math.ceil(Math.max(1, driverCount) / cars)),
|
||||
practicePerGroup: 1, qualRounds: 2,
|
||||
practiceMin: 3, qualMin: 4, finalMin: 5,
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeSponsorFormat(value = {}, driverCount = 0, carCount = 0) {
|
||||
const base = defaultSponsorFormat(driverCount, carCount);
|
||||
const int = (raw, fallback, min, max) => {
|
||||
const number = Math.round(Number(raw));
|
||||
return Number.isFinite(number) ? Math.max(min, Math.min(max, number)) : fallback;
|
||||
};
|
||||
return {
|
||||
practicePerGroup: int(value.practicePerGroup, base.practicePerGroup, 0, 5),
|
||||
qualRounds: int(value.qualRounds, base.qualRounds, 0, 6),
|
||||
practiceMin: int(value.practiceMin, base.practiceMin, 1, 60),
|
||||
qualMin: int(value.qualMin, base.qualMin, 1, 60),
|
||||
finalMin: int(value.finalMin, base.finalMin, 1, 120),
|
||||
groups: base.groups,
|
||||
};
|
||||
}
|
||||
|
||||
// Groups no bigger than the number of cars, as even as possible (9 drivers, 4 cars -> 3/3/3).
|
||||
export function splitSponsorGroups(driverIds, carCount) {
|
||||
return splitIntoBalancedGroups(driverIds, Math.max(1, carCount));
|
||||
}
|
||||
|
||||
// Round r: driver i of the group gets car (i + r) mod cars, so everyone tries other cars.
|
||||
export function rotateCars(driverIds, carIds, round) {
|
||||
const map = {};
|
||||
if (!carIds.length) return map;
|
||||
driverIds.forEach((driverId, index) => { map[driverId] = carIds[(index + round) % carIds.length]; });
|
||||
return map;
|
||||
}
|
||||
|
||||
// Finals: cars drawn by lot.
|
||||
export function drawCars(driverIds, carIds, random = Math.random) {
|
||||
const pool = [...carIds];
|
||||
for (let index = pool.length - 1; index > 0; index -= 1) {
|
||||
const other = Math.floor(random() * (index + 1));
|
||||
[pool[index], pool[other]] = [pool[other], pool[index]];
|
||||
}
|
||||
const map = {};
|
||||
driverIds.forEach((driverId, index) => { if (pool[index]) map[driverId] = pool[index]; });
|
||||
return map;
|
||||
}
|
||||
|
||||
// The practice and qualifying heats as plain specs; the caller turns them into sessions.
|
||||
// Names follow the race ones (Q1 H1 ...) so qualifying standings and Race Day order work.
|
||||
export function planSponsorHeats(driverIds, carIds, format, { practiceLabel = "Practice" } = {}) {
|
||||
const groups = splitSponsorGroups(driverIds, carIds.length);
|
||||
const heats = [];
|
||||
let round = 0;
|
||||
for (let practice = 1; practice <= format.practicePerGroup; practice += 1, round += 1) {
|
||||
groups.forEach((group, index) => heats.push({
|
||||
name: `${practiceLabel} ${format.practicePerGroup > 1 ? `${practice} ` : ""}G${index + 1}`.trim(),
|
||||
type: "practice", durationMin: format.practiceMin, driverIds: group, carAssignments: rotateCars(group, carIds, round),
|
||||
}));
|
||||
}
|
||||
for (let qual = 1; qual <= format.qualRounds; qual += 1, round += 1) {
|
||||
groups.forEach((group, index) => heats.push({
|
||||
name: `Q${qual} H${index + 1}`, type: "qualification", durationMin: format.qualMin, driverIds: group, carAssignments: rotateCars(group, carIds, round),
|
||||
}));
|
||||
}
|
||||
return heats;
|
||||
}
|
||||
|
||||
// The race config the shared race code reads (heat and final size = number of cars).
|
||||
export function sponsorRaceConfig(raceConfig, { driverIds, carIds, format }) {
|
||||
return {
|
||||
...raceConfig,
|
||||
driverIds: [...driverIds],
|
||||
participantsConfigured: true,
|
||||
carsPerHeat: Math.max(2, carIds.length),
|
||||
carsPerFinal: Math.max(2, carIds.length),
|
||||
qualifyingRounds: Math.max(1, format.qualRounds),
|
||||
qualDurationMin: format.qualMin,
|
||||
finalDurationMin: format.finalMin,
|
||||
qualifyingScoring: "best",
|
||||
finalsSource: format.qualRounds > 0 ? "qualifying" : "practice",
|
||||
finalLegs: 1,
|
||||
bumpCount: 0,
|
||||
reserveBumpSlots: false,
|
||||
};
|
||||
}
|
||||
|
||||
// Gives every session of the event without cars a draw (used after the finals are made).
|
||||
export function drawCarsForSessions(sessions, carIds, random = Math.random) {
|
||||
let count = 0;
|
||||
sessions.forEach((session) => {
|
||||
if (session.status !== "ready" || !Array.isArray(session.driverIds) || !session.driverIds.length) return;
|
||||
if (session.carAssignments && Object.keys(session.carAssignments).length) return;
|
||||
session.carAssignments = drawCars(session.driverIds, carIds, random);
|
||||
count += 1;
|
||||
});
|
||||
return count;
|
||||
}
|
||||
|
||||
// The driver a transponder belongs to in this heat: the one who drives the car with it.
|
||||
export function findDriverByCarTransponder(session, cars, transponder) {
|
||||
const assignments = session?.carAssignments;
|
||||
if (!assignments || typeof assignments !== "object") return null;
|
||||
const car = (cars || []).find((item) => String(item.transponder || "") === String(transponder || "") && Object.values(assignments).includes(item.id));
|
||||
if (!car) return null;
|
||||
const driverId = Object.keys(assignments).find((id) => assignments[id] === car.id);
|
||||
return driverId ? { driverId, car } : null;
|
||||
}
|
||||
Reference in new issue
Block a user