forked from WikiDeck/wikideck
Night 2 (2026-11-17), from the `claude/wikideck` fork's `night/2026-11-17` branch. It follows Night 1 (#22).
## What's in it
- **Duel engine** (fork #72, #80): 40-card decks of cards you own, Notability/Citations, keywords, effects T1–T12, Connected, a hidden-information-safe `viewFor`/`logFor`, and replay. Balance v1 tuning (#84) gives the first player about 51%.
- **Card profiles** (#70, #82): Figure, Location, Event, Institution or Doctrine, plus stats, keywords and rules text, all derived from real article data. Templates only.
- **Design and story** (#66, #69, #83): DUEL_DESIGN.md, plus the match log and the encyclopedic "Battle of X (2026)" recap.
- **PvP** (#74, #77, #81, #76, #85):
- The server checks ownership, and 40-card eligibility is enforced.
- Modes: Classic (ranked), Draft and Trending.
- Invites, quick match, Elo, a ladder and recap pages. Online matches render on the duel board.
- **Duel board UI** (#79): MTG-style card faces and combat declaration, built for 360 px first.
- **Polite Wikimedia client** (#73, #75): throttled, cached and identified requests, the mint-path fix, and a prefetch pool.
- **Android mining** (#78): native RandomX, as a sideload build. The emulator mined a testnet block.
- **Testnet ops** (#67, #68).
PR numbers refer to `claude/wikideck`.
## Status
- **Tests:** 423, of which 422 pass, 0 fail and 1 is skipped. The build is OK (`node:24-alpine`, `--network none`).
- **Testnet:** running this commit (036489c) on r350.
- **Untouched:** no changes to `server/testnet/consensus.ts`, `randomx.ts`, `native/` or the mainnet compose.
- **Review:** night-review reviewed fork #66–#79. #80–#85 came from the agents' unfinished branches and were not reviewed.
- **Known gaps:**
- The PvP end-to-end test hasn't been run (it needs two eom.dev accounts).
- Bot matches aren't reachable from Play.
- Card identity pre-bake hasn't been done.
- The RandomX verifier latch (`server/testnet/randomx.ts:140`) is still open.
This PR is for Eric to merge.
🤖 Generated with [Claude Code](https://claude.com/claude-code)Reviewed-on: WikiDeck/wikideck#23
Co-authored-by: Claude <claude@eom.dev>
210 lines
11 KiB
TypeScript
210 lines
11 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { createHash } from 'node:crypto';
|
|
import { Rng } from '../shared/game/rng.ts';
|
|
import {
|
|
DEFAULT_DUEL_RULESET, applyAction, createDuel, deserialize, duelEnginePort, isOver, legalActions, logFor, replay,
|
|
serialize, statsOf, toAct, validateDeck, viewFor,
|
|
} from '../shared/duel/index.ts';
|
|
import type { DuelAction, DuelCard, DuelState, Seat } from '../shared/duel/index.ts';
|
|
import { POOL, fig, randomDeck } from './duel-core-fixtures.ts';
|
|
|
|
const rules = DEFAULT_DUEL_RULESET;
|
|
|
|
/** Random legal policy that prefers progress so games stay short. */
|
|
function pick(rng: Rng, actions: DuelAction[]): DuelAction {
|
|
const plays = actions.filter(a => a.type === 'play' || a.type === 'toggleAttacker' || a.type === 'block');
|
|
if (plays.length && rng.next() < 0.75) return rng.pick(plays);
|
|
return rng.pick(actions);
|
|
}
|
|
|
|
function playOut(seed: string, maxActions = 3000) {
|
|
const decks: [DuelCard[], DuelCard[]] = [randomDeck(`${seed}/a`, 0), randomDeck(`${seed}/b`, 1)];
|
|
let state = createDuel(rules, decks, seed);
|
|
const rng = Rng.fromSeed(`${seed}/policy`);
|
|
const actions: { seat: Seat; action: DuelAction }[] = [];
|
|
const views: DuelState[] = [];
|
|
while (!isOver(state) && actions.length < maxActions) {
|
|
const seat = toAct(state)!;
|
|
assert.deepEqual(legalActions(state, (1 - seat) as Seat), [], 'non-acting seat has no actions');
|
|
const legal = legalActions(state, seat);
|
|
assert.ok(legal.length > 0, 'acting seat always has an action');
|
|
const action = pick(rng, legal);
|
|
actions.push({ seat, action });
|
|
state = applyAction(state, seat, action);
|
|
views.push(state);
|
|
}
|
|
return { decks, state, actions, views };
|
|
}
|
|
|
|
test('validateDeck enforces 40 owned-style cards', () => {
|
|
const deck = randomDeck('v', 0);
|
|
validateDeck(rules, deck);
|
|
assert.throws(() => validateDeck(rules, deck.slice(1)));
|
|
assert.throws(() => validateDeck(rules, [...deck.slice(1), deck[0]].map((c, i) => (i === 39 ? { ...c, cardId: deck[1].cardId } : c))));
|
|
assert.throws(() => validateDeck(rules, deck.map((c, i) => (i === 0 ? { ...c, keywords: ['Protected', 'Stub', 'Legacy'] } : c)) as DuelCard[]));
|
|
assert.throws(() => validateDeck(rules, deck.map((c, i) => (i === 0 ? { ...c, type: 'Basic' } : c)) as unknown as DuelCard[]));
|
|
});
|
|
|
|
test('createDuel deals 7 and starts in mulligan', () => {
|
|
const s = createDuel(rules, [randomDeck('c', 0), randomDeck('d', 1)], 'seed-1');
|
|
assert.equal(s.phase, 'mulligan');
|
|
assert.equal(s.players[0].hand.length, 7);
|
|
assert.equal(s.players[1].library.length, 33);
|
|
assert.equal(toAct(s), 0);
|
|
assert.deepEqual(legalActions(s, 0), [{ type: 'keep' }, { type: 'mulligan' }]);
|
|
const m = applyAction(s, 0, { type: 'mulligan' });
|
|
assert.deepEqual(legalActions(m, 0), [{ type: 'keep' }]);
|
|
const k = applyAction(m, 0, { type: 'keep' });
|
|
assert.equal(k.players[0].hand.length, 6, 'one card bottomed after one mulligan');
|
|
assert.equal(k.players[0].library.length, 34);
|
|
const both = applyAction(k, 1, { type: 'keep' });
|
|
assert.equal(both.phase, 'main');
|
|
assert.equal(both.turn, 1);
|
|
const first = both.firstSeat;
|
|
assert.equal(both.players[first].hand.length, first === 0 ? 6 : 7, 'first player skips the first draw');
|
|
assert.equal(both.players[first].citations, 1);
|
|
assert.equal(s.players[0].hand.length, 7, 'input not mutated');
|
|
});
|
|
|
|
test('random legal games end, are deterministic and replay exactly', () => {
|
|
for (let i = 0; i < 25; i++) {
|
|
const seed = `prop-${i}`;
|
|
const { decks, state, actions } = playOut(seed);
|
|
assert.ok(isOver(state), `game ${seed} ended (${actions.length} actions, turn ${state.turn})`);
|
|
const again = replay(rules, decks, seed, actions);
|
|
assert.equal(serialize(again), serialize(state));
|
|
assert.deepEqual(deserialize(serialize(state)), state);
|
|
assert.ok(['life', 'library', 'concede', 'timeout', 'turnCap'].includes(state.result!.reason));
|
|
}
|
|
});
|
|
|
|
test('viewFor and logFor never leak hidden cards', () => {
|
|
for (let i = 0; i < 8; i++) {
|
|
const { views } = playOut(`leak-${i}`);
|
|
for (const s of views) {
|
|
for (const seat of [0, 1] as Seat[]) {
|
|
const opp = (1 - seat) as Seat;
|
|
const hidden = new Set([...s.players[opp].hand, ...s.players[opp].library, ...s.players[seat].library].map(c => c.card.cardId));
|
|
// Cards revealed publicly earlier (played then bounced, etc.) may legitimately be known.
|
|
const publicIds = new Set(s.log.flatMap(e => {
|
|
if (e.kind === 'draw') return [];
|
|
return JSON.stringify(e).match(/sample-\d+#[01]/g) ?? [];
|
|
}));
|
|
const text = JSON.stringify(viewFor(s, seat)) + JSON.stringify(logFor(s, seat));
|
|
for (const id of hidden) {
|
|
if (publicIds.has(id)) continue;
|
|
assert.ok(!text.includes(`"${id}"`), `seat ${seat} sees hidden ${id} at version ${s.version}`);
|
|
}
|
|
assert.ok(!('library' in viewFor(s, seat).opponent));
|
|
assert.ok(!('rng' in viewFor(s, seat)));
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
test('combat: simultaneous damage, one blocker, sickness, Featured evasion', () => {
|
|
const filler = (seat: number) => Array.from({ length: 38 }, (_, i) => fig(`f${seat}-${i}`, 1, 1));
|
|
const r = { ...rules, openingHand: 0 };
|
|
let s = createDuel(r, [[fig('A', 3, 3), fig('Fly', 2, 2, ['Featured']), ...filler(0)], [fig('B', 2, 4), fig('C', 1, 1), ...filler(1)]], 'combat');
|
|
// Stack the libraries deterministically for the test.
|
|
s = structuredClone(s);
|
|
const top = (seat: 0 | 1, ids: string[]) => {
|
|
const lib = s.players[seat].library;
|
|
for (const id of ids.reverse()) { const i = lib.findIndex(c => c.card.cardId === id); lib.unshift(...lib.splice(i, 1)); }
|
|
};
|
|
s.firstSeat = 0; s.active = 0;
|
|
top(0, ['A', 'Fly']); top(1, ['B', 'C']);
|
|
s.players[0].hand = s.players[0].library.splice(0, 2);
|
|
s.players[1].hand = s.players[1].library.splice(0, 2);
|
|
s = applyAction(s, 0, { type: 'keep' });
|
|
s = applyAction(s, 1, { type: 'keep' });
|
|
assert.equal(s.active, 0);
|
|
const uidOf = (id: string) => [...s.players[0].hand, ...s.players[1].hand, ...s.battlefield].find(c => c.card.cardId === id)!.uid;
|
|
s = applyAction(s, 0, { type: 'play', uid: uidOf('A') });
|
|
s = applyAction(s, 0, { type: 'endPhase' });
|
|
assert.deepEqual(legalActions(s, 0), [{ type: 'confirmAttack' }], 'summoning sickness');
|
|
s = applyAction(s, 0, { type: 'confirmAttack' });
|
|
assert.equal(s.active, 1);
|
|
s = applyAction(s, 1, { type: 'play', uid: uidOf('B') });
|
|
s = applyAction(s, 1, { type: 'endPhase' });
|
|
s = applyAction(s, 1, { type: 'confirmAttack' });
|
|
// Turn 3: seat 0 has 2 Citations: play Fly, attack with A; B blocks; 3 dmg vs 4 toughness, 2 vs 3.
|
|
s = applyAction(s, 0, { type: 'play', uid: uidOf('Fly') });
|
|
s = applyAction(s, 0, { type: 'endPhase' });
|
|
s = applyAction(s, 0, { type: 'toggleAttacker', uid: uidOf('A') });
|
|
s = applyAction(s, 0, { type: 'confirmAttack' });
|
|
assert.equal(s.phase, 'block');
|
|
assert.deepEqual(legalActions(s, 1), [{ type: 'block', blocker: uidOf('B'), attacker: uidOf('A') }, { type: 'confirmBlocks' }]);
|
|
s = applyAction(s, 1, { type: 'block', blocker: uidOf('B'), attacker: uidOf('A') });
|
|
s = applyAction(s, 1, { type: 'confirmBlocks' });
|
|
assert.equal(s.battlefield.length, 3, 'nobody died');
|
|
assert.equal(s.players[1].life, 20);
|
|
const destroyedOrDamaged = s.log.filter(e => e.kind === 'combatDamage');
|
|
assert.equal(destroyedOrDamaged.length, 2);
|
|
// Seat 1 turn: pass. Seat 0: Fly attacks; B (no Featured/Translated) can't block it.
|
|
s = applyAction(s, 1, { type: 'endPhase' });
|
|
s = applyAction(s, 1, { type: 'confirmAttack' });
|
|
s = applyAction(s, 0, { type: 'endPhase' });
|
|
s = applyAction(s, 0, { type: 'toggleAttacker', uid: uidOf('Fly') });
|
|
s = applyAction(s, 0, { type: 'confirmAttack' });
|
|
assert.equal(s.players[1].life, 18, 'Featured unblockable by plain Figure: damage resolved without a block phase');
|
|
assert.equal(s.active, 1);
|
|
});
|
|
|
|
test('Connected bonus applies and is logged once', () => {
|
|
const a = fig('obama', 1, 1, [], { pageId: 1, title: 'Barack Obama', connect: { countries: ['Q30'], centuries: [21], links: [2], p31: ['Q5'] } });
|
|
const b: DuelCard = { ...fig('wh', 0, 0), pageId: 2, title: 'White House', type: 'Location', territory: 1, connect: { countries: ['Q30'], centuries: [19], links: [], p31: ['Q35112'] } };
|
|
let s = createDuel({ ...rules, deckSize: 2, openingHand: 2, citationsPerTurn: 5 }, [[a, b], [{ ...a, cardId: 'x' }, { ...b, cardId: 'y' }]], 'conn');
|
|
s = applyAction(s, 0, { type: 'keep' });
|
|
s = applyAction(s, 1, { type: 'keep' });
|
|
const me = s.active;
|
|
const hand = s.players[me].hand;
|
|
s = applyAction(s, me, { type: 'play', uid: hand.find(c => c.card.type === 'Figure')!.uid });
|
|
s = applyAction(s, me, { type: 'play', uid: s.players[me].hand[0].uid });
|
|
const figure = s.battlefield.find(p => p.card.type === 'Figure')!;
|
|
assert.deepEqual(statsOf(s, figure.uid), { power: 3, toughness: 3 });
|
|
assert.equal(s.log.filter(e => e.kind === 'connected').length, 1);
|
|
assert.equal(viewFor(s, me).battlefield.find(p => p.uid === figure.uid)!.connected?.tier, 'Linked');
|
|
});
|
|
|
|
test('empty library loses; concede and timeout', () => {
|
|
const tiny = { ...rules, deckSize: 8, openingHand: 7 };
|
|
const pool = POOL.slice(0, 16);
|
|
let s = createDuel(tiny, [pool.slice(0, 8), pool.slice(8, 16).map(c => ({ ...c, cardId: c.cardId + 'b' }))], 'deckout');
|
|
s = applyAction(s, 0, { type: 'keep' });
|
|
s = applyAction(s, 1, { type: 'keep' });
|
|
for (let i = 0; i < 20 && !isOver(s); i++) s = applyAction(s, toAct(s)!, { type: 'timeout' });
|
|
assert.equal(s.result?.reason, 'library');
|
|
const c = applyAction(createDuel(rules, [randomDeck('q', 0), randomDeck('r', 1)], 'c'), 1, { type: 'concede' });
|
|
assert.deepEqual(c.result, { winner: 0, reason: 'concede' });
|
|
assert.throws(() => applyAction(c, 0, { type: 'keep' }));
|
|
});
|
|
|
|
test('illegal actions throw', () => {
|
|
const s = createDuel(rules, [randomDeck('i', 0), randomDeck('j', 1)], 'illegal');
|
|
assert.throws(() => applyAction(s, 1, { type: 'keep' }), /Not your turn/);
|
|
assert.throws(() => applyAction(s, 0, { type: 'endPhase' }), /Illegal/);
|
|
});
|
|
|
|
test('engine port binding is EnginePort-shaped', () => {
|
|
const p = duelEnginePort;
|
|
assert.equal(p.deckSize(p.defaultRuleset), 40);
|
|
assert.throws(() => p.starterDeck());
|
|
const live = p.applyLive(rules, POOL.find(c => c.type === 'Figure')!, { trendPercent: 25 });
|
|
assert.ok(live.keywords.includes('Trending'));
|
|
const s = p.createMatch(rules, [randomDeck('p', 0), randomDeck('q', 1)], 'port');
|
|
assert.deepEqual(p.concedeAction(0), { type: 'concede' });
|
|
assert.equal(p.result(s).winner, null);
|
|
});
|
|
|
|
test('golden match', () => {
|
|
const { state, actions } = playOut('golden-1');
|
|
const digest = createHash('sha256').update(serialize(state)).digest('hex').slice(0, 16);
|
|
const summary = { actions: actions.length, turn: state.turn, result: state.result, log: state.log.length, digest };
|
|
assert.deepEqual(summary, GOLDEN);
|
|
});
|
|
|
|
// Regenerate deliberately (an engine rule change) and say so in the PR.
|
|
const GOLDEN = { actions: 72, turn: 13, result: { winner: 1, reason: 'life' }, log: 103, digest: 'e8bc8630d6f0ab0a' };
|