feat(team-race): one team race setup panel with auto split, car per team and start checks (1.67.0)
This commit is contained in:
1 parent
69a65748ce
commit
37076b8490
13 files changed
+344
-309
No files matched your search
@@ -0,0 +1,67 @@
|
||||
// Team race setup (Events → Manage → Team race): teams of drivers sharing a car (or each with
|
||||
// their own transponder), the race length, and the checks before the start. Plain data in,
|
||||
// new team lists out; the view (team_race_setup_view.js) saves them in event.raceConfig.teams.
|
||||
|
||||
// Drivers dealt out over `count` teams in turn (1,2,3,1,2,3...), so the teams differ by at most
|
||||
// one driver. Teams already there keep their id, name and car; extra ones are added.
|
||||
export function splitIntoTeams(driverIds, count, existing = [], { uid, nameFor }) {
|
||||
const total = Math.max(1, Math.round(Number(count) || 1));
|
||||
const teams = Array.from({ length: total }, (_, index) => {
|
||||
const kept = existing[index];
|
||||
return { id: kept?.id || uid("team"), name: kept?.name || nameFor(index + 1), driverIds: [], carIds: kept ? [...(kept.carIds || [])] : [] };
|
||||
});
|
||||
driverIds.forEach((driverId, index) => teams[index % total].driverIds.push(driverId));
|
||||
return teams;
|
||||
}
|
||||
|
||||
// Puts a driver on one team (teamId "" takes them off every team).
|
||||
export function moveDriverToTeam(teams, driverId, teamId) {
|
||||
return teams.map((team) => {
|
||||
const without = team.driverIds.filter((id) => id !== driverId);
|
||||
return { ...team, driverIds: team.id === teamId ? [...without, driverId] : without };
|
||||
});
|
||||
}
|
||||
|
||||
// The team's car (carId "" = none, the drivers' own transponders count). A car is on one team only.
|
||||
export function setTeamCar(teams, teamId, carId) {
|
||||
return teams.map((team) => {
|
||||
if (team.id === teamId) return { ...team, carIds: carId ? [carId] : [] };
|
||||
return carId ? { ...team, carIds: team.carIds.filter((id) => id !== carId) } : team;
|
||||
});
|
||||
}
|
||||
|
||||
export function renameTeam(teams, teamId, name) {
|
||||
return teams.map((team) => (team.id === teamId ? { ...team, name: String(name || "").trim() || team.name } : team));
|
||||
}
|
||||
|
||||
export function removeTeam(teams, teamId) {
|
||||
return teams.filter((team) => team.id !== teamId);
|
||||
}
|
||||
|
||||
// The transponders that count for a team: its car's, then each driver's own.
|
||||
export function getTeamTransponders(team, { drivers = [], cars = [] }) {
|
||||
const values = [
|
||||
...team.carIds.map((id) => cars.find((car) => car.id === id)?.transponder),
|
||||
...team.driverIds.map((id) => drivers.find((driver) => driver.id === id)?.transponder),
|
||||
];
|
||||
return [...new Set(values.map((value) => String(value || "").trim()).filter(Boolean))];
|
||||
}
|
||||
|
||||
// What to fix before the start: teams without drivers or transponder, a transponder on two
|
||||
// teams (laps would go to the wrong one), and chosen drivers on no team.
|
||||
export function findTeamRaceIssues(teams, { drivers = [], cars = [], driverIds = [] }) {
|
||||
const owners = new Map();
|
||||
teams.forEach((team) => getTeamTransponders(team, { drivers, cars }).forEach((tp) => owners.set(tp, [...(owners.get(tp) || []), team.name])));
|
||||
const placed = new Set(teams.flatMap((team) => team.driverIds));
|
||||
return {
|
||||
noTeams: teams.length < 2,
|
||||
empty: teams.filter((team) => !team.driverIds.length).map((team) => team.name),
|
||||
noTransponder: teams.filter((team) => team.driverIds.length && !getTeamTransponders(team, { drivers, cars }).length).map((team) => team.name),
|
||||
shared: [...owners.entries()].filter(([, names]) => names.length > 1).map(([transponder, names]) => ({ transponder, teams: names })),
|
||||
unplaced: driverIds.filter((id) => !placed.has(id)),
|
||||
};
|
||||
}
|
||||
|
||||
export function hasBlockingTeamIssues(issues) {
|
||||
return issues.noTeams || issues.empty.length > 0 || issues.noTransponder.length > 0 || issues.shared.length > 0;
|
||||
}
|
||||
Reference in new issue
Block a user