Clarify tutorial energy transfers and render localized action pictograms
This commit is contained in:
parent
3de2e364dc
commit
79dccd3bb3
9 changed files with 115 additions and 26 deletions
|
|
@ -7,6 +7,7 @@ import { LESSONS } from "../src/tutorials.js";
|
|||
import { snapshot } from "../src/view.js";
|
||||
import { mountConsole } from "../preview/app.js";
|
||||
import { pictogram } from "../preview/icons.js";
|
||||
import { tutorialTextHtml } from "../preview/tutorial-text.js";
|
||||
import { tutorialPhaseButton } from "../preview/tutorial-program.js";
|
||||
import { duration, practiceClockText } from "../preview/clock.js";
|
||||
import { createSoundDesign } from "../preview/sound.js";
|
||||
|
|
@ -63,6 +64,8 @@ export async function mountLesson(
|
|||
locale,
|
||||
storage,
|
||||
onProgress,
|
||||
// Keep the closed action tokens until the native guide renders them safely.
|
||||
formatText: (text) => text,
|
||||
});
|
||||
target.innerHTML = html
|
||||
.split("<body>")[1]
|
||||
|
|
@ -228,7 +231,7 @@ export async function mountLesson(
|
|||
programControls.innerHTML = `<div class="phase-lesson-controls">${s.state.phase === 1 ? transport("phase-deadline", "forward", t("phases.skip"), !ready || !s.state.game.threats.length || s.busy) : `<button data-guide="run" class="primary icon-label lesson-run" aria-keyshortcuts="Enter" title="${t("phases.execute")} · ↵" ${!ready || s.busy ? "disabled" : ""}>${pictogram("check")}<span>${t("phases.execute")}</span><kbd aria-hidden="true">↵</kbd></button>`}<div class="practice-clock"><div><strong data-practice-phase>${t("phases.deadline", { phase: s.state.phase, time: phaseTime })}</strong><small>${t("phases.total")} · <span data-practice-time>${practiceClockText(s.state, locale)}</span></small></div>${transport("phase-clock", s.state.tutorial.clockRunning ? "pause" : "clock", t(s.state.tutorial.clockRunning ? "basics.pause" : "basics.start_clock"))}</div></div>`;
|
||||
}
|
||||
guide.innerHTML = `<div class="native-guide-row"><nav class="basics-stages" aria-label="${t("tutorial.steps")}">${label.map((name, i) => `<button data-guide-step="${deadline ? i + 3 : i}" class="quiet ${stage === i ? "active" : ""}" ${i > stage || s.busy ? "disabled" : ""} ${stage === i ? 'aria-current="step"' : ""}><span class="step-number">${i + 1}</span><span>${name}</span></button>`).join("")}</nav><div class="native-guide-tools">${standalone ? `<a href="/">${t("tutorial.local_game")}</a>` : ""}${transport("sound", soundOn ? "speaker" : "muted", soundOn ? t("tutorial.mute_sounds") : t("tutorial.enable_sounds"))}</div></div>
|
||||
<h1>${escape(s.title)}</h1>${s.step === 0 && (chapter === "basics" || phaseLesson) ? missionOverview() : ""}${["basics", "first-shot", "energy"].includes(chapter) ? `<small class="lesson-scope">${t("timing.exercise", { turns: chapter === "energy" ? 4 : 3 })}${chapter === "basics" ? ` · ${deadline ? 2 : 1}/2` : ""}</small>` : ""}<p class="native-guide-text">${escape(missed ? t("tutorial.destination_missed_go_back_and_plan_another_route") : s.text)}</p>${chapter === "first-shot" && s.step < 2 ? `<p class="lesson-shield-note">${pictogram("shield")}<span>${t("lesson.first-shot.shield")}</span></p>` : ""}
|
||||
<h1>${escape(s.title)}</h1>${s.step === 0 && (chapter === "basics" || phaseLesson) ? missionOverview() : ""}${["basics", "first-shot", "energy"].includes(chapter) ? `<small class="lesson-scope">${t("timing.exercise", { turns: chapter === "energy" ? 4 : 3 })}${chapter === "basics" ? ` · ${deadline ? 2 : 1}/2` : ""}</small>` : ""}<p class="native-guide-text">${tutorialTextHtml(missed ? t("tutorial.destination_missed_go_back_and_plan_another_route") : s.text, locale)}</p>${chapter === "first-shot" && s.step < 2 ? `<p class="lesson-shield-note">${pictogram("shield")}<span>${t("lesson.first-shot.shield")}</span></p>` : ""}
|
||||
<div class="native-guide-controls">${transport("restart", "rewind", t("tutorial.restart_exercise"), s.busy)}${transport("previous", "left", t("tutorial.previous_step"), s.step === 0 || s.busy)}${transport("replay", "restart", t("tutorial.replay_step"), s.busy || s.step === 0)}${s.canContinue ? `<button data-guide="continue" class="primary icon-button" title="${t("tutorial.start")}" aria-label="${t("tutorial.start")}">${pictogram("right")}</button>` : ""}${s.completed && (nextChapter || (standalone && LESSONS[index + 1])) ? `<button data-guide="next" class="primary icon-label"><span>${t("tutorial.next_exercise")}</span>${pictogram("right")}</button>` : ""}</div>
|
||||
${s.error ? `<p role="alert">${escape(s.error)}</p>` : ""}`;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import { plannedRoute } from "./program-route.js";
|
|||
import { createProgramCursor } from "./program-cursor.js";
|
||||
import { translate, createLocaleSelection, lessonCopy } from "./i18n.js";
|
||||
import { basicsView } from "./basics.js";
|
||||
import { tutorialTextHtml } from "./tutorial-text.js";
|
||||
import { duration, practiceClockText } from "./clock.js";
|
||||
import { mountNavigation } from "./navigation.js";
|
||||
import {
|
||||
|
|
@ -83,16 +84,6 @@ export function mountConsole(root, host = null) {
|
|||
if (wasPlaying && !replay.playing) rerender();
|
||||
}, 100));
|
||||
const t = (key, params) => translate(locale, key, params);
|
||||
const actionWords = (text) =>
|
||||
text.replace(
|
||||
/\b[ABC]\b/g,
|
||||
(key) =>
|
||||
({
|
||||
A: t("app.weapons"),
|
||||
B: t("app.energy"),
|
||||
C: t("app.systems"),
|
||||
})[key],
|
||||
);
|
||||
const navigation = mountNavigation(root, {
|
||||
locale: () => locale,
|
||||
hosted: !!host,
|
||||
|
|
@ -419,7 +410,7 @@ export function mountConsole(root, host = null) {
|
|||
$("#lesson-guide").innerHTML =
|
||||
view.tutorial && !basic
|
||||
? `<ol>${lessonCopy(view.tutorial.id, locale)
|
||||
.instructions.map((p) => `<li>${esc(actionWords(p))}</li>`)
|
||||
.instructions.map((p) => `<li>${tutorialTextHtml(p, locale)}</li>`)
|
||||
.join(
|
||||
"",
|
||||
)}</ol><div class="lesson-actions">${view.stage === "programming" ? `<button id="lesson-clock" class="icon-label">${pictogram(view.tutorial.clockRunning ? "pause" : "clock")}<span>${view.tutorial.clockRunning ? t("app.pause_clock") : t("app.start_practice_clock")}</span></button><button id="lesson-next" class="icon-label">${pictogram(view.phase === 1 ? "step" : "lock")}<span>${view.phase === 1 ? t("app.phase_2") : t("app.finish_programming")}</span></button>` : ""} <button data-lesson="${view.tutorial.id}" class="quiet icon-button" title="${t("app.restart")}" aria-label="${t("app.restart")}">${pictogram("restart")}</button></div>`
|
||||
|
|
|
|||
|
|
@ -5,20 +5,20 @@
|
|||
"lesson.first-shot.lane": "Center lane",
|
||||
"lesson.first-shot.shield": "The center shield absorbs hits automatically while charged.",
|
||||
"lesson.energy.title": "Energy and coordination",
|
||||
"lesson.energy.instruction.0": "The center laser needs one energy per shot. Its reactor starts with three: refill it to fire four times. One refill uses a fuel capsule to fill the central reactor to capacity—not just add one energy. It does not refill the other reactors or shields.",
|
||||
"lesson.energy.instruction.1": "Android 1: fire on turns 1-4. Android 3: use the lift on turn 1, refill on turn 2. Then lock phase 1 to plan turn 4.",
|
||||
"lesson.energy.instruction.0": "The center laser needs one energy per shot. Its reactor starts with three: refill it to fire four times.\n\nIn the lower center room, [[action:energy]] consumes one fuel capsule to fill the central reactor to capacity—not just add one energy. It does not refill other reactors or shields.\n\nAt a shield, [[action:energy]] transfers energy from the same-zone reactor: enough to fill the shield, limited by the reactor’s remaining supply. At a side reactor, it transfers energy from the central reactor, up to capacity and limited by the central supply. These transfers use no fuel capsule.",
|
||||
"lesson.energy.instruction.1": "Android 1: [[action:weapons]] on turns 1-4. Android 3: use the lift on turn 1, then [[action:energy]] in the lower center room on turn 2 to refill the reactor. Then lock phase 1 to plan turn 4.",
|
||||
"lesson.repairs.title": "Repairing a malfunction",
|
||||
"lesson.repairs.instruction.0": "Repair the highlighted malfunction.",
|
||||
"lesson.repairs.instruction.1": "Move right on turn 1, then use B on turns 2, 3 and 4. While the malfunction is active, B repairs it instead of recharging the shield; it needs three repair actions.",
|
||||
"lesson.repairs.instruction.1": "Move right on turn 1, then use [[action:energy]] on turns 2, 3 and 4. While this shield malfunction is active, [[action:energy]] repairs it instead of recharging the shield. Three repair actions eliminate it.",
|
||||
"lesson.battlebots.title": "Battlebots and interceptors",
|
||||
"lesson.battlebots.instruction.0": "Collect the battlebots, then reach the interceptors.",
|
||||
"lesson.battlebots.instruction.1": "→ · systems · ← · ← · systems. In flight, play battlebots to fire; leave a slot empty to return.",
|
||||
"lesson.battlebots.instruction.1": "→ · [[action:systems]] · ← · ← · [[action:systems]]. In flight, play battlebots to fire; leave a slot empty to return.",
|
||||
"lesson.double-actions.title": "Two actions, in order",
|
||||
"lesson.double-actions.instruction.0": "Move and fire with one card.",
|
||||
"lesson.double-actions.instruction.1": "Compare → + weapons with weapons + →: the order determines which laser fires.",
|
||||
"lesson.double-actions.instruction.1": "Compare → + [[action:weapons]] with [[action:weapons]] + →: the order determines which laser fires.",
|
||||
"lesson.phasing.title": "Expansion threats",
|
||||
"lesson.phasing.instruction.0": "Hit the fighter while it is visible.",
|
||||
"lesson.phasing.instruction.1": "Weapons on turns 1-3. Watch the missed shot while it is phased out.",
|
||||
"lesson.phasing.instruction.1": "[[action:weapons]] on turns 1-3. Watch the missed shot while it is phased out.",
|
||||
"lesson.specializations.title": "Specializations",
|
||||
"lesson.specializations.instruction.0": "Launch a rocket remotely with the Rocketeer.",
|
||||
"lesson.specializations.instruction.1": "Program the basic ability on turn 1. The rocket hits next turn.",
|
||||
|
|
|
|||
|
|
@ -5,20 +5,20 @@
|
|||
"lesson.first-shot.lane": "Zone centrale",
|
||||
"lesson.first-shot.shield": "Le bouclier central absorbe les tirs automatiquement tant qu’il est chargé.",
|
||||
"lesson.energy.title": "Énergie et coordination",
|
||||
"lesson.energy.instruction.0": "Le laser central consomme une énergie par tir. Son réacteur en contient trois : ravitaillez-le pour tirer quatre fois. Un ravitaillement consomme une capsule et remplit le réacteur central jusqu’à sa capacité, au lieu d’ajouter une seule énergie. Il ne remplit pas les autres réacteurs ni les boucliers.",
|
||||
"lesson.energy.instruction.1": "Androïde 1 : tirer aux tours 1-4. Androïde 3 : ascenseur au tour 1, énergie au tour 2. Verrouillez ensuite la phase 1 pour préparer le tour 4.",
|
||||
"lesson.energy.instruction.0": "Le laser central consomme une énergie par tir. Son réacteur en contient trois : ravitaillez-le pour tirer quatre fois.\n\nDans la salle centrale du bas, [[action:energy]] consomme une capsule de carburant pour remplir le réacteur central jusqu’à sa capacité, et non ajouter une seule énergie. Cela ne remplit ni les autres réacteurs ni les boucliers.\n\nAu bouclier, [[action:energy]] transfère l’énergie du réacteur de la même zone : de quoi remplir le bouclier, dans la limite de l’énergie restante du réacteur. Dans un réacteur latéral, cette action transfère l’énergie du réacteur central, jusqu’à sa capacité et dans la limite de la réserve centrale. Ces transferts ne consomment aucune capsule.",
|
||||
"lesson.energy.instruction.1": "Androïde 1 : [[action:weapons]] aux tours 1-4. Androïde 3 : ascenseur au tour 1, puis [[action:energy]] dans la salle centrale du bas au tour 2 pour ravitailler le réacteur. Verrouillez ensuite la phase 1 pour préparer le tour 4.",
|
||||
"lesson.repairs.title": "Réparer une panne",
|
||||
"lesson.repairs.instruction.0": "Réparez la panne surlignée.",
|
||||
"lesson.repairs.instruction.1": "Allez à droite au tour 1, puis utilisez B aux tours 2, 3 et 4. Tant que la panne est active, B la répare au lieu de recharger le bouclier ; trois actions de réparation sont nécessaires.",
|
||||
"lesson.repairs.instruction.1": "Allez à droite au tour 1, puis utilisez [[action:energy]] aux tours 2, 3 et 4. Tant que cette panne de bouclier est active, [[action:energy]] la répare au lieu de recharger le bouclier. Trois actions de réparation l’éliminent.",
|
||||
"lesson.battlebots.title": "Robots et intercepteurs",
|
||||
"lesson.battlebots.instruction.0": "Prenez les robots, puis rejoignez les intercepteurs.",
|
||||
"lesson.battlebots.instruction.1": "→ · systèmes · ← · ← · systèmes. En vol, jouez robots pour tirer ; laissez vide pour rentrer.",
|
||||
"lesson.battlebots.instruction.1": "→ · [[action:systems]] · ← · ← · [[action:systems]]. En vol, jouez robots pour tirer ; laissez vide pour rentrer.",
|
||||
"lesson.double-actions.title": "Deux actions, dans l’ordre",
|
||||
"lesson.double-actions.instruction.0": "Déplacez-vous et tirez avec une seule carte.",
|
||||
"lesson.double-actions.instruction.1": "Comparez → + armes et armes + → : l’ordre choisit le laser utilisé.",
|
||||
"lesson.double-actions.instruction.1": "Comparez → + [[action:weapons]] et [[action:weapons]] + → : l’ordre choisit le laser utilisé.",
|
||||
"lesson.phasing.title": "Menaces de l’extension",
|
||||
"lesson.phasing.instruction.0": "Touchez le chasseur lorsqu’il est visible.",
|
||||
"lesson.phasing.instruction.1": "Armes aux tours 1-3. Observez le tir ignoré pendant son déphasage.",
|
||||
"lesson.phasing.instruction.1": "[[action:weapons]] aux tours 1-3. Observez le tir ignoré pendant son déphasage.",
|
||||
"lesson.specializations.title": "Spécialisations",
|
||||
"lesson.specializations.instruction.0": "Lancez une roquette à distance avec l’Artilleur.",
|
||||
"lesson.specializations.instruction.1": "Programmez sa capacité basique au tour 1. La roquette frappe au tour suivant.",
|
||||
|
|
|
|||
|
|
@ -472,6 +472,7 @@ export function createPreviewServer({ clock = Date.now, storagePath } = {}) {
|
|||
"/clock.js": "clock.js",
|
||||
"/local.js": "local.js",
|
||||
"/text.js": "text.js",
|
||||
"/tutorial-text.js": "tutorial-text.js",
|
||||
"/navigation.js": "navigation.js",
|
||||
"/icons.js": "icons.js",
|
||||
"/art.js": "art.js",
|
||||
|
|
|
|||
|
|
@ -2391,6 +2391,8 @@ input:not([type="checkbox"]):not([type="range"]) {
|
|||
.native-guide-tools a { color: #a9d7e7; font-size: .8rem; }
|
||||
.native-guide h1 { font-size: clamp(1.4rem, 3vw, 2rem); margin: 20px 0 8px; }
|
||||
.native-guide-text { line-height: 1.6; white-space: pre-line; max-width: 65ch; }
|
||||
.tutorial-action { display: inline-block; vertical-align: -0.25em; line-height: 1; }
|
||||
.tutorial-action .pictogram { width: 1.25em; height: 1.25em; }
|
||||
.native-guide-controls { margin-top: 16px; }
|
||||
.native-guide .basics-stages { grid-template-columns: repeat(3, 1fr); margin: 16px 0; }
|
||||
.native-tutorial .basics-top,
|
||||
|
|
|
|||
31
preview/tutorial-text.js
Normal file
31
preview/tutorial-text.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { translate } from "./i18n.js";
|
||||
import { pictogram } from "./icons.js";
|
||||
|
||||
// Catalogs contain text and a closed set of action tokens, never executable HTML.
|
||||
const actions = {
|
||||
weapons: ["A", "app.weapons"],
|
||||
energy: ["B", "app.energy"],
|
||||
systems: ["C", "app.systems"],
|
||||
};
|
||||
const escape = (text) => String(text).replace(/[&<>"']/g, (c) =>
|
||||
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
|
||||
|
||||
export function tutorialTextParts(text, locale) {
|
||||
return String(text).split(/(\[\[action:(?:weapons|energy|systems)\]\])/g)
|
||||
.filter(Boolean).map((text) => {
|
||||
const action = actions[text.slice(9, -2)];
|
||||
return text.startsWith("[[action:") && action
|
||||
? { type: "action", icon: action[0], label: translate(locale, action[1]) }
|
||||
: { type: "text", text };
|
||||
});
|
||||
}
|
||||
|
||||
export function tutorialTextPlain(text, locale) {
|
||||
return tutorialTextParts(text, locale).map((part) => part.label ?? part.text).join("");
|
||||
}
|
||||
|
||||
export function tutorialTextHtml(text, locale) {
|
||||
return tutorialTextParts(text, locale).map((part) => part.type === "text"
|
||||
? escape(part.text)
|
||||
: `<span class="tutorial-action" role="img" aria-label="${escape(part.label)}" title="${escape(part.label)}">${pictogram(part.icon)}</span>`).join("");
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
import { tutorialTextPlain } from "../preview/tutorial-text.js";
|
||||
import { translate, loadLocale } from "../preview/i18n.js";
|
||||
import { createTutorial } from "@boardgamers/protocol/tutorial";
|
||||
import { LESSONS, createLesson } from "./tutorials.js";
|
||||
|
|
@ -19,12 +20,12 @@ export function energyObjective(state) {
|
|||
// BGS owns checkpoints and saved action history. All moves still use our engine.
|
||||
export async function createLessonTutorial(
|
||||
id,
|
||||
{ locale = "en", storage, onProgress } = {},
|
||||
{ locale = "en", storage, onProgress, formatText = tutorialTextPlain } = {},
|
||||
) {
|
||||
locale = await loadLocale(locale);
|
||||
const lesson = LESSONS.find((l) => l.id === id);
|
||||
if (!lesson) throw Error("Unknown lesson");
|
||||
const t = (key, params) => translate(locale, key, params);
|
||||
const t = (key, params) => formatText(translate(locale, key, params), locale);
|
||||
const settleClock = (state) => {
|
||||
if (lesson.basics && state.stage === "resolution") {
|
||||
state.game.resolution.lastTurn = 3;
|
||||
|
|
|
|||
60
test/tutorial-text.test.js
Normal file
60
test/tutorial-text.test.js
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { loadLocale, translate } from "../preview/i18n.js";
|
||||
import { tutorialTextParts, tutorialTextPlain, tutorialTextHtml } from "../preview/tutorial-text.js";
|
||||
import { pictogram } from "../preview/icons.js";
|
||||
import { LESSONS } from "../src/tutorials.js";
|
||||
import { createLessonTutorial } from "../src/tutorial-controller.js";
|
||||
|
||||
for (const locale of ["en", "fr"]) {
|
||||
test(`${locale}: every chapter has safe action copy and controller fallback`, async () => {
|
||||
await loadLocale(locale);
|
||||
const catalog = JSON.parse(await readFile(new URL(`../preview/locales/${locale}.json`, import.meta.url)));
|
||||
const en = JSON.parse(await readFile(new URL("../preview/locales/en.json", import.meta.url)));
|
||||
for (const [key, text] of Object.entries(catalog).filter(([key]) => /^(lesson\.|tutorial_controller\.|timing\.|phases\.|basics\.)/.test(key))) {
|
||||
assert.doesNotMatch(text, /\b[BC]\b|\bA[, /]|perform A\b/, key);
|
||||
const tokens = (s) => [...s.matchAll(/\[\[action:[^\]]+\]\]/g)].map(m => m[0]).sort();
|
||||
assert.deepEqual(tokens(text), tokens(en[key]), key);
|
||||
assert.doesNotMatch(tutorialTextPlain(text, locale), /\[\[action:/, key);
|
||||
}
|
||||
for (const lesson of LESSONS) {
|
||||
const controller = await createLessonTutorial(lesson.id, { locale });
|
||||
assert.doesNotMatch(controller.snapshot.text, /\[\[action:/);
|
||||
await controller.continue();
|
||||
assert.doesNotMatch(controller.snapshot.text, /\[\[action:/);
|
||||
controller.destroy();
|
||||
}
|
||||
const repairs = translate(locale, "lesson.repairs.instruction.1");
|
||||
const parts = tutorialTextParts(repairs, locale).filter(p => p.type === "action");
|
||||
assert.equal(parts.length, 2);
|
||||
assert.ok(parts.every(p => p.icon === "B" && p.label === translate(locale, "app.energy")));
|
||||
assert.ok(tutorialTextHtml(repairs, locale).includes(pictogram("B")));
|
||||
assert.ok(tutorialTextHtml(repairs, locale).includes(`aria-label="${translate(locale, "app.energy")}"`));
|
||||
const rich = await createLessonTutorial("energy", { locale, formatText: text => text });
|
||||
assert.match(rich.snapshot.text, /\[\[action:energy\]\]/);
|
||||
rich.destroy();
|
||||
});
|
||||
}
|
||||
|
||||
test("tutorial rendering escapes markup and only renders whitelisted pictograms", async () => {
|
||||
await loadLocale("en");
|
||||
const html = tutorialTextHtml('<img src=x onerror="bad()"> & [[action:energy]] [[action:evil]]', "en");
|
||||
assert.doesNotMatch(html, /<img|onerror="bad/);
|
||||
assert.match(html, /<img/);
|
||||
assert.match(html, /&/);
|
||||
assert.match(html, /\[\[action:evil\]\]/);
|
||||
assert.equal((html.match(/<svg/g) ?? []).length, 1);
|
||||
});
|
||||
|
||||
test("energy and repairs explain transfer limits, fuel, and replacement actions", async () => {
|
||||
await loadLocale("en");
|
||||
const energy = translate("en", "lesson.energy.instruction.0");
|
||||
assert.match(energy, /same-zone reactor/);
|
||||
assert.match(energy, /enough to fill the shield, limited by the reactor/);
|
||||
assert.match(energy, /consumes one fuel capsule/);
|
||||
assert.match(energy, /transfers use no fuel capsule/);
|
||||
const repairs = translate("en", "lesson.repairs.instruction.1");
|
||||
assert.match(repairs, /instead of recharging/);
|
||||
assert.match(repairs, /Three repair actions eliminate it/);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue