puerto-rico/viewer/platform.js

130 lines
4.1 KiB
JavaScript

import { stripSecret } from "../engine/index.js";
/** Host preferences arrive separately from theme changes. Preserve seat appearance. */
export function mergePreferences(previous, update) {
return { ...previous, ...update };
}
/** Avatars and supporter badges are images supplied by the platform. */
export function imageUrl(value) {
if (typeof value !== "string" || !value.trim()) return "";
if (/^https?:\/\//i.test(value) || /^\/(?!\/)/.test(value)) return value;
return "";
}
/** The same state/move bridge serves live games and host-managed analysis branches. */
export function viewerLifecycle(ctx, ui, chat) {
let live,
replaying = false,
position = 1,
playerIndex,
pending,
timer;
const settle = (error) => {
clearTimeout(timer);
if (error) pending?.reject(error);
else pending?.resolve();
pending = undefined;
};
const info = () => {
if (live)
ctx.setReplayInfo({
start: 1,
current: position,
end: Math.max(1, live.historyLength),
});
};
const seek = (to) => {
if (!live || !replaying) return;
position = Math.max(1, Math.min(Math.max(1, live.historyLength), to));
ctx.fetchLog({ start: position - 1, end: position - 1 });
info();
};
return {
move(m) {
if (replaying || pending)
return Promise.reject(Error("Move unavailable"));
return new Promise((resolve, reject) => {
pending = {
resolve,
reject,
move: m,
player: playerIndex,
historyLength: live?.historyLength,
};
timer = setTimeout(() => {
settle(Error("No confirmation received. Please try again."));
ctx.fetchState();
}, 15000);
if (ctx.move(m) === false) settle(Error("Move rejected"));
});
},
chat,
onState(s) {
// BGS returns the raw saved engine state after the game ends.
if (s.finished && Array.isArray(s.history))
s = stripSecret(s, playerIndex);
live = s;
const awaitingMove =
pending &&
((["recruit", "recruitBonus"].includes(pending.move.type) &&
Number.isInteger(pending.historyLength) &&
s.historyLength <= pending.historyLength) ||
(pending.move.type === "assign" &&
s.tasks.some(
(t) => t.kind === "assign" && t.p === pending.player,
)) ||
(pending.move.type === "orderedPlacement" && !s.placementOrdered));
if (!replaying) {
position = Math.max(1, s.historyLength);
ui.render(s, !!awaitingMove);
} else {
// An undo or branch replacement may shorten the host history.
if (position > s.historyLength) seek(s.historyLength);
info();
}
if (!awaitingMove) settle();
},
onThumbnail: () => ui.thumbnail(),
onPlayer: (p) => {
playerIndex = p.index;
ui.setPlayer(p.index);
},
onAvatars: (avatars) => ui.setAvatars(avatars),
onPreferences: (p) => ui.setPreferences(p),
onTheme: (p) => ui.setPreferences(p),
onError: (error) => settle(Error(String(error))),
onMoveResult(result) {
// An acknowledgement can precede the authoritative state update.
if (result.ok) {
if (pending) ctx.fetchState();
} else {
settle(Error(result.error || "Move rejected"));
ctx.fetchState();
}
},
onReplayStart() {
replaying = true;
ui.setEnabled(false);
info();
},
onReplayTo: seek,
onReplayEnd() {
replaying = false;
position = Math.max(1, live?.historyLength ?? 1);
ui.setEnabled(true);
if (live) ui.render(live);
},
onLog(log) {
// Analysis returns full history from zero, while live replay requests
// return a slice. Only render a response containing the requested index;
// late responses for a different seek must not replace the current frame.
const frame = log.data?.frames?.[position - 1 - log.start];
if (replaying && frame) ui.render(frame);
else if (!replaying) ctx.fetchState();
},
destroy() {
settle(Error("Viewer closed"));
ui.destroy();
},
};
}