feat(houseofrc): preview and send results to the event's entries, heats or final placings (1.83.0)
This commit is contained in:
1 parent
f4a62b92d2
commit
ebdc6104e6
17 files changed
+295
-15
No files matched your search
+39
@@ -2213,6 +2213,25 @@ const TRANSLATIONS = {
|
||||
"houseofrc.entries_class": "Klass hos oss",
|
||||
"houseofrc.entries_hor_class": "Klass på House of RC",
|
||||
"houseofrc.refresh_button": "Hämta ändringar från House of RC",
|
||||
"houseofrc.results_button": "Skicka resultat till House of RC",
|
||||
"houseofrc.results_hint": "Förhandsgranska och skicka resultatet till {name}",
|
||||
"houseofrc.results_title": "Skicka resultat till House of RC",
|
||||
"houseofrc.results_mode_heats": "Heat och kval (efter varje heat)",
|
||||
"houseofrc.results_mode_final": "Slutresultat (med finalplacering)",
|
||||
"houseofrc.results_heats_hint": "Skickar varje heat och kvalplaceringen. Ingen slutplacering, så ingen förare får något meddelande ännu – kan skickas efter varje heat.",
|
||||
"houseofrc.results_final_hint": "Skickar också slutplaceringen och finalen. Förarna får meddelande från House of RC första gången de får en placering.",
|
||||
"houseofrc.results_col_driver": "Förare",
|
||||
"houseofrc.results_col_qual": "Kval",
|
||||
"houseofrc.results_col_final": "Placering",
|
||||
"houseofrc.results_col_heats": "Heat",
|
||||
"houseofrc.results_col_best": "Bästa varv",
|
||||
"houseofrc.results_missing": "Skickas inte (ingen anmälan på House of RC): {names}",
|
||||
"houseofrc.results_send": "Skicka ({count})",
|
||||
"houseofrc.results_sending": "Skickar …",
|
||||
"houseofrc.results_sent": "{count} anmälningar uppdaterade på House of RC.",
|
||||
"houseofrc.results_last": "senast skickat {time}",
|
||||
"houseofrc.results_close": "Stäng",
|
||||
"houseofrc.err_rejected": "House of RC tog inte emot resultatet, inget skrevs: {message}",
|
||||
"houseofrc.refresh_hint": "Nya anmälningar och avanmälningar sedan racet skapades ({name})",
|
||||
"houseofrc.refresh_title": "Ändrade anmälningar",
|
||||
"houseofrc.refresh_nothing": "Inga ändringar – deltagarna stämmer med House of RC.",
|
||||
@@ -4433,6 +4452,25 @@ const TRANSLATIONS = {
|
||||
"houseofrc.entries_class": "Our class",
|
||||
"houseofrc.entries_hor_class": "Class on House of RC",
|
||||
"houseofrc.refresh_button": "Fetch changes from House of RC",
|
||||
"houseofrc.results_button": "Send results to House of RC",
|
||||
"houseofrc.results_hint": "Preview and send the results to {name}",
|
||||
"houseofrc.results_title": "Send results to House of RC",
|
||||
"houseofrc.results_mode_heats": "Heats and qualifying (after every heat)",
|
||||
"houseofrc.results_mode_final": "Final results (with final placings)",
|
||||
"houseofrc.results_heats_hint": "Sends every heat and the qualifying placing. No final placing, so no driver is notified yet – can be sent after every heat.",
|
||||
"houseofrc.results_final_hint": "Also sends the final placings and mains. Drivers are notified by House of RC the first time they get a placing.",
|
||||
"houseofrc.results_col_driver": "Driver",
|
||||
"houseofrc.results_col_qual": "Qual.",
|
||||
"houseofrc.results_col_final": "Placing",
|
||||
"houseofrc.results_col_heats": "Heats",
|
||||
"houseofrc.results_col_best": "Best lap",
|
||||
"houseofrc.results_missing": "Not sent (no House of RC entry): {names}",
|
||||
"houseofrc.results_send": "Send ({count})",
|
||||
"houseofrc.results_sending": "Sending …",
|
||||
"houseofrc.results_sent": "{count} entries updated on House of RC.",
|
||||
"houseofrc.results_last": "last sent {time}",
|
||||
"houseofrc.results_close": "Close",
|
||||
"houseofrc.err_rejected": "House of RC did not accept the results, nothing was written: {message}",
|
||||
"houseofrc.refresh_hint": "New entries and withdrawals since the race was created ({name})",
|
||||
"houseofrc.refresh_title": "Changed entries",
|
||||
"houseofrc.refresh_nothing": "No changes – the participants match House of RC.",
|
||||
@@ -7482,6 +7520,7 @@ function renderEventManager(eventId) {
|
||||
escapeHtml,
|
||||
normalizeEvent,
|
||||
normalizeDriver,
|
||||
getRaceOverview,
|
||||
ensureRaceParticipantsConfigured,
|
||||
getSessionsForEvent,
|
||||
buildEventSchedule,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { openHouseOfRcEntryRefresh } from "./houseofrc_view.js";
|
||||
import { openHouseOfRcEntryRefresh, openHouseOfRcResultsPost } from "./houseofrc_view.js";
|
||||
import { mountSponsorSetup } from "./sponsor_event_view.js";
|
||||
import { mountTeamRaceSetup } from "./team_race_setup_view.js";
|
||||
import { orderSessionsForRunning, moveSessionInRunningOrder, applyFormatToPendingSessions, syncParticipantsToSessions } from "./event_race_logic.js";
|
||||
@@ -24,6 +24,7 @@ export function renderEventManagerView(context) {
|
||||
escapeHtml,
|
||||
normalizeEvent,
|
||||
normalizeDriver,
|
||||
getRaceOverview,
|
||||
ensureRaceParticipantsConfigured,
|
||||
getSessionsForEvent,
|
||||
getSelectedTeamEditId,
|
||||
@@ -613,6 +614,11 @@ export function renderEventManagerView(context) {
|
||||
},
|
||||
}));
|
||||
|
||||
// Race made from House of RC: preview and send the results to its entries.
|
||||
document.getElementById("houseOfRcSendResults")?.addEventListener("click", () => openHouseOfRcResultsPost({
|
||||
event, overview: getRaceOverview(event), state, t, escapeHtml, saveState,
|
||||
}));
|
||||
|
||||
document.getElementById("selectAllParticipants")?.addEventListener("click", () => {
|
||||
document.querySelectorAll(".race-participant").forEach((node) => {
|
||||
node.checked = true;
|
||||
|
||||
@@ -189,6 +189,7 @@ export function renderEventManagerMarkup(context) {
|
||||
<button id="selectAllParticipants" class="btn" type="button">${t("events.select_all_participants")}</button>
|
||||
<button id="clearParticipants" class="btn btn-danger" type="button">${t("events.clear_participants")}</button>
|
||||
${event.houseOfRc?.eventId ? `<button id="houseOfRcRefreshEntries" class="btn" type="button" title="${escapeHtml(t("houseofrc.refresh_hint", { name: event.houseOfRc.name || "" }))}">🔄 ${t("houseofrc.refresh_button")}</button>` : ""}
|
||||
${event.houseOfRc?.eventId ? `<button id="houseOfRcSendResults" class="btn" type="button" title="${escapeHtml(t("houseofrc.results_hint", { name: event.houseOfRc.name || "" }))}">📤 ${t("houseofrc.results_button")}</button>` : ""}
|
||||
</div>
|
||||
${(() => {
|
||||
const card = (driver) => {
|
||||
|
||||
@@ -168,3 +168,65 @@ export function applyHouseOfRcEntryChanges(event, drivers, diff, { addIndexes, r
|
||||
const before = event.raceConfig.driverIds || [];
|
||||
return [...before.filter((id) => !removeIds.has(id)), ...addedIds.filter((id) => !before.includes(id))];
|
||||
}
|
||||
|
||||
// A lap or race time as House of RC writes it: "13.201", "1:02.345".
|
||||
export function formatHouseOfRcTime(ms) {
|
||||
const total = Math.round(Number(ms));
|
||||
if (!Number.isFinite(total) || total <= 0 || total >= Number.MAX_SAFE_INTEGER / 2) return "";
|
||||
const minutes = Math.floor(total / 60000);
|
||||
const seconds = ((total % 60000) / 1000).toFixed(3);
|
||||
return minutes ? `${minutes}:${seconds.padStart(6, "0")}` : seconds;
|
||||
}
|
||||
|
||||
// Our results for House of RC's entries (POST /v1/timing/events/{id}/entries), from the race
|
||||
// overview (buildRaceOverview). Only participants linked to an entry are sent.
|
||||
// final: false - heats and qualifying only (semi-live; nobody is told yet)
|
||||
// final: true - also the final placings (`ranking`, which notifies the driver the first time)
|
||||
// Returns { entries, rows (for the preview), missing (participants without an entry) }.
|
||||
export function buildHouseOfRcResults(event, overview, { final = false, drivers = [] } = {}) {
|
||||
const entryIds = event?.houseOfRc?.entryIds || {};
|
||||
const participants = event?.raceConfig?.driverIds || [];
|
||||
const phase = (type) => overview?.phases?.find((item) => item.type === type);
|
||||
const qualifying = phase("qualification");
|
||||
const finals = phase("final");
|
||||
const qualRank = new Map((qualifying?.standings || []).map((row) => [row.driverId, Number(row.rank) || null]));
|
||||
// A driver bumped up from a lower main is in both: placed by the higher one (listed first).
|
||||
const finalRows = (finals?.standings || []).filter((row, index, all) => all.findIndex((other) => other.driverId === row.driverId) === index);
|
||||
const heatsOf = (driverId) => ["qualification", "final"].flatMap((type) => (phase(type)?.heats || []).flatMap(({ session, rows }) => {
|
||||
const row = rows.find((item) => item.driverId === driverId);
|
||||
if (!row) return [];
|
||||
const time = formatHouseOfRcTime(row.timeMs);
|
||||
return [{ heat: session.name || "", laps: row.laps, time: row.raceStatus ? row.raceStatus.toUpperCase() : time, best_lap: formatHouseOfRcTime(row.bestLapMs), position: row.pos, _type: type, _laps_time: time ? `${row.laps}/${time}` : "" }];
|
||||
}));
|
||||
const nameOf = (driverId) => drivers.find((driver) => driver.id === driverId)?.name || driverId;
|
||||
const missing = participants.filter((driverId) => entryIds[driverId] == null).map(nameOf);
|
||||
const rows = [];
|
||||
const entries = participants.filter((driverId) => entryIds[driverId] != null).map((driverId) => {
|
||||
const heats = heatsOf(driverId);
|
||||
const bestLap = heats.map((heat) => heat.best_lap).filter(Boolean).sort((a, b) => toMs(a) - toMs(b))[0] || "";
|
||||
const qualification = qualRank.get(driverId) || null;
|
||||
const result = { best_lap: bestLap, heats: heats.map(({ _type, _laps_time, ...heat }) => heat) };
|
||||
// No qualifying run yet: leave House of RC's qualification as it is.
|
||||
const entry = { id: entryIds[driverId], ...(qualRank.size ? { qualification, tq: qualification === 1 } : {}) };
|
||||
const finalIndex = finalRows.findIndex((row) => row.driverId === driverId);
|
||||
if (final && finalIndex >= 0) {
|
||||
const row = finalRows[finalIndex];
|
||||
const lastFinal = heats.filter((heat) => heat._type === "final").pop();
|
||||
entry.ranking = finalIndex + 1;
|
||||
entry.laps_time = lastFinal?._laps_time || "";
|
||||
result.main = row.mainKey || String(row.rank).replace(/\d+$/, "");
|
||||
const lower = (finals?.standings || []).filter((other) => other.driverId === driverId && other !== row).map((other) => other.mainKey || String(other.rank).replace(/\d+$/, ""));
|
||||
if (lower.length) result.bumped_from = lower;
|
||||
result.main_position = Number(String(row.rank).replace(/^\D+/, "")) || null;
|
||||
}
|
||||
entry.data = { result };
|
||||
rows.push({ driverId, name: nameOf(driverId), entryId: entryIds[driverId], qualification, ranking: entry.ranking ?? null, main: result.main || "", heats: heats.length, bestLap, lapsTime: entry.laps_time || "" });
|
||||
return entry;
|
||||
});
|
||||
return { entries, rows: rows.sort((a, b) => (a.ranking ?? 1e9) - (b.ranking ?? 1e9) || (a.qualification ?? 1e9) - (b.qualification ?? 1e9)), missing };
|
||||
}
|
||||
|
||||
const toMs = (text) => {
|
||||
const [minutes, seconds] = String(text).includes(":") ? String(text).split(":") : ["0", text];
|
||||
return Number(minutes) * 60000 + Number(seconds) * 1000;
|
||||
};
|
||||
+60
-1
@@ -1,4 +1,4 @@
|
||||
import { planHouseOfRcDriverUpdates, applyHouseOfRcDriverUpdates, planHouseOfRcEntries, applyHouseOfRcEntries, matchHouseOfRcClass, rememberHouseOfRcClass, diffHouseOfRcEntries, applyHouseOfRcEntryChanges } from "./houseofrc_logic.js";
|
||||
import { planHouseOfRcDriverUpdates, applyHouseOfRcDriverUpdates, planHouseOfRcEntries, applyHouseOfRcEntries, matchHouseOfRcClass, rememberHouseOfRcClass, diffHouseOfRcEntries, applyHouseOfRcEntryChanges, buildHouseOfRcResults } from "./houseofrc_logic.js";
|
||||
|
||||
// Settings → System → House of RC: save the club's API key on the server and test it.
|
||||
// The key is never read back; the server only tells whether one is set (and its last 4 chars).
|
||||
@@ -20,7 +20,11 @@ export function describeHouseOfRcError(t, code, body = {}) {
|
||||
timeout: "houseofrc.err_unreachable",
|
||||
unreachable: "houseofrc.err_unreachable",
|
||||
invalid_key: "houseofrc.err_invalid_key",
|
||||
not_your_event: "houseofrc.err_not_your_event",
|
||||
event_not_found: "houseofrc.err_event_not_found",
|
||||
invalid_event: "houseofrc.err_event_not_found",
|
||||
}[body.error];
|
||||
if (body.error === "rejected") return t("houseofrc.err_rejected", { message: [body.message, ...Object.values(body.errors || {}).flat()].filter(Boolean).join(" ") || "422" });
|
||||
return key ? t(key, { seconds: body.retryAfter || 60 }) : t("houseofrc.err_other", { status: body.status || code });
|
||||
}
|
||||
|
||||
@@ -346,3 +350,58 @@ export async function openHouseOfRcEntryRefresh({ event, state, t, escapeHtml, m
|
||||
onApply(selectedIds);
|
||||
});
|
||||
}
|
||||
|
||||
// Manage → "Send results to House of RC": a preview of what goes to each entry, then one
|
||||
// all-or-nothing write. "Heats" leaves out the final placings, so nobody is notified yet
|
||||
// and it can be sent after every heat; "Final results" adds them.
|
||||
export function openHouseOfRcResultsPost({ event, overview, state, t, escapeHtml, saveState }) {
|
||||
const { body, close } = openHorModal({ id: "houseOfRcResultsOverlay", title: t("houseofrc.results_title"), escapeHtml });
|
||||
const hasFinals = Boolean(overview?.phases?.find((phase) => phase.type === "final")?.standings?.length);
|
||||
let final = hasFinals && Boolean(overview?.finished);
|
||||
const render = () => {
|
||||
const { entries, rows, missing } = buildHouseOfRcResults(event, overview, { final, drivers: state.drivers });
|
||||
const where = [event.houseOfRc.name, event.houseOfRc.className].filter(Boolean).join(" – ");
|
||||
body.innerHTML = `
|
||||
<p><strong>${escapeHtml(where)}</strong>${event.houseOfRc.resultsSentAt ? ` <span class="hint">· ${escapeHtml(t("houseofrc.results_last", { time: new Date(event.houseOfRc.resultsSentAt).toLocaleString() }))}</span>` : ""}</p>
|
||||
<div class="hor-choice">
|
||||
<label><input type="radio" name="horResultsMode" value="heats" ${final ? "" : "checked"} /> ${escapeHtml(t("houseofrc.results_mode_heats"))}</label>
|
||||
<label><input type="radio" name="horResultsMode" value="final" ${final ? "checked" : ""} ${hasFinals ? "" : "disabled"} /> ${escapeHtml(t("houseofrc.results_mode_final"))}</label>
|
||||
</div>
|
||||
<p class="hint">${escapeHtml(t(final ? "houseofrc.results_final_hint" : "houseofrc.results_heats_hint"))}</p>
|
||||
<div class="table-wrap"><table class="hor-import-table">
|
||||
<thead><tr><th>${escapeHtml(t("houseofrc.results_col_driver"))}</th><th>${escapeHtml(t("houseofrc.results_col_qual"))}</th><th>${escapeHtml(t("houseofrc.results_col_final"))}</th><th>${escapeHtml(t("houseofrc.results_col_heats"))}</th><th>${escapeHtml(t("houseofrc.results_col_best"))}</th></tr></thead>
|
||||
<tbody>${rows.map((row) => `<tr data-hor-result-row>
|
||||
<td>${escapeHtml(row.name)}</td>
|
||||
<td>${row.qualification === 1 ? "TQ" : escapeHtml(String(row.qualification ?? "–"))}</td>
|
||||
<td>${row.ranking ? escapeHtml(`${row.ranking} (${row.main})`) : "–"}${row.lapsTime ? ` <span class="hint">${escapeHtml(row.lapsTime)}</span>` : ""}</td>
|
||||
<td>${row.heats}</td><td>${escapeHtml(row.bestLap || "–")}</td>
|
||||
</tr>`).join("")}</tbody></table></div>
|
||||
${missing.length ? `<p class="hint">⚠️ ${escapeHtml(t("houseofrc.results_missing", { names: missing.join(", ") }))}</p>` : ""}
|
||||
<p data-hor-result-status></p>
|
||||
<div class="actions mt-16">
|
||||
<button class="btn btn-primary" type="button" data-hor-send ${entries.length ? "" : "disabled"}>📤 ${escapeHtml(t("houseofrc.results_send", { count: entries.length }))}</button>
|
||||
<button class="btn" type="button" data-hor-close>${escapeHtml(t("houseofrc.results_close"))}</button>
|
||||
</div>`;
|
||||
body.querySelectorAll("[name=horResultsMode]").forEach((radio) => radio.addEventListener("change", () => { final = radio.value === "final"; render(); }));
|
||||
body.querySelector("[data-hor-send]").addEventListener("click", async (click) => {
|
||||
const button = click.currentTarget;
|
||||
const statusLine = body.querySelector("[data-hor-result-status]");
|
||||
button.disabled = true;
|
||||
statusLine.textContent = t("houseofrc.results_sending");
|
||||
const { status: code, body: data } = await postJson("/api/houseofrc/results", { eventId: event.houseOfRc.eventId, entries });
|
||||
if (code !== 200 || !data.ok) {
|
||||
statusLine.className = "error";
|
||||
statusLine.textContent = describeHouseOfRcError(t, code, data);
|
||||
button.disabled = false;
|
||||
return;
|
||||
}
|
||||
event.houseOfRc.resultsSentAt = new Date().toISOString();
|
||||
event.houseOfRc.resultsSentFinal = final;
|
||||
saveState();
|
||||
statusLine.className = "";
|
||||
statusLine.textContent = `✅ ${t("houseofrc.results_sent", { count: data.updated })}`;
|
||||
});
|
||||
};
|
||||
render();
|
||||
return { close };
|
||||
}
|
||||
@@ -44,6 +44,8 @@ export function buildRaceOverview(event, deps) {
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user