BGS lets the only human of a game against bots take back their last saved move (protocol 0.10.0). The top toolbar shows an undo control while BGS reports it available and the board is live (hidden in replays, analysis, to spectators, once the game is over and while a move is pending); it asks BGS to undo, which then sends the earlier position. That position resets local selections (shown colony, selected amoeba, new amoeba number), also when the moves were taken back elsewhere, and drops replay positions cached beyond it. On the narrowest phones the header tightens to fit the extra control. The chat preview emulates it locally. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
164 lines
5 KiB
JavaScript
164 lines
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, undoRequested, liveLength;
|
|
const chat = new ChatController();
|
|
const ui = mountGame(ctx.target, {
|
|
chat,
|
|
onOpenPlayer: ctx.openPlayer,
|
|
onColorBlindChange: (enabled) =>
|
|
ctx.updatePreference(COLOR_BLIND_PREFERENCE.name, enabled),
|
|
onUndo() {
|
|
undoRequested = true;
|
|
ctx.undo();
|
|
},
|
|
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);
|
|
},
|
|
onUndoAvailable(available) {
|
|
ui.setUndoAvailable(available);
|
|
},
|
|
async onState(state) {
|
|
await localization.ready;
|
|
// After "Undo my move", BGS sends the earlier position (fewer moves).
|
|
const length = state?.historyLength;
|
|
if (undoRequested || length < liveLength) ui.resetSelection();
|
|
undoRequested = false;
|
|
liveLength = length;
|
|
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) },
|
|
);
|