Use colored goods cubes in localized tutorial prose
This commit is contained in:
parent
f717e85148
commit
4d19b7565b
6 changed files with 137 additions and 14 deletions
|
|
@ -4,6 +4,9 @@ import { readFile, mkdir } from "node:fs/promises";
|
|||
import { extname, resolve } from "node:path";
|
||||
import { chromium } from "playwright";
|
||||
import { languages } from "../viewer/localization/runtime.js";
|
||||
import { createTutorial } from "@boardgamers/protocol/tutorial";
|
||||
import { chapters } from "../viewer/tutorials.js";
|
||||
import { availableMoves } from "../engine/index.js";
|
||||
const dist = resolve("dist");
|
||||
const server = createServer(async (req, res) => {
|
||||
try {
|
||||
|
|
@ -69,6 +72,57 @@ try {
|
|||
await prose.locator(".icon-coin").first().waitFor();
|
||||
}
|
||||
}
|
||||
const saved = new Map();
|
||||
const trade = await createTutorial({
|
||||
...chapters.trade,
|
||||
storage: {
|
||||
getItem: (key) => saved.get(key) ?? null,
|
||||
setItem: (key, value) => saved.set(key, value),
|
||||
removeItem: (key) => saved.delete(key),
|
||||
},
|
||||
});
|
||||
while (trade.snapshot.step < 5) {
|
||||
if (trade.snapshot.canContinue) await trade.continue();
|
||||
else {
|
||||
const snap = trade.snapshot;
|
||||
const step = chapters.trade.steps[snap.step];
|
||||
const move = availableMoves(snap.state, 0).find(
|
||||
(m) => !step.validateMove?.(snap.state, m),
|
||||
);
|
||||
assert.equal(await trade.play(move), true);
|
||||
}
|
||||
}
|
||||
trade.destroy();
|
||||
await page.addInitScript(
|
||||
(entries) => {
|
||||
for (const [key, value] of entries) localStorage.setItem(key, value);
|
||||
},
|
||||
[...saved],
|
||||
);
|
||||
for (const locale of ["en", "fr"]) {
|
||||
await page.goto(`${base}/?lesson=trade&locale=${locale}`);
|
||||
const prose = page.locator(".bgs-tutorial-body > p").first();
|
||||
await prose.locator(".icon-copra").waitFor();
|
||||
assert.equal(await prose.locator(".icon-beef").count(), 2);
|
||||
assert.equal(await prose.locator(".resource-symbol").count(), 0);
|
||||
await page.locator("[data-color-blind]").click();
|
||||
assert.equal(await prose.locator(".resource-symbol").count(), 3);
|
||||
// The guide must retain the setting when the controller renders a new step.
|
||||
await page.locator(".bgs-tutorial-playback button").nth(3).click();
|
||||
assert.equal(await prose.locator(".resource-symbol").count(), 2);
|
||||
await page.locator(".bgs-tutorial-playback button").nth(1).click();
|
||||
assert.equal(await prose.locator(".resource-symbol").count(), 3);
|
||||
await page.locator("[data-color-blind]").click();
|
||||
assert.equal(await prose.locator(".resource-symbol").count(), 0);
|
||||
assert.ok(
|
||||
await page.evaluate(
|
||||
() => document.documentElement.scrollWidth <= innerWidth,
|
||||
),
|
||||
);
|
||||
await page
|
||||
.locator(".vanuatu-tutorial")
|
||||
.screenshot({ path: `.local/qa/prose-export-${locale}.png` });
|
||||
}
|
||||
await page.goto(`${base}/?new&hotseat&locale=fr`);
|
||||
await page.locator('[data-character-choice="beggar"]').click();
|
||||
const help = page.locator('dialog [data-character-help="beggar"]');
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import { LESSON } from "../viewer/tutorial-copy.js";
|
|||
import { languages } from "../viewer/localization/runtime.js";
|
||||
|
||||
for (const locale of Object.keys(languages))
|
||||
test(`tutorial prose preserves ${locale} grammar around the four symbols`, async () => {
|
||||
test(`tutorial prose preserves ${locale} grammar around action and resource symbols`, async () => {
|
||||
const catalog = JSON.parse(
|
||||
await readFile(
|
||||
new URL(`../viewer/localization/${locale}.json`, import.meta.url),
|
||||
|
|
@ -17,6 +17,29 @@ for (const locale of Object.keys(languages))
|
|||
const html = pictogramText(text, locale);
|
||||
for (const name of ["coin", "point", "draw", "build"])
|
||||
assert.ok(html.includes(`icon-${name}`), `${locale}: ${name}`);
|
||||
for (const source of [
|
||||
LESSON.tradeCompare,
|
||||
LESSON.tradeChoose,
|
||||
LESSON.tradeIncomplete,
|
||||
]) {
|
||||
const goods = pictogramText(catalog[source], locale, true);
|
||||
assert.equal(
|
||||
(goods.match(/icon-copra/g) ?? []).length,
|
||||
1,
|
||||
`${locale}: copra`,
|
||||
);
|
||||
assert.equal(
|
||||
(goods.match(/icon-beef/g) ?? []).length,
|
||||
source === LESSON.tradeChoose ? 1 : 2,
|
||||
`${locale}: beef`,
|
||||
);
|
||||
assert.equal(
|
||||
(goods.match(/resource-symbol/g) ?? []).length,
|
||||
source === LESSON.tradeChoose ? 2 : 3,
|
||||
`${locale}: color-blind symbols`,
|
||||
);
|
||||
}
|
||||
assert.match(pictogramText(catalog.kava, locale), /icon-kava/);
|
||||
// Replace the generated icon with its accessible name: no translated text,
|
||||
// punctuation or amounts may disappear or change order.
|
||||
const plain = html.replace(
|
||||
|
|
|
|||
|
|
@ -112,14 +112,50 @@ const terms = {
|
|||
"بازیکن اول",
|
||||
],
|
||||
};
|
||||
const goods = {
|
||||
en: ["kava", "copra", "beef"],
|
||||
fr: ["kava", "coprah|copra", "bœuf|boeuf"],
|
||||
de: ["Kava", "Kopra", "Rindfleisch"],
|
||||
nl: ["kava", "kopra", "rundvlees"],
|
||||
da: ["kava", "kopra|kopraen", "oksekød"],
|
||||
it: ["kava", "copra", "manzo"],
|
||||
"pt-BR": ["kava", "copra", "boi"],
|
||||
pl: [
|
||||
"kava|kavy|kavę|kavą",
|
||||
"kopra|kopry|koprę|koprą",
|
||||
"wołowina|wołowiny|wołowinę|wołowiną",
|
||||
],
|
||||
ro: ["kava", "copra", "vită|vita"],
|
||||
ru: [
|
||||
"кава|кавы|каву|кавой",
|
||||
"копра|копры|копру|копрой",
|
||||
"говядина|говядины|говядину|говядиной",
|
||||
],
|
||||
el: ["κάβα", "κόπρα", "μοσχάρι"],
|
||||
hi: ["कावा", "खोपरा|खोपरे", "गोमांस"],
|
||||
ko: ["카바", "코프라", "소고기"],
|
||||
"zh-TW": ["卡瓦", "椰乾", "牛肉"],
|
||||
vi: ["kava", "cùi dừa khô", "thịt bò"],
|
||||
fa: ["کاوا", "مغز خشک نارگیل", "گوشت گاو"],
|
||||
};
|
||||
const cache = new Map();
|
||||
const escapePattern = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
function vocabulary(locale) {
|
||||
if (cache.has(locale)) return cache.get(locale);
|
||||
const names = ["coin", "point", "build", "draw", "first"];
|
||||
const names = [
|
||||
"coin",
|
||||
"point",
|
||||
"build",
|
||||
"draw",
|
||||
"first",
|
||||
"kava",
|
||||
"copra",
|
||||
"beef",
|
||||
];
|
||||
const words = new Map(
|
||||
(terms[locale] ?? terms.en).flatMap((group, i) =>
|
||||
group.split("|").map((word) => [word.toLowerCase(), names[i]]),
|
||||
[...(terms[locale] ?? terms.en), ...(goods[locale] ?? goods.en)].flatMap(
|
||||
(group, i) =>
|
||||
group.split("|").map((word) => [word.toLowerCase(), names[i]]),
|
||||
),
|
||||
);
|
||||
const body = [...words.keys()]
|
||||
|
|
@ -137,7 +173,7 @@ function vocabulary(locale) {
|
|||
cache.set(locale, result);
|
||||
return result;
|
||||
}
|
||||
export function pictogramText(text, locale = "en") {
|
||||
export function pictogramText(text, locale = "en", colorBlind = false) {
|
||||
const { words, pattern } = vocabulary(resolveLocale(locale));
|
||||
let result = "",
|
||||
end = 0;
|
||||
|
|
@ -145,7 +181,7 @@ export function pictogramText(text, locale = "en") {
|
|||
const label = match[0],
|
||||
name = words.get(label.toLowerCase());
|
||||
result += esc(text.slice(end, match.index));
|
||||
result += `<span class="prose-icon" role="img" aria-label="${esc(label)}" title="${esc(label)}">${icon(name, label)}</span>`;
|
||||
result += `<span class="prose-icon" role="img" aria-label="${esc(label)}" title="${esc(label)}">${icon(name, label, false, colorBlind)}</span>`;
|
||||
end = match.index + label.length;
|
||||
}
|
||||
return result + esc(text.slice(end));
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ import { PRICES, VALUES } from "../engine/catalog.js";
|
|||
import { pictogramText } from "./prose.js";
|
||||
import { translateText } from "./localization/index.js";
|
||||
export function rulesHtml(lang = "en", colorBlind = false) {
|
||||
const prose = (text) => pictogramText(t(text), lang);
|
||||
const prose = (text) => pictogramText(t(text), lang, colorBlind);
|
||||
const t = (text) => translateText(text, lang);
|
||||
const icon = (name) =>
|
||||
renderIcon(name, t(iconLabel(name)), false, colorBlind);
|
||||
|
|
|
|||
|
|
@ -21,9 +21,16 @@ export async function mountLesson(target, options) {
|
|||
const guide = document.createElement("div");
|
||||
guide.className = "vanuatu-tutorial";
|
||||
target.append(guide);
|
||||
let errorFrame;
|
||||
let errorFrame,
|
||||
colorBlind = false;
|
||||
const ui = mountGame(target, {
|
||||
language: options.locale ?? "en",
|
||||
onPreference: (name, value) => {
|
||||
if (name === "colorBlind") {
|
||||
colorBlind = value;
|
||||
renderProse(tutorial.snapshot);
|
||||
}
|
||||
},
|
||||
onMove: async (m) => {
|
||||
const accepted = await tutorial.play(m);
|
||||
if (!accepted) {
|
||||
|
|
@ -59,13 +66,14 @@ export async function mountLesson(target, options) {
|
|||
const error = guide.querySelector('[role="alert"]');
|
||||
error.setAttribute("translate", "no");
|
||||
error.classList.add("tutorial-error");
|
||||
const offProse = tutorial.subscribe((snapshot) => {
|
||||
function renderProse(snapshot) {
|
||||
[snapshot.text, snapshot.hint].forEach((source, i) => {
|
||||
const paragraph = paragraphs[i];
|
||||
if (!paragraph || !source) return;
|
||||
paragraph.innerHTML = pictogramText(
|
||||
translateText(source, options.locale),
|
||||
options.locale,
|
||||
colorBlind,
|
||||
);
|
||||
});
|
||||
// Validators often return the current instruction. Show it once, as the
|
||||
|
|
@ -73,13 +81,14 @@ export async function mountLesson(target, options) {
|
|||
paragraphs[0].hidden =
|
||||
!!snapshot.error && snapshot.error.trim() === snapshot.text.trim();
|
||||
if (snapshot.error) {
|
||||
error.innerHTML = `${icon("warning", "")}<span>${pictogramText(translateText(snapshot.error, options.locale), options.locale)}</span>`;
|
||||
error.innerHTML = `${icon("warning", "")}<span>${pictogramText(translateText(snapshot.error, options.locale), options.locale, colorBlind)}</span>`;
|
||||
guide.querySelector(".bgs-tutorial-body").hidden = false;
|
||||
const collapse = guide.querySelector(".bgs-tutorial-heading button");
|
||||
collapse.setAttribute("aria-expanded", "true");
|
||||
collapse.textContent = translateText("Hide", options.locale);
|
||||
}
|
||||
});
|
||||
}
|
||||
const offProse = tutorial.subscribe(renderProse);
|
||||
return () => {
|
||||
cancelAnimationFrame(errorFrame);
|
||||
offProse();
|
||||
|
|
|
|||
|
|
@ -171,7 +171,7 @@ export function mountGame(target, options = {}) {
|
|||
const charName = (c) => esc(CHARACTERS[c]?.["name"] ?? c);
|
||||
const charHelp = (c) => esc(characterHelp[c]?.[0] ?? "");
|
||||
const charHelpHtml = (c) =>
|
||||
`<span data-character-help="${esc(c)}" data-prose-locale="${lang}" translate="no">${pictogramText(translateText(characterHelp[c]?.[0] ?? "", lang), lang)}</span>`;
|
||||
`<span data-character-help="${esc(c)}" data-prose-locale="${lang}:${colorBlind}" translate="no">${pictogramText(translateText(characterHelp[c]?.[0] ?? "", lang), lang, colorBlind)}</span>`;
|
||||
function btn(content, attr = "", className = "") {
|
||||
return `<button type="button" class="${className}" ${attr}>${content}</button>`;
|
||||
}
|
||||
|
|
@ -509,15 +509,16 @@ export function mountGame(target, options = {}) {
|
|||
rules.dataset.presentation = rulesKey;
|
||||
}
|
||||
for (const help of dialog.querySelectorAll("[data-character-help]")) {
|
||||
if (help.dataset.proseLocale === lang) continue;
|
||||
if (help.dataset.proseLocale === rulesKey) continue;
|
||||
help.innerHTML = pictogramText(
|
||||
translateText(
|
||||
characterHelp[help.dataset.characterHelp]?.[0] ?? "",
|
||||
lang,
|
||||
),
|
||||
lang,
|
||||
colorBlind,
|
||||
);
|
||||
help.dataset.proseLocale = lang;
|
||||
help.dataset.proseLocale = rulesKey;
|
||||
}
|
||||
activity.update(s, analysis, colorBlind);
|
||||
updateUnread();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue