Offer "Undo my move" in games against bots
BGS lets the only human of a game against bots take back their last saved move (protocol 0.10.0, `undo:available` / `undo`). The header strip shows an undo control while BGS offers it and the player follows the live game: it is hidden for spectators, during replays and analysis, once the game is over and while a sent move still awaits the host's answer. When BGS sends the earlier position, purchases staged after it are dropped, selections are reset and the automatic production choice is not submitted again for that round. The dev harness keeps undo points like BGS. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
69234af9a5
commit
7f63fe6e65
27 changed files with 301 additions and 31 deletions
|
|
@ -21,7 +21,8 @@
|
|||
"preview:tutorial": "node tests/tutorial-preview.mjs",
|
||||
"test:locales": "node scripts/check-locales.mjs packages/viewer/src/localization && node --test scripts/localization.test.mjs",
|
||||
"test:scroll": "node tests/mobile-scroll.mjs",
|
||||
"test:bulk": "node tests/bulk-purchase-browser.mjs"
|
||||
"test:bulk": "node tests/bulk-purchase-browser.mjs",
|
||||
"test:undo": "node tests/undo-browser.mjs"
|
||||
},
|
||||
"devDependencies": {
|
||||
"oxfmt": "0.61.0",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "outpost-viewer",
|
||||
"version": "0.1.23",
|
||||
"version": "0.1.24",
|
||||
"description": "Browser viewer for the Outpost game on boardgamers.space",
|
||||
"license": "AGPL-3.0",
|
||||
"files": [
|
||||
|
|
@ -16,7 +16,7 @@
|
|||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@boardgamers/protocol": "0.8.1",
|
||||
"@boardgamers/protocol": "0.10.0",
|
||||
"outpost-engine": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@
|
|||
<div class="board">
|
||||
<div class="page">
|
||||
{#if __DEV__}<ReplayBar {store} />{/if}
|
||||
<HeaderStrip {state} />
|
||||
<HeaderStrip {state} {store} />
|
||||
<GameEndBanner {store} />
|
||||
|
||||
<div class="columns">
|
||||
|
|
|
|||
|
|
@ -52,12 +52,16 @@ export function startDevBackend(emitter: ViewerEmitter<GameState, Move>, options
|
|||
|
||||
const human = 0;
|
||||
let ended = false;
|
||||
// Like BGS in a game against bots: the saved state before each human move is an undo point.
|
||||
const undoPoints: string[] = [];
|
||||
let botTimer: number | undefined;
|
||||
|
||||
function publish(): void {
|
||||
emitter.emit("state", stripSecret(state, human));
|
||||
emitter.emit("player", { index: human });
|
||||
emitter.emit("settings", playerSettings(state, human));
|
||||
emitter.emit("avatars", AVATARS.slice(0, playerCount));
|
||||
emitter.emit("undo:available", undoPoints.length > 0 && !state.ended);
|
||||
if (state.ended && !ended) {
|
||||
ended = true;
|
||||
return;
|
||||
|
|
@ -78,7 +82,7 @@ export function startDevBackend(emitter: ViewerEmitter<GameState, Move>, options
|
|||
if (seat === undefined) {
|
||||
return;
|
||||
}
|
||||
window.setTimeout(() => {
|
||||
botTimer = window.setTimeout(() => {
|
||||
if (state.ended) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -127,6 +131,7 @@ export function startDevBackend(emitter: ViewerEmitter<GameState, Move>, options
|
|||
if (state.ended) {
|
||||
return;
|
||||
}
|
||||
const before = JSON.stringify(state);
|
||||
try {
|
||||
state = applyMove(state, move, human);
|
||||
} catch (error) {
|
||||
|
|
@ -134,6 +139,17 @@ export function startDevBackend(emitter: ViewerEmitter<GameState, Move>, options
|
|||
publish();
|
||||
return;
|
||||
}
|
||||
undoPoints.push(before);
|
||||
publish();
|
||||
});
|
||||
|
||||
emitter.on("undo", () => {
|
||||
const previous = state.ended ? undefined : undoPoints.pop();
|
||||
if (previous === undefined) {
|
||||
return;
|
||||
}
|
||||
window.clearTimeout(botTimer);
|
||||
state = JSON.parse(previous) as GameState;
|
||||
publish();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
<script lang="ts">
|
||||
import { VICTORY_VP, MID_THRESHOLD, bigThreshold, scores, colonyEra, type GameState } from "outpost-engine";
|
||||
import type { ViewerStore } from "./store.svelte";
|
||||
|
||||
interface Props {
|
||||
state: GameState;
|
||||
store: ViewerStore;
|
||||
}
|
||||
|
||||
let { state }: Props = $props();
|
||||
let { state, store }: Props = $props();
|
||||
|
||||
const era = $derived(state.ended ? null : (state.era ?? colonyEra(state)));
|
||||
|
||||
|
|
@ -58,6 +60,29 @@
|
|||
<span class="item dim" title="Finish the round when someone reaches 75 VP. The player with the most VP wins.">
|
||||
{VICTORY_VP} VP · finish the round
|
||||
</span>
|
||||
{#if store.canUndoMove}
|
||||
<button
|
||||
class="undo-move"
|
||||
type="button"
|
||||
title="Undo my move"
|
||||
aria-label="Undo my move"
|
||||
onclick={() => store.undoMove()}
|
||||
>
|
||||
<svg
|
||||
viewBox="0 0 20 20"
|
||||
width="18"
|
||||
height="18"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="1.8"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<path d="M7 3.5 3 7.5l4 4M3 7.5h9.5a4.5 4.5 0 0 1 0 9H9" />
|
||||
</svg>
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
|
|
@ -121,4 +146,26 @@
|
|||
color: var(--text-dim);
|
||||
margin-left: auto;
|
||||
}
|
||||
/* Negative block margins keep the strip's height when the control comes and goes. */
|
||||
.undo-move {
|
||||
position: relative;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
margin: -6px -6px -6px 0;
|
||||
padding: 0;
|
||||
color: var(--text-mid);
|
||||
}
|
||||
.undo-move:hover {
|
||||
color: var(--text);
|
||||
}
|
||||
@media (any-pointer: coarse) {
|
||||
.undo-move::before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
inset: -7px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
|
|||
|
|
@ -239,6 +239,10 @@ export class ViewerStore {
|
|||
exchangeTarget = $state<number | null>(null);
|
||||
/** One auto card-suggestion per context (payment due, discard); reset by cancel(). */
|
||||
autoSuggested = $state(false);
|
||||
/** BGS lets the only human of a game against bots take back their last saved move. */
|
||||
undoAvailable = $state(false);
|
||||
/** A move was sent and the host has not answered yet (move:result or a new state). */
|
||||
moveInFlight = $state(false);
|
||||
|
||||
// Purchases staged this turn. They are only sent to the server as part of
|
||||
// the endTurn move (the whole turn is one move); until then `draft` holds
|
||||
|
|
@ -293,9 +297,9 @@ export class ViewerStore {
|
|||
this.send({ action: "mega", take: this.autoMega === "maximum" ? this.megaEligible : {} });
|
||||
}
|
||||
|
||||
get canEditSettings(): boolean {
|
||||
/** A seated player following the live game (not a spectator, replay or analysis). */
|
||||
get playingLive(): boolean {
|
||||
return (
|
||||
this.settings !== null &&
|
||||
!!this.me &&
|
||||
!this.me.dropped &&
|
||||
!this.liveState?.ended &&
|
||||
|
|
@ -304,6 +308,20 @@ export class ViewerStore {
|
|||
);
|
||||
}
|
||||
|
||||
get canEditSettings(): boolean {
|
||||
return this.settings !== null && this.playingLive;
|
||||
}
|
||||
|
||||
get canUndoMove(): boolean {
|
||||
return this.undoAvailable && !this.moveInFlight && this.playingLive;
|
||||
}
|
||||
|
||||
undoMove(): void {
|
||||
if (this.canUndoMove) {
|
||||
this.commands.undo();
|
||||
}
|
||||
}
|
||||
|
||||
setAutoPassBids(value: boolean): void {
|
||||
if (this.canEditSettings && this.commands.updateSetting("autoPassBids", value)) {
|
||||
this.settings = { ...this.settings, autoPassBids: value };
|
||||
|
|
@ -318,6 +336,7 @@ export class ViewerStore {
|
|||
}
|
||||
|
||||
setState(state: GameState): void {
|
||||
const rewound = !!this.liveState && state.log.length < this.liveState.log.length;
|
||||
this.liveState = state;
|
||||
this.logEntries = [...state.log];
|
||||
this.seenLog = state.log.length;
|
||||
|
|
@ -326,6 +345,13 @@ export class ViewerStore {
|
|||
this.cancel();
|
||||
this.lastMoveAt = Date.now();
|
||||
}
|
||||
if (rewound) {
|
||||
// An undo brought back an earlier position: purchases staged after it no longer
|
||||
// apply, and re-submitting the automatic production choice would redo the move.
|
||||
this.turnBuys = [];
|
||||
this.automaticMegaRound = `${this.playerIndex}:${state.round}`;
|
||||
}
|
||||
this.moveInFlight = false;
|
||||
this.rebuildDraft();
|
||||
this.commands.replaceLog([...this.logLines]);
|
||||
}
|
||||
|
|
@ -1140,11 +1166,12 @@ export class ViewerStore {
|
|||
if (this.changingChoice && (move.action === "mega" || move.action === "bid" || move.action === "bidPass")) {
|
||||
move = { ...move, revision: this.editingChoice };
|
||||
}
|
||||
this.commands.move($state.snapshot(move));
|
||||
const sent = this.commands.move($state.snapshot(move));
|
||||
this.cancel();
|
||||
if (this.optimistic) {
|
||||
this.applyOptimistic(move);
|
||||
}
|
||||
this.moveInFlight = sent;
|
||||
}
|
||||
|
||||
// Apply my own move to the local (stripped) state right away so the UI feels
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} {p1}-fabrik",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} tabt (ingen byttepenge).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "Disse kort kan købe {p0}. Køb færre alligevel?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "Køb {p0} alligevel · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "Køb {p0} alligevel · ◈ {p1}",
|
||||
"Undo my move": "Fortryd mit træk"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} {p1}-Fabrik",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} verloren (kein Wechselgeld).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "Diese Karten reichen für {p0}. Trotzdem weniger kaufen?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "{p0} trotzdem kaufen · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "{p0} trotzdem kaufen · ◈ {p1}",
|
||||
"Undo my move": "Zug zurücknehmen"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} εργοστάσιο {p1}",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} χάνονται (χωρίς ρέστα).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "Αυτές οι κάρτες μπορούν να αγοράσουν {p0}. Αγορά λιγότερων ούτως ή άλλως;",
|
||||
"Buy {p0} anyway · ◈ {p1}": "Αγορά {p0} ούτως ή άλλως · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "Αγορά {p0} ούτως ή άλλως · ◈ {p1}",
|
||||
"Undo my move": "Αναίρεση κίνησης"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} {p1} factory",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} lost (no change).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "These cards can buy {p0}. Buy fewer anyway?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "Buy {p0} anyway · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "Buy {p0} anyway · ◈ {p1}",
|
||||
"Undo my move": "Undo my move"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} کارخانهٔ {p1}",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} از دست میرود (بدون باقیمانده).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "این کارتها برای خرید {p0} کافیاند. باز هم کمتر بخرید؟",
|
||||
"Buy {p0} anyway · ◈ {p1}": "به هر حال {p0} بخرید · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "به هر حال {p0} بخرید · ◈ {p1}",
|
||||
"Undo my move": "بازگرداندن حرکت"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} usine {p1}",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} perdus (pas de monnaie).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "Ces cartes peuvent acheter {p0}. En acheter moins quand même ?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "Acheter {p0} quand même · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "Acheter {p0} quand même · ◈ {p1}",
|
||||
"Undo my move": "Annuler mon coup"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} {p1} कारखाना",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} खोए (बाकी नहीं मिलता)।",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "इन कार्डों से {p0} खरीद सकते हैं। फिर भी कम खरीदें?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "फिर भी {p0} खरीदें · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "फिर भी {p0} खरीदें · ◈ {p1}",
|
||||
"Undo my move": "अपनी चाल वापस लें"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} fabbrica {p1}",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} persi (nessun resto).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "Queste carte possono comprare {p0}. Comprarne comunque meno?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "Compra comunque {p0} · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "Compra comunque {p0} · ◈ {p1}",
|
||||
"Undo my move": "Annulla la mossa"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p1} 공장 {p0}개",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} 손실 (거스름돈 없음).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "이 카드로 {p0} 구매 가능. 그래도 더 적게 구매할까요?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "그래도 {p0} 구매 · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "그래도 {p0} 구매 · ◈ {p1}",
|
||||
"Undo my move": "내 수 되돌리기"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} {p1}-fabriek",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} verloren (geen wisselgeld).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "Deze kaarten kunnen {p0} kopen. Toch minder kopen?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "Koop toch {p0} · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "Koop toch {p0} · ◈ {p1}",
|
||||
"Undo my move": "Zet ongedaan maken"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} fabryka {p1}",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} stracone (bez reszty).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "Te karty wystarczą na {p0}. Mimo to kupić mniej?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "Kup {p0} mimo to · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "Kup {p0} mimo to · ◈ {p1}",
|
||||
"Undo my move": "Cofnij ruch"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} fábrica de {p1}",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} perdidos (sem troco).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "Estas cartas podem comprar {p0}. Comprar menos mesmo assim?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "Comprar {p0} mesmo assim · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "Comprar {p0} mesmo assim · ◈ {p1}",
|
||||
"Undo my move": "Desfazer jogada"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} fabrică {p1}",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} pierdute (fără rest).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "Aceste cărți pot cumpăra {p0}. Cumperi totuși mai puțin?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "Cumpără totuși {p0} · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "Cumpără totuși {p0} · ◈ {p1}",
|
||||
"Undo my move": "Anulează mutarea"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} фабрика {p1}",
|
||||
"◈ {p0} lost (no change).": "◈ {p0} потеряно (без сдачи).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "Этих карт хватит на {p0}. Всё равно купить меньше?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "Всё равно купить {p0} · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "Всё равно купить {p0} · ◈ {p1}",
|
||||
"Undo my move": "Отменить ход"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} nhà máy {p1}",
|
||||
"◈ {p0} lost (no change).": "Mất ◈ {p0} (không trả lại tiền thừa).",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "Các thẻ này có thể mua {p0}. Vẫn mua ít hơn?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "Vẫn mua {p0} · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "Vẫn mua {p0} · ◈ {p1}",
|
||||
"Undo my move": "Hoàn tác nước đi"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1212,5 +1212,6 @@
|
|||
"{p0} {p1} factory": "{p0} 座{p1}工廠",
|
||||
"◈ {p0} lost (no change).": "損失 ◈ {p0}(不找零)。",
|
||||
"These cards can buy {p0}. Buy fewer anyway?": "這些卡牌可購買 {p0}。仍要買更少嗎?",
|
||||
"Buy {p0} anyway · ◈ {p1}": "仍然購買 {p0} · ◈ {p1}"
|
||||
"Buy {p0} anyway · ◈ {p1}": "仍然購買 {p0} · ◈ {p1}",
|
||||
"Undo my move": "撤回我的行動"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@ export const mountTutorial: TutorialMount = async (target, { chapter, onProgress
|
|||
addLog: () => false,
|
||||
replaceLog: () => false,
|
||||
setReplayInfo: () => false,
|
||||
undo: () => false,
|
||||
},
|
||||
false
|
||||
);
|
||||
|
|
|
|||
|
|
@ -34,6 +34,9 @@ registerViewer<GameState, Move>(
|
|||
store.setState(state);
|
||||
await tick();
|
||||
},
|
||||
onMoveResult() {
|
||||
store.moveInFlight = false;
|
||||
},
|
||||
onPlayer({ index }) {
|
||||
store.setPlayer(index);
|
||||
},
|
||||
|
|
@ -50,6 +53,9 @@ registerViewer<GameState, Move>(
|
|||
onSettings(settings) {
|
||||
store.settings = settings;
|
||||
},
|
||||
onUndoAvailable(available) {
|
||||
store.undoAvailable = available;
|
||||
},
|
||||
onLog(log) {
|
||||
store.onGamelog(log);
|
||||
},
|
||||
|
|
|
|||
10
pnpm-lock.yaml
generated
10
pnpm-lock.yaml
generated
|
|
@ -40,8 +40,8 @@ importers:
|
|||
packages/viewer:
|
||||
dependencies:
|
||||
'@boardgamers/protocol':
|
||||
specifier: 0.8.1
|
||||
version: 0.8.1
|
||||
specifier: 0.10.0
|
||||
version: 0.10.0
|
||||
outpost-engine:
|
||||
specifier: workspace:*
|
||||
version: link:../engine
|
||||
|
|
@ -67,8 +67,8 @@ importers:
|
|||
|
||||
packages:
|
||||
|
||||
'@boardgamers/protocol@0.8.1':
|
||||
resolution: {integrity: sha512-BZNj+wof1aR1nSybRMqmQ+yAVNR3ZZy+mczIz2KbXSLXMLL3Jv21yi50CRbJ2ipdkPiPatcSP0nJj+xJbFgIfQ==}
|
||||
'@boardgamers/protocol@0.10.0':
|
||||
resolution: {integrity: sha512-U+nOK8xeQANvjzwacHirBW+ItDf9AOmHLtkoyTI/XYFCOpL6lPsEgCbmcxnmkXv2a1/lgtVN3WpSERHUqkQkXw==}
|
||||
|
||||
'@jridgewell/gen-mapping@0.3.13':
|
||||
resolution: {integrity: sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==}
|
||||
|
|
@ -772,7 +772,7 @@ packages:
|
|||
|
||||
snapshots:
|
||||
|
||||
'@boardgamers/protocol@0.8.1':
|
||||
'@boardgamers/protocol@0.10.0':
|
||||
dependencies:
|
||||
zod: 4.6.2
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
packages:
|
||||
- "packages/*"
|
||||
minimumReleaseAgeExclude:
|
||||
- "@boardgamers/protocol@0.3.1 || 0.3.3 || 0.4.0 || 0.4.2 || 0.5.0 || 0.6.0 || 0.6.1 || 0.6.2 || 0.8.1"
|
||||
- "@boardgamers/protocol@0.3.1 || 0.3.3 || 0.4.0 || 0.4.2 || 0.5.0 || 0.6.0 || 0.6.1 || 0.6.2 || 0.8.1 || 0.10.0"
|
||||
|
|
|
|||
156
tests/undo-browser.mjs
Normal file
156
tests/undo-browser.mjs
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
import assert from "node:assert/strict";
|
||||
import { chromium } from "playwright";
|
||||
import { previewServer } from "./tutorial-preview.mjs";
|
||||
import { applyMove, chooseMove, initGame } from "../packages/engine/dist/index.js";
|
||||
import { currentPlayer, stripSecret } from "../packages/engine/dist/wrapper.js";
|
||||
|
||||
const hand = [
|
||||
{ t: "ore", v: 5 },
|
||||
{ t: "ore", v: 6 },
|
||||
{ t: "water", v: 12 },
|
||||
{ t: "water", v: 9 },
|
||||
];
|
||||
const earlier = initGame(3, {}, "undo-browser");
|
||||
let later = structuredClone(earlier);
|
||||
do {
|
||||
const current = currentPlayer(later);
|
||||
const seat = Array.isArray(current) ? current[0] : current;
|
||||
later = applyMove(later, chooseMove(later, seat), seat);
|
||||
} while (!(later.phase === "actions" && later.activeSeat === 0 && later.round > earlier.round));
|
||||
// The same hand in both positions, so purchases staged later would still be payable after an undo.
|
||||
earlier.players[0].hand = structuredClone(hand);
|
||||
later.players[0].hand = structuredClone(hand);
|
||||
|
||||
const mega = initGame(3, {}, "undo-browser");
|
||||
mega.phase = "mega";
|
||||
mega.players[0].pendingMega = [9, 10, 11, 12].map((v) => ({ t: "water", v }));
|
||||
mega.players[0].megaGroups = { water: 1 };
|
||||
const megaChosen = applyMove(structuredClone(mega), { action: "mega", take: { water: 1 } }, 0);
|
||||
|
||||
async function launch(page, settings) {
|
||||
await page.evaluate((settings) => {
|
||||
window.emitter = outpost.launch("#app");
|
||||
window.undoRequests = 0;
|
||||
window.sentMoves = [];
|
||||
emitter.on("undo", () => undoRequests++);
|
||||
emitter.on("move", (move) => sentMoves.push(move));
|
||||
emitter.emit("preferences", { sound: false });
|
||||
emitter.emit("player", { index: 0 });
|
||||
emitter.emit("settings", settings);
|
||||
}, settings);
|
||||
}
|
||||
|
||||
async function show(page, state, seat = 0) {
|
||||
await page.evaluate((state) => emitter.emit("state", state), stripSecret(state, seat));
|
||||
const strip = page.locator(".strip");
|
||||
await strip.getByText(`Round ${state.round}`, { exact: true }).waitFor();
|
||||
await strip
|
||||
.getByText(state.ended ? "Game over" : state.phase === "mega" ? "Production" : "Actions", { exact: true })
|
||||
.waitFor();
|
||||
}
|
||||
|
||||
async function checkUndoControl(page) {
|
||||
await launch(page, { autoPassBids: false, autoMega: "ask" });
|
||||
await show(page, later);
|
||||
const undo = page.getByRole("button", { name: "Undo my move", exact: true });
|
||||
const strip = page.locator(".strip");
|
||||
const stripHeight = (await strip.boundingBox()).height;
|
||||
assert.equal(await undo.count(), 0, "hidden until BGS offers undo");
|
||||
await page.evaluate(() => emitter.emit("undo:available", true));
|
||||
await undo.waitFor();
|
||||
assert.equal(await undo.getAttribute("title"), "Undo my move");
|
||||
assert.equal((await strip.boundingBox()).height, stripHeight, "the header keeps its height");
|
||||
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true);
|
||||
|
||||
await page.locator(".buy.res-ore").click();
|
||||
await page.locator(".purchase-flow .confirm").click();
|
||||
await page.getByText(/Staged this turn: 1 Ore factory/).waitFor();
|
||||
await page.locator(".buy.res-water").click();
|
||||
assert.equal(await page.locator(".pcard.selected").count(), 2);
|
||||
await undo.click();
|
||||
assert.equal(await page.evaluate(() => undoRequests), 1);
|
||||
assert.deepEqual(await page.evaluate(() => sentMoves), []);
|
||||
|
||||
await show(page, earlier);
|
||||
assert.equal(
|
||||
await page.getByText(/Staged this turn/).count(),
|
||||
0,
|
||||
"purchases staged after the undone move are dropped"
|
||||
);
|
||||
assert.equal(await page.locator(".purchase-flow").count(), 0);
|
||||
assert.equal(await page.locator(".pcard.selected").count(), 0);
|
||||
assert.equal(await page.locator(".actionbar .cash").textContent(), "◈ 32");
|
||||
assert.equal(await undo.count(), 1, "earlier moves can be taken back too");
|
||||
|
||||
await page.evaluate(() => emitter.emit("replay:start"));
|
||||
await undo.waitFor({ state: "hidden" });
|
||||
await page.evaluate(() => emitter.emit("replay:end"));
|
||||
await undo.waitFor();
|
||||
await page.evaluate(() => emitter.emit("preferences", { sound: false, analysis: true }));
|
||||
await undo.waitFor({ state: "hidden" });
|
||||
await page.evaluate(() => emitter.emit("preferences", { sound: false }));
|
||||
await undo.waitFor();
|
||||
|
||||
await page.getByRole("button", { name: "Assign operators…", exact: true }).click();
|
||||
await page.getByRole("button", { name: "End turn", exact: true }).click();
|
||||
const [move] = await page.evaluate(() => sentMoves);
|
||||
assert.equal(move.action, "endTurn");
|
||||
await undo.waitFor({ state: "hidden" });
|
||||
await page.evaluate((move) => emitter.emit("move:result", { move, ok: true }), move);
|
||||
await undo.waitFor();
|
||||
|
||||
await page.evaluate(() => emitter.emit("player", {}));
|
||||
await show(page, later, -1);
|
||||
assert.equal(await undo.count(), 0, "spectators cannot undo");
|
||||
await page.evaluate(() => emitter.emit("player", { index: 0 }));
|
||||
await show(page, { ...later, ended: true });
|
||||
assert.equal(await undo.count(), 0, "hidden once the game is over");
|
||||
await show(page, later);
|
||||
await undo.waitFor();
|
||||
|
||||
await page.evaluate(() => emitter.emit("preferences", { locale: "fr", sound: false }));
|
||||
await page.locator('.undo-move[title="Annuler mon coup"][aria-label="Annuler mon coup"]').waitFor();
|
||||
await page.evaluate(() => emitter.emit("preferences", { locale: "en", sound: false }));
|
||||
await undo.waitFor();
|
||||
await page.evaluate(() => emitter.emit("undo:available", false));
|
||||
await undo.waitFor({ state: "hidden" });
|
||||
assert.equal(await page.evaluate(() => undoRequests), 1);
|
||||
}
|
||||
|
||||
async function checkAutomaticChoiceAfterUndo(page) {
|
||||
const automatic = { autoPassBids: false, autoMega: "maximum" };
|
||||
await launch(page, automatic);
|
||||
await page.evaluate((state) => emitter.emit("state", state), stripSecret(mega, 0));
|
||||
await page.waitForFunction(() => sentMoves.length === 1);
|
||||
assert.deepEqual(await page.evaluate(() => sentMoves), [{ action: "mega", take: { water: 1 } }]);
|
||||
|
||||
await launch(page, automatic);
|
||||
await page.evaluate(() => emitter.emit("undo:available", true));
|
||||
await show(page, megaChosen);
|
||||
await show(page, mega);
|
||||
await page.getByRole("button", { name: "Take all as singles", exact: true }).waitFor();
|
||||
await page.waitForTimeout(300);
|
||||
assert.deepEqual(await page.evaluate(() => sentMoves), [], "the undone production choice is not submitted again");
|
||||
}
|
||||
|
||||
const server = previewServer();
|
||||
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
||||
const browser = await chromium.launch({ executablePath: process.env.OUTPOST_CHROMIUM_EXECUTABLE });
|
||||
try {
|
||||
for (const width of [390, 1400]) {
|
||||
const page = await browser.newPage({ viewport: { width, height: 950 } });
|
||||
const errors = [];
|
||||
page.on("pageerror", (error) => errors.push(error.message));
|
||||
await page.setContent('<meta name="viewport" content="width=device-width,initial-scale=1"><div id="app"></div>');
|
||||
await page.addStyleTag({ path: "packages/viewer/dist/outpost-viewer.css" });
|
||||
await page.addScriptTag({ url: `http://127.0.0.1:${server.address().port}/bundle.js` });
|
||||
await checkUndoControl(page);
|
||||
await checkAutomaticChoiceAfterUndo(page);
|
||||
assert.deepEqual(errors, []);
|
||||
await page.close();
|
||||
}
|
||||
console.log("Undo: availability, replay/analysis/spectator/pending visibility and rewound positions passed.");
|
||||
} finally {
|
||||
await browser.close();
|
||||
await new Promise((resolve) => server.close(resolve));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue