152 lines
4.5 KiB
JavaScript
152 lines
4.5 KiB
JavaScript
import { mountLocalization, localizeTutorial } from "./localization/index.js";
|
|
import {
|
|
createBoardThumbnail,
|
|
installPlayerCards,
|
|
} from "./host-presentation.js";
|
|
import "./style.css";
|
|
import { registerViewer } from "@boardgamers/protocol/viewer";
|
|
import { ChatController } from "@boardgamers/protocol/chat";
|
|
import { createTutorial } from "@boardgamers/protocol/tutorial";
|
|
import { mountTutorialGuide } from "@boardgamers/protocol/tutorial/dom";
|
|
import { stripSecret } from "../engine/index.js";
|
|
import { chapters } from "./tutorials.js";
|
|
import { richText } from "./rich-text.js";
|
|
import { mountGame } from "./ui.js";
|
|
import { COLOR_BLIND_PREFERENCE } from "./preferences.js";
|
|
import { createReplay } from "./replay.js";
|
|
export async function mountLesson(target, options) {
|
|
const chapter = chapters[options.chapter];
|
|
if (!chapter) throw new Error("Unknown tutorial chapter.");
|
|
let storage;
|
|
try {
|
|
storage = window.localStorage;
|
|
} catch {}
|
|
const tutorial = await createTutorial({
|
|
...chapter,
|
|
storage,
|
|
onProgress: options.onProgress,
|
|
});
|
|
const ui = mountGame(target, { onMove: (m) => tutorial.play(m) });
|
|
ui.setPlayer(0);
|
|
let previousStep;
|
|
const off = tutorial.subscribe((snapshot) => {
|
|
if (previousStep !== snapshot.step) {
|
|
ui.closeModal();
|
|
previousStep = snapshot.step;
|
|
}
|
|
ui.render(stripSecret(snapshot.state));
|
|
ui.setEnabled(
|
|
!snapshot.busy && !snapshot.completed && !snapshot.canContinue,
|
|
);
|
|
});
|
|
const removeGuide = mountTutorialGuide(ui.guide, tutorial, {
|
|
nextChapter: options.nextChapter,
|
|
});
|
|
const offLabels = tutorial.subscribe(() => {
|
|
for (const text of ui.guide.querySelectorAll(
|
|
".bgs-tutorial-body > p, .bgs-tutorial-feedback",
|
|
)) {
|
|
text.innerHTML = richText(
|
|
text.firstElementChild?.getAttribute("data-localized-rich-text") ??
|
|
text.textContent,
|
|
);
|
|
}
|
|
});
|
|
return () => {
|
|
offLabels();
|
|
off();
|
|
removeGuide();
|
|
tutorial.destroy();
|
|
ui.destroy();
|
|
};
|
|
}
|
|
registerViewer(
|
|
"primordialSoup",
|
|
(ctx) => {
|
|
const localization = mountLocalization(ctx.target);
|
|
let pending, timeout;
|
|
const chat = new ChatController();
|
|
const ui = mountGame(ctx.target, {
|
|
chat,
|
|
onOpenPlayer: ctx.openPlayer,
|
|
onColorBlindChange: (enabled) =>
|
|
ctx.updatePreference(COLOR_BLIND_PREFERENCE.name, enabled),
|
|
onMove: (m) =>
|
|
new Promise((resolve, reject) => {
|
|
pending = { resolve, reject };
|
|
timeout = setTimeout(() => {
|
|
pending = undefined;
|
|
reject(
|
|
new Error(
|
|
"Waiting for the server. Refresh if your move has not appeared.",
|
|
),
|
|
);
|
|
}, 15000);
|
|
ctx.move(m);
|
|
}),
|
|
});
|
|
const thumbnail = createBoardThumbnail(ctx.target);
|
|
const removeCards = installPlayerCards(ctx.target, ctx);
|
|
const replay = createReplay({
|
|
render(state) {
|
|
localization.setState(state);
|
|
ui.render(state);
|
|
},
|
|
setReplayMode: ui.setReplayMode,
|
|
fetchLog: ctx.fetchLog,
|
|
setReplayInfo: ctx.setReplayInfo,
|
|
});
|
|
return {
|
|
chat,
|
|
onReplayStart: replay.start,
|
|
onReplayTo: replay.to,
|
|
onReplayEnd: replay.end,
|
|
onLog({ data }) {
|
|
if (!replay.receive(data)) ctx.fetchState();
|
|
},
|
|
onThumbnail(size) {
|
|
return thumbnail.render(
|
|
ctx.target.querySelector(".board"),
|
|
size,
|
|
"#eef1e5",
|
|
".pond-tools, .board-actions",
|
|
);
|
|
},
|
|
async onPreferences(preferences) {
|
|
if (!(await localization.setLocale(preferences.locale))) return;
|
|
ui.setChatNotifications(preferences.chatNotifications !== false);
|
|
ui.setAnalysisMode(preferences.analysis === true);
|
|
ui.setColorBlind(preferences[COLOR_BLIND_PREFERENCE.name] === true);
|
|
ui.setPlayerAppearance(preferences);
|
|
},
|
|
onPlayer(player) {
|
|
ui.setPlayer(player.index);
|
|
},
|
|
async onState(state) {
|
|
await localization.ready;
|
|
replay.update(state);
|
|
if (pending) {
|
|
clearTimeout(timeout);
|
|
pending.resolve();
|
|
pending = undefined;
|
|
}
|
|
},
|
|
onError(error) {
|
|
if (pending) {
|
|
clearTimeout(timeout);
|
|
pending.reject(error);
|
|
pending = undefined;
|
|
}
|
|
},
|
|
destroy() {
|
|
localization.destroy();
|
|
removeCards();
|
|
thumbnail.destroy();
|
|
clearTimeout(timeout);
|
|
pending?.resolve();
|
|
ui.destroy();
|
|
},
|
|
};
|
|
},
|
|
{ tutorial: localizeTutorial(mountLesson) },
|
|
);
|