61 lines
2.3 KiB
JavaScript
61 lines
2.3 KiB
JavaScript
import * as E from "../engine/index.js";
|
|
|
|
// Standalone host for exercising the same replay events used by BGS.
|
|
export function mountReplayPreview(nav, players = 4) {
|
|
let state = E.init(players, [], { newGenes: true }, "replay-preview");
|
|
state.players.forEach(
|
|
(p, i) => (p.name = ["You", "Maya", "Leo", "Nina", "Sam", "Alex"][i]),
|
|
);
|
|
for (let turn = 0; !state.finished && turn < 2000; turn++)
|
|
state = E.moveAI(state, E.currentPlayer(state));
|
|
nav
|
|
.querySelectorAll("#new-genes, #new-three, #new-four, #new-five, #new-six")
|
|
.forEach((el) =>
|
|
el.id === "new-genes" ? el.closest("label").remove() : el.remove(),
|
|
);
|
|
nav.insertAdjacentHTML(
|
|
"beforeend",
|
|
`<button data-replay="start">Replay</button><button data-replay="first" aria-label="First position">⏮</button><button data-replay="previous" aria-label="Previous position">◀</button><span data-replay-position></span><button data-replay="next" aria-label="Next position">▶</button><button data-replay="last" aria-label="Last position">⏭</button><button data-replay="end">Close replay</button>`,
|
|
);
|
|
const emitter = window.primordialSoup.launch("#game");
|
|
let info;
|
|
emitter.on("replay:info", (value) => {
|
|
info = value;
|
|
nav.querySelector("[data-replay-position]").textContent =
|
|
`${value.current} / ${value.end}`;
|
|
for (const name of ["first", "previous", "next", "last"]) {
|
|
nav.querySelector(`[data-replay="${name}"]`).disabled = [
|
|
"first",
|
|
"previous",
|
|
].includes(name)
|
|
? value.current === value.start
|
|
: value.current === value.end;
|
|
}
|
|
});
|
|
emitter.on("ready", () => {
|
|
emitter.emit("replay:start");
|
|
emitter.emit("replay:to", players * 3);
|
|
});
|
|
nav.addEventListener("click", (event) => {
|
|
const command = event.target.closest("[data-replay]")?.dataset.replay;
|
|
if (command === "start" || command === "end")
|
|
emitter.emit(`replay:${command}`);
|
|
else if (command && info)
|
|
emitter.emit(
|
|
"replay:to",
|
|
{
|
|
first: info.start,
|
|
previous: info.current - 1,
|
|
next: info.current + 1,
|
|
last: info.end,
|
|
}[command],
|
|
);
|
|
});
|
|
emitter.emit("player", { index: 0 });
|
|
emitter.emit("chat:state", {
|
|
canSend: false,
|
|
reason: "not-a-player",
|
|
mentions: [],
|
|
});
|
|
emitter.emit("state", state);
|
|
}
|