feat(training): base training stats on practice, keep race laps separate
Training stats count Open/Free practice by default; laps from qualifying, heats and finals are kept and shown under Based on: Races or All, marked with race, session and series. Races are listed per race session and never split into stints. Same picker in the mobile profile. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9a9379aa83
commit
af79b610db
15 files changed
+377
-42
No files matched your search
+72
-22
@@ -2,11 +2,30 @@ import { sortValuesAttribute } from "./list_sorting.js";
|
||||
import { getDriverProfileGroups, getDriverProfileReferences } from "./driver_profile_logic.js";
|
||||
import { normalizeTrackLayouts } from "./track_layouts_logic.js";
|
||||
import { renderTrackLayoutBar, bindTrackLayoutBar } from "./track_layout_bar.js";
|
||||
import { buildTrainingRaceSessions } from "./training_logic.js";
|
||||
|
||||
// Which track layout the pace/record/consistency numbers are for: "current" (default),
|
||||
// "all" (overall), "none" (laps from before any layout existed), or a layout id. Module
|
||||
// state like the tab selections, so it survives re-renders while the page is open.
|
||||
let selectedTrainingLayout = "current";
|
||||
// Which laps the stats are built from: "practice" (default - training), "race" (qualifying,
|
||||
// heats, finals) or "all". Race laps are marked 🏁 with their race, session and series.
|
||||
let selectedTrainingSource = "practice";
|
||||
|
||||
function trainingSourceSelectMarkup(state, t, escapeHtml) {
|
||||
const option = (value) => `<option value="${value}" ${selectedTrainingSource === value ? "selected" : ""}>${escapeHtml(t(`training.source_${value}`))}</option>`;
|
||||
return `
|
||||
<label class="training-driver-select">
|
||||
<span>${escapeHtml(t("training.source"))}</span>
|
||||
<select id="training-source">${option("practice")}${option("race")}${option("all")}</select>
|
||||
</label>`;
|
||||
}
|
||||
|
||||
// "🏁 ÖRC Winter #3 2WD · Kval 2 · ÖRC Winter Championship" for a race stint or day.
|
||||
function raceOriginLabel({ eventId, eventName, sessionName }, state) {
|
||||
const series = (state.series || []).find((item) => eventId && item.eventIds?.includes(eventId));
|
||||
return `🏁 ${[eventName, sessionName, series?.name].filter(Boolean).join(" · ")}`;
|
||||
}
|
||||
|
||||
function trainingLayoutSelectMarkup(state, t, escapeHtml) {
|
||||
const layouts = normalizeTrackLayouts(state.trackLayouts);
|
||||
@@ -32,12 +51,37 @@ function bindTrainingLayoutControls({ state, t, addTrackLayout, renderView }) {
|
||||
selectedTrainingLayout = event.target.value;
|
||||
renderView();
|
||||
});
|
||||
document.getElementById("training-source")?.addEventListener("change", (event) => {
|
||||
selectedTrainingSource = event.target.value;
|
||||
renderView();
|
||||
});
|
||||
bindTrackLayoutBar(document.getElementById("view"), { state, t, addTrackLayout, renderView });
|
||||
}
|
||||
|
||||
const EXERCISE_KIND_EMOJI = { timer: "⏱️", evenLaps: "🎯", stepTraining: "🪜" };
|
||||
|
||||
function renderStintRow(row, { t, escapeHtml, formatLap, formatRaceClock }) {
|
||||
function renderRaceSessionsSection(rows, { t, escapeHtml, formatLap, state }) {
|
||||
return `
|
||||
<div class="training-section-title">
|
||||
<div><h3>${escapeHtml(t("training.race_sessions"))}</h3><p class="hint">${escapeHtml(t("training.race_sessions_hint"))}</p></div>
|
||||
</div>
|
||||
<div class="table-wrap training-history-wrap">
|
||||
<table data-sort-id="training-race-sessions" class="training-history-table">
|
||||
<thead><tr><th>${escapeHtml(t("training.date"))}</th><th>${escapeHtml(t("training.race_session"))}</th><th>${escapeHtml(t("training.laps"))}</th><th>${escapeHtml(t("training.best_lap"))}</th><th>${escapeHtml(t("training.average_lap"))}</th><th>${escapeHtml(t("training.consistency"))}</th></tr></thead>
|
||||
<tbody>${rows.length ? rows.map((row) => `
|
||||
<tr data-sort-values="${sortValuesAttribute([row.startedAt, row.eventName, row.lapCount, row.bestLapMs || null, row.averageLapMs || null, row.consistencyPct])}">
|
||||
<td>${escapeHtml(new Date(row.startedAt).toLocaleString(undefined, { dateStyle: "short", timeStyle: "short" }))}</td>
|
||||
<td class="training-race-origin">${escapeHtml(raceOriginLabel(row, state))}</td>
|
||||
<td>${row.lapCount}</td>
|
||||
<td>${formatLap(row.bestLapMs)}</td>
|
||||
<td>${formatLap(row.averageLapMs)}</td>
|
||||
<td>${row.consistencyPct == null ? "-" : `${row.consistencyPct.toFixed(1)}%`}</td>
|
||||
</tr>`).join("") : `<tr><td colspan="6">${escapeHtml(t("training.no_race_sessions"))}</td></tr>`}</tbody>
|
||||
</table>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function renderStintRow(row, { t, escapeHtml, formatLap, formatRaceClock, state }) {
|
||||
const paceRetention = row.endChangeMs == null
|
||||
? "-"
|
||||
: row.endChangeMs > 0
|
||||
@@ -144,7 +188,7 @@ export function renderTrainingView(deps) {
|
||||
const selectedDriver = selectedProfile.representative;
|
||||
const selectedClassDriver = selectedProfile.drivers.find((driver) => String(driver.classId || "") === selectedClassId) || selectedDriver;
|
||||
const profileReferences = getDriverProfileReferences(state, selectedProfileId);
|
||||
const stats = buildDriverTrainingStats(state, selectedProfileId, selectedClassId, { layout: selectedTrainingLayout });
|
||||
const stats = buildDriverTrainingStats(state, selectedProfileId, selectedClassId, { layout: selectedTrainingLayout, source: selectedTrainingSource });
|
||||
const channelStats = buildDriverTimingChannelStats(state, selectedProfileId, selectedClassId);
|
||||
const activePractice = buildDriverActivePracticeStats(state, selectedProfileId, Date.now(), selectedClassId);
|
||||
const evenLapsConfig = (state.training.evenLaps || []).find((entry) => entry.driverId === selectedClassDriver.id && entry.classId === selectedClassId) || null;
|
||||
@@ -169,7 +213,7 @@ export function renderTrainingView(deps) {
|
||||
const highlights = buildTrainingHighlights({ ...stats, latestSplitGainMs: channelStats.latestSplitGainMs });
|
||||
const paceBuckets = buildTrainingPaceBuckets(state, selectedProfileId, selectedClassId, 30).slice(-24);
|
||||
const weekdayStats = buildTrainingWeekdayStats(state, selectedProfileId, selectedClassId);
|
||||
const stintAnalysis = buildTrainingStintAnalysis(state, selectedProfileId, selectedClassId, { layout: selectedTrainingLayout });
|
||||
const stintAnalysis = buildTrainingStintAnalysis(state, selectedProfileId, selectedClassId, { layout: selectedTrainingLayout, source: selectedTrainingSource });
|
||||
const markerComparisons = buildTrainingMarkerComparisons(state, selectedProfileId, selectedClassId, 30).slice().reverse();
|
||||
const goals = state.training.goals.filter((goal) => (
|
||||
profileReferences.has(String(goal.driverId || ""))
|
||||
@@ -267,7 +311,7 @@ export function renderTrainingView(deps) {
|
||||
const dailyRows = stats.dailySummaries.length
|
||||
? stats.dailySummaries.slice(0, 10).map((day) => `
|
||||
<tr>
|
||||
<td data-sort-value="${day.latestTimestamp}">${escapeHtml(new Date(day.latestTimestamp).toLocaleDateString(undefined, { dateStyle: "medium" }))}</td>
|
||||
<td data-sort-value="${day.latestTimestamp}">${escapeHtml(new Date(day.latestTimestamp).toLocaleDateString(undefined, { dateStyle: "medium" }))}${day.raceLaps ? day.raceSessions.map((label) => `<br><small class="training-race-origin">🏁 ${escapeHtml(label)}</small>`).join("") : ""}</td>
|
||||
<td>${day.laps}</td>
|
||||
<td>${formatLap(day.bestLapMs)}</td>
|
||||
<td>${formatLap(day.averageLapMs)}</td>
|
||||
@@ -317,7 +361,7 @@ export function renderTrainingView(deps) {
|
||||
: t("training.trend_stable");
|
||||
const clubRows = profiles
|
||||
.filter((profile) => !selectedClassId || profile.classIds.includes(selectedClassId))
|
||||
.map((profile) => ({ profile, driver: profile.representative, stats: profile.id === selectedProfileId ? stats : buildDriverTrainingStats(state, profile.id, selectedClassId, { layout: selectedTrainingLayout }) }))
|
||||
.map((profile) => ({ profile, driver: profile.representative, stats: profile.id === selectedProfileId ? stats : buildDriverTrainingStats(state, profile.id, selectedClassId, { layout: selectedTrainingLayout, source: selectedTrainingSource }) }))
|
||||
.sort((left, right) => right.stats.lastLapAt - left.stats.lastLapAt || String(left.driver?.name || "").localeCompare(String(right.driver?.name || "")))
|
||||
.map((entry) => {
|
||||
const milestone = getNextTrainingMilestone(entry.stats);
|
||||
@@ -366,9 +410,26 @@ export function renderTrainingView(deps) {
|
||||
.filter((row, index, rows) => rows.findIndex((item) => item.id === row.id) === index);
|
||||
const stintColumns = `<tr><th>${escapeHtml(t("training.stint"))}</th><th>${escapeHtml(t("training.start"))}</th><th>${escapeHtml(t("training.laps"))}</th><th>${escapeHtml(t("training.best_lap"))}</th><th>${escapeHtml(t("training.trimmed_pace"))}</th><th>${escapeHtml(t("training.race_pace_10"))}</th><th>${escapeHtml(t("training.mistakes"))}</th><th>${escapeHtml(t("training.start_end"))}</th><th>${escapeHtml(t("training.consistency"))}</th><th>${escapeHtml(t("training.stint_five_minute"))}</th><th>${escapeHtml(t("training.stint_duration"))}</th></tr>`;
|
||||
const featuredStintRows = featuredStints.length
|
||||
? featuredStints.map((row) => renderStintRow(row, { t, escapeHtml, formatLap, formatRaceClock })).join("")
|
||||
? featuredStints.map((row) => renderStintRow(row, { t, escapeHtml, formatLap, formatRaceClock, state })).join("")
|
||||
: `<tr><td colspan="11">${escapeHtml(t("training.no_stints"))}</td></tr>`;
|
||||
const allStintRows = stintAnalysis.rows.map((row) => renderStintRow(row, { t, escapeHtml, formatLap, formatRaceClock })).join("");
|
||||
const allStintRows = stintAnalysis.rows.map((row) => renderStintRow(row, { t, escapeHtml, formatLap, formatRaceClock, state })).join("");
|
||||
// Stints are for practice only; with "Races" chosen the race-session table replaces them.
|
||||
const stintSectionMarkup = `
|
||||
<div class="training-section-title">
|
||||
<div><h3>${escapeHtml(t("training.stint_analysis"))}</h3><p class="hint">${escapeHtml(t("training.stint_analysis_hint"))}</p></div>
|
||||
${stintAnalysis.best ? `<span class="badge success">${escapeHtml(t("training.best_stint", { stint: stintAnalysis.best.number, laps: stintAnalysis.best.lapCount, pace: formatLap(stintAnalysis.best.paceMs) }))}</span>` : ""}
|
||||
</div>
|
||||
<div class="table-wrap training-history-wrap">
|
||||
<table data-sort-id="training-stints-featured" class="training-history-table training-stint-table"><thead>${stintColumns}</thead><tbody>${featuredStintRows}</tbody></table>
|
||||
</div>
|
||||
${stintAnalysis.rows.length > featuredStints.length ? `
|
||||
<details class="training-stint-all">
|
||||
<summary>${escapeHtml(t("training.show_all_stints", { count: stintAnalysis.rows.length }))}</summary>
|
||||
<div class="table-wrap training-history-wrap">
|
||||
<table data-sort-id="training-stints-all" class="training-history-table training-stint-table"><thead>${stintColumns}</thead><tbody>${allStintRows}</tbody></table>
|
||||
</div>
|
||||
</details>` : ""}
|
||||
`;
|
||||
const markerMarkup = markerComparisons.length
|
||||
? markerComparisons.map((marker) => {
|
||||
const comparison = marker.improvementMs == null
|
||||
@@ -404,6 +465,7 @@ export function renderTrainingView(deps) {
|
||||
<select id="training-class">${classOptions}</select>
|
||||
</label>
|
||||
${trainingLayoutSelectMarkup(state, t, escapeHtml)}
|
||||
${trainingSourceSelectMarkup(state, t, escapeHtml)}
|
||||
${renderTrackLayoutBar({ state, t, escapeHtml })}
|
||||
<div class="actions training-hero-actions">
|
||||
<button class="btn" id="trainingBackOverview" type="button">${escapeHtml(t("training.back_overview"))}</button>
|
||||
@@ -729,20 +791,7 @@ export function renderTrainingView(deps) {
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div class="training-section-title">
|
||||
<div><h3>${escapeHtml(t("training.stint_analysis"))}</h3><p class="hint">${escapeHtml(t("training.stint_analysis_hint"))}</p></div>
|
||||
${stintAnalysis.best ? `<span class="badge success">${escapeHtml(t("training.best_stint", { stint: stintAnalysis.best.number, laps: stintAnalysis.best.lapCount, pace: formatLap(stintAnalysis.best.paceMs) }))}</span>` : ""}
|
||||
</div>
|
||||
<div class="table-wrap training-history-wrap">
|
||||
<table data-sort-id="training-stints-featured" class="training-history-table training-stint-table"><thead>${stintColumns}</thead><tbody>${featuredStintRows}</tbody></table>
|
||||
</div>
|
||||
${stintAnalysis.rows.length > featuredStints.length ? `
|
||||
<details class="training-stint-all">
|
||||
<summary>${escapeHtml(t("training.show_all_stints", { count: stintAnalysis.rows.length }))}</summary>
|
||||
<div class="table-wrap training-history-wrap">
|
||||
<table data-sort-id="training-stints-all" class="training-history-table training-stint-table"><thead>${stintColumns}</thead><tbody>${allStintRows}</tbody></table>
|
||||
</div>
|
||||
</details>` : ""}
|
||||
${selectedTrainingSource === "race" ? renderRaceSessionsSection(buildTrainingRaceSessions(state, selectedProfileId, selectedClassId, { layout: selectedTrainingLayout }), { t, escapeHtml, formatLap, state }) : stintSectionMarkup}
|
||||
<p class="hint training-method-note">${escapeHtml(t("training.stats_method"))}</p>
|
||||
<section class="training-insights">
|
||||
${trainingInsight(t("training.average_lap"), formatLap(stats.averageLapMs))}
|
||||
@@ -1060,7 +1109,7 @@ export function buildTrainingOverviewRows(state, profiles, buildDriverTrainingSt
|
||||
driver: profile.representative,
|
||||
classId,
|
||||
className: state.classes.find((item) => String(item.id || "") === String(classId || ""))?.name || classId || "-",
|
||||
stats: buildDriverTrainingStats(state, profile.id, classId, { layout: selectedTrainingLayout }),
|
||||
stats: buildDriverTrainingStats(state, profile.id, classId, { layout: selectedTrainingLayout, source: selectedTrainingSource }),
|
||||
}));
|
||||
}).sort((left, right) => (
|
||||
right.stats.lastLapAt - left.stats.lastLapAt
|
||||
@@ -1169,6 +1218,7 @@ function renderTrainingOverview({ addTrackLayout, state, dom, t, escapeHtml, for
|
||||
</select>
|
||||
</label>
|
||||
${trainingLayoutSelectMarkup(state, t, escapeHtml)}
|
||||
${trainingSourceSelectMarkup(state, t, escapeHtml)}
|
||||
${renderTrackLayoutBar({ state, t, escapeHtml })}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
Reference in new issue
Block a user