Support group battle preparation powers
Verify and publish checkpoint container / container (push) Successful in 10m57s
Verify and publish checkpoint container / container (push) Successful in 10m57s
This commit is contained in:
1 parent
d8d2e7207b
commit
83ccd71821
10 files changed
+405
-26
No files matched your search
@@ -0,0 +1,102 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { coalitionOf, emptyCoalitionState } from '../game/coalition-alliance';
|
||||
import { coalitionBattleBeneficiary } from '../game/coalition-battle-preparation';
|
||||
import { nativeBattlePowerOpportunity, type NativeBattlePowerGame } from '../game/battle-power-question';
|
||||
import { applyAction, createGame, newPlayer } from '../game/engine';
|
||||
|
||||
/** The ledger starts staged because the lobby does not expose group play;
|
||||
* the pair and unanimous third admission use real Nexus actions. */
|
||||
function groupGame(): NativeBattlePowerGame {
|
||||
let game = createGame('GROUPPOWER', newPlayer('bg', 'Bene Gesserit', 'beneGesserit'), false);
|
||||
game.players.push(newPlayer('a', 'Atreides', 'atreides'), newPlayer('e', 'Emperor', 'emperor'),
|
||||
newPlayer('h', 'Harkonnen', 'harkonnen'));
|
||||
Object.assign(game, {status:'playing', phase:1, turn:2, nexus:true, order:['bg','a','e','h'],
|
||||
ready:[], storm:18, response:null, decision:null, phaseOpening:null});
|
||||
game.coalitionState = emptyCoalitionState();
|
||||
game = applyAction(game, 'bg', {type:'alliance', target:'a'});
|
||||
game = applyAction(game, 'a', {type:'alliance', target:'bg'});
|
||||
const pair = coalitionOf(game.players, game.coalitionState, 'bg'); assert.ok(pair);
|
||||
game = applyAction(game, 'bg', {type:'coalitionPropose', groupId:pair.id, entrant:'e',
|
||||
expectedRevision:game.coalitionState!.revision});
|
||||
for (const id of ['a', 'e']) {
|
||||
const proposal = game.coalitionState!.proposal!;
|
||||
game = applyAction(game, id, {type:'coalitionAnswer', event:proposal.event, nexus:proposal.nexus,
|
||||
turn:proposal.turn, accept:true, expectedRevision:game.coalitionState!.revision});
|
||||
}
|
||||
assert.deepEqual(coalitionOf(game.players, game.coalitionState, 'bg')!.members, ['a','bg','e']);
|
||||
return {status:'playing', phase:6, advanced:false, expansions:[], players:game.players,
|
||||
coalitionState:game.coalitionState,
|
||||
battle:{event:'group-battle', territory:'carthag', attacker:'e', defender:'h', revealed:false,
|
||||
prepared:true, preparation:{kind:'voice', owner:'bg', beneficiary:'e'}}};
|
||||
}
|
||||
|
||||
void test('group Voice and Prescience select either actual allied combatant against an outsider', () => {
|
||||
const source = groupGame();
|
||||
const before = structuredClone(source);
|
||||
for (const [owner, otherAlly] of [['bg','a'], ['a','bg']] as const) {
|
||||
for (const ally of [otherAlly, 'e']) {
|
||||
assert.equal(coalitionBattleBeneficiary(source, owner, ally, 'h'), ally);
|
||||
assert.equal(coalitionBattleBeneficiary(source, owner, 'h', ally, true), ally);
|
||||
}
|
||||
assert.equal(coalitionBattleBeneficiary(source, owner, owner, 'h'), owner);
|
||||
}
|
||||
assert.deepEqual(source, before);
|
||||
});
|
||||
|
||||
void test('group Voice is ready now and allied Prescience remains the future owned opportunity', () => {
|
||||
const source = groupGame();
|
||||
assert.deepEqual(nativeBattlePowerOpportunity(source, 'bg', 'voice'), {
|
||||
event:'group-battle', territory:'carthag', power:'voice', owner:'bg', beneficiary:'e', target:'h', ready:true,
|
||||
});
|
||||
assert.deepEqual(nativeBattlePowerOpportunity(source, 'a', 'prescience'), {
|
||||
event:'group-battle', territory:'carthag', power:'prescience', owner:'a', beneficiary:'e', target:'h', ready:false,
|
||||
});
|
||||
source.battle!.preparation = {kind:'prescience', owner:'a', beneficiary:'e'};
|
||||
assert.equal(nativeBattlePowerOpportunity(source, 'a', 'prescience')?.ready, true);
|
||||
assert.equal(nativeBattlePowerOpportunity(source, 'bg', 'voice'), null);
|
||||
source.response = {};
|
||||
assert.equal(nativeBattlePowerOpportunity(source, 'a', 'prescience')?.ready, false);
|
||||
});
|
||||
|
||||
void test('each group ally supplies the beneficiary for the corresponding native power', () => {
|
||||
const source = groupGame();
|
||||
source.battle!.attacker = 'a';
|
||||
source.battle!.preparation = {kind:'voice', owner:'bg', beneficiary:'a'};
|
||||
assert.equal(nativeBattlePowerOpportunity(source, 'bg', 'voice')?.beneficiary, 'a');
|
||||
source.battle!.attacker = 'bg';
|
||||
source.battle!.preparation = {kind:'prescience', owner:'a', beneficiary:'bg'};
|
||||
assert.equal(nativeBattlePowerOpportunity(source, 'a', 'prescience')?.beneficiary, 'bg');
|
||||
});
|
||||
|
||||
void test('unsupported group profiles and ambiguous battles cannot borrow an ally power', () => {
|
||||
const source = groupGame();
|
||||
for (const patch of [{advanced:true}, {expansions:['ix']}, {homeworlds:{}},
|
||||
{nexusCards:{}}, {leaderSkills:{}}, {techTokens:{}}, {strongholdCards:{}},
|
||||
{ecazTreachery:true}, {discoveryEnabled:true}]) {
|
||||
const unsupported = {...source, ...patch};
|
||||
assert.equal(coalitionBattleBeneficiary(unsupported, 'bg', 'e', 'h'), null);
|
||||
assert.equal(nativeBattlePowerOpportunity(unsupported, 'bg', 'voice'), null);
|
||||
}
|
||||
assert.equal(coalitionBattleBeneficiary(source, 'bg', 'a', 'e'), null);
|
||||
assert.equal(coalitionBattleBeneficiary(source, 'bg', 'bg', 'a'), null);
|
||||
assert.equal(coalitionBattleBeneficiary(source, 'bg', 'e', 'e'), null);
|
||||
source.battle!.attacker = 'a'; source.battle!.defender = 'e';
|
||||
source.battle!.preparation = {kind:'voice', owner:'bg', beneficiary:'a'};
|
||||
assert.equal(nativeBattlePowerOpportunity(source, 'bg', 'voice'), null);
|
||||
});
|
||||
|
||||
void test('legacy self and reciprocal-pair selection keep their existing behavior', () => {
|
||||
const pair: NativeBattlePowerGame = {status:'playing', phase:6, players:[
|
||||
{id:'bg', faction:'beneGesserit', ally:'e'}, {id:'e', faction:'emperor', ally:'bg'},
|
||||
{id:'h', faction:'harkonnen', ally:null},
|
||||
], battle:{event:'pair', territory:'carthag', attacker:'e', defender:'h', revealed:false,
|
||||
preparation:{kind:'voice', owner:'bg', beneficiary:'e'}}};
|
||||
assert.equal(coalitionBattleBeneficiary(pair, 'bg', 'bg', 'h', true), 'bg');
|
||||
assert.equal(coalitionBattleBeneficiary(pair, 'bg', 'e', 'h', true), 'e');
|
||||
assert.equal(nativeBattlePowerOpportunity(pair, 'bg', 'voice')?.beneficiary, 'e');
|
||||
pair.players = pair.players.map(player => player.id === 'e' ? {...player, ally:null} : player);
|
||||
assert.equal(coalitionBattleBeneficiary(pair, 'bg', 'e', 'h'), 'e');
|
||||
assert.equal(coalitionBattleBeneficiary(pair, 'bg', 'e', 'h', true), null);
|
||||
assert.equal(nativeBattlePowerOpportunity(pair, 'bg', 'voice'), null);
|
||||
});
|
||||
@@ -0,0 +1,192 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { createElement } from 'react';
|
||||
import { renderToStaticMarkup } from 'react-dom/server';
|
||||
import { BattlePreparation } from '../components/battle-preparation';
|
||||
import { baseDeck } from '../game/cards';
|
||||
import { coalitionOf, emptyCoalitionState } from '../game/coalition-alliance';
|
||||
import { botActions } from '../game/bots';
|
||||
import { applyAction, battles, createGame, newPlayer, viewGame, type Action, type Game } from '../game/engine';
|
||||
|
||||
type Power = 'voice' | 'prescience';
|
||||
const send = (game: Game, id: string, action: Action) => applyAction(game, id, action);
|
||||
const player = (game: Game, id: string) => game.players.find(seat => seat.id === id)!;
|
||||
|
||||
/** Group entry is unavailable in the lobby. Stage a Nexus and form the pair
|
||||
* and group with real actions, then stage conserved forces at an unresolved
|
||||
* battle. Card acquisition is a physical transfer from the undealt deck. */
|
||||
function source(power: Power, beneficiary: 'e' | 'h' | 'owner', group = true) {
|
||||
const faction = power === 'voice' ? 'beneGesserit' : 'atreides';
|
||||
const owner = power === 'voice' ? 'b' : 'a';
|
||||
let game = createGame('GROUPBATTLE', newPlayer(owner, faction, faction), false);
|
||||
game.players.push(newPlayer('e', 'Emperor', 'emperor'),
|
||||
newPlayer('h', 'Harkonnen', 'harkonnen'), newPlayer('g', 'Guild', 'guild'));
|
||||
Object.assign(game, {status:'playing', phase:1, turn:2, nexus:true,
|
||||
order:[owner,'e','h','g'], ready:[], storm:18, deck:baseDeck(),
|
||||
response:null, decision:null, phaseOpening:null});
|
||||
game.coalitionState = emptyCoalitionState();
|
||||
game = send(game, owner, {type:'alliance', target:'e'});
|
||||
game = send(game, 'e', {type:'alliance', target:owner});
|
||||
if (group) {
|
||||
const pair = coalitionOf(game.players, game.coalitionState, owner); assert.ok(pair);
|
||||
game = send(game, owner, {type:'coalitionPropose', groupId:pair.id, entrant:'h',
|
||||
expectedRevision:game.coalitionState!.revision});
|
||||
for (const id of ['e','h']) {
|
||||
const proposal = game.coalitionState!.proposal!;
|
||||
game = send(game, id, {type:'coalitionAnswer', event:proposal.event, nexus:proposal.nexus,
|
||||
turn:proposal.turn, accept:true, expectedRevision:game.coalitionState!.revision});
|
||||
}
|
||||
}
|
||||
const beneficiaryId = beneficiary === 'owner' ? owner : beneficiary;
|
||||
Object.assign(game, {phase:6, nexus:false, ready:[], response:null, decision:null,
|
||||
phaseOpening:null, battle:null});
|
||||
for (const seat of game.players) {
|
||||
seat.forces = {}; seat.reserves = 20; seat.tanks = 0; seat.spice = 10;
|
||||
seat.hand = []; seat.traitors = []; seat.traitorChoices = [];
|
||||
}
|
||||
for (const id of [beneficiaryId,'g']) {
|
||||
player(game,id).forces = {'arrakeen:10':4};
|
||||
player(game,id).reserves = 16;
|
||||
}
|
||||
const index = game.deck.findIndex(card => card.effect === 'karama'); assert.ok(index >= 0);
|
||||
const [karama] = game.deck.splice(index, 1);
|
||||
player(game,'g').hand.push(karama);
|
||||
const choice = battles(game).find(item => item.territory === 'arrakeen' &&
|
||||
[item.attacker,item.defender].includes(beneficiaryId) && [item.attacker,item.defender].includes('g'));
|
||||
assert.ok(choice, 'The staged force position offers an unresolved legal battle.');
|
||||
game.active = choice.chooser;
|
||||
const enemy = choice.attacker === choice.chooser ? choice.defender : choice.attacker;
|
||||
game = send(game, choice.chooser, {type:'chooseBattle', territory:'arrakeen', target:enemy});
|
||||
return {game, owner, beneficiary:beneficiaryId, karama};
|
||||
}
|
||||
function passResponses(game: Game): Game {
|
||||
for (let count = 0; game.response && count < 24; count++) {
|
||||
const next = game.players.find(seat => !game.response!.passed.includes(seat.id));
|
||||
assert.ok(next);
|
||||
game = send(game, next.id, {type:'passResponse'});
|
||||
}
|
||||
assert.equal(game.response ?? null, null);
|
||||
return game;
|
||||
}
|
||||
const action = (power: Power): Action => power === 'voice'
|
||||
? {type:'voice', kind:'projectile', must:false}
|
||||
: {type:'prescience', field:'weapon'};
|
||||
|
||||
for (const power of ['voice','prescience'] as const) {
|
||||
for (const ally of ['e','h'] as const) {
|
||||
void test(`Basic group ${power} supports ${ally} in their battle against an outsider`, () => {
|
||||
const {game:before, owner, beneficiary} = source(power, ally);
|
||||
assert.deepEqual(before.battle?.preparation, {kind:power, owner, beneficiary});
|
||||
let game = send(before, owner, action(power));
|
||||
assert.equal(game.response?.kind, power);
|
||||
assert.equal(game.response?.owner, owner);
|
||||
if (power === 'voice') assert.deepEqual(game.battle?.voice,
|
||||
{target:'g', kind:'projectile', must:false});
|
||||
else assert.deepEqual(game.battle?.prescience, {player:beneficiary, field:'weapon'});
|
||||
game = passResponses(game);
|
||||
if (power === 'prescience') {
|
||||
assert.equal(game.battle?.preparation?.kind, 'prescienceAnswer');
|
||||
assert.equal(game.battle?.preparation?.owner, 'g');
|
||||
game = send(game, 'g', {type:'prescienceAnswer', value:null});
|
||||
assert.equal(game.battle?.prescience?.value, null);
|
||||
}
|
||||
assert.equal(game.battle?.revealed, false);
|
||||
assert.deepEqual(game.battle?.plans, {});
|
||||
});
|
||||
void test(`Karama cancels group ${power} offered to ${ally}`, () => {
|
||||
const {game:before, owner, karama} = source(power, ally);
|
||||
let game = send(before, owner, action(power));
|
||||
game = send(game, 'g', {type:'card', mode:'cancel', card:karama.id});
|
||||
game = passResponses(game);
|
||||
assert.equal(game.battle?.[power], undefined);
|
||||
assert.equal(game.discard.filter(card => card.id === karama.id).length, 1);
|
||||
assert.equal(game.battle?.revealed, false);
|
||||
});
|
||||
}
|
||||
for (const mode of ['pair','owner'] as const)
|
||||
void test(`Basic ${power} retains the ${mode === 'pair' ? 'pair ally' : 'owner'} beneficiary`, () => {
|
||||
const {game, owner, beneficiary} = source(power, mode === 'pair' ? 'e' : 'owner', mode !== 'pair');
|
||||
assert.deepEqual(game.battle?.preparation, {kind:power, owner, beneficiary});
|
||||
assert.equal(send(game, owner, action(power)).response?.kind, power);
|
||||
});
|
||||
void test(`a Medium bot proposes a legal group ${power} decision`, () => {
|
||||
const {game, owner} = source(power, 'h');
|
||||
const view = viewGame(game, owner);
|
||||
view.players.find(seat => seat.id === owner)!.bot = 'Medium';
|
||||
const choice = botActions(view).find(candidate => candidate.type === power || candidate.type === 'declineBattlePower');
|
||||
assert.ok(choice);
|
||||
const after = send(game, owner, choice);
|
||||
assert.ok(after.response || after.battle?.preparation?.kind !== power);
|
||||
});
|
||||
void test(`group ${power} renders the owner's controls and names its beneficiary`, () => {
|
||||
const {game, owner, beneficiary} = source(power, 'h');
|
||||
const own = viewGame(game, owner);
|
||||
assert.equal(own.battle?.preparation?.beneficiary, beneficiary);
|
||||
assert.ok([own.battle!.attacker, own.battle!.defender].includes('g'));
|
||||
const ownMarkup = renderToStaticMarkup(createElement(BattlePreparation, {
|
||||
game:own, busy:false, send:async () => {},
|
||||
}));
|
||||
assert.match(ownMarkup, /Harkonnen/);
|
||||
assert.match(ownMarkup, power === 'voice' ? /Use the Voice/ : /Reveal weapon/);
|
||||
const observer = viewGame(game, 'g');
|
||||
const observerMarkup = renderToStaticMarkup(createElement(BattlePreparation, {
|
||||
game:observer, busy:false, send:async () => {},
|
||||
}));
|
||||
assert.match(observerMarkup, /Waiting for/);
|
||||
assert.doesNotMatch(observerMarkup, /Use the Voice|Reveal weapon/);
|
||||
});
|
||||
}
|
||||
|
||||
void test('group Voice and Prescience both aid the third ally in the same battle', () => {
|
||||
let game = createGame('GROUPBOTH', newPlayer('b', 'Bene Gesserit', 'beneGesserit'), false);
|
||||
game.players.push(newPlayer('a', 'Atreides', 'atreides'),
|
||||
newPlayer('e', 'Emperor', 'emperor'), newPlayer('g', 'Guild', 'guild'));
|
||||
Object.assign(game, {status:'playing', phase:1, turn:2, nexus:true,
|
||||
order:['b','a','e','g'], ready:[], storm:18, deck:baseDeck(),
|
||||
response:null, decision:null, phaseOpening:null});
|
||||
game.coalitionState = emptyCoalitionState();
|
||||
game = send(game, 'b', {type:'alliance', target:'a'});
|
||||
game = send(game, 'a', {type:'alliance', target:'b'});
|
||||
const pair = coalitionOf(game.players, game.coalitionState, 'b'); assert.ok(pair);
|
||||
game = send(game, 'b', {type:'coalitionPropose', groupId:pair.id, entrant:'e',
|
||||
expectedRevision:game.coalitionState!.revision});
|
||||
for (const id of ['a','e']) {
|
||||
const proposal = game.coalitionState!.proposal!;
|
||||
game = send(game, id, {type:'coalitionAnswer', event:proposal.event, nexus:proposal.nexus,
|
||||
turn:proposal.turn, accept:true, expectedRevision:game.coalitionState!.revision});
|
||||
}
|
||||
Object.assign(game, {phase:6, nexus:false, ready:[], response:null, decision:null,
|
||||
phaseOpening:null, battle:null});
|
||||
for (const seat of game.players) {
|
||||
seat.forces = {}; seat.reserves = 20; seat.tanks = 0; seat.spice = 10;
|
||||
seat.hand = []; seat.traitors = []; seat.traitorChoices = [];
|
||||
}
|
||||
for (const id of ['e','g']) {
|
||||
player(game,id).forces = {'arrakeen:10':4};
|
||||
player(game,id).reserves = 16;
|
||||
}
|
||||
const karamaIndex = game.deck.findIndex(card => card.effect === 'karama');
|
||||
assert.ok(karamaIndex >= 0);
|
||||
player(game, 'g').hand.push(...game.deck.splice(karamaIndex, 1));
|
||||
const choice = battles(game).find(item => item.territory === 'arrakeen' &&
|
||||
[item.attacker,item.defender].includes('e') && [item.attacker,item.defender].includes('g'));
|
||||
assert.ok(choice);
|
||||
game.active = choice.chooser;
|
||||
const enemy = choice.attacker === choice.chooser ? choice.defender : choice.attacker;
|
||||
game = send(game, choice.chooser, {type:'chooseBattle', territory:'arrakeen', target:enemy});
|
||||
assert.deepEqual(game.battle?.preparation, {kind:'voice', owner:'b', beneficiary:'e'});
|
||||
game = send(game, 'b', {type:'voice', kind:'projectile', must:false});
|
||||
assert.equal(game.response?.kind, 'voice');
|
||||
assert.deepEqual(game.battle?.voice, {target:'g', kind:'projectile', must:false});
|
||||
game = passResponses(game);
|
||||
assert.deepEqual(game.battle?.preparation, {kind:'prescience', owner:'a', beneficiary:'e'});
|
||||
game = send(game, 'a', {type:'prescience', field:'weapon'});
|
||||
assert.equal(game.response?.kind, 'prescience');
|
||||
game = passResponses(game);
|
||||
assert.deepEqual(game.battle?.prescience, {player:'e', field:'weapon'});
|
||||
assert.equal(game.battle?.preparation?.owner, 'g');
|
||||
game = send(game, 'g', {type:'prescienceAnswer', value:null});
|
||||
assert.equal(game.battle?.prescience?.value, null);
|
||||
assert.equal(game.battle?.voice?.target, 'g');
|
||||
assert.deepEqual(game.battle?.plans, {});
|
||||
});
|
||||
Reference in new issue
Block a user