Release engine 2.0.27 and viewer 2.3.28: correct replay deck counts

This commit is contained in:
coyotte508 2026-11-10 23:20:59 +00:00
commit d381197fdc
8 changed files with 200 additions and 33 deletions

View file

@ -0,0 +1,37 @@
{
"game": "powergrid",
"gameVersion": 13,
"publishedAt": "2026-11-10T23:20:59.060Z",
"engine": {
"version": "2.0.27",
"package": {
"name": "powergrid-engine",
"version": "2.0.27",
"url": "https://s3.fr-par.scw.cloud/bgs-assets/games/powergrid/13/engine/0ec1895a4cef47cf/powergrid-engine-2.0.27.tgz"
},
"entryPoint": "dist/wrapper.js",
"sha256": "0ec1895a4cef47cf483eb3258a69f135ea6d3e93676da1eb18e50e38634c9e4b",
"bytes": 380402
},
"viewer": {
"version": "2.3.28",
"url": "https://s3.fr-par.scw.cloud/bgs-assets/games/powergrid/13/viewer/split-98a08c0f1dd22aa8074f/powergrid-viewer.umd.min.js",
"stylesheets": [
"https://s3.fr-par.scw.cloud/bgs-assets/games/powergrid/13/viewer/split-98a08c0f1dd22aa8074f/powergrid-viewer.css"
],
"scriptBytes": 1232200,
"bundle": "split-98a08c0f1dd22aa8074f",
"files": 42,
"sha256": "899502f7c46c9c6ecadcf399a7d84224b997d3b143632f1a66548af7ab331abd"
},
"verification": {
"engineTests": 476,
"viewerUnitTests": 95,
"localization": true,
"browser": true,
"layout": true,
"publishedViewerReplayCounts": [14, 4],
"publishedFrenchLocale": true
},
"verified": true
}

View file

@ -1,6 +1,6 @@
{
"name": "powergrid-engine",
"version": "2.0.26",
"version": "2.0.27",
"description": "An engine for Power Grid",
"main": "dist/index.js",
"types": "index.ts",

View file

@ -2262,11 +2262,13 @@ export function scores(G: GameState): number[] {
// queue never enters a saved state or a scenario snapshot.
const publicReplayDraws = new WeakMap<GameState, PowerPlant[]>();
export function reconstructState(gameState: GameState, to?: number, forcePublicDraws = false): GameState {
// The third argument is retained for callers that previously opted into public
// draws. All secret-seed reconstructions now use that path.
export function reconstructState(gameState: GameState, to?: number, _forcePublicDraws = false): GameState {
const initialState = getBaseState(gameState);
const G = cloneDeep(initialState);
if (to != undefined && gameState.seed == 'secret') {
if (gameState.seed == 'secret') {
if (gameState.knownPowerPlantDeck) {
G.map = gameState.map;
G.blockedCities = gameState.blockedCities?.slice();
@ -2276,40 +2278,44 @@ export function reconstructState(gameState: GameState, to?: number, forcePublicD
// by the same setupDeck, so its market sizes are authoritative even
// though the (seeded) contents are not. knownPowerPlantDeck is
// actual-then-future at game start with the draws appended, so the
// first actualN+futureN entries seed the market and the rest the deck.
// first actualN+futureN entries seed the market; the rest are draws.
const actualN = initialState.actualMarket.length;
const futureN = initialState.futureMarket.length;
G.actualMarket = G.powerPlantsDeck.splice(0, actualN);
G.futureMarket = G.powerPlantsDeck.splice(0, futureN);
G.players[G.currentPlayers[0]].availableMoves = availableMoves(G, G.players[G.currentPlayers[0]]);
// cloneDeep: addPowerPlant() replaces powerPlantsDeck with this array and
// shift()s draws off it — sharing the caller's array would consume
// knownPowerPlantDeckStep3 across successive replays (each replayTo
// must see the full deck).
G.powerPlantDeckAfterStep3 = cloneDeep(gameState.knownPowerPlantDeckStep3);
G.knownPowerPlantDeck = G.actualMarket.concat(G.futureMarket);
if (forcePublicDraws) {
publicReplayDraws.set(
G,
// Observations are a draw transcript, not a physical deck: they omit
// unseen cards and include cards drawn again after being bottomed.
// Keep setup's physical deck for its size and normal lifecycle (bottoming,
// Step 3 shuffles, Manhattan recycling, Spain's held-back plants), but
// take card identities from the transcript. Never replace the physical
// deck with the Step 3 transcript, which may still be only a prefix.
publicReplayDraws.set(
G,
cloneDeep(
gameState.knownPowerPlantDeck
.slice(actualN + futureN)
.concat(gameState.knownPowerPlantDeckStep3 || [])
);
G.powerPlantsDeck = cloneDeep(initialState.powerPlantsDeck);
G.powerPlantDeckAfterStep3 = undefined;
if (G.map.name === 'Middle East') {
G.powerPlantsDeck = defaultSetupDeck(
G.players.length,
G.options.variant!,
seedrandom('public-replay'),
G.options.useNewRechargedSetup ?? true,
gameDeck(G.options),
G.map.name
).powerPlantsDeck;
removePlantsForMiddleEastStep1(G);
G.players[G.currentPlayers[0]].availableMoves = availableMoves(G, G.players[G.currentPlayers[0]]);
}
)
);
G.powerPlantsDeck = cloneDeep(initialState.powerPlantsDeck);
G.powerPlantDeckAfterStep3 = undefined;
if (G.map.name === 'Middle East') {
// Setup already filtered its randomly seeded market. Repeat that
// filtering from the recorded initial market and an unfiltered deck.
G.powerPlantsDeck = defaultSetupDeck(
G.players.length,
G.options.variant!,
seedrandom('public-replay'),
G.options.useNewRechargedSetup ?? true,
gameDeck(G.options),
G.map.name
).powerPlantsDeck;
removePlantsForMiddleEastStep1(G);
G.players[G.currentPlayers[0]].availableMoves = availableMoves(G, G.players[G.currentPlayers[0]]);
}
G.cardsLeft = G.powerPlantsDeck.length;
}
}

View file

@ -0,0 +1,72 @@
import { expect } from 'chai';
import 'mocha';
import { replay } from '../wrapper';
import { ended, moveAI, reconstructState, setup, stripSecret } from './engine';
import USAOriginal from './fixtures/USAOriginal.json';
import { GameState, MapName } from './gamestate';
import { maps } from './maps';
function expectPublicPosition(actual: GameState, expected: GameState) {
expect(actual.cardsLeft, 'remaining physical cards').to.equal(expected.cardsLeft);
expect(actual.powerPlantsDeck.length, 'physical deck size').to.equal(expected.cardsLeft);
expect(actual.actualMarket).to.deep.equal(expected.actualMarket);
expect(actual.futureMarket).to.deep.equal(expected.futureMarket);
expect(actual.step).to.equal(expected.step);
expect(actual.phase).to.equal(expected.phase);
}
describe('public replay physical deck counts', function () {
this.timeout(120000);
for (const [to, count] of [
[200, 14],
[400, 4],
]) {
it(`preserves unseen cards at USA move prefix ${to}`, () => {
const live = replay(USAOriginal as any, { to });
expect(live.cardsLeft).to.equal(count);
const publicState = stripSecret(live);
const before = JSON.stringify(publicState);
expectPublicPosition(reconstructState(publicState, live.log.length), live);
expectPublicPosition(reconstructState(publicState), live);
expectPublicPosition(reconstructState(publicState, live.log.length, true), live);
expect(JSON.stringify(publicState)).to.equal(before);
});
}
it('recomputes historical counts rather than copying the latest count', () => {
const latest = replay(USAOriginal as any, { to: 400 });
const earlier = replay(USAOriginal as any, { to: 200 });
const publicState = stripSecret(latest);
for (const expected of [earlier, latest, earlier]) {
expectPublicPosition(reconstructState(publicState, expected.log.length), expected);
}
});
// Exercise each map's own setup size and lifecycle, including China/Russia's
// smaller markets, Spain's held-back plants, Middle East's repeated Step 3,
// and Manhattan's recycle pile and deck depletions.
for (const map of maps) {
for (const variant of ['original', 'recharged'] as const) {
it(`${map.name} ${variant}: live prefixes and historical scrubbing`, () => {
let live = setup(3, { map: map.name as MapName, variant, fastBid: true }, 'public-counts');
const positions: GameState[] = [];
for (let i = 0; i < 1000 && !ended(live); i++) {
if (i % 30 === 0) {
positions.push(JSON.parse(JSON.stringify(live)));
expectPublicPosition(reconstructState(stripSecret(live), live.log.length), live);
}
live = moveAI(live, live.currentPlayers[0]);
}
expect(ended(live), 'bot game finished').to.equal(true);
positions.push(live);
const publicState = stripSecret(live);
const before = JSON.stringify(publicState);
for (const position of positions.reverse()) {
expectPublicPosition(reconstructState(publicState, position.log.length), position);
}
expect(JSON.stringify(publicState)).to.equal(before);
});
}
}
});

View file

@ -1,6 +1,6 @@
{
"name": "powergrid-viewer",
"version": "2.3.27",
"version": "2.3.28",
"repository": {
"type": "git",
"url": "git+https://github.com/boardgamers/powergrid.git",

View file

@ -1,5 +1,5 @@
import { checkHostPresentation } from './host-presentation-smoke.mjs';
import { checkMiddleEastReplay } from './replay-smoke.mjs';
import { checkMiddleEastReplay, checkReplayDeckCount } from './replay-smoke.mjs';
import { checkUndoMove } from './undo-smoke.mjs';
import { checkPlayerColors } from './player-colors-smoke.mjs';
import assert from 'node:assert/strict';
@ -431,6 +431,7 @@ try {
assert.deepEqual(await page.evaluate(() => preferenceUpdates), [{ name: 'colorBlind', value: false }]);
await checkPlayerColors(page, engine);
await checkHostPresentation(page, 'host', `/tmp/powergrid-board-thumbnail-${width}.png`);
await checkReplayDeckCount(page);
if (width === 1400) await checkMiddleEastReplay(page);
await checkUndoMove(page);
assert.deepEqual(errors, []);

View file

@ -1,5 +1,57 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const engine = require('../../engine/dist/index.js');
const wrapper = require('../../engine/dist/wrapper.js');
// Public replay must count unseen cards as well as recorded draws, including
// plants returned to the bottom before the Step 3 shuffle.
export async function checkReplayDeckCount(page) {
const fixture = JSON.parse(await readFile(new URL('../../engine/src/fixtures/USAOriginal.json', import.meta.url)));
for (const to of [200, 400]) {
// Fresh document: destroyed Vue roots can remain in the sandbox DOM.
await page.reload();
const live = wrapper.replay(fixture, { to });
const state = JSON.parse(JSON.stringify(engine.stripSecret(live)));
// Mount this historical checkpoint as a standalone snapshot, not a
// tentative move response (the viewer deliberately ignores those on load).
state.newTurn = true;
const indices = state.log
.map((entry, index) => ({ entry, index }))
.filter(({ entry }) => entry.type === 'move')
.map(({ index }) => index);
await page.evaluate((state) => {
window.host = window.powergrid.launch('#app');
host.emit('preferences', { sound: false });
host.emit('player', {});
host.emit('state', state);
}, state);
const expectCount = async (count) => {
await page.waitForFunction(
(count) => document.querySelector('.game')?.__vue__?.G?.cardsLeft === count,
count
);
if (count > 0) {
await page.waitForFunction(
(count) => document.querySelector('[data-deck-count]')?.textContent.trim() === String(count),
count
);
} else assert.equal(await page.locator('[data-deck-count]').count(), 0);
};
await expectCount(live.cardsLeft);
await page.evaluate(() => host.emit('replay:start'));
for (const position of [indices.length, indices.length - 1, 1, indices.length - 1, indices.length]) {
const expected = wrapper.replay(live, { to: indices[position - 1] + 1 }).cardsLeft;
await page.evaluate((position) => host.emit('replay:to', position), position);
await expectCount(expected);
}
console.log(
`Replay deck count: round ${live.round}, step ${live.step}, ${live.cardsLeft} cards; latest/back/start/repeated seeks passed`
);
}
}
// Public spectator snapshot of MHABKH0215, with player names anonymized.
// Move 331 draws the Step 3 card during Middle East's Step 1 -> Step 2 transition.

View file

@ -1108,10 +1108,9 @@ let nextGeographyClipId = 0;
const log = this._futureState!.log.map((l, i) => ({ index: i, ...l })).filter((l) => l.type == 'move');
to = log[to - 1].index;
// Middle East reshuffles at Step 2: replay the recorded draws so the
// Step 3 card cannot be drawn repeatedly from a reconstructed deck.
const recordedDraws = this._futureState!.map.name === 'Middle East';
this.replaceState(reconstructState(this._futureState!, to + 1, recordedDraws), false);
// Public reconstruction uses recorded draws for card identities while
// tracking the full physical deck separately, including bottomed cards.
this.replaceState(reconstructState(this._futureState!, to + 1), false);
this.emitter.emit('replay:info', {
start: 1,