lägg till stegträning-övning + v1.3.0

Andra guidade träningsövningen i Open Practice: konfigurera en
stegserie (antal varv + lugnt/kontrollerat/snabbt per steg),
förvalt till klassiska pyramiden. Training-sidan visar aktuellt
steg, kvarvarande varv och en progress-pill per steg. När
sekvensen är klar jämförs de två kontrollerade stegens snitt
neutralt (t.ex. "+0.90s"), utan att kalla en försämring för
trötthet.

syncTrainingLedger sprider nu resten av det normaliserade
training-objektet istället för att lista fält manuellt, så
samma klass av bugg som drabbade Jämna varv inte kan upprepas
för framtida fält.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
larssandandClaude Sonnet 5 committed 2026-09-09 09:02:44 +02:00
1 parent 9122e91f2e
commit 01e1165748
9 files changed
+390 -16

No files matched your search

+101 -6
View File
@@ -131,7 +131,32 @@ export function normalizeTrainingState(training, { uid }) {
})
: [];
return { goals, ledger, shares, markers, clubGoal, evenLaps };
const seenStepTrainingConfigs = new Set();
const stepTraining = Array.isArray(source.stepTraining)
? source.stepTraining
.map((entry) => ({
id: String(entry?.id || uid("step_training")),
driverId: String(entry?.driverId || ""),
classId: String(entry?.classId || ""),
steps: Array.isArray(entry?.steps)
? entry.steps
.map((step) => ({
effort: ["calm", "controlled", "fast"].includes(String(step?.effort || "")) ? String(step.effort) : "calm",
lapCount: Math.max(0, Math.round(Number(step?.lapCount || 0) || 0)),
}))
.filter((step) => step.lapCount > 0)
: [],
updatedAt: String(entry?.updatedAt || new Date().toISOString()),
}))
.filter((entry) => {
const key = `${entry.driverId}:${entry.classId}`;
if (!entry.driverId || !entry.classId || !entry.steps.length || seenStepTrainingConfigs.has(key)) return false;
seenStepTrainingConfigs.add(key);
return true;
})
: [];
return { goals, ledger, shares, markers, clubGoal, evenLaps, stepTraining };
}
function getPassingSourceKey(sessionId, passing, index) {
@@ -175,13 +200,12 @@ export function syncTrainingLedger(training, state, { uid }) {
});
});
// Spread the rest of `normalized` rather than listing each field by name: an evenLaps
// config once went missing here because this rebuild listed fields individually and
// silently dropped a newly-added one. Only `ledger` actually needs to change.
return normalizeTrainingState({
goals: normalized.goals,
...normalized,
ledger: [...retained, ...current],
shares: normalized.shares,
markers: normalized.markers,
clubGoal: normalized.clubGoal,
evenLaps: normalized.evenLaps,
}, { uid });
}
@@ -832,6 +856,77 @@ export function buildEvenLapsProgress(state, driverId, classId, config, nowTs =
};
}
const STEP_TRAINING_EFFORTS = ["calm", "controlled", "fast"];
// "Stegträning" (step training): a driver-configured sequence of steps (lap count plus a
// perceived-effort instruction - calm/controlled/fast), run live inside an already-active
// Open Practice session. Effort is guidance shown to the driver, never an enforced lap-time
// target: there is no reliable way to measure how hard a driver is actually pushing. When
// the config repeats "controlled" (the classic calm/controlled/fast/controlled/calm shape),
// the first and last controlled steps are compared afterward as a neutral pace difference,
// never automatically labeled as fatigue.
export function buildStepTrainingProgress(state, driverId, classId, config, nowTs = Date.now()) {
const steps = Array.isArray(config?.steps)
? config.steps
.map((step) => ({
effort: STEP_TRAINING_EFFORTS.includes(String(step?.effort || "")) ? String(step.effort) : "calm",
lapCount: Math.max(0, Math.round(Number(step?.lapCount || 0) || 0)),
}))
.filter((step) => step.lapCount > 0)
: [];
if (!steps.length) return null;
const active = getActivePracticeLaps(state, driverId, classId);
const liveLaps = active?.laps || [];
const totalLaps = steps.reduce((sum, step) => sum + step.lapCount, 0);
let cursor = 0;
const stepStates = steps.map((step, index) => {
const stepLaps = liveLaps.slice(cursor, cursor + step.lapCount);
cursor += step.lapCount;
const paceValues = stepLaps.filter((lap) => !lap.excludedFromPace).map((lap) => lap.lapMs);
return {
index,
effort: step.effort,
lapCount: step.lapCount,
lapsCompleted: stepLaps.length,
done: stepLaps.length >= step.lapCount,
averageLapMs: paceValues.length ? Math.round(paceValues.reduce((sum, value) => sum + value, 0) / paceValues.length) : null,
};
});
const complete = liveLaps.length >= totalLaps;
const currentStepIndex = complete ? steps.length - 1 : stepStates.findIndex((step) => !step.done);
const currentStepState = stepStates[currentStepIndex] || null;
const controlledSteps = stepStates.filter((step) => step.effort === "controlled" && step.averageLapMs != null);
const controlledComparison = controlledSteps.length >= 2
? {
firstAvgMs: controlledSteps[0].averageLapMs,
lastAvgMs: controlledSteps.at(-1).averageLapMs,
diffMs: controlledSteps.at(-1).averageLapMs - controlledSteps[0].averageLapMs,
}
: null;
return {
active: Boolean(active),
totalSteps: steps.length,
totalLaps,
completedLaps: Math.min(liveLaps.length, totalLaps),
complete,
currentStepIndex,
currentStep: currentStepState && {
index: currentStepState.index,
effort: currentStepState.effort,
lapCount: currentStepState.lapCount,
lapsCompleted: currentStepState.lapsCompleted,
lapsRemaining: Math.max(0, currentStepState.lapCount - currentStepState.lapsCompleted),
},
steps: stepStates,
controlledComparison,
};
}
export function buildTrainingRecommendation(stats) {
if (!stats || stats.totalLaps < 10) {
return {