Play tutorial programs automatically after launch

This commit is contained in:
coyotte508 2026-10-31 18:12:39 +01:00
commit 0ae5e3c174
3 changed files with 24 additions and 4 deletions

View file

@ -13,6 +13,8 @@ export async function mountLesson(target, { chapter = "basics", locale = "en", o
if (index < 0) throw Error("Unknown tutorial chapter");
const sound = createSoundDesign();
let soundOn = false, disposed = false, lastStep = -1, lastTurn = 0, lastTick = performance.now();
let playing = false, previousStage, playbackBusy = false;
const canPlay = s => s.state.stage === "resolution" && !s.completed && !s.state.game.resolution?.outcome && !(chapter === "basics" && completedTurns(s.state) >= 3);
let storage;
try { storage = window.localStorage; } catch {}
const tutorial = await createLessonTutorial(chapter, { locale, storage, onProgress });
@ -65,11 +67,14 @@ export async function mountLesson(target, { chapter = "basics", locale = "en", o
guide.innerHTML = `<div class="native-guide-top"><span>${escape(LESSONS[index].title[locale === "fr" ? 0 : 1])}</span><div class="native-guide-tools">${standalone ? `<a href="/">${t("Partie locale", "Local game")}</a>` : ''}${transport("sound", soundOn ? "speaker" : "muted", soundOn ? t("Couper les sons", "Mute sounds") : t("Activer les sons", "Enable sounds"))}</div></div>
<nav class="basics-stages" aria-label="${t("Étapes", "Steps")}">${label.map((name, i) => `<button data-guide-step="${i}" class="quiet ${Math.min(s.step, 2) === i ? 'active' : ''}" ${i > s.step || s.busy ? 'disabled' : ''} ${s.step === i ? 'aria-current="step"' : ''}><span class="step-number">${i + 1}</span><span>${name}</span></button>`).join('')}</nav>
<h1>${escape(s.title)}</h1><p class="native-guide-text">${escape(missed ? t("Destination manquée. Revenez préparer un autre trajet.", "Destination missed. Go back and plan another route.") : s.text)}</p>
<div class="native-guide-controls">${transport("restart", "rewind", t("Recommencer l’exercice", "Restart exercise"), s.busy)}${transport("previous", "left", t("Étape précédente", "Previous step"), s.step === 0 || s.busy)}${transport("replay", "restart", t("Rejouer cette étape", "Replay step"), s.busy || s.step === 0)}${s.canContinue ? `<button data-guide="continue" class="primary icon-button" title="${t("Commencer", "Start")}" aria-label="${t("Commencer", "Start")}">${pictogram("right")}</button>` : ''}${s.completed && (nextChapter || standalone && LESSONS[index + 1]) ? `<button data-guide="next" class="primary icon-label"><span>${t("Exercice suivant", "Next exercise")}</span>${pictogram("right")}</button>` : ''}</div>
<div class="native-guide-controls">${canPlay(s) ? transport("playback", playing ? "pause" : "right", playing ? t("Pause", "Pause") : t("Lire le programme", "Play program")) : ""}${transport("restart", "rewind", t("Recommencer l’exercice", "Restart exercise"), s.busy)}${transport("previous", "left", t("Étape précédente", "Previous step"), s.step === 0 || s.busy)}${transport("replay", "restart", t("Rejouer cette étape", "Replay step"), s.busy || s.step === 0)}${s.canContinue ? `<button data-guide="continue" class="primary icon-button" title="${t("Commencer", "Start")}" aria-label="${t("Commencer", "Start")}">${pictogram("right")}</button>` : ''}${s.completed && (nextChapter || standalone && LESSONS[index + 1]) ? `<button data-guide="next" class="primary icon-label"><span>${t("Exercice suivant", "Next exercise")}</span>${pictogram("right")}</button>` : ''}</div>
${s.error ? `<p role="alert">${escape(s.error)}</p>` : ''}`;
}
const unsubscribe = tutorial.subscribe(s => {
if (disposed) return;
if (s.state.stage === "resolution" && previousStage === "programming") playing = true;
previousStage = s.state.stage;
if (!canPlay(s)) playing = false;
if (s.step !== lastStep) {
ui.resetTutorial();
@ -99,7 +104,11 @@ export async function mountLesson(target, { chapter = "basics", locale = "en", o
if (action === "sound") {
soundOn = !soundOn; sound.enable(soundOn); sound.cue("select"); renderGuide(tutorial.snapshot); return;
}
if (action === "playback") {
playing = !playing; renderGuide(tutorial.snapshot); return;
}
if (!action && button.dataset.guideStep === undefined) return;
playing = false;
ui.resetTutorial();
lastTurn = -1;
sound.cue("back");
@ -108,7 +117,7 @@ export async function mountLesson(target, { chapter = "basics", locale = "en", o
while (tutorial.snapshot.step > step) await tutorial.previousStep();
} else if (action === "continue") await tutorial.continue();
else if (action === "previous") await tutorial.previousStep();
else if (action === "replay") await tutorial.restartStep();
else if (action === "replay") { await tutorial.restartStep(); playing = canPlay(tutorial.snapshot); renderGuide(tutorial.snapshot); }
else if (action === "restart") await tutorial.restart();
else if (action === "next") {
if (nextChapter) nextChapter.open();
@ -118,11 +127,19 @@ export async function mountLesson(target, { chapter = "basics", locale = "en", o
lastTick = performance.now();
};
guide.addEventListener("click", onClick);
// Playback pacing only: each tick executes the next deterministic engine step.
const playback = setInterval(async () => {
if (!playing || disposed || playbackBusy || tutorial.snapshot.busy || !canPlay(tutorial.snapshot)) return;
playbackBusy = true;
try { await play({ type: "resolve" }); }
catch { playing = false; renderGuide(tutorial.snapshot); }
finally { playbackBusy = false; }
}, 450);
const ticker = setInterval(() => {
if (!tutorial.snapshot.busy) void tick().catch(() => {});
}, 5000);
return () => {
disposed = true; clearInterval(ticker); unsubscribe(); tutorial.destroy(); ui.destroy(); sound.destroy();
disposed = true; clearInterval(playback); clearInterval(ticker); unsubscribe(); tutorial.destroy(); ui.destroy(); sound.destroy();
guide.removeEventListener("click", onClick);
target.classList.remove("native-tutorial");
};

View file

@ -2446,3 +2446,6 @@ input:not([type="checkbox"]):not([type="range"]) {
@media (max-width: 800px) {
.native-tutorial.learning-basics .flight-layout { grid-template-columns: 1fr; gap: 20px; }
}
.native-tutorial[data-tutorial-stage="resolution"] #controls,
.native-tutorial.learning-basics[data-tutorial-stage="resolution"] #controls { display: none !important; }

View file

@ -42,7 +42,7 @@ export function createLessonTutorial(id, { locale = "en", storage, onProgress }
text: lesson.basics ? t("Rejoignez la salle marquée en deux actions. La troisième case reste vide : attendre.", "Reach the marked room in two actions. Leave the third slot empty: wait.") : t("Programmez les cartes sur le vaisseau, puis terminez la programmation. Le chrono est facultatif ici.", "Program cards on the ship, then finish planning. The clock is optional here."),
complete: state => state.stage === "resolution" },
{ id: "execute", title: t("Exécuter les tours", "Run the turns"),
text: t("Programme verrouillé. Avancez la résolution avec ▶. Les équipiers agissent ensemble, puis les menaces.", "Program locked. Advance resolution with ▶. Crew act together, then threats."),
text: t("Programme verrouillé. Les équipiers agissent ensemble, puis les menaces.", "Program locked. Crew act together, then threats."),
complete: state => lesson.basics ? completedTurns(state) >= 3 && state.game.resolution.ship.crew[0].station === state.tutorial.targetStation : !!state.game.resolution?.outcome },
],
completion: { title: t("Exercice terminé", "Exercise complete"), text: lesson.basics ? t("Destination atteinte ! Votre programme vous y a conduit.", "Destination reached! Your program took you there.") : t("Rejouez pour comparer un autre programme, ou passez à la suite.", "Replay to compare another program, or move on.") },