Support mixed-deck Emperor gifts at Ix openings
Verify and publish checkpoint container / container (push) Successful in 10m47s

This commit is contained in:
Quintine committed 2026-11-16 13:40:59 +11:00
1 parent 0c64d320ea
commit 66c563be71
7 files changed
+204 -14

No files matched your search

@@ -0,0 +1,150 @@
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 {botActions, runBots} from '../game/bots';
import {DIFFICULTIES} from '../game/bot-profiles';
import {territory} from '../game/board';
import {takeKullCard} from './fixture-choam-kull';
import {nextStrongholdFactionsNativeStep} from './fixture-stronghold-factions';
import {advanceSemutaNexusTrade} from './fixture-semuta-nexus-trade';
import {beforeIxSpiceDraw, orderIxSpiceCards} from './fixture-ix-spice-cards';
const act=(game:Game,id:string,action:Action)=>applyAction(game,id,action);
const seat=(game:Game,id:string)=>{const p=game.players.find(row=>row.id===id);assert.ok(p);return p;};
function inventory(game:Game) {
const cards=[...game.deck,...game.discard,...(game.richeseCache??[]),...(game.richeseRemoved??[]),
...(game.ixSetupCards??[]),...game.players.flatMap(p=>p.hand)].map(card=>card.id).sort();
assert.equal(new Set(cards).size,cards.length);return cards;
}
function passResponse(game:Game) {
for(let n=0;game.response&&n<20;n++) {
const p=game.players.find(row=>!game.response!.passed.includes(row.id));assert.ok(p);
game=act(game,p.id,{type:'passResponse'});
}
return game;
}
function passSemuta(game:Game) {
for(let n=0;game.pendingTreacheryDiscard?.reaction?.stage==='offer'&&n<game.players.length;n++) {
const pending=game.pendingTreacheryDiscard,reaction=pending.reaction;assert.ok(reaction?.stage==='offer');
const p=game.players.find(row=>!reaction.passed.includes(row.id));assert.ok(p);
game=act(game,p.id,{type:'semutaPass',event:pending.batch.event});
}
return game;
}
/** Public Basic selected-deck setup; stage the alliance, original physical
* hands and wallets after setup. No opening, pass or reaction is invented. */
function fixture(nexus:boolean, independent=false) {
let game=createGame('MIXOPEN',newPlayer('m','Moritani','moritani'),false,['ix','choam','ecaz']);
for(const [id,faction] of [['e','emperor'],['a','atreides'],['c','choam'],['r','richese'],['i','ixians']] as const)
joinGame(game,newPlayer(id,faction,faction));
if(nexus)game=act(game,'m',{type:'moduleOption',module:'nexusCards',enabled:true});
if(independent)game=act(game,'m',{type:'moduleOption',module:'ecazTreachery',enabled:true});
for(const p of game.players)game=act(game,p.id,{type:'ready'});
game=act(game,'m',{type:'start'});
assert.equal(game.kullPreview,true);
assert.equal(independent ? game.semutaRecruitsPreview : game.semutaPreview,true);
for(let n=0;game.status==='setup'&&n<100;n++) {
if(game.decision?.kind==='moritaniSetup')game=act(game,'m',{type:'decision',territory:'sietch_tabr',sector:territory('sietch_tabr').sectors[0]});
else {const step=nextStrongholdFactionsNativeStep(game);assert.ok(step);game=act(game,step.actor,step.action);}
}
assert.equal(game.status,'playing');assert.ok(game.phaseOpening);
const original=inventory(game);
seat(game,'e').ally='a';seat(game,'a').ally='e';seat(game,'e').spice=10;seat(game,'a').spice=3;
for(const p of game.players)game.deck.push(...p.hand.splice(0));
const kull=takeKullCard(game,'ix-kull-wahad');seat(game,'c').hand.push(kull);
const originalKarama=takeKullCard(game,'karama');seat(game,'m').hand.push(originalKarama);
const counter=takeKullCard(game,'karama');seat(game,'i').hand.push(counter);
const semuta=takeKullCard(game,'richese-semuta-drug');seat(game,'r').hand.push(semuta);
assert.deepEqual(inventory(game),original);
for(const id of ['e','a','r','i'])game=act(game,id,{type:'ready'});
assert.deepEqual(viewGame(game,'e').emperorPhaseOpeningGift,{recipient:'a',maximum:10});
return {game,original,kull:kull.id,karama:originalKarama.id,counter:counter.id,semuta:semuta.id};
}
for(const nexus of [false,true]) for(const mode of ['allow','decline','success','counter'] as const) void test(
`Basic mixed-deck${nexus?' Nexus':''} opening gift ${mode} resumes original passes`,()=>{
const f=fixture(nexus),before=structuredClone(f.game.phaseOpening);
let game=act(f.game,'e',{type:'emperorGift',amount:2});
if(mode==='allow')game=passResponse(game);
else {
game=act(game,'m',{type:'card',mode:'cancel',card:f.karama});assert.equal(game.pendingKull?.stage,'offer');
game=act(game,'c',mode==='decline'?{type:'kullDecision',event:game.pendingKull!.event,decline:true}:
{type:'kullDecision',event:game.pendingKull!.event,source:'printed',card:f.kull});
if(mode==='success') {assert.equal(game.pendingKull?.stage,'counter');game=passResponse(game);}
if(mode==='counter') {
assert.equal(game.pendingKull?.stage,'counter');
game=act(game,'i',{type:'card',mode:'cancel',card:f.counter});
}
assert.equal(game.pendingTreacheryDiscard?.reaction?.stage,'offer');
const event=game.pendingTreacheryDiscard!.batch.event;
if(mode==='counter')game=act(game,'r',{type:'semutaCommit',event});else game=passSemuta(game);
if(game.pendingTreacheryDiscard?.reaction?.stage==='offer')game=passSemuta(game);
if(game.response)game=passResponse(game);
}
assert.deepEqual(game.phaseOpening,before);
assert.equal(seat(game,'e').spice,mode==='allow'||mode==='success'?8:10);
assert.equal(seat(game,'a').spice,mode==='allow'||mode==='success'?5:3);
assert.deepEqual(inventory(game),f.original);
});
void test('Basic mixed-deck opening waits for the human Emperor before the final bot pass',()=>{
const f=fixture(true);let game=act(f.game,'m',{type:'ready'});
seat(game,'c').bot='Easy';const priority=viewGame(game,'e').emperorPhaseOpeningPriority;assert.ok(priority);
for(const level of DIFFICULTIES) {
const view=viewGame(game,'c');view.players.find(p=>p.id==='c')!.bot=level;
assert.ok(!botActions(view).some(action=>action.type==='ready'));
}
assert.deepEqual(runBots(game,5).phaseOpening,game.phaseOpening);
game=act(game,'e',{type:'emperorPhaseOpeningContinue',event:priority.event});
game=act(game,'c',{type:'ready'});assert.equal(game.phaseOpening,null);
});
void test('Basic independent Ecaz cards retain the opening gift and Semuta cancellation',()=>{
const f=fixture(false,true),opening=structuredClone(f.game.phaseOpening);
let game=act(f.game,'e',{type:'emperorGift',amount:2});
game=act(game,'m',{type:'card',mode:'cancel',card:f.karama});
assert.equal(game.pendingKull?.stage,'offer');
game=act(game,'c',{type:'kullDecision',event:game.pendingKull!.event,decline:true});
assert.equal(game.pendingTreacheryDiscard?.reaction?.stage,'offer');
game=passSemuta(game);
assert.deepEqual(game.phaseOpening,opening);assert.equal(seat(game,'e').spice,10);
assert.deepEqual(inventory(game),f.original);
});
void test('Basic independent Ecaz cards preserve the original gift through a Kull counter',()=>{
const f=fixture(false,true),opening=structuredClone(f.game.phaseOpening);
let game=act(f.game,'e',{type:'emperorGift',amount:2});
game=act(game,'m',{type:'card',mode:'cancel',card:f.karama});
game=act(game,'c',{type:'kullDecision',event:game.pendingKull!.event,source:'printed',card:f.kull});
game=act(game,'i',{type:'card',mode:'cancel',card:f.counter});
const pending=game.pendingTreacheryDiscard;assert.equal(pending?.reaction?.stage,'offer');
game=act(game,'r',{type:'semutaCommit',event:pending!.batch.event});
if(game.pendingTreacheryDiscard?.reaction?.stage==='offer')game=passSemuta(game);
if(game.response)game=passResponse(game);
assert.deepEqual(game.phaseOpening,opening);assert.equal(seat(game,'e').spice,10);
assert.equal(seat(game,'r').hand.some(card=>card.id===f.counter),true);
assert.deepEqual(inventory(game),f.original);
});
void test('Basic mixed-deck gift keeps an earned Nexus and the later Ix opening',()=>{
const f=fixture(true);let game=f.game;
seat(game,'e').ally=null;seat(game,'a').ally=null;
for(const p of game.players) {
p.reserves+=Object.values(p.forces).reduce((sum,n)=>sum+n,0);p.forces={};
if(p.elites) {p.elites.reserves+=Object.values(p.elites.forces).reduce((sum,n)=>sum+n,0);p.elites.forces={};}
}
game=advanceSemutaNexusTrade(game,beforeIxSpiceDraw);orderIxSpiceCards(game,['land','land']);
game=advanceSemutaNexusTrade(game,state=>state.turn===2&&beforeIxSpiceDraw(state));
orderIxSpiceCards(game,['worm','land','land']);
game=advanceSemutaNexusTrade(game,state=>!!state.nexus&&!state.spiceWindow&&!state.phaseOpening&&!state.response&&!state.decision);
game=act(game,'e',{type:'alliance',target:'a'});game=act(game,'a',{type:'alliance',target:'e'});
game=advanceSemutaNexusTrade(game,state=>state.nexusCards?.phase?.stage==='drawing');
assert.ok(game.nexusCards!.phase!.eligible.includes('i'));
game=act(game,'i',{type:'nexusCardChoice',turn:game.turn,card:null,choice:'draw',ownRedraws:0});
const card=game.nexusCards!.cards!.hands.i;assert.ok(card);
game=advanceSemutaNexusTrade(game,state=>state.phase===3&&!!state.phaseOpening&&!state.response&&!state.decision);
const opening=structuredClone(game.phaseOpening),balance=seat(game,'e').spice;
assert.ok(viewGame(game,'e').emperorPhaseOpeningGift);
game=act(game,'e',{type:'emperorGift',amount:1});game=passResponse(game);
assert.deepEqual(game.phaseOpening,opening);assert.equal(seat(game,'e').spice,balance-1);
assert.equal(game.nexusCards!.cards!.hands.i,card);
});