engine: seed upkeep container seizures from the game seed
Upkeep seizures picked containers with a bare Math.random, breaking the platform guarantee that the same seed yields the same game and making wrapper.replay diverge from the original whenever a seizure occurred (seizures are log *events*, not moves, so nothing recorded the choice). Seed a per-upkeep seedrandom stream from the persisted game seed plus the visible log length — which uniquely identifies the upkeep within the game, since each upkeep immediately follows its own Pass/Accept/Decline log entry — so both live play and full replays reproduce the same seizures. Typing removeRandom along the way exposed that warehouse/factory-store seizure messages read .color on a ContainerOnStore (always undefined); they now correctly read .piece.color.
This commit is contained in:
parent
46534ebd30
commit
c72ebf77bd
2 changed files with 53 additions and 19 deletions
|
|
@ -1,7 +1,10 @@
|
|||
import { expect } from 'chai';
|
||||
import { cloneDeep, range } from 'lodash';
|
||||
import { ended, move, setup } from './engine';
|
||||
import AbstractJudge from './fixtures/Abstract-judge-7215.json';
|
||||
import { GameEventName } from './log';
|
||||
import type { Move } from './move';
|
||||
import { MoveName } from './move';
|
||||
|
||||
describe('Engine', () => {
|
||||
it('should setup a game correctly', () => {
|
||||
|
|
@ -28,4 +31,29 @@ describe('Engine', () => {
|
|||
expect(G.currentPlayers).to.deep.equal([0]);
|
||||
expect(ended(G)).to.be.false;
|
||||
});
|
||||
|
||||
it('should seize upkeep containers deterministically from the game seed', () => {
|
||||
const base = setup(2, {}, 'upkeep-seed-test');
|
||||
const A = base.currentPlayers[0];
|
||||
const B = 1 - A;
|
||||
|
||||
// Force B into upkeep debt: two loans, no money, and a pile of island
|
||||
// containers for the bank to pick from
|
||||
base.players[B].loans = base.loansLeft.splice(0, 2);
|
||||
base.players[B].money = 0;
|
||||
base.players[B].containersOnIsland = base.containersLeft.splice(0, 12);
|
||||
|
||||
// A's pass hands the turn to B and runs B's upkeep, seizing two random
|
||||
// containers. Same seed + same history ⇒ the same containers, every time.
|
||||
const results = range(5).map(() => move(cloneDeep(base), { name: MoveName.Pass, data: true }, A));
|
||||
|
||||
expect(results[0].players[B].containersOnIsland).to.have.length(10);
|
||||
expect(
|
||||
results[0].log.filter((item) => item.type === 'event' && item.event.name === GameEventName.Upkeep)
|
||||
).to.have.length(2);
|
||||
|
||||
for (const result of results.slice(1)) {
|
||||
expect(JSON.parse(JSON.stringify(result))).to.deep.equal(JSON.parse(JSON.stringify(results[0])));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -916,6 +916,12 @@ export function nextPlayer(G: GameState) {
|
|||
}
|
||||
|
||||
function doUpkeep(G: GameState) {
|
||||
// Container seizures must be reproducible: the platform guarantees that the same
|
||||
// seed yields the same game, and `wrapper.replay` re-runs upkeep from the log. The
|
||||
// seed comes from the persisted state, and the visible log length uniquely
|
||||
// identifies this upkeep within the game (each upkeep immediately follows its own
|
||||
// Pass/Accept/Decline log entry), so together they pin a per-upkeep random stream.
|
||||
const rng = seedrandom(`${G.seed}:upkeep:${G.log.length}`);
|
||||
const player = G.players[G.currentPlayers[0]];
|
||||
const loanCount = player.loans.length;
|
||||
const interest: string[] = [];
|
||||
|
|
@ -924,7 +930,7 @@ function doUpkeep(G: GameState) {
|
|||
player.money--;
|
||||
interest.push('money');
|
||||
} else if (player.containersOnIsland.length > 0) {
|
||||
const container = removeRandom(player.containersOnIsland);
|
||||
const container = removeRandom(player.containersOnIsland, rng);
|
||||
G.log.push({
|
||||
type: 'event',
|
||||
event: {
|
||||
|
|
@ -936,32 +942,32 @@ function doUpkeep(G: GameState) {
|
|||
});
|
||||
} else if (player.containersOnWarehouseStore.length + player.containersOnFactoryStore.length > 0) {
|
||||
if (player.containersOnWarehouseStore.length >= 2) {
|
||||
const c1 = removeRandom(player.containersOnWarehouseStore);
|
||||
const c2 = removeRandom(player.containersOnWarehouseStore);
|
||||
const c1 = removeRandom(player.containersOnWarehouseStore, rng);
|
||||
const c2 = removeRandom(player.containersOnWarehouseStore, rng);
|
||||
G.log.push({
|
||||
type: 'event',
|
||||
event: {
|
||||
name: GameEventName.Upkeep,
|
||||
interest: `The bank seizes a ${containerColorHTML(
|
||||
c1.color
|
||||
)} container and a ${containerColorHTML(c2.color)} container from ${playerNameHTML(
|
||||
c1.piece.color
|
||||
)} container and a ${containerColorHTML(c2.piece.color)} container from ${playerNameHTML(
|
||||
player
|
||||
)}'s warehouses`,
|
||||
},
|
||||
});
|
||||
} else if (player.containersOnWarehouseStore.length == 1) {
|
||||
const c1 = removeRandom(player.containersOnWarehouseStore);
|
||||
const c1 = removeRandom(player.containersOnWarehouseStore, rng);
|
||||
if (player.containersOnFactoryStore.length >= 1) {
|
||||
const c2 = removeRandom(player.containersOnFactoryStore);
|
||||
const c2 = removeRandom(player.containersOnFactoryStore, rng);
|
||||
G.log.push({
|
||||
type: 'event',
|
||||
event: {
|
||||
name: GameEventName.Upkeep,
|
||||
interest: `The bank seizes a ${containerColorHTML(
|
||||
c1.color
|
||||
)} container from ${playerNameHTML(player)}'s warehouses and a ${
|
||||
c2.color
|
||||
} container from ${playerNameHTML(player)}'s factory`,
|
||||
c1.piece.color
|
||||
)} container from ${playerNameHTML(player)}'s warehouses and a ${containerColorHTML(
|
||||
c2.piece.color
|
||||
)} container from ${playerNameHTML(player)}'s factory`,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
|
|
@ -970,22 +976,22 @@ function doUpkeep(G: GameState) {
|
|||
event: {
|
||||
name: GameEventName.Upkeep,
|
||||
interest: `The bank seizes a ${containerColorHTML(
|
||||
c1.color
|
||||
c1.piece.color
|
||||
)} container from ${playerNameHTML(player)}'s warehouses`,
|
||||
},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const c1 = removeRandom(player.containersOnFactoryStore);
|
||||
const c1 = removeRandom(player.containersOnFactoryStore, rng);
|
||||
if (player.containersOnFactoryStore.length >= 1) {
|
||||
const c2 = removeRandom(player.containersOnFactoryStore);
|
||||
const c2 = removeRandom(player.containersOnFactoryStore, rng);
|
||||
G.log.push({
|
||||
type: 'event',
|
||||
event: {
|
||||
name: GameEventName.Upkeep,
|
||||
interest: `The bank seizes a ${containerColorHTML(
|
||||
c1.color
|
||||
)} container and a ${containerColorHTML(c2.color)} container from ${playerNameHTML(
|
||||
c1.piece.color
|
||||
)} container and a ${containerColorHTML(c2.piece.color)} container from ${playerNameHTML(
|
||||
player
|
||||
)}'s factory`,
|
||||
},
|
||||
|
|
@ -996,7 +1002,7 @@ function doUpkeep(G: GameState) {
|
|||
event: {
|
||||
name: GameEventName.Upkeep,
|
||||
interest: `The bank seizes a ${containerColorHTML(
|
||||
c1.color
|
||||
c1.piece.color
|
||||
)} container from ${playerNameHTML(player)}'s factory`,
|
||||
},
|
||||
});
|
||||
|
|
@ -1044,8 +1050,8 @@ function remove(array, value) {
|
|||
return array.splice(array.indexOf(aux), 1);
|
||||
}
|
||||
|
||||
function removeRandom(array) {
|
||||
return array.splice(Math.floor(Math.random() * array.length), 1)[0];
|
||||
function removeRandom<T>(array: T[], rng: () => number): T {
|
||||
return array.splice(Math.floor(rng() * array.length), 1)[0];
|
||||
}
|
||||
|
||||
function prettyShipPosition(G: GameState, data: ShipPosition, simple = false): string {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue