174 lines
10 KiB
TypeScript
174 lines
10 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import { createGame, joinGame, newPlayer, viewGame, type Game } from '../game/engine';
|
|
import { botActions } from '../game/bots';
|
|
import { DIFFICULTIES } from '../game/bot-profiles';
|
|
import { richeseRevealInterruptSupported } from '../game/richese-reveal-interruption';
|
|
import { actTruthMovementNativeEarly as act, nextTruthMovementNativeEarlyStep } from './fixture-truth-movement-native-early';
|
|
import { truthMovementInventory as inventory } from './fixture-truth-movement';
|
|
|
|
const richese = 'richese', guild = 'guild', atreides = 'atreides';
|
|
function player(game: Game, id: string) {
|
|
const seat = game.players.find(row => row.id === id); assert.ok(seat); return seat;
|
|
}
|
|
const clean = (game: Game) => !game.response && !game.decision && !game.phaseOpening &&
|
|
!game.truthtrance && !game.pendingTreacheryDiscard;
|
|
|
|
/** Original public Basic setup and native actions. Only an actual Truthtrance's
|
|
* held location and the order of unopened Spice cards are staged. */
|
|
type QuestionMode = 'fact' | 'knownFreeform' | 'unknownFreeform' | 'future';
|
|
function source(answer: 'yes' | 'no', mode: QuestionMode = 'fact') {
|
|
let game = createGame('REVEALAFTERFACT', newPlayer(richese, 'Richese', 'richese'), false, ['choam']);
|
|
joinGame(game, newPlayer(guild, 'Guild', 'guild'));
|
|
joinGame(game, newPlayer(atreides, 'Atreides', 'atreides'));
|
|
for (const seat of game.players) game = act(game, seat.id, { type: 'ready' });
|
|
game = act(game, game.host, { type: 'start' });
|
|
const reach = (until: (state: Game) => boolean) => {
|
|
for (let count = 0; count < 2000; count++) {
|
|
if (until(game)) return;
|
|
assert.notEqual(game.status, 'finished');
|
|
const step = nextTruthMovementNativeEarlyStep(game, {
|
|
position: 'first', method: 'onceAround', direction: 'clockwise', keepUnbid: false,
|
|
});
|
|
assert.ok(step, `Missing native action at ${game.turn}/${game.phase}/${game.decision?.kind}`);
|
|
game = act(game, step.actor, step.action);
|
|
}
|
|
throw Error('The genuine Basic parent was not reached');
|
|
};
|
|
reach(state => state.status === 'playing');
|
|
const original = inventory(game);
|
|
for (const position of [0, 1]) {
|
|
const from = game.spiceDeck.findIndex((card, i) => i >= position && 'territory' in card && !card.discovery);
|
|
assert.ok(from >= position); [game.spiceDeck[position], game.spiceDeck[from]] = [game.spiceDeck[from], game.spiceDeck[position]];
|
|
}
|
|
reach(state => state.turn === 1 && state.phase === 5 && state.active === richese && clean(state));
|
|
const owner = player(game, richese), marker = owner.noField!.tokens.find(token => token.value === 3);
|
|
assert.ok(marker); assert.ok(owner.noFieldEvent);
|
|
game = act(game, richese, { type: 'ship', noField: marker.id, event: owner.noFieldEvent,
|
|
territory: 'polar_sink', sector: 0, allyPayment: 0 });
|
|
reach(clean);
|
|
const asker = player(game, atreides);
|
|
let truth = asker.hand.find(card => card.effect === 'truthtrance');
|
|
if (!truth) {
|
|
const held = game.players.find(seat => seat.id !== atreides && seat.hand.some(card => card.effect === 'truthtrance'));
|
|
if (held) {
|
|
const from = held.hand.findIndex(card => card.effect === 'truthtrance');
|
|
truth = held.hand.splice(from, 1)[0];
|
|
const replacement = asker.hand.pop();
|
|
if (replacement) held.hand.push(replacement);
|
|
} else {
|
|
const from = game.deck.findIndex(card => card.effect === 'truthtrance'); assert.ok(from >= 0);
|
|
truth = game.deck.splice(from, 1)[0];
|
|
if (asker.hand.length >= 4) game.deck.push(asker.hand.pop()!);
|
|
}
|
|
asker.hand.push(truth);
|
|
}
|
|
// Avoid creating a separate simultaneous Truthtrance order question.
|
|
for (const seat of game.players.filter(row => row.id !== atreides)) {
|
|
for (let i = seat.hand.length - 1; i >= 0; i--) if (seat.hand[i].effect === 'truthtrance') {
|
|
game.deck.push(...seat.hand.splice(i, 1));
|
|
const replacement = game.deck.findIndex(card => card.effect !== 'truthtrance');
|
|
assert.ok(replacement >= 0); seat.hand.push(game.deck.splice(replacement, 1)[0]);
|
|
}
|
|
}
|
|
assert.deepEqual(inventory(game), original);
|
|
game = act(game, atreides, { type: 'card', card: truth.id });
|
|
while (game.truthtrance?.stage === 'priority') {
|
|
const seat = game.players.find(row => !game.truthtrance!.passed.includes(row.id)); assert.ok(seat);
|
|
game = act(game, seat.id, { type: 'truthPass' });
|
|
}
|
|
assert.equal(game.truthtrance?.stage, 'ask');
|
|
const fact = { kind: 'spice', compare: 'gte', value: answer === 'yes' ? 0 : Number.MAX_SAFE_INTEGER } as const;
|
|
const question = mode === 'future'
|
|
? { kind: 'freeform', scope: 'currentTurn', target: guild, text: 'Will you ship six forces later this turn?' }
|
|
: mode === 'knownFreeform'
|
|
? { kind: 'freeform', scope: 'fact', target: guild, text: `Do you have at least ${fact.value} spice?` }
|
|
: mode === 'unknownFreeform'
|
|
? { kind: 'freeform', scope: 'fact', target: guild, text: 'Are you thinking of a sandworm right now?' }
|
|
: { kind: 'fact', target: guild, fact };
|
|
game = act(game, atreides, { type: 'truthAsk', question });
|
|
if (mode === 'fact' || mode === 'knownFreeform') assert.equal(viewGame(game, guild).truthAnswer, answer);
|
|
if (mode === 'unknownFreeform') assert.equal(viewGame(game, guild).truthAnswer, null);
|
|
game = act(game, guild, { type: 'truthAnswer', answer });
|
|
reach(clean);
|
|
assert.equal(game.truthHistory!.at(-1)!.question.kind, mode === 'fact' ? 'fact' : 'freeform');
|
|
assert.equal(game.truthHistory!.at(-1)!.answer, answer);
|
|
assert.deepEqual(inventory(game), original);
|
|
// A real held Karama keeps the Guild-rate response open for inspection.
|
|
if (!player(game, atreides).hand.some(card => card.effect === 'karama')) {
|
|
const holder = game.players.find(seat => seat.id !== atreides && seat.hand.some(card => card.effect === 'karama'));
|
|
const from = holder ? holder.hand : game.deck;
|
|
const index = from.findIndex(card => card.effect === 'karama'); assert.ok(index >= 0);
|
|
const card = from.splice(index, 1)[0];
|
|
if (player(game, atreides).hand.length >= 4) from.push(player(game, atreides).hand.pop()!);
|
|
player(game, atreides).hand.push(card);
|
|
}
|
|
assert.deepEqual(inventory(game), original);
|
|
game = act(game, richese, { type: 'endMovement' });
|
|
reach(state => state.phase === 5 && state.active === guild && clean(state) && !player(state, guild).shipped);
|
|
const event = player(game, richese).noFieldEvent; assert.ok(event);
|
|
game = act(game, guild, { type: 'ship', territory: 'arrakeen', sector: 10, amount: 3, allyPayment: 0 });
|
|
assert.equal(game.response?.kind, 'guildRate'); assert.equal(game.pendingShipment?.player, guild);
|
|
return { game, event, marker: marker.id, original };
|
|
}
|
|
|
|
for (const answer of ['yes', 'no'] as const) void test(
|
|
`A public current-fact Truthtrance ${answer} allows Richese to reveal while Guild's original rate is pending`, () => {
|
|
const { game, event, marker, original } = source(answer), before = structuredClone(game);
|
|
assert.equal(viewGame(game, richese).richeseNoField?.canReveal, true);
|
|
const next = act(game, richese, { type: 'revealNoField', token: marker, event });
|
|
assert.deepEqual(game, before);
|
|
assert.deepEqual(next.response, before.response); assert.deepEqual(next.pendingShipment, before.pendingShipment);
|
|
assert.deepEqual(next.truthHistory, before.truthHistory);
|
|
assert.equal(player(next, richese).noField?.deployed, null);
|
|
assert.equal(player(next, richese).forces['polar_sink:0'], 3);
|
|
assert.deepEqual(inventory(next), original);
|
|
let done = next;
|
|
for (let count = 0; done.response?.kind === 'guildRate' && count < done.players.length; count++) {
|
|
const seat = done.players.find(row => !done.response!.passed.includes(row.id)); assert.ok(seat);
|
|
done = act(done, seat.id, { type: 'passResponse' });
|
|
}
|
|
assert.equal(done.pendingShipment, null); assert.equal(player(done, guild).forces['arrakeen:10'], 3);
|
|
assert.deepEqual(inventory(done), original);
|
|
},
|
|
);
|
|
|
|
void test('all four bot profiles can respond to the original rate after a factual answer and reveal', () => {
|
|
const { game, event, marker } = source('yes');
|
|
const revealed = act(game, richese, { type: 'revealNoField', token: marker, event });
|
|
for (const difficulty of DIFFICULTIES) {
|
|
const view = viewGame(revealed, atreides); view.players.find(seat => seat.id === atreides)!.bot = difficulty;
|
|
const action = botActions(view)[0]; assert.ok(action, difficulty);
|
|
assert.doesNotThrow(() => act(revealed, atreides, action), difficulty);
|
|
}
|
|
});
|
|
|
|
void test('a real current-turn freeform promise retains the public history fence', () => {
|
|
const { game, event, marker } = source('no', 'future'), before = structuredClone(game);
|
|
assert.equal(game.truthHistory!.at(-1)!.question.kind, 'freeform');
|
|
assert.equal(richeseRevealInterruptSupported(game), false);
|
|
assert.equal(viewGame(game, richese).richeseNoField?.canReveal, false);
|
|
assert.throws(() => act(game, richese, { type: 'revealNoField', token: marker, event }));
|
|
assert.deepEqual(game, before);
|
|
});
|
|
|
|
for (const mode of ['knownFreeform', 'unknownFreeform'] as const)
|
|
for (const answer of ['yes', 'no'] as const) void test(
|
|
`A ${mode === 'knownFreeform' ? 'recognized' : 'unrecognized'} current-fact freeform ${answer} retains the reveal and Guild-rate continuation`, () => {
|
|
const { game, event, marker, original } = source(answer, mode);
|
|
const record = game.truthHistory!.at(-1)!;
|
|
assert.ok(record.question.kind === 'freeform');
|
|
assert.equal(record.question.scope, 'fact');
|
|
assert.equal(!!record.question.interpreted, mode === 'knownFreeform');
|
|
assert.equal(viewGame(game, richese).richeseNoField?.canReveal, true);
|
|
let next = act(game, richese, { type: 'revealNoField', token: marker, event });
|
|
assert.deepEqual(next.response, game.response); assert.deepEqual(next.pendingShipment, game.pendingShipment);
|
|
while (next.response?.kind === 'guildRate') {
|
|
const seat = next.players.find(row => !next.response!.passed.includes(row.id)); assert.ok(seat);
|
|
next = act(next, seat.id, { type: 'passResponse' });
|
|
}
|
|
assert.equal(next.pendingShipment, null); assert.equal(player(next, guild).forces['arrakeen:10'], 3);
|
|
assert.deepEqual(inventory(next), original);
|
|
},
|
|
);
|