v0.7.3 — a game that asks before it ends, and a results screen worth reading

Two issues off the tracker, and they are halves of one thing: the end of a game.
Neither ships on the 0.4.9 line — Jesse's call, that line may be complete and
these are not fixes people mid-playtest need.

EXTENDED PLAY (#11). The official result is settled at the original game length
and never changes: in a five-Day game extended to eight, the winner is whoever
led at the end of Day 5. Extending grants exactly one Day and the question is put
again at the end of it — solitaire the player decides alone, multiplayer it is
unanimous and one refusal ends it there. Only days-based endings offer it; a §3.4
collision breach is final, during an extended Day exactly as during the scheduled
game.

It could not be a client-side change. `check` refused every intent once `status`
left `active`; the server never loads a `finished` game back into memory; and a
save is `{ seed, config, history }` replayed through the engine, so a "continue"
the history does not record did not happen. Hence a fourth status,
`awaitingExtension`, and a `game.extend` intent. `config.days` never moves —
`extraDays` counts the borrowed Days and `official` freezes the outcome, the
standings and the statistics at the first ending.

THE RESULTS SCREEN (#16). `GAME OVER — revenueFloor` was `outcome.reason`, an
internal enum interpolated into the page at the one moment the game has the
player's whole attention. Every reason now has a sentence with the game's own
numbers in it. Around it: the result and winner, standings, the rules the game
was dealt under, a per-player breakdown, and the railroad — trains through the
Division and how many worked en route, loads made up and broken, passengers, cars
switched, trains destroyed. It shares the Day-end dialog's blocks rather than
reimplementing them, and stays reopenable so continuing does not cost you the
results.

Statistics are folded, not recorded: `state.tally` counts what the event stream
says happened, hooked at `applyIntent` and `advance` because `reduce` never sees
the phase driver's events — and those are the interesting ones. Nothing in the
rules reads it, and it rides the Frame, so multiplayer gets the same numbers as
solitaire from one implementation.

THREE BUGS FOUND IN TESTING, all of which would have shipped:

  - a saved game containing a vote could not be resumed (NO_ACTOR). A history is
    a flat Intent[] with no seat recorded; the replay derives who acted from the
    turn order, which cannot work for an intent every seat may send in any order.
    `game.extend` carries its voter, checked against the authenticated seat.
  - an all-bot game hung on the question for ever. `driveBots` loops on
    `currentActor`, null the moment the game stops, so it cannot cast a vote, and
    the bot-vote driver returned early with no humans to follow.
  - the balance harness became unbounded — `test/sim.test.ts` went from under a
    second to never finishing. `randomBot` took another Day about half the time,
    so every seeded game ran to playGame's 50,000-turn cap. Fixed in the driver,
    not in a policy, so it holds for bots not yet written.

All three have regression tests. 832 tests pass, against 793 before this change.

NOT BUILT, and a correction. #16's own comment said `trainStoodStill` "is emitted
per Stage, so a run of them is exactly the sat-on-a-siding streak". It is not:
reading advance.ts, it fires once per game and only for a train whose profile
sets `stopEarnsPoint` — the X18 Circus — with `stopPointClaimed` preventing a
second. The streak was built, rendered "1 Stage at (0,0)", and was taken out
again. There is no per-Stage "this train did not move" signal in the engine, so
"longest an engine sat on a siding" needs one first; TODO.md #36 records what it
would take, and the Circus set-up is reported instead. Badges remain the second
pass #16 asks for (TODO.md #33), and because the statistics are derived rather
than recorded, that pass can add any of them retroactively to games already
played and saved.

Extended play has not yet been played at a real table (TODO.md #35): the
multiplayer vote has only been driven through `session.intent`, never through two
browsers.

Closes #11
Closes #16

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EAgJSmeV8zrMh55Mj85ESb
This commit is contained in:
Jesse.Markowitz
2026-08-29 04:23:26 -04:00
co-authored by Claude Opus 5
parent 510e33bac7
commit 45580d8b61
30 changed files with 2264 additions and 91 deletions
+119
View File
@@ -412,3 +412,122 @@ describe('the four transient signals (2026-08-23)', () => {
assert.equal(later.scheduled, undefined, 'a reconnecting client was re-sent an old timetable flash');
});
});
// ---------------------------------------------------------------------------
describe('§3.3 extended play across the server (Gitea#11)', () => {
/**
* A one-Day game, so these tests reach the end of the timetable by actually PLAYING to it.
*
* There is no back door into a session's engine state and there should not be — `connect`,
* `intent`, `exportSave` and `summary` are the whole surface. So the clock is run down through the
* same calls a client makes, which has the side benefit of exercising the real path: what is under
* test here is the session's handling of the vote (the turn guard, the bots, the saved status),
* and reaching it any other way would prove less.
*/
const oneDay: GameConfig = { ...config, days: 1 };
/** What seat `seat` can currently see. `connect` always yields a full Frame, never a delta. */
const frameOf = (session: GameSession, seat: PlayerIndex) => session.connect(seat).frame!;
/** Plays until the game stops asking for ordinary moves. Returns the Frame it stopped on. */
function playToTheEnd(session: GameSession, seats: PlayerIndex[]) {
let seq = 0;
for (let i = 0; i < 5_000; i++) {
const acting = seats.find((s) => session.connect(s).menu !== null);
if (acting === undefined) break;
const menu = session.connect(acting).menu!;
if (menu.options.length === 0) break;
if (!session.intent(acting, seq++, menu.options[0]!).accepted) break;
}
return { frame: frameOf(session, seats[0]!), seq };
}
it('stops to ask rather than ending, and every seat can see the question', () => {
const session = createSession(42, oneDay, ['Alice', 'Bob']);
const { frame } = playToTheEnd(session, [0, 1] as PlayerIndex[]);
assert.equal(frame.status, 'awaitingExtension', 'the game did not stop to ask');
assert.ok(frame.official, 'the official result did not reach the client');
assert.deepEqual(frame.extensionVotes, [null, null], 'the votes did not reach the client');
});
it('accepts the vote from a seat that is not the current actor', () => {
// `currentActor` is null once the game has stopped, so the ordinary turn guard would refuse
// every vote with NOT_YOUR_TURN. Both seats vote here and neither of them is the actor.
const session = createSession(42, oneDay, ['Alice', 'Bob']);
const { seq } = playToTheEnd(session, [0, 1] as PlayerIndex[]);
const a = session.intent(0 as PlayerIndex, seq + 1, { type: 'game.extend', player: 0, agree: true });
assert.equal(a.accepted, true, 'seat 0 could not vote');
const b = session.intent(1 as PlayerIndex, seq + 2, { type: 'game.extend', player: 1, agree: true });
assert.equal(b.accepted, true, 'seat 1 could not vote');
const after = frameOf(session, 0 as PlayerIndex);
assert.equal(after.extraDays, 1, 'a unanimous table was not given its Day');
assert.equal(after.status, 'active', 'play did not resume');
});
it('bots agree only once every human has, and never lead', () => {
// "Bots will not disagree with the human. Humans get to vote first" (Jesse, 2026-08-28).
const session = createSession(42, oneDay, ['Alice', 'Botty'], [1 as PlayerIndex]);
const { seq } = playToTheEnd(session, [0, 1] as PlayerIndex[]);
assert.equal(frameOf(session, 0 as PlayerIndex).status, 'awaitingExtension');
assert.equal(
frameOf(session, 0 as PlayerIndex).extensionVotes[1],
null,
'the bot voted before the human did',
);
session.intent(0 as PlayerIndex, seq + 1, { type: 'game.extend', player: 0, agree: true });
const after = frameOf(session, 0 as PlayerIndex);
assert.equal(after.extraDays, 1, 'the bot did not follow the human into another Day');
assert.equal(after.status, 'active');
});
it('a human refusal ends it, and no bot overrides that', () => {
const session = createSession(42, oneDay, ['Alice', 'Botty'], [1 as PlayerIndex]);
const { seq } = playToTheEnd(session, [0, 1] as PlayerIndex[]);
session.intent(0 as PlayerIndex, seq + 1, { type: 'game.extend', player: 0, agree: false });
const after = frameOf(session, 0 as PlayerIndex);
assert.equal(after.status, 'finished');
assert.equal(after.extraDays, 0);
});
it('REGRESSION: an all-bot game ends rather than hanging on the question', () => {
/**
* `driveBots` loops on `currentActor`, which is null the moment the game stops to ask — so it
* cannot cast the vote itself, and the bots' vote is driven separately. The first cut of that
* driver returned early when there were no humans to follow, on the reasoning that a bot-only
* table would decline through the ordinary path. It has no ordinary path: nothing ever asked
* the bots, and an all-bot session sat on the question for ever without reaching `finished`.
* Caught by `summary()`'s own "a finished game waits on nobody" test.
*/
const session = createSession(4242, oneDay, ['A', 'B'], [0, 1] as PlayerIndex[]);
assert.equal(frameOf(session, 0 as PlayerIndex).status, 'finished', 'the bots never answered');
assert.equal(session.summary().waitingOn, null);
assert.equal(frameOf(session, 0 as PlayerIndex).extraDays, 0, 'bots voted themselves another Day');
});
it('saves a game awaiting its vote as ACTIVE, so a restart resumes it', () => {
// `server/index.ts` never loads a `finished` game back into memory. A game paused on the
// extension question is waiting on its table, not over — persisting it as finished would strand
// it on disk mid-decision.
const session = createSession(42, oneDay, ['Alice', 'Bob']);
playToTheEnd(session, [0, 1] as PlayerIndex[]);
assert.equal(frameOf(session, 0 as PlayerIndex).status, 'awaitingExtension');
assert.equal(session.exportSave().status, 'active', 'a paused game was saved as finished');
assert.equal(session.summary().status, 'active');
});
it('resumes a paused game from its history, vote and all', () => {
const session = createSession(42, oneDay, ['Alice', 'Bob']);
const { seq } = playToTheEnd(session, [0, 1] as PlayerIndex[]);
session.intent(0 as PlayerIndex, seq + 1, { type: 'game.extend', player: 0, agree: true });
const resumed = resumeSession(session.exportSave());
const a = frameOf(session, 0 as PlayerIndex);
const b = frameOf(resumed, 0 as PlayerIndex);
assert.deepEqual(b.extensionVotes, a.extensionVotes, 'the votes did not survive the replay');
assert.equal(b.extraDays, a.extraDays, 'the extra Day did not survive the replay');
assert.deepEqual(b.official, a.official, 'the official result did not survive the replay');
});
});