153 lines
9.1 KiB
TypeScript
153 lines
9.1 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 { 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));
|
|
}
|
|
});
|