152 lines
9.3 KiB
TypeScript
152 lines
9.3 KiB
TypeScript
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} from '../game/bots';
|
|
import {DIFFICULTIES} from '../game/bot-profiles';
|
|
import {takeKullCard} from './fixture-choam-kull';
|
|
import {advanceSemutaNexusTrade, nextSemutaNexusTradeNativeStep} 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 player=game.players.find(row=>row.id===id);assert.ok(player);return player;};
|
|
const quiet=(game:Game)=>!game.phaseOpening&&!game.response&&!game.decision&&!game.truthtrance&&!game.pendingTreacheryDiscard;
|
|
function inventory(game:Game) {
|
|
const cards=[...game.deck,...game.discard,...(game.richeseCache??[]),...(game.richeseRemoved??[]),
|
|
...(game.auction?.cards.slice(game.auction.index+Number(game.currentAuctionSale?.origin==='normal'))??[]),
|
|
...(game.ixAuction?.cards??[]),...game.players.flatMap(player=>player.hand)].map(card=>card.id).sort();
|
|
assert.equal(new Set(cards).size,cards.length);return cards;
|
|
}
|
|
function finishResponses(game:Game):Game {
|
|
for(let n=0;game.response&&n<20;n++) {
|
|
const actor=game.players.find(player=>!game.response!.passed.includes(player.id));assert.ok(actor);
|
|
game=act(game,actor.id,{type:'passResponse'});
|
|
}
|
|
return game;
|
|
}
|
|
function finishDiscardOffers(game:Game):Game {
|
|
for(let n=0;game.pendingTreacheryDiscard?.reaction?.stage==='offer'&&n<20;n++) {
|
|
const pending=game.pendingTreacheryDiscard,reaction=pending.reaction;
|
|
assert.ok(reaction&&reaction.stage==='offer');
|
|
const actor=game.players.find(player=>!reaction.passed.includes(player.id));assert.ok(actor);
|
|
game=act(game,actor.id,{type:'semutaPass',event:pending.batch.event});
|
|
}
|
|
return game;
|
|
}
|
|
|
|
/** Genuine public Advanced setup, real second-turn worm, Emperor/Guild
|
|
* alliance and original paid cache sale. Staged unopened Spice order,
|
|
* conserved starting board positions, purses and physical hand/deck custody. */
|
|
function fixture(method:'onceAround'|'silent', withPromise=false) {
|
|
let game=createGame('ADVRICHGIFT',newPlayer('e','Emperor','emperor'),true,['choam','ix']);
|
|
for(const [id,faction] of [['g','guild'],['c','choam'],['r','richese'],['a','atreides']] as const)
|
|
joinGame(game,newPlayer(id,faction,faction));
|
|
for(const player of game.players)game=act(game,player.id,{type:'ready'});
|
|
game=act(game,'e',{type:'start',advancedPreview:true});
|
|
game=advanceSemutaNexusTrade(game,state=>state.status==='playing');
|
|
for(const player of game.players){player.reserves+=Object.values(player.forces).reduce((sum,n)=>sum+n,0);player.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&&quiet(state));
|
|
game=act(game,'e',{type:'alliance',target:'g'});game=act(game,'g',{type:'alliance',target:'e'});
|
|
game=advanceSemutaNexusTrade(game,state=>state.phase===3&&state.phaseOpening?.initialize===true);
|
|
for(const player of game.players)game.deck.push(...player.hand.splice(0));
|
|
const karama=takeKullCard(game,'karama');seat(game,'a').hand.push(karama);
|
|
const truth=withPromise ? takeKullCard(game,'truthtrance') : null;
|
|
if(truth)seat(game,'a').hand.push(truth);
|
|
seat(game,'g').spice=7;seat(game,'e').spice=8;
|
|
game=advanceSemutaNexusTrade(game,state=>state.decision?.kind==='richeseCache');
|
|
const selected=game.richeseCache?.[0];assert.ok(selected);
|
|
const physical=inventory(game),card=selected.id;
|
|
game=act(game,'r',{type:'decision',event:game.richeseBidding!.event,card,method,
|
|
...(method==='onceAround'?{direction:'clockwise'}:{})});
|
|
game=act(game,'e',{type:'pledgeAid',amount:1});
|
|
if(truth) {
|
|
game=act(game,'a',{type:'card',card:truth.id});
|
|
for(let n=0;game.truthtrance?.stage==='priority'&&n<20;n++) {
|
|
const actor=game.players.find(player=>!game.truthtrance!.passed.includes(player.id));assert.ok(actor);
|
|
game=act(game,actor.id,{type:'truthPass'});
|
|
}
|
|
assert.equal(game.truthtrance?.stage,'ask');
|
|
game=act(game,'a',{type:'truthAsk',question:{kind:'pledgeAid',target:'e',
|
|
turn:game.turn,phase:3,ally:'g',minimum:4}});
|
|
assert.ok(viewGame(game,'e').truthPledgeAnswers?.includes('yes'));
|
|
game=act(game,'e',{type:'truthAnswer',answer:'yes'});
|
|
}
|
|
let bid=false;
|
|
for(let n=0;!game.pendingEmperorAuctionGift&&n<300;n++) {
|
|
const lot=game.richeseAuction;
|
|
if(lot&&!lot.outcome&&!game.response&&!game.decision&&lot.method==='onceAround'&&lot.active) {
|
|
const buyer=lot.active==='g'&&!bid;
|
|
game=act(game,lot.active,{type:'richeseBid',event:lot.event,amount:buyer?3:null,...(buyer?{allyPayment:1}:{})});
|
|
if(buyer)bid=true;
|
|
} else if(lot&&!lot.outcome&&!game.response&&!game.decision&&lot.method==='silent') {
|
|
const actor=lot.eligible.find(id=>!Object.hasOwn(lot.sealed,id));assert.ok(actor);
|
|
const buyer=actor==='g'&&!bid;
|
|
game=act(game,actor,{type:'richeseBid',event:lot.event,amount:buyer?3:0,...(buyer?{allyPayment:1}:{})});
|
|
if(buyer)bid=true;
|
|
} else {
|
|
const next=nextSemutaNexusTradeNativeStep(game);game=act(game,next.actor,next.action);
|
|
}
|
|
}
|
|
assert.equal(bid,true);assert.ok(game.pendingEmperorAuctionGift,'Emperor receives the original cache purchase opportunity');
|
|
assert.equal(game.richeseAuction?.outcome?.kind,'sold');assert.equal(game.currentAuctionSale??null,null);
|
|
assert.equal(viewGame(game,'e').emperorAuctionGift?.recipient,'g');
|
|
return {game,card,karama:karama.id,physical};
|
|
}
|
|
|
|
for(const method of ['onceAround','silent'] as const) for(const mode of ['continue','allow','cancel'] as const) void test(
|
|
`Advanced ${method} cache purchase retains funding and bank routing after Emperor ${mode}`,()=>{
|
|
const f=fixture(method),before=structuredClone(f.game),lot=structuredClone(f.game.richeseAuction);
|
|
const funding=structuredClone(f.game.richeseFunding),aid=structuredClone(f.game.aid);
|
|
const offer=viewGame(f.game,'e').emperorAuctionGift;assert.ok(offer);
|
|
for(const difficulty of DIFFICULTIES) {
|
|
const view=viewGame(f.game,'e');view.players.find(player=>player.id==='e')!.bot=difficulty;
|
|
const action=botActions(view).find(row=>row.type==='emperorAuctionGiftContinue');assert.ok(action,difficulty);
|
|
assert.doesNotThrow(()=>act(f.game,'e',action),difficulty);
|
|
}
|
|
let game=f.game;
|
|
if(mode!=='continue') {
|
|
game=act(game,'e',{type:'emperorGift',amount:2});assert.equal(game.response?.kind,'emperorGift');
|
|
if(mode==='cancel')game=finishDiscardOffers(act(game,'a',{type:'card',mode:'cancel',card:f.karama}));
|
|
else game=finishResponses(game);
|
|
}
|
|
assert.deepEqual(game.richeseAuction,lot);assert.deepEqual(game.richeseFunding,funding);assert.deepEqual(game.aid,aid);
|
|
assert.equal(game.pendingEmperorAuctionGift?.event,offer.event);
|
|
assert.equal(seat(game,'e').spice,seat(before,'e').spice-(mode==='allow'?2:0));
|
|
assert.equal(seat(game,'g').spice,seat(before,'g').spice+(mode==='allow'?2:0));
|
|
game=act(game,'e',{type:'emperorAuctionGiftContinue',event:offer.event});
|
|
game=finishResponses(game);
|
|
assert.ok(seat(game,'g').hand.some(row=>row.id===f.card));
|
|
assert.equal(seat(game,'g').spice,seat(before,'g').spice+(mode==='allow'?2:0)-2);
|
|
assert.equal(seat(game,'e').spice,seat(before,'e').spice-(mode==='allow'?2:0));
|
|
assert.equal(game.aid.e?.amount??0,0);
|
|
assert.equal(seat(game,'r').spice,seat(before,'r').spice+2,'Emperor-funded one goes to the bank, buyer-funded two to Richese');
|
|
assert.deepEqual(inventory(game),f.physical);
|
|
});
|
|
|
|
void test('Advanced cache gift allows a live pledge Yes without changing the original winning split',()=>{
|
|
const f=fixture('onceAround',true),before=structuredClone(f.game);
|
|
const lot=structuredClone(f.game.richeseAuction),funding=structuredClone(f.game.richeseFunding);
|
|
const offer=viewGame(f.game,'e').emperorAuctionGift;assert.ok(offer);
|
|
assert.equal(f.game.aid.e?.amount,1);
|
|
assert.equal(f.game.pendingEmperorAuctionGift?.quote?.allyPayment,1);
|
|
assert.ok(f.game.pledgePromises?.at(-1)?.answer);
|
|
const guide=viewGame(f.game,'e').pledgePromiseGuide;assert.ok(guide);
|
|
assert.deepEqual(guide.action,{type:'pledgeAid',amount:4,emperorAuctionGiftEvent:offer.event});
|
|
let game=act(f.game,'e',guide.action);
|
|
assert.equal(game.pledgePromises?.at(-1)?.fulfilled,true);
|
|
assert.equal(game.aid.e?.amount,4);
|
|
assert.deepEqual(game.richeseAuction,lot);assert.deepEqual(game.richeseFunding,funding);
|
|
assert.equal(game.pendingEmperorAuctionGift?.quote?.allyPayment,1);
|
|
game=act(game,'e',{type:'emperorAuctionGiftContinue',event:offer.event});
|
|
game=finishResponses(game);
|
|
assert.ok(seat(game,'g').hand.some(row=>row.id===f.card));
|
|
assert.equal(seat(game,'g').spice,seat(before,'g').spice-2);
|
|
assert.equal(seat(game,'r').spice,seat(before,'r').spice+2);
|
|
assert.equal(game.aid.e?.amount,3,'Only the one-spice winning contribution is spent');
|
|
assert.equal(seat(game,'e').spice,seat(before,'e').spice-3);
|
|
assert.equal(game.pledgePromises?.at(-1)?.fulfilled,true);
|
|
assert.deepEqual(inventory(game),f.physical);
|
|
});
|