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>
188 lines
6.1 KiB
JavaScript
188 lines
6.1 KiB
JavaScript
import * as E from "../engine/index.js";
|
|
import { chatSegments } from "@boardgamers/protocol/chat";
|
|
import { prepared } from "./tutorials.js";
|
|
import {
|
|
COLOR_BLIND_PREFERENCE,
|
|
readLocalColorBlindMode,
|
|
saveLocalColorBlindMode,
|
|
} from "./preferences.js";
|
|
|
|
export function mountChatPreview(nav) {
|
|
let state = prepared({ round: 3 });
|
|
state.log = [
|
|
{ round: 2, phase: 0, text: "You #1 swims for 1 BP." },
|
|
{ round: 2, phase: 0, text: "You #1 rolls 2 and 3." },
|
|
{ round: 2, phase: 0, text: "You #1 chooses east." },
|
|
{
|
|
round: 2,
|
|
phase: 0,
|
|
text: "You #1 carries 2 blue nutrients, 1 red nutrient from C2 to D2 with Tentacle.",
|
|
},
|
|
{ round: 2, phase: 0, text: "You #1 moves to D2." },
|
|
{ round: 2, phase: 0, text: "Leo #1 moves to D2.", genes: ["holding"] },
|
|
{
|
|
round: 2,
|
|
phase: 0,
|
|
text: "You #1 feeds on 1 red nutrient, 1 blue nutrient, 1 yellow nutrient at D2.",
|
|
},
|
|
{ round: 2, phase: 0, text: "You #1 excretes two green nutrients at D2." },
|
|
{ round: 2, phase: 0, text: "Maya #2 moves to B4." },
|
|
{ round: 2, phase: 1, text: "The current is west. Ozone layer: 12." },
|
|
{
|
|
round: 2,
|
|
phase: 1,
|
|
text: "Maya discards Speed; remaining deficit: 0 BP.",
|
|
},
|
|
{ round: 2, phase: 2, text: "Leo returns Movement I to the gene pool." },
|
|
{ round: 2, phase: 2, text: "Leo buys Movement II for 5 BP." },
|
|
{ round: 2, phase: 2, text: "Leo finishes buying genes." },
|
|
{ round: 2, phase: 2, text: "Nina didn't buy any genes." },
|
|
{
|
|
round: 2,
|
|
phase: 4,
|
|
text: "You #1 attacks Maya #1 with Aggression for 1 BP.",
|
|
},
|
|
{ round: 2, phase: 4, text: "Maya #1 uses Defense for 1 BP." },
|
|
{ round: 2, phase: 4, text: "You #1 rolls 3; Maya #1 rolls 5." },
|
|
{ round: 2, phase: 4, text: "You #1 fails to attack Maya #1." },
|
|
{ round: 3, phase: 0, text: "You #2 moves to B5." },
|
|
{
|
|
round: 3,
|
|
phase: 0,
|
|
text: "You #2 takes 1 blue nutrient from C5 with Suction I.",
|
|
},
|
|
{
|
|
round: 3,
|
|
phase: 0,
|
|
text: "You #2 feeds on 1 red nutrient, 1 blue nutrient, 1 yellow nutrient at B5.",
|
|
},
|
|
{ round: 3, phase: 0, text: "You #2 excretes two green nutrients at B5." },
|
|
];
|
|
let sequence = 0;
|
|
let failNext = false;
|
|
let spectator = false;
|
|
const emitter = window.primordialSoup.launch("#game");
|
|
emitter.on("update:preference", ({ name, value }) => {
|
|
if (name === COLOR_BLIND_PREFERENCE.name) {
|
|
saveLocalColorBlindMode(value === true);
|
|
emitter.emit("preferences", { [name]: value === true });
|
|
}
|
|
});
|
|
emitter.emit("preferences", {
|
|
[COLOR_BLIND_PREFERENCE.name]: readLocalColorBlindMode(),
|
|
});
|
|
const people = state.players.map((p, i) => ({
|
|
id: String(i),
|
|
name: p.name,
|
|
playerIndex: i,
|
|
}));
|
|
const names = new Map(people.map((p) => [p.id, p.name]));
|
|
const status = document.createElement("span");
|
|
status.setAttribute("role", "status");
|
|
status.textContent = "Local chat · messages stay here";
|
|
function message(playerIndex, text, ago = 0) {
|
|
return {
|
|
_id: (++sequence).toString(16).padStart(24, "0"),
|
|
type: "text",
|
|
author: state.players[playerIndex].name,
|
|
playerIndex,
|
|
text,
|
|
segments: chatSegments(text, names, true),
|
|
createdAt: new Date(Date.now() - ago).toISOString(),
|
|
};
|
|
}
|
|
function permissions() {
|
|
emitter.emit("player", { index: spectator ? undefined : 0 });
|
|
emitter.emit("chat:state", {
|
|
canSend: !spectator,
|
|
reason: spectator ? "not-a-player" : "",
|
|
mentions: people,
|
|
});
|
|
}
|
|
emitter.on("chat:send", ({ text, requestId }) => {
|
|
setTimeout(() => {
|
|
if (failNext) {
|
|
failNext = false;
|
|
emitter.emit("chat:result", {
|
|
requestId,
|
|
ok: false,
|
|
error: "Could not send. Try again.",
|
|
});
|
|
status.textContent = "Send failed; draft kept";
|
|
} else {
|
|
emitter.emit("chat:appended", [message(0, text)]);
|
|
emitter.emit("chat:result", { requestId, ok: true });
|
|
status.textContent = "Message sent locally";
|
|
}
|
|
}, 300);
|
|
});
|
|
emitter.on("chat:read", () => {
|
|
status.textContent = "Read receipt received locally";
|
|
});
|
|
emitter.on("player:clicked", ({ index }) => {
|
|
status.textContent = `Player link: ${state.players[index].name}`;
|
|
});
|
|
// Like BGS for the only human of a game against bots, "Undo my move" brings
|
|
// back the position before each of their moves.
|
|
const undoStates = [];
|
|
emitter.on("move", (move) => {
|
|
const before = state;
|
|
state = E.move(state, move, 0);
|
|
undoStates.push(before);
|
|
emitter.emit("state", E.stripSecret(state));
|
|
emitter.emit("undo:available", true);
|
|
});
|
|
emitter.on("undo", () => {
|
|
if (!undoStates.length) return;
|
|
state = undoStates.pop();
|
|
emitter.emit("state", E.stripSecret(state));
|
|
emitter.emit("undo:available", undoStates.length > 0);
|
|
status.textContent = "Move taken back locally";
|
|
});
|
|
const controls = [
|
|
[
|
|
"Receive message",
|
|
() =>
|
|
emitter.emit("chat:appended", [
|
|
message(1, "@You Ready for the next round?"),
|
|
]),
|
|
],
|
|
[
|
|
"Fail next send",
|
|
() => {
|
|
failNext = true;
|
|
status.textContent = "Next send will fail locally";
|
|
},
|
|
],
|
|
[
|
|
"Toggle spectator",
|
|
() => {
|
|
spectator = !spectator;
|
|
permissions();
|
|
},
|
|
],
|
|
["Refresh position", () => emitter.emit("state", E.stripSecret(state))],
|
|
];
|
|
for (const [label, click] of controls) {
|
|
const button = document.createElement("button");
|
|
button.textContent = label;
|
|
button.onclick = click;
|
|
nav.append(button);
|
|
}
|
|
nav.append(status);
|
|
permissions();
|
|
emitter.emit("state", E.stripSecret(state));
|
|
emitter.emit("chat:messages", [
|
|
message(1, "Good luck, everyone!", 86400000),
|
|
message(2, "I like the little amoebas.", 86300000),
|
|
message(0, "Trying Movement I this time.", 86200000),
|
|
message(3, "@Maya The gene pool explains each ability.", 400000),
|
|
message(1, "Thanks! Saving some BP for division.", 300000),
|
|
message(2, "The current is going east this round.", 200000),
|
|
message(
|
|
3,
|
|
"Rules: https://boardgamers.space/page/primordial-soup/rules",
|
|
100000,
|
|
),
|
|
]);
|
|
}
|