fix tränings länken och race day
This commit is contained in:
1 parent
375bfb2416
commit
28e5c96010
7 files changed
+531
-6
No files matched your search
@@ -0,0 +1,133 @@
|
||||
function unique(values) {
|
||||
return [...new Set(values.map((value) => String(value || "").trim()).filter(Boolean))];
|
||||
}
|
||||
|
||||
function duplicateValues(values) {
|
||||
const counts = new Map();
|
||||
values.filter(Boolean).forEach((value) => counts.set(value, (counts.get(value) || 0) + 1));
|
||||
return [...counts.entries()].filter(([, count]) => count > 1).map(([value]) => value);
|
||||
}
|
||||
|
||||
function getEvent(state, session) {
|
||||
return state.events.find((event) => event.id === session?.eventId) || null;
|
||||
}
|
||||
|
||||
function getRaceDriverIds(state, session, event) {
|
||||
if (Array.isArray(session?.driverIds) && session.driverIds.length) {
|
||||
return unique(session.driverIds);
|
||||
}
|
||||
if (event?.raceConfig?.participantsConfigured) {
|
||||
return unique(event.raceConfig.driverIds || []);
|
||||
}
|
||||
return state.drivers
|
||||
.filter((driver) => !event?.classId || driver.classId === event.classId)
|
||||
.map((driver) => driver.id);
|
||||
}
|
||||
|
||||
function check(id, level, data = {}) {
|
||||
return { id, level, ...data };
|
||||
}
|
||||
|
||||
export function buildRaceDayQueue(state) {
|
||||
const sessions = Array.isArray(state?.sessions) ? state.sessions : [];
|
||||
let current = sessions.find((session) => session.id === state?.activeSessionId) || null;
|
||||
if (!current) {
|
||||
current = sessions.find((session) => ["running", "paused"].includes(session.status))
|
||||
|| sessions.find((session) => session.status === "ready")
|
||||
|| null;
|
||||
}
|
||||
if (!current) {
|
||||
return { current: null, next: null, afterNext: null, upcoming: [] };
|
||||
}
|
||||
|
||||
const eventSessions = sessions.filter((session) => session.eventId === current.eventId);
|
||||
const currentIndex = eventSessions.findIndex((session) => session.id === current.id);
|
||||
const upcoming = eventSessions
|
||||
.slice(Math.max(0, currentIndex + 1))
|
||||
.filter((session) => !["finished", "stopped"].includes(session.status));
|
||||
return { current, next: upcoming[0] || null, afterNext: upcoming[1] || null, upcoming };
|
||||
}
|
||||
|
||||
export function buildRaceDayPreflight(state, session, { backendAvailable = false } = {}) {
|
||||
if (!session) {
|
||||
return { ready: false, checks: [check("session", "error")] };
|
||||
}
|
||||
|
||||
const event = getEvent(state, session);
|
||||
const checks = [
|
||||
check("session", "ok", { sessionId: session.id }),
|
||||
check("backend", backendAvailable ? "ok" : "warning"),
|
||||
check("decoder", state?.decoder?.connected ? "ok" : "error"),
|
||||
];
|
||||
|
||||
if (state?.settings?.splitDecoderEnabled) {
|
||||
checks.push(check("split_decoder", state?.decoder?.splitConnected ? "ok" : "warning"));
|
||||
}
|
||||
if (state?.settings?.pitDecoderEnabled) {
|
||||
const pitConnected = state?.decoder?.pitEntryConnected && state?.decoder?.pitExitConnected;
|
||||
checks.push(check("pit_decoders", pitConnected ? "ok" : "warning"));
|
||||
}
|
||||
|
||||
const minLapMs = Math.max(0, Number(event?.raceConfig?.minLapMs || 0) || 0);
|
||||
const maxLapMs = Math.max(0, Number(event?.raceConfig?.maxLapMs || 0) || 0);
|
||||
checks.push(check("lap_window", event?.mode !== "race" || (maxLapMs > minLapMs && maxLapMs > 0) ? "ok" : "error", { minLapMs, maxLapMs }));
|
||||
|
||||
let entrantIds = [];
|
||||
let transponders = [];
|
||||
if (session.type === "open_practice") {
|
||||
checks.push(check("participants", "ok", { openPractice: true, count: 0 }));
|
||||
checks.push(check("transponders", "ok", { openPractice: true, count: 0 }));
|
||||
} else if (session.mode === "track") {
|
||||
const assignments = Array.isArray(session.assignments) ? session.assignments : [];
|
||||
checks.push(check("participants", assignments.length ? "ok" : "error", { count: assignments.length }));
|
||||
const cars = assignments.map((assignment) => state.cars.find((car) => car.id === assignment.carId)).filter(Boolean);
|
||||
const missingCount = assignments.length - cars.filter((car) => String(car.transponder || "").trim()).length;
|
||||
transponders = cars.map((car) => String(car.transponder || "").trim()).filter(Boolean);
|
||||
checks.push(check("transponders", missingCount ? "error" : "ok", { count: transponders.length, missingCount }));
|
||||
} else if (session.type === "team_race") {
|
||||
const teams = Array.isArray(event?.raceConfig?.teams) ? event.raceConfig.teams : [];
|
||||
checks.push(check("participants", teams.length ? "ok" : "error", { count: teams.length, teamRace: true }));
|
||||
const teamTransponders = teams.map((team) => {
|
||||
const driverTransponders = (team.driverIds || [])
|
||||
.map((id) => state.drivers.find((driver) => driver.id === id)?.transponder)
|
||||
.map((value) => String(value || "").trim())
|
||||
.filter(Boolean);
|
||||
const carTransponders = (team.carIds || [])
|
||||
.map((id) => state.cars.find((car) => car.id === id)?.transponder)
|
||||
.map((value) => String(value || "").trim())
|
||||
.filter(Boolean);
|
||||
return unique([...driverTransponders, ...carTransponders]);
|
||||
});
|
||||
const missingCount = teamTransponders.filter((values) => !values.length).length;
|
||||
transponders = teamTransponders.flat();
|
||||
checks.push(check("transponders", missingCount ? "error" : "ok", { count: transponders.length, missingCount, teamRace: true }));
|
||||
} else {
|
||||
entrantIds = getRaceDriverIds(state, session, event);
|
||||
checks.push(check("participants", entrantIds.length ? "ok" : "error", { count: entrantIds.length }));
|
||||
const drivers = entrantIds.map((id) => state.drivers.find((driver) => driver.id === id)).filter(Boolean);
|
||||
const missingCount = entrantIds.length - drivers.filter((driver) => String(driver.transponder || "").trim()).length;
|
||||
transponders = drivers.map((driver) => String(driver.transponder || "").trim()).filter(Boolean);
|
||||
checks.push(check("transponders", missingCount ? "error" : "ok", { count: transponders.length, missingCount }));
|
||||
}
|
||||
|
||||
const duplicates = duplicateValues(transponders);
|
||||
checks.push(check("duplicate_transponders", duplicates.length ? "error" : "ok", { duplicates }));
|
||||
|
||||
if (String(session.startMode || "").toLowerCase() === "position" && session.type !== "team_race") {
|
||||
const gridIds = unique(session.manualGridIds || session.driverIds || []);
|
||||
const expectedIds = session.mode === "track"
|
||||
? unique((session.assignments || []).map((assignment) => assignment.driverId))
|
||||
: entrantIds;
|
||||
const gridComplete = expectedIds.length > 0
|
||||
&& gridIds.length === expectedIds.length
|
||||
&& expectedIds.every((id) => gridIds.includes(id));
|
||||
checks.push(check("grid", gridComplete ? "ok" : "error", { count: gridIds.length, expectedCount: expectedIds.length }));
|
||||
} else {
|
||||
checks.push(check("grid", "ok", { notRequired: true }));
|
||||
}
|
||||
|
||||
const raceAudioEnabled = state?.settings?.raceAudio?.enabled !== false;
|
||||
checks.push(check("audio", raceAudioEnabled ? "ok" : "warning"));
|
||||
|
||||
return { ready: !checks.some((item) => item.level === "error"), checks };
|
||||
}
|
||||
Reference in new issue
Block a user