engine: replace destructive undo with tentative-turn model

- wrapper move() now accepts the whole current turn as a Move[] buffer
  (bare move objects still accepted) and replays it from the committed state
- new toSave(): returns undefined while the turn is tentative so the platform
  neither persists mid-turn states nor grants time increments for them
- engine tracks commitment in G.newTurn: tentative iff the last visible log
  entry is a move by a player still in currentPlayers; Pass/Accept/Decline/Bid
  always commit (bids and bid-phase loans live in the hidden log and were
  never undoable)
- delete MoveName.Undo entirely (move type, engine case, available-moves
  offerings): the saved log is now append-only
- wrapper moveAI() plays atomic moves until the turn commits (bot driver and
  dropPlayer auto-play require committed states); dropPlayer marks committed
- logSlice() now includes the stripped state so the acting player's viewer
  receives tentative states via the move response
- bump to 2.0.0 (new major: move payload shape changed, Undo removed)
This commit is contained in:
coyotte508 2026-08-16 20:55:44 +00:00
commit da7491450c
6 changed files with 93 additions and 47 deletions

View file

@ -1,6 +1,6 @@
{
"name": "container-engine",
"version": "1.0.11",
"version": "2.0.0",
"description": "An engine for Container",
"main": "dist/index.js",
"types": "index.ts",

View file

@ -24,7 +24,6 @@ export interface AvailableMoves {
[MoveName.Accept]?: number[];
[MoveName.Decline]?: boolean[];
[MoveName.Pass]?: boolean[];
[MoveName.Undo]?: boolean[];
}
export function availableMoves(G: GameState, player: Player): AvailableMoves {
@ -36,10 +35,6 @@ export function availableMoves(G: GameState, player: Player): AvailableMoves {
if (player.loans.length < 2) moves[MoveName.GetLoan] = [true];
// Undo
const lastLog = G.log[G.log.length - 1];
if (lastLog.type == 'move' && G.currentPlayers.includes(lastLog.player)) moves[MoveName.Undo] = [true];
return moves;
}
@ -53,10 +48,6 @@ export function availableMoves(G: GameState, player: Player): AvailableMoves {
const highestBidder = G.players[G.highestBidders[0]];
if (player.money >= highestBidder.bid + highestBidder.additionalBid) moves[MoveName.Decline] = [true];
// Undo
const lastLog = G.log[G.log.length - 1];
if (lastLog.type == 'move' && G.currentPlayers.includes(lastLog.player)) moves[MoveName.Undo] = [true];
return moves;
}
@ -301,10 +292,6 @@ export function availableMoves(G: GameState, player: Player): AvailableMoves {
}
}
// Undo
const lastLog = G.log[G.log.length - 1];
if (lastLog.type == 'move' && G.currentPlayers.includes(lastLog.player)) moves[MoveName.Undo] = [true];
return moves;
}

View file

@ -85,6 +85,7 @@ export function setup(numPlayers: number, { beginner = true }: GameOptions, seed
hiddenLog: [],
seed,
round: 1,
newTurn: true,
} as GameState;
const colors = shuffle(containerColors, rng() + '');
@ -608,18 +609,6 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
break;
}
case MoveName.Undo: {
asserts<Moves.MoveUndo>(move);
const lastLog = G.log[G.log.length - 1];
if (lastLog.type == 'move' && G.currentPlayers.includes(lastLog.player) && !fake) {
G.log.pop();
G = reconstructState(getBaseState(G), G.log);
}
return G;
}
}
player.availableMoves = null;
@ -628,6 +617,22 @@ export function move(G: GameState, move: Move, playerNumber: number, fake?: bool
G.currentPlayers.forEach((p) => (G.players[p].availableMoves = availableMoves(G, G.players[p])));
// Tentative-turn bookkeeping: the state is committed (`newTurn`) once the mover can
// no longer undo. This mirrors the old undo-availability rule — undo was offered
// whenever the last visible log entry was a move by a player still in
// `currentPlayers` — with turn-boundary moves committing unconditionally:
// - Pass / Accept / Decline end the turn (even in the edge case where the same
// player is up again because everyone else is dropped);
// - Bid (and loans taken during the bid phase) go to the hidden log and were never
// undoable: bids are single-move committed turns.
const lastLog = G.log[G.log.length - 1];
const turnBoundary =
move.name === MoveName.Pass ||
move.name === MoveName.Accept ||
move.name === MoveName.Decline ||
move.name === MoveName.Bid;
G.newTurn = turnBoundary || !(lastLog?.type === 'move' && G.currentPlayers.includes(lastLog.player));
return G;
}
@ -682,7 +687,7 @@ export function moveAI(G: GameState, playerNumber: number): GameState {
data = dataArr[Math.floor(Math.random() * dataArr.length)];
if (moveName == MoveName.Undo || moveName == MoveName.GetLoan || moveName == MoveName.PayLoan) {
if (moveName == MoveName.GetLoan || moveName == MoveName.PayLoan) {
moveName = null;
} else if (moveName == MoveName.Sail) {
if (data == ShipPosition.Island) {
@ -1043,16 +1048,6 @@ function removeRandom(array) {
return array.splice(Math.floor(Math.random() * array.length), 1)[0];
}
function getBaseState(G: GameState): GameState {
const baseState = setup(G.players.length, G.options, G.seed);
baseState.players.forEach((player, i) => {
player.name = G.players[i].name;
player.isAI = G.players[i].isAI;
});
return baseState;
}
function prettyShipPosition(G: GameState, data: ShipPosition, simple = false): string {
switch (data) {
case ShipPosition.Island:

View file

@ -135,4 +135,11 @@ export interface GameState {
hiddenLog: LogItem[];
seed: string;
round: number;
/**
* Tentative-turn marker. `true` (or `undefined`, for robustness) when the state is
* committed: the last mover can no longer undo and the state can be persisted.
* `false` while a player's turn is still in progress (mid-turn moves can be undone
* by the viewer by replaying a shortened turn buffer from the last committed state).
*/
newTurn?: boolean;
}

View file

@ -88,11 +88,6 @@ export declare namespace Moves {
name: MoveName.Pass;
data: boolean;
}
export interface MoveUndo {
name: MoveName.Undo;
data: boolean;
}
}
export type Move =
@ -110,7 +105,6 @@ export type Move =
| Moves.MoveAccept
| Moves.MoveDecline
| Moves.MovePass
| Moves.MoveUndo
| Moves.MoveDomesticSale;
export enum MoveName {
@ -129,5 +123,4 @@ export enum MoveName {
Accept = 'accept',
Decline = 'decline',
Pass = 'pass',
Undo = 'undo',
}

View file

@ -15,17 +15,73 @@ export function setPlayerMetaData(G: GameState, player: number, metaData: { name
return G;
}
export async function move(G: GameState, move: Move, player: number) {
G = engine.move(G, move, player);
/**
* Execute the current turn of `player` so far.
*
* The payload is the **whole turn buffer**: an array of atomic moves accumulated by the
* viewer since the last committed state. `G` is always a committed state (tentative
* states are never persisted by the platform), so the buffer is replayed from it in
* order. A bare move object is also accepted and treated as a one-element buffer.
*
* While the resulting state is still tentative (the mover could undo, i.e.
* `G.newTurn === false`), `toSave` returns `undefined`: the platform then sends the
* tentative state back to the acting player without persisting it or granting a time
* increment. Undo is implemented by the viewer replaying a shortened buffer — or, when
* the buffer empties, by doing nothing at all, since the saved state *is* the turn start.
*/
export async function move(G: GameState, move: Move | Move[] | null | undefined, player: number) {
const moves: Move[] = move == null ? [] : Array.isArray(move) ? move : [move];
if (moves.length === 0) {
// Nothing to apply — flag the result as tentative so nothing gets persisted
// and no time increment is granted.
return { ...G, newTurn: false };
}
for (const m of moves) {
G = engine.move(G, m, player);
}
return G;
}
/**
* Only committed states are persisted. Tentative states (mid-turn, still undoable)
* return `undefined` so the platform neither saves them nor grants a time increment.
*/
export function toSave(G: GameState): GameState | undefined {
return G.newTurn === false ? undefined : G;
}
export function factions(G: GameState) {
return G.players.map((pl) => engine.playerColors[pl.id]);
}
export { ended, moveAI, scores, stripSecret } from './src/engine';
export { ended, scores, stripSecret } from './src/engine';
/**
* Play a full turn for `player`. The engine's own `moveAI` plays one atomic move at a
* time, which can leave the state tentative (`toSave` would refuse to persist it); the
* platform's bot driver and `dropPlayer` auto-play both require committed states, so we
* keep playing until the turn commits.
*/
export function moveAI(G: GameState, player: number): GameState {
for (let i = 0; i < 500 && !engine.ended(G) && G.currentPlayers.includes(player); i++) {
G = engine.moveAI(G, player);
if (G.newTurn !== false) {
return G;
}
}
// Safety net — should be unreachable (Pass is always available in the move phase
// and commits the turn). The state is consistent (every atomic move was legal and
// logged), it just did not reach a turn boundary; persisting it keeps the game
// going instead of wedging it.
G.newTurn = true;
return G;
}
export function rankings(G: GameState) {
const sortedPlayers = cloneDeep(G.players)
@ -71,6 +127,10 @@ export async function dropPlayer(G: GameState, player: number) {
engine.nextPlayer(G);
// Dropping a player always yields a committed state: tentative states are never
// persisted, so `G` was committed to begin with and the drop simply advances the turn.
G.newTurn = true;
return G;
}
@ -92,6 +152,10 @@ export function logLength(G: GameState, _player?: number) {
export function logSlice(G: GameState, options?: { player?: number; start?: number; end?: number }) {
const stripped = engine.stripSecret(G, options?.player);
return {
// The full (stripped) state. This is how the acting player's viewer receives
// tentative states: they are never persisted or broadcast, only returned in the
// move response's log slice.
state: stripped,
log: stripped.log.slice(options?.start, options?.end),
availableMoves:
options?.end === undefined