176 lines
8.7 KiB
JavaScript
176 lines
8.7 KiB
JavaScript
// Le générateur de « WOD du jour », hors ligne (lot 5c, ADR 0011) : un WOD composé
|
||
// par des règles, sans IA. Déterministe : le même jour, le même matériel et le même
|
||
// tirage donnent le même WOD (« Un autre » passe au tirage suivant).
|
||
// Règles de composition :
|
||
// - un format tiré parmi AMRAP, EMOM, For Time en tours, et 21-15-9 ;
|
||
// - deux ou trois mouvements, de modalités différentes autant que le matériel le
|
||
// permet : cardio (M), gymnastique (G), charges (W) ;
|
||
// - seulement ce que le matériel coché permet (sans matériel renseigné : poids du
|
||
// corps et course) ;
|
||
// - pas un mouvement fait dans les deux derniers jours, tant qu'il en reste d'autres ;
|
||
// - des répétitions à l'échelle du format (moins par intervalle d'EMOM, plus par tour
|
||
// d'un For Time court ; 12-9-6 plutôt que 21-15-9 pour un mouvement lourd) ;
|
||
// - pas de mouvement de gymnastique avancé (pompes en équilibre, muscle-up) : le profil
|
||
// ne dit pas encore le niveau.
|
||
// Glossaire : docs/glossaire/wod.md
|
||
|
||
import { EXERCISES } from "./exercises.js";
|
||
import { canDo } from "./kit.js";
|
||
import { addDays } from "./dates.js";
|
||
|
||
/** @typedef {"M"|"G"|"W"} Modality */
|
||
/** @typedef {import("./program.js").ProgramSession} ProgramSession */
|
||
|
||
/**
|
||
* Les mouvements du générateur : leur modalité, les répétitions d'un tour d'AMRAP
|
||
* (ou une distance, des calories), et la charge conseillée.
|
||
* @type {readonly {ref: string, m: Modality, reps: number, unit?: string, note?: string}[]}
|
||
*/
|
||
export const WOD_MOVES = Object.freeze([
|
||
{ ref: "run", m: "M", reps: 200, unit: "m" },
|
||
{ ref: "rowing-machine", m: "M", reps: 250, unit: "m" },
|
||
{ ref: "assault-bike", m: "M", reps: 12, unit: "cal" },
|
||
{ ref: "ski-erg", m: "M", reps: 12, unit: "cal" },
|
||
{ ref: "jump-rope", m: "M", reps: 50 },
|
||
{ ref: "double-under", m: "M", reps: 30 },
|
||
{ ref: "pull-up", m: "G", reps: 7 },
|
||
{ ref: "push-up", m: "G", reps: 10 },
|
||
{ ref: "burpee", m: "G", reps: 8 },
|
||
{ ref: "sit-up", m: "G", reps: 15 },
|
||
{ ref: "bodyweight-squat", m: "G", reps: 15 },
|
||
{ ref: "box-jump", m: "G", reps: 10, note: "Box de 60 / 50 cm" },
|
||
{ ref: "toes-to-bar", m: "G", reps: 8 },
|
||
{ ref: "forward-lunge", m: "G", reps: 12, note: "En alternant les jambes" },
|
||
{ ref: "mountain-climber", m: "G", reps: 20 },
|
||
{ ref: "ring-row", m: "G", reps: 10 },
|
||
{ ref: "ring-dip", m: "G", reps: 7 },
|
||
{ ref: "kettlebell-swing", m: "W", reps: 15, note: "Kettlebell 24 / 16 kg" },
|
||
{ ref: "wall-ball", m: "W", reps: 12, note: "Ballon de 9 / 6 kg" },
|
||
{ ref: "dumbbell-snatch", m: "W", reps: 10, note: "Haltère 22,5 / 15 kg, en alternant les bras" },
|
||
{ ref: "thruster", m: "W", reps: 10, note: "Haltères 2 × 15 / 10 kg" },
|
||
{ ref: "devil-press", m: "W", reps: 6, note: "Haltères 2 × 15 / 10 kg" },
|
||
{ ref: "goblet-squat", m: "W", reps: 12, note: "Haltère ou kettlebell 24 / 16 kg" },
|
||
{ ref: "ball-slam", m: "W", reps: 12, note: "Ballon de 9 / 6 kg" },
|
||
{ ref: "barbell-thruster", m: "W", reps: 9, note: "Barre légère : 43 / 29 kg" },
|
||
{ ref: "power-clean", m: "W", reps: 6, note: "Barre moyenne : 61 / 43 kg" },
|
||
{ ref: "deadlift", m: "W", reps: 9, note: "Barre moyenne : 84 / 57 kg" },
|
||
{ ref: "front-squat", m: "W", reps: 8, note: "Barre moyenne : 52 / 35 kg" },
|
||
{ ref: "push-press", m: "W", reps: 8, note: "Barre légère : 43 / 29 kg" },
|
||
{ ref: "sumo-deadlift-high-pull", m: "W", reps: 10, note: "Barre légère : 34 / 25 kg" },
|
||
]);
|
||
|
||
/** Les mouvements qui ne demandent rien : sans matériel renseigné, on reste là. */
|
||
const NO_KIT = /** @type {string[]} */ ([]);
|
||
|
||
/**
|
||
* Générateur pseudo-aléatoire à graine (mulberry32) : le même tirage pour la même graine.
|
||
* @param {number} seed
|
||
*/
|
||
function random(seed) {
|
||
let a = seed >>> 0;
|
||
return () => {
|
||
a = (a + 0x6d2b79f5) >>> 0;
|
||
let t = a;
|
||
t = Math.imul(t ^ (t >>> 15), t | 1);
|
||
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
||
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
||
};
|
||
}
|
||
|
||
/** Une graine stable pour un texte (FNV-1a). @param {string} text */
|
||
function hash(text) {
|
||
let h = 0x811c9dc5;
|
||
for (const c of text) h = Math.imul(h ^ c.charCodeAt(0), 0x01000193) >>> 0;
|
||
return h;
|
||
}
|
||
|
||
/** @template T @param {() => number} rnd @param {readonly T[]} list @returns {T} */
|
||
const pick = (rnd, list) => list[Math.floor(rnd() * list.length)];
|
||
|
||
/**
|
||
* @typedef {object} GeneratorInput
|
||
* @property {string} today AAAA-MM-JJ
|
||
* @property {number} [draw] tirage du jour, à partir de 1
|
||
* @property {readonly string[]} [kit] matériel coché ; absent : poids du corps et course
|
||
* @property {{date: string, exercises: {id: string}[]}[]} [sessions] l'historique, pour éviter les mouvements récents
|
||
*/
|
||
|
||
/**
|
||
* Le WOD du jour : une séance WOD ponctuelle, valide (validateProgram) et faisable
|
||
* avec le matériel.
|
||
* @param {GeneratorInput} input
|
||
* @returns {Omit<ProgramSession, "key">}
|
||
*/
|
||
export function generateWod({ today, draw = 1, kit, sessions = [] }) {
|
||
const rnd = random(hash(`${today}|${draw}|${[...(kit ?? ["-"])].sort().join(",")}`));
|
||
const since = addDays(today, -2);
|
||
// Les deux jours d'avant, pas aujourd'hui : faire le WOD du jour ne doit pas le changer.
|
||
const recent = new Set(sessions.filter((s) => s.date >= since && s.date < today).flatMap((s) => s.exercises.map((x) => x.id)));
|
||
const possible = WOD_MOVES.filter((x) => EXERCISES[x.ref] && canDo(x.ref, kit ?? NO_KIT));
|
||
const fresh = possible.filter((x) => !recent.has(x.ref));
|
||
const pool = fresh.length >= 3 ? fresh : possible;
|
||
|
||
// Le format, puis le nombre de mouvements qu'il demande.
|
||
const kind = pick(rnd, /** @type {const} */ (["amrap", "amrap", "emom", "rounds", "couplet"]));
|
||
const count = kind === "couplet" ? 2 : kind === "emom" ? pick(rnd, [2, 3]) : 3;
|
||
|
||
// Les modalités : toutes différentes tant qu'il y en a assez ; un couplet 21-15-9
|
||
// préfère charges + gymnastique.
|
||
const byM = /** @type {Record<Modality, typeof pool>} */ ({ M: [], G: [], W: [] });
|
||
for (const x of pool) byM[x.m].push(x);
|
||
const order = /** @type {Modality[]} */ (kind === "couplet" ? ["W", "G", "M"] : shuffle(rnd, ["M", "G", "W"]));
|
||
/** @type {typeof pool} */
|
||
const chosen = [];
|
||
for (const m of order) if (chosen.length < count && byM[m].length) chosen.push(pick(rnd, byM[m]));
|
||
// Pas assez de modalités (poids du corps seul) : on complète, sans doublon ni même muscle principal.
|
||
const rest = shuffle(rnd, pool.filter((x) => !chosen.includes(x)));
|
||
for (const x of rest) {
|
||
if (chosen.length >= count) break;
|
||
const first = EXERCISES[x.ref].primary[0];
|
||
if (!chosen.some((c) => EXERCISES[c.ref].primary[0] === first)) chosen.push(x);
|
||
}
|
||
for (const x of rest) if (chosen.length < count && !chosen.includes(x)) chosen.push(x);
|
||
|
||
const amount = (/** @type {(typeof pool)[number]} */ x, /** @type {number} */ factor) => {
|
||
const n = x.unit === "m" ? Math.max(100, Math.round((x.reps * factor) / 50) * 50) : Math.max(1, Math.round(x.reps * factor));
|
||
return x.unit ? `${n} ${x.unit}` : String(n);
|
||
};
|
||
/** @param {(typeof pool)[number]} x @param {string} reps */
|
||
const move = (x, reps) => ({ ref: x.ref, sets: 1, reps, rest: 0, ...(x.note ? { note: x.note } : {}) });
|
||
|
||
const name = `WOD du jour · ${frenchDay(today)}${draw > 1 ? ` (${draw})` : ""}`;
|
||
if (kind === "amrap") {
|
||
const minutes = pick(rnd, [10, 12, 15, 20]);
|
||
return { name, oneOff: true, format: { type: "amrap", minutes }, exercises: chosen.map((x) => move(x, amount(x, 1))) };
|
||
}
|
||
if (kind === "emom") {
|
||
const minutes = chosen.length * pick(rnd, [4, 5]);
|
||
return { name, oneOff: true, format: { type: "emom", minutes }, exercises: chosen.map((x) => move(x, amount(x, x.m === "M" ? 0.6 : 0.8))) };
|
||
}
|
||
if (kind === "rounds") {
|
||
const rounds = pick(rnd, [3, 4, 5]);
|
||
const factor = rounds === 3 ? 1.5 : rounds === 4 ? 1.2 : 1;
|
||
return { name, oneOff: true, format: { type: "fortime", rounds, capMinutes: rounds * 4 }, exercises: chosen.map((x) => move(x, amount(x, factor))) };
|
||
}
|
||
// 21-15-9 : un couplet court et intense ; 12-9-6 pour un mouvement lourd (peu de
|
||
// répétitions par tour d'AMRAP) ; un mouvement de cardio garde une distance.
|
||
return {
|
||
name, oneOff: true, format: { type: "fortime", capMinutes: 10 },
|
||
exercises: chosen.map((x) => move(x, x.unit ? `${amount(x, 1)} par tour` : x.reps >= 9 ? "21-15-9" : "12-9-6")),
|
||
};
|
||
}
|
||
|
||
/** @template T @param {() => number} rnd @param {readonly T[]} list @returns {T[]} */
|
||
function shuffle(rnd, list) {
|
||
const out = [...list];
|
||
for (let i = out.length - 1; i > 0; i--) {
|
||
const j = Math.floor(rnd() * (i + 1));
|
||
[out[i], out[j]] = [out[j], out[i]];
|
||
}
|
||
return out;
|
||
}
|
||
|
||
const MONTHS = ["janv.", "févr.", "mars", "avr.", "mai", "juin", "juil.", "août", "sept.", "oct.", "nov.", "déc."];
|
||
/** « 23 nov. » @param {string} day */
|
||
const frenchDay = (day) => `${Number(day.slice(8, 10))} ${MONTHS[Number(day.slice(5, 7)) - 1]}`;
|