Preserve Basic actions while Sandtrout waits
Verify and publish checkpoint container / container (push) Canceled after 0s

This commit is contained in:
Quintine committed 2026-11-16 17:10:21 +11:00
1 parent 338f2d8980
commit c86102583f
10 files changed
+398 -3

No files matched your search

@@ -0,0 +1,62 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { viewGame, type Game } from '../game/engine';
import { actIxSpice as act, advanceIxSpice as advance, createIxSpiceFixture,
orderIxSpiceCards } from './fixture-ix-spice-cards';
const seat = (game: Game, id: string) => {
const player = game.players.find(row => row.id === id); assert.ok(player); return player;
};
/** Native Basic Ix setup and actual Sandtrout draw; only unopened Spice order
* is selected by the existing fixture. */
function source() {
const f = createIxSpiceFixture();
let game = f.game;
orderIxSpiceCards(game, ['sandtrout', 'land']);
game = advance(game, state => state.turn === 1 && state.phase === 2 && !!state.phaseOpening);
assert.equal(game.sandtrout, true);
assert.ok(game.log.some(entry => String(entry.text ?? '').includes('Sandtrout')));
return game;
}
void test('an actual waiting Sandtrout does not block a bribe during the next Ix phase opening', () => {
const game = source(), before = structuredClone(game);
const opening = structuredClone(game.phaseOpening);
const payer = seat(game, 'fremen').spice, recipient = seat(game, 'atreides').spice;
const owed = seat(game, 'atreides').bribes;
const options = viewGame(game, 'fremen').bribeOptions;
assert.equal(options.blocked, null);
assert.ok(options.targets.some(target => target.id === 'atreides'));
let after = act(game, 'fremen', {type:'bribe', target:'atreides', amount:1});
assert.deepEqual(game, before);
assert.deepEqual(after.phaseOpening, opening);
assert.equal(seat(after, 'fremen').spice, payer - 1);
assert.equal(seat(after, 'atreides').spice, recipient);
assert.equal(seat(after, 'atreides').bribes, owed + 1);
assert.equal(after.sandtrout, true);
for (const difficulty of DIFFICULTIES) {
const view = viewGame(after, 'harkonnen');
view.players.find(row => row.id === 'harkonnen')!.bot = difficulty;
const ready = botActions(view).find(action => action.type === 'ready');
assert.ok(ready, difficulty); assert.doesNotThrow(() => act(after, 'harkonnen', ready), difficulty);
}
for (const player of after.players) after = act(after, player.id, {type:'ready'});
assert.equal(after.phaseOpening, null);
assert.equal(after.sandtrout, true);
assert.equal(seat(after, 'atreides').bribes, owed + 1);
});
void test('without Sandtrout the same Basic Ix opening still accepts a bribe', () => {
const f = createIxSpiceFixture();
let game = f.game;
orderIxSpiceCards(game, ['land']);
game = advance(game, state => state.turn === 1 && state.phase === 2 && !!state.phaseOpening);
assert.equal(!!game.sandtrout, false);
assert.equal(viewGame(game, 'fremen').bribeOptions.blocked, null);
const next = act(game, 'fremen', {type:'bribe', target:'atreides', amount:1});
assert.deepEqual(next.phaseOpening, game.phaseOpening);
assert.equal(seat(next, 'atreides').bribes, seat(game, 'atreides').bribes + 1);
});
@@ -0,0 +1,152 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { applyAction, createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine';
import { territory } from '../game/board';
import { placeTerror } from '../game/moritani-terror';
import { botActions, runBots } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { beforeIxSpiceDraw, orderIxSpiceCards } from './fixture-ix-spice-cards';
import { nextStrongholdFactionsNativeStep } from './fixture-stronghold-factions';
const act = (g: Game, id: string, action: Action) => applyAction(g, id, action);
function seat(g: Game, id: string) { const p = g.players.find(row => row.id === id); assert.ok(p); return p; }
const clean = (g: Game) => !g.response && !g.decision && !g.phaseOpening && !g.truthtrance && !g.pendingTreacheryDiscard;
function cards(g: Game) {
const ids = [...g.deck, ...g.discard, ...(g.auction?.cards.slice(g.auction.index) ?? []),
...(g.ixAuction?.cards ?? []), ...g.players.flatMap(p => p.hand)].map(c => c.id).sort();
assert.equal(new Set(ids).size, ids.length); return ids;
}
function advance(g: Game, until: (g: Game) => boolean): Game {
for (let n = 0; n < 2000; n++) {
if (until(g)) return g;
assert.notEqual(g.status, 'finished', JSON.stringify({turn:g.turn,phase:g.phase,log:g.log.filter(x => /Extortion|extortion/i.test(x.text))}));
if (g.status === 'setup' && g.setupStage === 'forces' && seat(g, 'ecaz').reserves === 20)
g = act(g, 'ecaz', {type:'ecazSetup', placements:{'imperial_basin:9':6}});
else if (g.status === 'setup' && !g.setupStage && g.decision?.kind === 'moritaniSetup')
g = act(g, 'moritani', {type:'decision', territory:'oh_gap', sector:9});
else if (g.decision?.kind === 'ecazPlacement' && !g.response && !g.phaseOpening)
g = act(g, 'ecaz', {type:'decision', decline:true});
else if (g.decision?.kind === 'moritaniPlacement' && !g.response && !g.phaseOpening)
g = act(g, 'moritani', {type:'decision', decline:true});
else {
const next = nextStrongholdFactionsNativeStep(g); assert.ok(next, `No native continuation at ${g.phase}/${g.decision?.kind}`);
g = act(g, next.actor, next.action);
}
}
throw Error('Native play did not reach the Extortion choice');
}
function passResponse(g: Game): Game {
while (g.response) {
const responder = g.players.find(p => !g.response!.passed.includes(p.id)); assert.ok(responder);
g = act(g, responder.id, {type:'passResponse'});
}
return g;
}
/** Genuine public setup, first Spice draw, Ecaz alliance and Extortion entry.
* Unopened Spice order, conserved board placement, Terror location, one Karama
* and the final payer's wallet are staged for this narrow timing case. */
function fixture() {
let g = createGame('EXTORTIONSANDTROUT', newPlayer('ecaz','Ecaz','ecaz'), false, ['ecaz','ix']);
for (const id of ['moritani','emperor','atreides','guild'] as const) joinGame(g, newPlayer(id,id,id));
for (const p of g.players) g = act(g,p.id,{type:'ready'});
g = act(g,'ecaz',{type:'start'});
g = advance(g, state => state.status === 'playing');
const inventory = cards(g);
for (const p of g.players) {
p.reserves += Object.values(p.forces).reduce((sum, amount) => sum + amount, 0);
p.forces = {};
}
g = advance(g, beforeIxSpiceDraw);
orderIxSpiceCards(g, ['sandtrout','land']);
g = advance(g, state => state.phase === 4 && !state.response && !state.phaseOpening && state.decision?.kind === 'ecazPlacement');
assert.equal(g.sandtrout,true);
const spots = ['habbanya_ridge_sietch','sietch_tabr','carthag','arrakeen','tueks_sietch']
.map(id => ({territory:id,sector:territory(id).sectors.find(s => s !== g.storm)}))
.filter((row): row is {territory:string;sector:number} => row.sector !== undefined);
assert.ok(spots.length >= 2);
const [allianceSpot, terrorSpot] = spots;
const self = g.ecazAmbassadors!.tokens.find(row => row.effect === 'ecaz'); assert.ok(self);
g = act(g,'ecaz',{type:'decision', token:self.id, territory:allianceSpot.territory});
g = advance(g, state => !state.response && state.decision?.kind === 'ecazPlacement');
g = act(g,'ecaz',{type:'decision',decline:true});
g = advance(g, state => state.phase === 5 && clean(state));
const terror = g.moritaniTerror!.tokens.find(row => row.kind === 'extortion'); assert.ok(terror);
g.moritaniTerror = placeTerror(g.moritaniTerror!,terror.id,terrorSpot.territory,g.turn);
let emperorEntered = false, atreidesEntered = false;
for (let n=0; g.phase === 5 && n<100; n++) {
if (!clean(g)) {
const next = nextStrongholdFactionsNativeStep(g); assert.ok(next);
g = act(g,next.actor,next.action); continue;
}
if (g.active === 'emperor' && !emperorEntered) {
g = act(g,'emperor',{type:'ship',amount:1,territory:allianceSpot.territory,sector:allianceSpot.sector});
g = advance(g,state => !state.response && state.pendingAmbassador?.stage === 'offer');
const event = g.pendingAmbassador!.event;
g = act(g,'ecaz',{type:'decision',event,trigger:true,beneficiary:'ecaz',choice:'alliance'});
g = act(g,'emperor',{type:'decision',event,accept:true});
assert.equal(g.pendingAmbassador?.stage,'loan');
g = act(g,'ecaz',{type:'decision',event,loan:false});
assert.equal(seat(g,'ecaz').ally,'emperor'); assert.equal(seat(g,'emperor').ally,'ecaz');
assert.equal(g.sandtrout,true); emperorEntered = true;
} else if (g.active === 'atreides' && !atreidesEntered) {
g = act(g,'atreides',{type:'ship',amount:1,territory:terrorSpot.territory,sector:terrorSpot.sector});
g = advance(g,state => !state.response && state.decision?.kind === 'moritaniTerror');
g = act(g,'moritani',{type:'decision',reveal:true});
assert.equal(g.moritaniTerror!.tokens.find(row => row.id === terror.id)?.status,'extortion');
atreidesEntered = true;
} else {
const next = nextStrongholdFactionsNativeStep(g); assert.ok(next);
g = act(g,next.actor,next.action);
}
}
assert.ok(emperorEntered && atreidesEntered); assert.equal(g.sandtrout,true);
assert.ok(g.moritaniExtortion, JSON.stringify({phase:g.phase,log:g.log.slice(-8)}));
g = advance(g,state => state.phase === 8 && state.decision?.kind === 'moritaniExtortion');
while (g.decision?.kind === 'moritaniExtortion' && g.decision.player !== 'ecaz')
g = act(g,g.decision.player,{type:'decision',event:g.decision.event,pay:false});
assert.ok(g.decision?.kind === 'moritaniExtortion' && g.decision.player === 'ecaz');
seat(g,'ecaz').spice = 2;
assert.ok(seat(g,'emperor').spice >= 1);
const guild = seat(g,'guild');
let karama = guild.hand.find(card => card.effect === 'karama');
if (!karama) {
const source = g.players.find(p => p.id !== 'guild' && p.hand.some(c => c.effect === 'karama'))?.hand ?? g.deck;
const index = source.findIndex(c => c.effect === 'karama'); assert.ok(index >= 0);
[karama] = source.splice(index,1); guild.hand.push(karama);
}
assert.deepEqual(cards(g),inventory);
return {g, token:terror.id, karama:karama.id, inventory};
}
for (const cancel of [false,true]) void test(`Basic Emperor gift during Extortion with waiting Sandtrout ${cancel ? 'can be canceled' : 'enables payment'}`, () => {
const f = fixture(), before = structuredClone(f.g);
const event = f.g.decision!.kind === 'moritaniExtortion' ? f.g.decision.event : '';
assert.ok(event); assert.equal(viewGame(f.g,'ecaz').extortion.pending?.canPay,false);
assert.ok(viewGame(f.g,'emperor').emperorExtortionGift);
for (const id of ['ecaz','atreides','guild','moritani']) assert.equal(viewGame(f.g,id).emperorExtortionGift,null);
let g = act(f.g,'emperor',{type:'emperorGift',amount:1});
assert.equal(g.response?.kind,'emperorGift');
g = cancel ? act(g,'guild',{type:'card',mode:'cancel',card:f.karama}) : passResponse(g);
assert.deepEqual(g.decision,before.decision); assert.deepEqual(g.moritaniExtortion,before.moritaniExtortion);
assert.equal(seat(g,'ecaz').spice,cancel ? 2 : 3);
assert.equal(seat(g,'emperor').spice,seat(before,'emperor').spice - (cancel ? 0 : 1));
assert.equal(g.sandtrout,true);
g = act(g,'ecaz',{type:'decision',event,pay:!cancel});
assert.equal(g.moritaniTerror!.tokens.find(row => row.id === f.token)?.status,cancel ? 'extortion' : 'removed');
assert.deepEqual(cards(g),f.inventory);
});
void test('a bot Ecaz waits for the human Emperor during Extortion while Sandtrout waits', () => {
const f = fixture();
assert.ok(f.g.decision?.kind === 'moritaniExtortion');
const event = f.g.decision.event;
for (const difficulty of DIFFICULTIES) {
let g = structuredClone(f.g); seat(g,'ecaz').bot = difficulty;
assert.deepEqual(viewGame(g,'emperor').emperorExtortionPriority,{event,recipient:'ecaz',canContinue:true});
assert.ok(!botActions(viewGame(g,'ecaz')).some(action => action.type === 'decision' && action.event === event));
const waiting = runBots(g,2); assert.equal(waiting.decision?.kind,'moritaniExtortion');
g = act(g,'emperor',{type:'emperorExtortionContinue',event});
const action = botActions(viewGame(g,'ecaz')).find(row => row.type === 'decision' && row.event === event);
assert.ok(action,difficulty); assert.doesNotThrow(() => act(g,'ecaz',action));
}
});
@@ -0,0 +1,133 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { botActions } from '../game/bots';
import { DIFFICULTIES } from '../game/bot-profiles';
import { createGame, joinGame, newPlayer, viewGame, type Action, type Game } from '../game/engine';
import { beforeIxSpiceDraw, orderIxSpiceCards } from './fixture-ix-spice-cards';
import { actTruthMovementNativeEarly as act, nextTruthMovementNativeEarlyStep } from './fixture-truth-movement-native-early';
import { truthMovementInventory as inventory } from './fixture-truth-movement';
const richese = 'richese', choam = 'choam', atreides = 'atreides';
function seat(game: Game, id: string) {
const player = game.players.find(row => row.id === id); assert.ok(player); return player;
}
const clean = (g: Game) => !g.response && !g.decision && !g.phaseOpening &&
!g.truthtrance && !g.pendingRevival && !g.pendingTreacheryDiscard;
function next(game: Game) {
if (game.turn === 1 && game.phase === 5 && game.active === richese && clean(game)) {
const player = seat(game, richese);
if (!player.shipped) {
const token = player.noField!.tokens.find(row => row.value === 3); assert.ok(token);
return { actor:richese, action:{ type:'ship', noField:token.id, event:player.noFieldEvent,
territory:'polar_sink', sector:0, allyPayment:0 } satisfies Action };
}
return { actor:richese, action:{type:'endMovement'} satisfies Action };
}
if (game.phase === 5 && game.active && clean(game))
return { actor:game.active, action:{type:'endMovement'} satisfies Action };
return nextTruthMovementNativeEarlyStep(game, { position:'first', method:'onceAround', direction:'clockwise' });
}
function reach(game: Game, until: (state: Game) => boolean) {
for (let i = 0; i < 2400; i++) {
if (until(game)) return game;
assert.notEqual(game.status, 'finished');
const step = next(game); assert.ok(step, `Original action missing at ${game.turn}/${game.phase}/${game.decision?.kind}`);
game = act(game, step.actor, step.action);
}
throw Error('Native game did not reach the requested Revival control');
}
/** Genuine Basic Ix/CHOAM public setup, paid marker and actual Sandtrout draw.
* Only unopened Spice order, physical prior casualties/board placement, and a
* physical La La La card location are staged. */
function source() {
let game = createGame('REVEALSANDTROUT', newPlayer(richese, 'Richese', 'richese'), false, ['ix', 'choam']);
joinGame(game, newPlayer(choam, 'CHOAM', 'choam'));
joinGame(game, newPlayer(atreides, 'Atreides', 'atreides'));
game = act(game, richese, {type:'moduleOption', module:'kullPreview', enabled:false});
for (const player of game.players) game = act(game, player.id, {type:'ready'});
game = act(game, richese, {type:'start'});
game = reach(game, state => state.status === 'playing');
game = reach(game, beforeIxSpiceDraw);
orderIxSpiceCards(game, ['land', 'land']);
game = reach(game, state => state.turn === 2 && beforeIxSpiceDraw(state));
assert.ok(seat(game, richese).noField?.deployed, 'The marker was shipped in the original first turn');
orderIxSpiceCards(game, ['sandtrout', 'land']);
game = reach(game, state => state.turn === 2 && state.phase === 4 && clean(state));
assert.equal(game.sandtrout, true, 'The original Sandtrout draw remains waiting');
const owner = seat(game, richese), marker = owner.noField?.deployed, event = owner.noFieldEvent;
assert.ok(marker); assert.ok(event);
assert.ok(owner.reserves >= 3);
owner.reserves -= 3; owner.tanks += 3;
const moved = owner.reserves - 1; assert.ok(moved >= 0);
owner.reserves -= moved; owner.forces['polar_sink:0'] = (owner.forces['polar_sink:0'] ?? 0) + moved;
const physical = inventory(game);
const c = seat(game, choam);
if (!c.hand.some(card => card.name === 'La La La')) {
let index = game.deck.findIndex(card => card.name === 'La La La');
if (index >= 0) c.hand.push(game.deck.splice(index, 1)[0]);
else {
const holder = game.players.find(player => player.hand.some(card => card.name === 'La La La'));
if (holder) {
index = holder.hand.findIndex(card => card.name === 'La La La');
c.hand.push(holder.hand.splice(index, 1)[0]);
} else {
index = game.discard.findIndex(card => card.name === 'La La La'); assert.ok(index >= 0);
c.hand.push(game.discard.splice(index, 1)[0]);
}
}
}
assert.deepEqual(inventory(game), physical);
game = act(game, richese, {type:'revive', amount:2, elite:0});
assert.ok(game.decision?.kind === 'choamFreeRevival');
assert.equal(game.decision.recipient, richese);
return {game, token:marker.tokenId, event, physical};
}
for (const deny of [false, true]) void test(
`Richese reveals its marker with waiting Sandtrout before CHOAM ${deny ? 'prevents' : 'allows'} its free return`, () => {
const f = source(), before = structuredClone(f.game), p = seat(f.game, richese);
const field = p.forces['polar_sink:0'] ?? 0, tanks = p.tanks, revived = p.revived;
const forceCount = (g: Game) => {
const owner = seat(g, richese);
return owner.reserves + owner.tanks + Object.values(owner.forces).reduce((sum, amount) => sum + amount, 0);
};
assert.equal(p.reserves, 1); assert.equal(forceCount(f.game), 20);
const ownerView = viewGame(f.game, richese);
assert.equal(ownerView.richeseNoField?.canReveal, true);
let game = act(f.game, richese, {type:'revealNoField', token:f.token, event:f.event});
assert.deepEqual(f.game, before);
assert.deepEqual(game.decision, before.decision);
assert.deepEqual(game.pendingRevival, before.pendingRevival);
assert.equal(game.sandtrout, true);
assert.equal(seat(game, richese).reserves, 0);
assert.equal(seat(game, richese).forces['polar_sink:0'], field + 1);
assert.equal(forceCount(game), 20);
for (const difficulty of DIFFICULTIES) {
const view = viewGame(game, choam); view.players.find(row => row.id === choam)!.bot = difficulty;
const action = botActions(view)[0]; assert.ok(action, difficulty);
assert.doesNotThrow(() => act(game, choam, action), difficulty);
}
if (deny) {
const card = seat(game, choam).hand.find(row => row.name === 'La La La'); assert.ok(card);
game = act(game, choam, {type:'card', mode:'choam', card:card.id});
} else game = act(game, choam, {type:'decision', decline:true});
for (let i = 0; (game.response || game.pendingTreacheryDiscard) && i < 40; i++) {
if (game.pendingTreacheryDiscard?.reaction) {
const reaction = game.pendingTreacheryDiscard.reaction; assert.ok(reaction.stage === 'offer');
const player = game.players.find(row => !reaction.passed.includes(row.id)); assert.ok(player);
game = act(game, player.id, {type:'semutaPass', event:game.pendingTreacheryDiscard.batch.event});
} else if (game.response) {
const player = game.players.find(row => !game.response!.passed.includes(row.id)); assert.ok(player);
game = act(game, player.id, {type:'passResponse'});
} else break;
}
assert.equal(game.pendingRevival, null);
assert.equal(seat(game, richese).tanks, tanks - (deny ? 0 : 2));
assert.equal(seat(game, richese).reserves, deny ? 0 : 2);
assert.equal(seat(game, richese).revived, revived + (deny ? 0 : 2));
assert.equal(seat(game, richese).forces['polar_sink:0'], field + 1);
assert.equal(game.sandtrout, true); assert.equal(forceCount(game), 20);
assert.deepEqual(inventory(game), f.physical);
},
);