feat(houseofrc): create a race from a House of RC event and its entries (1.80.0)

This commit is contained in:
larssand committed 2026-11-08 11:52:57 +01:00
1 parent 42df6a786f
commit 9c1f5713e6
18 files changed
+444 -21

No files matched your search

+47 -1
View File
@@ -88,6 +88,7 @@ function createHouseOfRc({ dataDir, env = process.env, fetchImpl = globalThis.fe
id: user.id ?? null,
name: String(user.name || "").trim(),
model: String(user.model?.name || "").trim(),
// Often only the model name carries the make ("Xray XB4 2025"), with no brand object.
brand: String(user.model?.brand?.name || "").trim(),
};
});
@@ -95,7 +96,52 @@ function createHouseOfRc({ dataDir, env = process.env, fetchImpl = globalThis.fe
return { ok: true, status: 200, asked: numbers.length, members };
}
return { status, setKey, get, testConnection, lookupTransponders };
const pickEvent = (event) => ({
id: event.id, name: String(event.name || ""), type: event.type || "", status: event.status || null,
start: event.start || null, end: event.end || null, city: event.city || "", country: event.country || "",
organizerId: event.organizer_id ?? null, seriesId: event.series_id ?? null, url: event.web_url || "",
});
// Upcoming events in a date range (the API has no country or text filter, so filter here).
// At most 5 pages of 100 to stay well inside the rate limit.
async function listEvents({ from, to, country = "" } = {}) {
const events = [];
for (let page = 1; page <= 5; page += 1) {
const result = await get("/v1/events", { start_from: from, start_to: to, sort: "start", per_page: 100, page });
if (!result.ok) return result;
const rows = Array.isArray(result.data?.data) ? result.data.data : [];
events.push(...rows.map(pickEvent));
if (!result.data?.links?.next || rows.length < 100) break;
}
const wanted = String(country || "").toUpperCase();
return { ok: true, status: 200, events: events.filter((event) => !wanted || String(event.country).toUpperCase() === wanted) };
}
// One event and its entered drivers. /results on an event that hasn't been run yet lists the
// entries (0 points); cancelled and waiting-list entries are already left out.
async function getEventEntries(eventId, eventclassId) {
// An id, or a pasted link like https://houseofrc.com/events/2253.
const text = String(eventId || "").trim();
const id = Number(text.match(/events\/(\d+)/)?.[1] || text.match(/^\d+$/)?.[0]);
if (!Number.isInteger(id) || id < 1) return { ok: false, status: 400, error: "invalid_event" };
const event = await get(`/v1/events/${id}`);
if (!event.ok) return event.status === 404 ? { ...event, error: "event_not_found" } : event;
const results = await get("/results", { event_id: id, eventclass_id: eventclassId || undefined });
if (!results.ok) return results;
const entries = (Array.isArray(results.data) ? results.data : []).map((row) => ({
houseOfRcId: row.user_id ?? null,
name: String(row.name || [row.firstname, row.lastname].filter(Boolean).join(" ")).trim(),
number: row.number ?? null,
club: String(row.club || "").trim(),
country: String(row.country || "").trim(),
car: [row.brand1 && !String(row.product || "").toLowerCase().startsWith(String(row.brand1).toLowerCase()) ? row.brand1 : "", row.product].filter(Boolean).join(" ").trim(),
brand: String(row.brand1 || "").trim(),
points: row.points ?? null,
})).filter((entry) => entry.name);
return { ok: true, status: 200, event: pickEvent(event.data?.data || {}), entries };
}
return { status, setKey, get, testConnection, lookupTransponders, listEvents, getEventEntries };
}
module.exports = { createHouseOfRc, DEFAULT_BASE_URL };