Track last action and limit state save checkpoints
This commit is contained in:
parent
f972f8f7fe
commit
dfda63b6f0
7 changed files with 74 additions and 6 deletions
BIN
packages/engine/outpost-engine-0.3.0.tgz
Normal file
BIN
packages/engine/outpost-engine-0.3.0.tgz
Normal file
Binary file not shown.
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "outpost-engine",
|
||||
"version": "0.2.1",
|
||||
"version": "0.3.0",
|
||||
"description": "Unofficial Outpost rules engine for boardgamers.space",
|
||||
"license": "AGPL-3.0",
|
||||
"files": [
|
||||
|
|
|
|||
|
|
@ -220,6 +220,7 @@ export function applyMove(state: GameState, rawMove: Move | unknown, seat: numbe
|
|||
}
|
||||
|
||||
state.moveCount += 1;
|
||||
state.lastAction = move.action;
|
||||
state.log.push(info ? { type: "move", player: seat, move, info } : { type: "move", player: seat, move });
|
||||
postMove(state, move);
|
||||
return state;
|
||||
|
|
|
|||
|
|
@ -141,4 +141,6 @@ export interface GameState {
|
|||
moveCount: number;
|
||||
ended: boolean;
|
||||
messages: string[];
|
||||
/** Action of the most recently applied move (drives toSave checkpointing). */
|
||||
lastAction?: Move["action"];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { moveAI } from "./ai.js";
|
||||
import { initGame } from "./moves.js";
|
||||
import { applyMove, initGame } from "./moves.js";
|
||||
import { replay } from "./replay.js";
|
||||
import * as wrapper from "../wrapper.js";
|
||||
import type { GameState } from "./types.js";
|
||||
|
|
@ -98,6 +98,57 @@ test("dropPlayer removes the player from play and can end the game", async () =>
|
|||
assert.equal(ranks.length, 3);
|
||||
});
|
||||
|
||||
test("toSave persists only at checkpoints, not intermediate moves", () => {
|
||||
const state = initGame(3, {}, "tosave-spec");
|
||||
// Give everyone a comfortable hand so auctions/bids/buys are legal.
|
||||
for (const p of state.players) {
|
||||
p.hand = [
|
||||
{ t: "water", v: 10 },
|
||||
{ t: "water", v: 10 },
|
||||
{ t: "water", v: 10 },
|
||||
{ t: "water", v: 10 },
|
||||
{ t: "water", v: 10 },
|
||||
{ t: "water", v: 10 },
|
||||
];
|
||||
}
|
||||
// Opening an auction / bidding are intermediate: no save.
|
||||
const opener = state.activeSeat;
|
||||
applyMove(state, { action: "auction", marketIndex: 0, bid: 25 }, opener);
|
||||
assert.equal(wrapper.toSave(state), undefined);
|
||||
const bidder = state.auction?.activeBidder as number;
|
||||
applyMove(state, { action: "bid", amount: 30 }, bidder);
|
||||
assert.equal(wrapper.toSave(state), undefined);
|
||||
// Everyone else passes until the auction resolves to a payment.
|
||||
let guard = 0;
|
||||
while (state.phase === "auction" && guard++ < 20) {
|
||||
applyMove(state, { action: "bidPass" }, state.auction?.activeBidder as number);
|
||||
}
|
||||
assert.equal(state.phase, "auctionPayment");
|
||||
// Resolve the auction: the winner pays -> checkpoint.
|
||||
const winner = state.auction?.highBidder as number;
|
||||
const hand = state.players[winner]?.hand ?? [];
|
||||
applyMove(state, { action: "pay", cards: hand.map((_, i) => i) }, winner);
|
||||
assert.ok(wrapper.toSave(state));
|
||||
// Buying a factory is intermediate: no save.
|
||||
const me = state.players[state.activeSeat];
|
||||
if (me && me.hand.length > 0) {
|
||||
const before = state.moveCount;
|
||||
applyMove(state, { action: "buyFactory", factory: "ore", cards: me.hand.map((_, i) => i) }, state.activeSeat);
|
||||
if (state.moveCount > before) {
|
||||
assert.equal(wrapper.toSave(state), undefined);
|
||||
}
|
||||
}
|
||||
// Ending the turn is a checkpoint.
|
||||
applyMove(state, { action: "endTurn", manned: [] }, state.activeSeat);
|
||||
assert.ok(wrapper.toSave(state));
|
||||
});
|
||||
|
||||
test("toSave always persists a finished game", () => {
|
||||
const state = play(initGame(4, {}, "tosave-end"), 100000);
|
||||
assert.ok(state.ended);
|
||||
assert.ok(wrapper.toSave(state));
|
||||
});
|
||||
|
||||
test("messages drain once", () => {
|
||||
const state = play(initGame(4, {}, "messages-spec"), 500);
|
||||
const first = wrapper.messages(state);
|
||||
|
|
|
|||
|
|
@ -148,8 +148,20 @@ export function playerSettings(data: GameState, player: number): Record<string,
|
|||
return { autoPassBids: data.players[player]?.settings.autoPassBids === true };
|
||||
}
|
||||
|
||||
export function toSave(data: GameState): GameState {
|
||||
return data;
|
||||
// Persist only at meaningful checkpoints (a full turn completed, an auction
|
||||
// resolved, discards finalized, or the game ending). Intermediate moves —
|
||||
// opening an auction, placing a bid, passing, buying a factory/colonist/robot —
|
||||
// return undefined so the platform skips the write. Each toSave costs the
|
||||
// platform time, and the game is fully replayable from the log, so skipping
|
||||
// intermediate saves is safe: the acting player's viewer still gets the
|
||||
// tentative state via logSlice, and the next checkpoint persists everything.
|
||||
const SAVE_ACTIONS = new Set(["endTurn", "pay", "discard"]);
|
||||
|
||||
export function toSave(data: GameState): GameState | undefined {
|
||||
if (data.ended || (data.lastAction !== undefined && SAVE_ACTIONS.has(data.lastAction))) {
|
||||
return data;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function messages(data: GameState): { messages: string[]; data: GameState } {
|
||||
|
|
|
|||
|
|
@ -32,9 +32,11 @@
|
|||
// Force a reflow so the animation restarts with the new values.
|
||||
void el.offsetWidth;
|
||||
el.classList.add("fly");
|
||||
timer = setTimeout(launch, 25000 + Math.random() * 45000);
|
||||
// Space subsequent comets out: roughly one every 3 minutes.
|
||||
timer = setTimeout(launch, 150000 + Math.random() * 90000);
|
||||
};
|
||||
timer = setTimeout(launch, 6000 + Math.random() * 8000);
|
||||
// First comet after ~40s so the scene doesn't feel static on arrival.
|
||||
timer = setTimeout(launch, 38000 + Math.random() * 6000);
|
||||
return () => clearTimeout(timer);
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue