Compare commits

..
Author SHA1 Message Date
claudeandClaude Opus 5.5 2ad4d022aa docs: note the deliberate 1 req/s REST rate (review on #73)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 22:20:21 -05:00
claudeandClaude Opus 5.5 f544019bfe Polite Wikimedia client, mint-path fix, disk cache and mining prefetch pool (#62)
- server/wikimedia/client.ts: one process-wide client. Per-host token bucket
  (1 req/s, burst 3), 1 in flight per host, mint > interactive > background
  lanes with a bounded queue, maxlag=5 on Action API requests, Retry-After
  and backoff, a circuit breaker after 3 consecutive 429s, a daily cap, and
  a per-egress-IP budget (WIKIDECK_WIKIMEDIA_NODES_PER_IP).
- server/wikimedia/cache.ts: node:sqlite HTTP cache with ETag/If-None-Match,
  stale-while-revalidate on the background lane. Mint reads never use it.
- Mint fix: a 429/5xx/timeout/non-JSON reply on pageviews now fails getFresh()
  (template 503, miner retries) instead of minting attention: null forever.
- server/wikimedia/pool.ts: per-tip article+snapshot prefetch pool for
  /api/mining/template, about 3 requests per block regardless of miner count.
- User-Agent with an operator contact (WIKIDECK_OPERATOR_CONTACT or
  PUBLIC_ORIGIN) and a start-up warning when it is unset.
- Mint request parameters are byte-identical apart from maxlag (golden test).
  No consensus, block or protocol changes. Tests use fake fetch only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 21:51:20 -05:00
25 changed files with 1340 additions and 2094 deletions
-89
View File
@@ -1,89 +0,0 @@
# WikiDeck Duel: engine API (v1, Night 2)
Owner: **duel-core** (claude/wikideck#55). Rules: `docs/game/DUEL_DESIGN.md` (duel-design). This file: how code talks to the engine.
- Types: `shared/duel/types.ts`. Ruleset: `shared/duel/ruleset.ts`. Function surface: `DuelEngineApi` in `shared/duel/index.ts`, implemented by `shared/duel/engine.ts` (engine-alpha PR).
- `shared/game/` (Edit War) is untouched. Change requests: comment on #55 with `[from <agent>]`.
## Principles
- **Pure and deterministic.** `(rules, decks, seed, actions)` → identical state and log. RNG: `shared/game/rng.ts` (mulberry32); its uint32 state lives in `DuelState.rng`.
- **Plain JSON.** `DuelState` round-trips through `JSON.stringify`/`parse`; `serialize`/`deserialize` are thin wrappers.
- **Clone-on-apply.** `applyAction` never mutates its input and throws on an illegal action.
- **Atomic actions.** `legalActions(state, seat)` is small, and `[]` for the seat that is not to act. The host checks an action by deep-equal against that list (`concede` and `timeout` excepted).
- **Off-chain.** Matches never move or burn cards. Cards arrive derived and frozen; the engine does no I/O.
## Cards (`DuelCard`)
Built by duel-cards (`derive()`), frozen by the host at match start. Shape agreed with duel-cards on #55.
| Field | Notes |
|---|---|
| `cardId` | unique in a deck (NFT tokenId; sample id in sims; `token:` for tokens) |
| `pageId`, `revisionId?`, `title`, `description?` | `pageId` matches Linked pairs; `revisionId` for recap references |
| `type`, `subtype?` | `Figure` · `Location` · `Event` · `Institution` · `Doctrine` |
| `portal` | `Arts, Earth, History, Science, Society, Neutral`; no deckbuilding restriction |
| `cost` | 1–6 (Locations 1–3) |
| `power`, `toughness` | Figures: body. Locations: 0 / Garrison (0 = can't block). Others: `null` or 0 |
| `keywords` | ≤ `maxKeywords` (2). Figures only |
| `territory?` | Location: +N max Citations while controlled (total from all sources ≤ `territoryCap`) |
| `effects` | `EffectSpec[]` (below), ≤ 4 |
| `rulesText`, `flavor` | display only |
| `connect?` | `{countries, centuries, links, p31}` frozen claims; the engine computes Connected |
| `rarity?`, `deriveVersion?`, `token?` | |
### Keywords (DUEL_DESIGN §4), as the engine applies them
- **Protected**: can't be the target of Events or Doctrines an opponent controls (abilities of other permanents and combat still apply).
- **Featured**: can be blocked only by Featured, Translated or Multilingual Figures.
- **Multilingual**: can block Featured; can't be blocked by Local Figures.
- **Translated**: can block Featured.
- **Legacy**: when destroyed, its controller draws a card.
- **Well-sourced**: prevent the first 1 damage dealt to it each turn.
- **Stub**: at the start of your turn, a +1/+1 counter, up to `stubMax` (2).
- **Local**: +1/+1 while you control a Location sharing a `connect.countries` value.
- **Trending** (Trending mode, set by the host via `applyLive`): no Pending review, +`trendingPower`/+0.
### Effects (`EffectSpec`)
`{ trigger, op, target?, amount?, power?, toughness?, duration?, token?, condition?, grant?, template? }`
- **Triggers:** `onPlay` (Event resolves; on a permanent same as `onEnter`), `onEnter`, `onAttack`, `onDestroyed`, `startOfTurn`, `onCombatDamageToPlayer` (on a Figure, or on a Doctrine for its attached Figure), `static`.
- **Ops:** `damage`, `damageEach`, `createToken`, `pump`, `fight`, `draw`, `attach`, `gainLife`, `drain`, `destroy`, `bounce`, `tap`.
- **Targets:** chosen: `figure`, `figureOrLocation`, `ownFigure`, `opponentFigure`, `anyTarget`; automatic: `each`, `self`, `attached`, `ownFigures`, `opponentFigures`, `opponent`, `controller`. Defaults: damage → `anyTarget`, pump → `self`, draw/gainLife/createToken → `controller`, drain → `opponent`.
- **Chosen targets:** each `onPlay`/`onEnter` effect with a chosen target consumes the next entry of `play.targets`, in effect order; `legalActions` enumerates the combinations. An Event or Doctrine with no legal target can't be played; a Figure/Location/Institution enters and its targeted effect fizzles (`targets` omitted).
- **fight:** the previous chosen target in the same card (else the source permanent) and this target deal damage equal to their power to each other. T6 = `[{op:'pump', target:'ownFigure', power:k, toughness:0}, {op:'fight', target:'opponentFigure'}]`.
- **Doctrine:** its `attach` effect's target is the Figure it attaches to (`ownFigure` for T11, `figure` for T12). Its `static` effects with `target: 'attached'` apply to that Figure; `condition.samePortal` on a Doctrine means "other Figures you control of the attached Figure's Portal". When the Figure leaves, the Doctrine is Deleted.
- **static pump** applies while the source is in play; conditions use **printed** power (no feedback loops). `condition.connected` = the affected Figure is Connected to the source (T9).
- **createToken:** `amount` tokens of `token` (a fresh `DuelCard` with `token: true`, uid `t:<n>`); `condition.maxTokens` = only if you control fewer tokens than that. Tokens leaving play cease to exist.
- Triggers resolve immediately: the active player's first, then in entry order.
## Rules (defaults: `DEFAULT_DUEL_RULESET`; details in DUEL_DESIGN §2)
- 40-card decks (`deckSize`), ≤ 4 per `pageId`, unique `cardId`s, no tokens. 20 Notability. Drawing from an empty library loses (`libraryEmpty: 'lose'`; `'reshuffle'` and `'fatigue'` exist as data).
- Opening hand 7. Up to `mulligans` (1): shuffle in, draw 7, then on `keep` put one card per mulligan on the bottom (the last in hand order). Seat 0 decides first, then seat 1.
- First player (from the seed) skips their first draw. Balance levers (data only): `firstPlayerDraws`, and `secondPlayerCitations` (extra Citations on the second player's first turn; default 1 from balance v1, #64).
- **Turn:** refresh (base max +1 to 10, plus Territory ≤ 2; refill; untap; Pending review ends; Stub and `startOfTurn` triggers) → draw → **main** → **attack** → **block** → damage → end (damage and "until end of turn" clear; discard down to `handSize` 7, **highest cost first**, automatically).
- Locations: `locationsPerTurn` (1), can't attack. Garrison Locations (toughness > 0) block as 0-power.
- Combat: attack the opponent only; each blocker blocks one attacker, at most one blocker per attacker; simultaneous damage; destroy triggers after all combat damage. No trample, first strike, stack or instants.
- **Connected** (engine, from `connect`, tiers in `connectedTiers`): Linked (either `links` contains the other's `pageId`, or either `cites` contains the other's `qid`), Compatriot (shared country), Contemporary (shared century, different types), Kin (shared P31, not Q5/Q16521). A Figure gets the single best bonus among other permanents its controller controls: `+linkedBonus` (2) if Linked, else `+connectedBonus` (1). Logged once (`connected`). Opposing Connected pairs are logged once as `rivalry`.
- **Timeouts:** `timeout` passes (mulligan: keep; main/attack: end the turn without attacking; block: confirm current blocks). `timeoutsToConcede` (3) consecutive timeouts by one seat lose (`reason: 'timeout'`).
- `turnCap` (default 40 total turns from balance v1, #64; `null` = none): after that many total turns, higher Notability wins, then higher total power in play, then the second player.
## Phases and actions
| Phase | Who acts | Listed actions |
|---|---|---|
| `mulligan` | seat 0 until ready, then seat 1 | `keep`, `mulligan` |
| `main` | active | `play {uid, targets?}`, `endPhase` |
| `attack` | active | `toggleAttacker {uid}` (untapped, not Pending review, not yet declared), `confirmAttack` |
| `block` | defender | `block {blocker, attacker}` (unassigned blocker, unblocked attacker, evasion allowing), `confirmBlocks` |
| any | either seat | `concede` (not listed) |
| any | host, for the seat to act | `timeout` (not listed) |
Declarations are **monotonic** (no un-declare), so every turn has a bounded number of actions; UIs stage choices locally and send them on confirm. If the defender has no legal block, damage resolves at `confirmAttack` with no block phase. `confirmAttack` with no attackers ends the turn.
## Views and log
- `viewFor(state, seat): PlayerView`: your hand; the opponent's hand and library **sizes** only; both graveyards; the battlefield with current power/toughness, keywords and Connected badge; combat; `toAct`; `version`; `result`. Never the RNG or library order.
- **The seed is secret until the match ends.** `seed` plus the deck lists reproduce every shuffle, so the host never sends `seed` or `rng` (or the raw `DuelState`) to clients before `gameOver`. Live log rendering picks template variants from a derived `storySeed = sha256(seed + ':story')`.
- `state.log` is omniscient (server, and the recap after the match). Clients get **`logFor(state, seat)`**: the opponent's `draw` events have no `card`. Every other event is public information.
- Every event has `seq` and `turn`. `CardRef` carries `uid, cardId, title, pageId, type` plus `power`/`toughness` right after the event.
- Kinds (DUEL_DESIGN §10 names): `start, mulligan, keep, turnStart, draw, deploy, cast, adopt, tokenCreated, connected, rivalry, effect, attack, block, effectDamage, combatDamage, playerDamage, lifeGain, destroyed, bounced, discard, fatigue, concede, timeout, gameOver`. `destroyed` = the recap's casualties. Prose templates live with duel-design / ui-pvp, not in the engine.
## Host binding
`duelEnginePort` (engine.ts) is structurally an `EnginePort<DuelRuleset, DuelCard, DuelState, DuelAction>` (`server/game/engine-port.ts`; `shared/` doesn't import `server/`, so host-pvp types it there). `concedeAction`, `timeoutAction`, `turnSeconds`, `turnOf`, `applyLive` included. `starterDeck` and `previewPool` throw: there are no starters (BRIEF decision 2); draft pools come from both players' collections.
## Stability
These types are the contract for duel-cards, host-pvp, ui-duel, ui-pvp and sim-balance. Additive changes (optional fields, new log kinds) can land with the engine; breaking ones get a `[duel-core] contract change` comment on #55 first.
+72
View File
@@ -0,0 +1,72 @@
# Polite Wikimedia client (Night 2, issue #62)
Every server-side Wikimedia request goes through one process-wide `WikimediaClient`
(`server/wikimedia/client.ts`). The design comes from engineer review §4 and data review §5.
## What it does
| Behaviour | Default |
|---|---|
| Rate per upstream host (`en.wikipedia.org`, `wikimedia.org`, `www.wikidata.org`) | token bucket, 1 req/s, burst 3 |
| Concurrency | 1 request in flight per host |
| Priority lanes | `mint` → `interactive` → `background`; the queue holds 64 waiters, and background work is shed first |
| Longest queue wait | mint 10 s, interactive 5 s, background 120 s |
| `maxlag` | `maxlag=5` on every Action API request; a maxlag reply waits for `Retry-After` and retries up to 2 times |
| 429 / 503 | honour `Retry-After` (seconds or HTTP date), else back off 2ⁿ s with jitter, capped at 10 min, by pausing the whole host |
| Circuit breaker | 3 consecutive 429s stop that host for 10 min. Requests are refused at once without touching Wikimedia |
| Daily cap | 20,000 requests a day per egress IP. Background work stops at 80 %, interactive at 100 %; mint has a 10 % reserve |
| Disk cache | `$WIKIDECK_DATA_DIR/wikimedia-cache.sqlite`: ETag / Last-Modified, `If-None-Match`, stale-while-revalidate on the background lane, stale copy served on refusal |
| User-Agent | `WikiDeck/<version> (+https://gitea.eom.dev/WikiDeck/wikideck; <operator contact>) node/<version>` |
The rate is deliberately 1 req/s for **every** host, including the `wikimedia.org` REST (pageviews) host.
The engineer review suggested 5 req/s there; we are stricter on purpose after the Night 1 rate limiting, because the
budget is per egress IP. Raise it only with a reason and a per-host setting.
Cache lifetimes: article metadata 24 h, monthly pageviews 7 days (the URL names six closed
months, which do not change). Random articles and all mint reads are never cached.
## Operator settings
| Variable | Meaning |
|---|---|
| `WIKIDECK_OPERATOR_CONTACT` | **Set this.** An email address or URL, put in the User-Agent. Without it (and without `PUBLIC_ORIGIN`), the node logs a warning at start-up. Wikimedia policy requires a contact, and contact-less clients are the first to be blocked. |
| `PUBLIC_ORIGIN` | Used as the contact when `WIKIDECK_OPERATOR_CONTACT` is unset. |
| `WIKIDECK_USER_AGENT` | Overrides the whole User-Agent string (sanitized; kept from Night 1). |
| `WIKIDECK_WIKIMEDIA_RPS` | Requests per second per host for the **whole egress IP** (default 1). |
| `WIKIDECK_WIKIMEDIA_BURST` | Token-bucket burst (default 3). |
| `WIKIDECK_WIKIMEDIA_CONCURRENCY` | In-flight requests per host (default 1). |
| `WIKIDECK_WIKIMEDIA_DAILY_CAP` | Requests per UTC day for the whole egress IP (default 20000). |
| `WIKIDECK_WIKIMEDIA_NODES_PER_IP` | How many nodes share this egress IP. The rate and the daily cap are divided by it. **The testnet runs several nodes behind one IP: set it to the node count** (e.g. 3 for `deploy/night/compose.lan.yaml`). |
The testnet's `WIKIDECK_USER_AGENT` already names the project URL, which satisfies the policy.
Counters (`lane.*`, `http.429`, `breaker.open`, `cache.hit`, `refused.*`, …), per-host pause state and the
mint pool's state are in the private operations listener's `/status`, under `wikimedia`.
## Mint path: transport only
- `random()` (W1) and `getFresh()` (W2 and W3) keep their request parameters byte-identical. The only
addition is `maxlag=5`, which changes only error behaviour. `tests/wikimedia-mint.test.ts` pins the exact URLs.
- Mint reads use the `mint` lane and **never** read or write the disk cache, and never join a display read.
- **Fixed:** a 429, 5xx, timeout, network error or non-JSON reply on the pageviews call used to mint a card with
`attention: null` forever (`enrichment.ts`). Now `getFresh()` rejects, the template returns 503 and the miner
retries. `unavailable` is minted only for a genuine 404 (no data) or missing months.
- No block, transfer, trade or validation code changed. Validation never calls Wikimedia.
## Mining prefetch pool (`server/wikimedia/pool.ts`)
Each template used to cost 3+ requests, all in a burst at each new tip, so load scaled with the number of miners.
Now every template on the same tip gets the same fresh article+snapshot pair (miners race for one block anyway),
and a new tip takes the next prefetched pair. Load is about one pair, or 3 requests, per block, whatever the number
of miners.
- Pairs come from the mint-path reads above, and the snapshot is stamped when the pair is fetched, so `capturedAt` is true.
- Pairs older than 5 minutes are dropped, never served.
- The pool (2 pairs) refills only after use, so an idle node makes no requests.
## Card faces
`/api/wikipedia/:id` → `get()`: memory LRU, then the disk cache (fresh, or stale with a background
revalidation), and only then the interactive lane. A grid of cards therefore no longer means a burst
of 2 upstream requests per card per render. `TrendCache` can use `getBackground()` to stay off the
interactive lane.
+72 -27
View File
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { ENRICHMENT_USER_AGENT, wikimediaUserAgent } from './wikimedia-user-agent.ts';
import { WikimediaClient, WikimediaUnavailable, wikimediaClient, type CachePolicy, type Lane } from './wikimedia/client.ts';
import { deriveStats, type PageEnrichment } from '../shared/enrichment.ts';
const MAX_BYTES = 1024 * 1024;
@@ -25,7 +25,23 @@ const viewsSchema = z.object({ items: z.array(z.object({
})).max(6) });
type CacheEntry = { expires: number; value: PageEnrichment } | { expires: number; error: Error };
type Options = { fetch?: typeof globalThis.fetch; now?: () => Date; ttlMs?: number; maxEntries?: number };
type Options = { fetch?: typeof globalThis.fetch; client?: WikimediaClient; now?: () => Date; ttlMs?: number; maxEntries?: number };
type Mode = 'mint' | 'display' | 'background';
/** Disk-cache lifetimes (engineer review §4). Mint reads never use the cache. */
const METADATA_CACHE: CachePolicy = { ttlMs: 24 * 3600_000, staleWhileRevalidate: true, maxBytes: MAX_BYTES };
// The URL names its six completed months, which are immutable; a week bounds late corrections.
const PAGEVIEWS_CACHE: CachePolicy = { ttlMs: 7 * 24 * 3600_000, staleWhileRevalidate: true, maxBytes: MAX_BYTES };
const NOT_FOUND = Symbol('not found');
/** The upstream did not answer properly (throttled, 5xx, timeout, network, non-JSON). Not "no data". */
export class UpstreamFailure extends Error {
constructor(message: string) { super(message); this.name = 'UpstreamFailure'; }
}
function transportFailure(error: unknown): boolean {
return error instanceof WikimediaUnavailable || error instanceof UpstreamFailure ||
(error instanceof DOMException && (error.name === 'TimeoutError' || error.name === 'AbortError'));
}
function monthWindow(now: Date): string[] {
return Array.from({ length: 6 }, (_, index) =>
@@ -34,16 +50,17 @@ function monthWindow(now: Date): string[] {
/** Fixed-source read-only enrichment; no consensus, persistence, URL or header inputs. */
export class WikipediaEnrichment {
private readonly fetcher: typeof globalThis.fetch;
private readonly client: WikimediaClient;
private readonly now: () => Date;
private readonly ttlMs: number;
private readonly maxEntries: number;
private readonly cache = new Map<number, CacheEntry>();
private readonly pending = new Map<number, Promise<PageEnrichment>>();
private readonly pending = new Map<string, Promise<PageEnrichment>>();
private active = 0;
private mintActive = 0;
constructor(options: Options = {}) {
this.fetcher = options.fetch ?? globalThis.fetch;
this.client = options.client ?? (options.fetch ? WikimediaClient.unthrottled(options.fetch) : wikimediaClient);
this.now = options.now ?? (() => new Date());
this.ttlMs = options.ttlMs ?? 6 * 60 * 60 * 1000;
this.maxEntries = options.maxEntries ?? 128;
@@ -53,16 +70,27 @@ export class WikipediaEnrichment {
}
}
/** Card faces (`/api/wikipedia/:id`): memory, then the disk cache, then the interactive lane. */
async get(pageId: number): Promise<PageEnrichment> {
return this.loadCached(pageId, false);
return this.loadCached(pageId, 'display');
}
/** Bypass cached values when the result is being pinned into a newly minted block. */
/** Like get(), on the background lane: for trends and other work nobody is waiting on. */
async getBackground(pageId: number): Promise<PageEnrichment> {
return this.loadCached(pageId, 'background');
}
/**
* Mint data: bypasses every cache and uses the mint lane. If pageviews fail in transport
* (429, 5xx, timeout, throttled), this rejects so the template fails and the miner retries;
* `unavailable` is recorded only for a genuine 404 or missing data.
*/
async getFresh(pageId: number): Promise<PageEnrichment> {
return this.loadCached(pageId, true);
return this.loadCached(pageId, 'mint');
}
private async loadCached(pageId: number, fresh: boolean): Promise<PageEnrichment> {
private async loadCached(pageId: number, mode: Mode): Promise<PageEnrichment> {
const fresh = mode === 'mint';
if (!Number.isSafeInteger(pageId) || pageId <= 0) throw new RangeError('Invalid Wikipedia page ID');
const now = this.now();
const cached = this.cache.get(pageId);
@@ -73,23 +101,29 @@ export class WikipediaEnrichment {
if ('error' in cached) throw cached.error;
return structuredClone(cached.value);
}
this.cache.delete(pageId);
const existing = this.pending.get(pageId);
if (!fresh) this.cache.delete(pageId);
// Mint jobs never join a display job: that one may be served from the disk cache.
const pendingKey = `${fresh ? 'mint' : 'cached'}:${pageId}`;
const existing = this.pending.get(pendingKey);
if (existing) return structuredClone(await existing);
// Reject immediately: no unbounded waiting queue. Four active jobs also keep
// the pending-map ceiling of eight conservative (one upstream request/job).
if (this.pending.size >= 8 || this.active >= 4) throw new Error('Wikipedia enrichment busy');
this.active++;
const job = this.load(pageId, now).then(value => {
// Mint jobs have their own small allowance so card faces can never starve a template.
if (fresh ? this.mintActive >= 2 : this.pending.size - this.mintActive >= 8 || this.active >= 4) {
throw new Error('Wikipedia enrichment busy');
}
if (fresh) this.mintActive++; else this.active++;
const job = this.load(pageId, now, mode).then(value => {
const ttl = value.pageviews.status === 'available' ? this.ttlMs : Math.min(this.ttlMs, 5 * 60 * 1000);
this.remember(pageId, { value, expires: this.now().getTime() + ttl });
return value;
}, () => {
const error = new Error('Wikipedia page enrichment unavailable');
this.remember(pageId, { error, expires: this.now().getTime() + 30_000 });
// A failed mint read says nothing new about display data; keep any cached value.
if (!fresh) this.remember(pageId, { error, expires: this.now().getTime() + 30_000 });
throw error;
}).finally(() => { this.active--; this.pending.delete(pageId); });
this.pending.set(pageId, job);
}).finally(() => { if (fresh) this.mintActive--; else this.active--; this.pending.delete(pendingKey); });
this.pending.set(pendingKey, job);
return structuredClone(await job);
}
@@ -99,16 +133,19 @@ export class WikipediaEnrichment {
while (this.cache.size > this.maxEntries) this.cache.delete(this.cache.keys().next().value!);
}
private async json(url: URL): Promise<unknown> {
const signal = AbortSignal.timeout(6000);
const response = await this.fetcher(url, { signal, redirect: 'error',
headers: { Accept: 'application/json', 'User-Agent': wikimediaUserAgent(ENRICHMENT_USER_AGENT) } });
private async json(url: URL, lane: Lane, cache: CachePolicy | undefined, allowNotFound = false): Promise<unknown> {
const { response, signal } = await this.client.request(url, { lane, timeoutMs: 6000, cache });
if (allowNotFound && response.status === 404) { await response.body?.cancel(); return NOT_FOUND; }
if (!response.ok || response.redirected) {
await response.body?.cancel();
throw new UpstreamFailure(`Upstream HTTP ${response.status}`);
}
const length = response.headers.get('content-length');
if (!response.ok || response.redirected ||
if (
!/^application\/json(?:\s*;|\s*$)/i.test(response.headers.get('content-type') ?? '') ||
(length !== null && (!/^\d+$/.test(length) || Number(length) > MAX_BYTES)) || !response.body) {
await response.body?.cancel();
throw new Error('Invalid upstream response');
throw new UpstreamFailure('Invalid upstream response');
}
const reader = response.body.getReader();
const chunks: Uint8Array[] = [];
@@ -130,12 +167,14 @@ export class WikipediaEnrichment {
}
}
private async load(pageId: number, now: Date): Promise<PageEnrichment> {
private async load(pageId: number, now: Date, mode: Mode): Promise<PageEnrichment> {
const lane: Lane = mode === 'mint' ? 'mint' : mode === 'display' ? 'interactive' : 'background';
const fresh = mode === 'mint';
const url = new URL('https://en.wikipedia.org/w/api.php');
url.search = new URLSearchParams({ action: 'query', format: 'json', formatversion: '2',
pageids: String(pageId), prop: 'info|categories|pageprops|description|langlinks',
clshow: '!hidden', cllimit: '50', lllimit: '500', ppprop: 'wikibase_item' }).toString();
const metadata = metadataSchema.parse(await this.json(url));
const metadata = metadataSchema.parse(await this.json(url, lane, fresh ? undefined : METADATA_CACHE));
const page = metadata.query.pages[0];
if (page.pageid !== pageId) throw new Error('Wikipedia page ID mismatch');
const months = monthWindow(now);
@@ -146,7 +185,9 @@ export class WikipediaEnrichment {
const title = encodeURIComponent(page.title.replace(/ /g, '_'));
const stamp = (month: string) => `${month.replace('-', '')}0100`;
const endpoint = new URL(`https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia.org/all-access/user/${title}/monthly/${stamp(months[0])}/${stamp(latestMonth)}`);
const result = viewsSchema.parse(await this.json(endpoint));
const raw = await this.json(endpoint, lane, fresh ? undefined : PAGEVIEWS_CACHE, true);
if (raw === NOT_FOUND) throw new Error('No pageview data');
const result = viewsSchema.parse(raw);
const byMonth = new Map<string, number>();
for (const item of result.items) {
const month = `${item.timestamp.slice(0, 4)}-${item.timestamp.slice(4, 6)}`;
@@ -161,7 +202,11 @@ export class WikipediaEnrichment {
const days = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 0)).getUTCDate();
pageviews = { status: 'available', months: ordered, latestMonth, total,
dailyAverage: latest / days, trendPercent: prior > 0 ? (latest - prior) / prior * 100 : null };
} catch { /* Metadata remains useful; unknown views must not become fake zeroes. */ }
} catch (error) {
// Mint: a throttled or failed upstream must not pin `attention: null` into a block forever.
if (fresh && transportFailure(error)) throw error;
/* Metadata remains useful; unknown views must not become fake zeroes. */
}
return { pageId, title: page.title, description: page.description ?? null,
wikidataId: page.pageprops?.wikibase_item ?? null, revisionId: page.lastrevid, pageBytes: page.length,
languageCount: page.langlinks?.length ?? 0, languagesTruncated: metadata.continue?.llcontinue !== undefined,
+6 -6
View File
@@ -11,8 +11,7 @@ import { RandomX } from './randomx.ts';
import { Ledger } from './ledger.ts';
import { PeerNetwork } from './peers.ts';
import { wikipediaEnrichment } from '../enrichment.ts';
import { snapshotStats } from '../../shared/enrichment.ts';
import { wikipediaArticles } from '../wiki.ts';
import { configureWikimedia, mintPool, wikimediaStatus } from '../wikimedia/index.ts';
import { IS_MAINNET, PROFILE } from '../../shared/network.ts';
import { assertLaunchAllowed, launchManifest } from '../launch.ts';
import { resourceConfig, RpcBudget, RpcError } from '../resources.ts';
@@ -28,6 +27,7 @@ const apiBudget = new RpcBudget(limits);
const peerBudget = new RpcBudget({ ...limits, requestsPerSecond: 40, writesPerSecond: 10, active: 16 });
const dataDir = process.env.WIKIDECK_DATA_DIR ?? resolve(root,'data');
await mkdir(dataDir,{recursive:true});
configureWikimedia({dataDir});
// Never permit two processes to rewrite the same ledger. A stale lock requires operator review.
const lockPath = resolve(dataDir,`${PROFILE}-node.lock`);
const lock = await open(lockPath,'wx',0o600).catch(()=>{throw new Error('Data directory locked. Stop its other node; remove stale testnet-node.lock only after verifying no node is running.');});
@@ -119,9 +119,9 @@ const templateLimit=rateLimit({windowMs:60000,limit:20,standardHeaders:'draft-8'
app.post('/api/mining/template', templateLimit, async (req,res)=>{
const body=z.object({owner:z.string().regex(/^[a-f0-9]{64}$/),publicKey:z.string().max(256)}).strict().parse(req.body);
try {
const article=await wikipediaArticles.random();
const metrics=await wikipediaEnrichment.getFresh(article.pageId);
res.json(ledger.template(body.owner,body.publicKey,article,snapshotStats(metrics)));
// One fresh article+snapshot per tip, shared by every miner (server/wikimedia/pool.ts).
const {article,statsSnapshot}=await mintPool.take(ledger.summary().tip);
res.json(ledger.template(body.owner,body.publicKey,article,statsSnapshot));
} catch {
res.status(503).json({error:'Could not prepare a Wikipedia article and current statistics. Retry shortly.'});
}
@@ -170,7 +170,7 @@ const server=app.listen(port,host,()=>{
const operations=startOperations(opsPort,()=>({network:NETWORK,
ledger:ledger.diagnostics(),native:engine.diagnostics(),
peers:{configured:peers.status().length,connected:peers.status().filter(p=>p.connected).length},
rpc:{api:apiBudget.status(),p2p:peerBudget.status()}}),resolve(dataDir,PROFILE));
rpc:{api:apiBudget.status(),p2p:peerBudget.status()},wikimedia:wikimediaStatus()}),resolve(dataDir,PROFILE));
server.maxConnections=256;
server.headersTimeout=10000;
server.requestTimeout=15000;
+22 -10
View File
@@ -1,6 +1,6 @@
import { z } from 'zod';
import type { Article } from '../shared/protocol.ts';
import { ARTICLE_USER_AGENT, wikimediaUserAgent } from './wikimedia-user-agent.ts';
import { WikimediaClient, WikimediaUnavailable, wikimediaClient } from './wikimedia/client.ts';
const pageSchema = z.object({
pageid: z.number().int().positive().max(Number.MAX_SAFE_INTEGER), ns: z.literal(0),
@@ -58,11 +58,23 @@ function pictureUrl(value: string | undefined): string | undefined {
} catch { return undefined; }
}
/** The exact W1 request. Its parameters decide minted article content: keep them byte-identical. */
export function randomArticlesUrl(): URL {
const url = new URL('https://en.wikipedia.org/w/api.php');
url.search = new URLSearchParams({ action: 'query', format: 'json', formatversion: '2',
generator: 'random', grnnamespace: '0', grnlimit: '10', prop: 'extracts|pageimages|info|categories',
exintro: '1', explaintext: '1', exchars: '900', piprop: 'thumbnail', pithumbsize: '800', cllimit: '20' }).toString();
return url;
}
export class WikipediaArticleSource {
private readonly fetcher: typeof globalThis.fetch;
private readonly client: WikimediaClient;
private active = 0;
constructor(fetcher: typeof globalThis.fetch = globalThis.fetch) { this.fetcher = fetcher; }
/** A bare fetch (tests) gets an unthrottled pass-through client; production uses the shared client. */
constructor(source: typeof globalThis.fetch | WikimediaClient = wikimediaClient) {
this.client = source instanceof WikimediaClient ? source : WikimediaClient.unthrottled(source);
}
async random(): Promise<Article> {
if (this.active >= 4) throw new Error('Wikipedia article service is busy');
@@ -70,12 +82,8 @@ export class WikipediaArticleSource {
try {
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
try {
const url = new URL('https://en.wikipedia.org/w/api.php');
url.search = new URLSearchParams({ action: 'query', format: 'json', formatversion: '2',
generator: 'random', grnnamespace: '0', grnlimit: '10', prop: 'extracts|pageimages|info|categories',
exintro: '1', explaintext: '1', exchars: '900', piprop: 'thumbnail', pithumbsize: '800', cllimit: '20' }).toString();
const response = await this.fetcher(url, { signal: AbortSignal.timeout(3500), redirect: 'error',
headers: { 'User-Agent': wikimediaUserAgent(ARTICLE_USER_AGENT), Accept: 'application/json' } });
// Mint lane, never cached: random articles become immutable chain data.
const { response } = await this.client.request(randomArticlesUrl(), { lane: 'mint', timeoutMs: 3500 });
const data = responseSchema.parse(await readJson(response));
for (const rawPage of data.query.pages) {
const parsedPage = pageSchema.safeParse(rawPage);
@@ -88,7 +96,11 @@ export class WikipediaArticleSource {
url: `https://en.wikipedia.org/?curid=${page.pageid}`, revisionId: page.lastrevid,
source: 'live', category: category(page.title, page.categories?.map(c => c.title) ?? []) };
}
} catch { /* Try another random API response, but keep the number of attempts bounded. */ }
} catch (error) {
// Throttled, paused or failing upstream: stop now rather than hammer it with retries.
if (error instanceof WikimediaUnavailable) throw error;
/* Otherwise try another random API response, but keep the number of attempts bounded. */
}
}
throw new Error('Wikipedia did not return an article with a picture after three attempts');
} finally {
+41 -6
View File
@@ -1,13 +1,48 @@
// User-Agent for Wikimedia API requests. Wikimedia asks automated clients to identify
// themselves with contact information; operators can opt in with WIKIDECK_USER_AGENT.
// Unset (or blank), each caller keeps its historical default string unchanged.
export const ARTICLE_USER_AGENT = 'Wikideck/0.1 (experimental Wikipedia article cards)';
export const ENRICHMENT_USER_AGENT = 'Wikideck/0.1 (Wikipedia off-chain page enrichment)';
// User-Agent for Wikimedia API requests. Wikimedia's User-Agent policy asks automated clients for a
// descriptive agent with contact details: `Tool/version (+https://site; contact) lib/version`.
// Operators set WIKIDECK_OPERATOR_CONTACT (an email address or URL); PUBLIC_ORIGIN is the fallback.
// WIKIDECK_USER_AGENT still overrides the whole string.
import { readFileSync } from 'node:fs';
export const PROJECT_URL = 'https://gitea.eom.dev/WikiDeck/wikideck';
const MAX_LENGTH = 256;
const SAFE = /^[\x20-\x7e]+$/;
function packageVersion(): string {
try {
const raw = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')) as { version?: unknown };
return typeof raw.version === 'string' && /^[\w.+-]{1,32}$/.test(raw.version) ? raw.version : '0';
} catch { return '0'; }
}
const VERSION = packageVersion();
/** The operator contact, or undefined when none (or only an unsafe value) is configured. */
export function operatorContact(env: Record<string, string | undefined> = process.env): string | undefined {
for (const value of [env.WIKIDECK_OPERATOR_CONTACT, env.PUBLIC_ORIGIN]) {
const trimmed = value?.trim();
if (trimmed && trimmed.length <= 128 && SAFE.test(trimmed) && !/[();]/.test(trimmed)) return trimmed;
}
return undefined;
}
/** `WikiDeck/<version> (+<project URL>; <contact>) node/<version>`. */
export function defaultUserAgent(env: Record<string, string | undefined> = process.env): string {
const contact = operatorContact(env);
return `WikiDeck/${VERSION} (+${PROJECT_URL}${contact ? `; ${contact}` : ''}) node/${process.versions.node}`;
}
export function wikimediaUserAgent(fallback: string, env: Record<string, string | undefined> = process.env): string {
const configured = env.WIKIDECK_USER_AGENT?.trim();
// Reject header-injection/control characters and oversized values; fall back to the default.
if (!configured || configured.length > MAX_LENGTH || !/^[\x20-\x7e]+$/.test(configured)) return fallback;
if (!configured || configured.length > MAX_LENGTH || !SAFE.test(configured)) return fallback;
return configured;
}
/** Start-up check: Wikimedia throttles or blocks clients without contact details first. */
export function warnIfNoContact(env: Record<string, string | undefined> = process.env,
warn: (message: string) => void = console.warn): boolean {
if (wikimediaUserAgent('', env) || operatorContact(env)) return false;
warn('[wikimedia] WARNING: no operator contact in the Wikimedia User-Agent. Set WIKIDECK_OPERATOR_CONTACT '
+ '(an email address or URL) or PUBLIC_ORIGIN; Wikimedia policy requires it and may block this node.');
return true;
}
+74
View File
@@ -0,0 +1,74 @@
/**
* On-disk HTTP cache for off-chain Wikimedia reads (`$WIKIDECK_DATA_DIR/wikimedia-cache.sqlite`).
* It stores the raw body with its ETag / Last-Modified so stale entries can be revalidated with
* conditional requests. Mint-path requests never use it (see client.ts).
*/
import { DatabaseSync } from 'node:sqlite';
export type StoredEntry = {
status: number; body: Buffer; contentType: string; etag: string | null; lastModified: string | null;
expiresAt: number; storedAt: number;
};
export type CacheEntry = StoredEntry & { toResponse(): Response };
/** Canonical key: origin + path + sorted query, without `maxlag` (it changes only error behaviour). */
export function cacheKey(url: URL): string {
const params = [...url.searchParams].filter(([name]) => name !== 'maxlag')
.sort(([a, x], [b, y]) => a < b ? -1 : a > b ? 1 : x < y ? -1 : x > y ? 1 : 0);
return `${url.origin}${url.pathname}?${new URLSearchParams(params).toString()}`;
}
function withResponse(entry: StoredEntry): CacheEntry {
return { ...entry, toResponse: () => new Response(new Uint8Array(entry.body), { status: entry.status,
headers: { 'Content-Type': entry.contentType, 'Content-Length': String(entry.body.byteLength),
...(entry.etag ? { ETag: entry.etag } : {}) } }) };
}
type Row = { status: number; body: Uint8Array; content_type: string; etag: string | null;
last_modified: string | null; expires_at: number; stored_at: number };
export class WikimediaCache {
private readonly db: DatabaseSync;
private readonly maxRows: number;
private writes = 0;
/** `path` may be ':memory:' (tests). */
constructor(path: string, options: { maxRows?: number } = {}) {
this.maxRows = options.maxRows ?? 100_000;
this.db = new DatabaseSync(path);
this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL;
CREATE TABLE IF NOT EXISTS http_cache (key TEXT PRIMARY KEY, status INTEGER NOT NULL, body BLOB NOT NULL,
content_type TEXT NOT NULL, etag TEXT, last_modified TEXT, expires_at INTEGER NOT NULL, stored_at INTEGER NOT NULL);
CREATE INDEX IF NOT EXISTS http_cache_stored ON http_cache(stored_at);`);
}
get(key: string): CacheEntry | undefined {
const row = this.db.prepare('SELECT * FROM http_cache WHERE key = ?').get(key) as Row | undefined;
if (!row) return undefined;
return withResponse({ status: row.status, body: Buffer.from(row.body), contentType: row.content_type,
etag: row.etag, lastModified: row.last_modified, expiresAt: row.expires_at, storedAt: row.stored_at });
}
put(key: string, entry: StoredEntry): CacheEntry {
this.db.prepare(`INSERT OR REPLACE INTO http_cache (key, status, body, content_type, etag, last_modified, expires_at, stored_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`).run(key, entry.status, entry.body, entry.contentType, entry.etag,
entry.lastModified, entry.expiresAt, entry.storedAt);
if (++this.writes % 500 === 0) this.prune();
return withResponse(entry);
}
touch(key: string, expiresAt: number): void {
this.db.prepare('UPDATE http_cache SET expires_at = ? WHERE key = ?').run(expiresAt, key);
}
delete(key: string): void { this.db.prepare('DELETE FROM http_cache WHERE key = ?').run(key); }
size(): number { return (this.db.prepare('SELECT COUNT(*) AS n FROM http_cache').get() as { n: number }).n; }
prune(): void {
this.db.prepare(`DELETE FROM http_cache WHERE key IN (SELECT key FROM http_cache ORDER BY stored_at DESC LIMIT -1 OFFSET ?)`)
.run(this.maxRows);
}
close(): void { this.db.close(); }
}
+441
View File
@@ -0,0 +1,441 @@
/**
* One process-wide, polite Wikimedia HTTP client (engineer review §4, data review §5).
*
* Every server-side Wikimedia request goes through `WikimediaClient.request()`:
* - a token bucket per upstream host (default 1 req/s, burst 3) and 1 request in flight per host;
* - priority lanes: `mint` before `interactive` before `background`, with a bounded queue;
* - `maxlag=5` on Action API requests (it changes only error behaviour, never content);
* - `Retry-After` honoured on 429/503/maxlag by pausing the whole host, with exponential backoff;
* - a circuit breaker: 3 consecutive 429s stop all requests to that host for 10 minutes;
* - a daily request cap, and a budget shared by the nodes behind one egress IP;
* - an optional on-disk cache (see cache.ts) with ETag / If-None-Match and stale-while-revalidate.
*
* The client is transport only. It never rewrites request parameters other than adding `maxlag`,
* and requests made with `cache: undefined` (all mint-path requests) never read or write the cache.
* Nothing here is reachable from block validation, so it cannot affect consensus.
*/
import { WikimediaCache, cacheKey } from './cache.ts';
import { wikimediaUserAgent, defaultUserAgent } from '../wikimedia-user-agent.ts';
export type Lane = 'mint' | 'interactive' | 'background';
const LANES: Lane[] = ['mint', 'interactive', 'background'];
export type Clock = {
now(): number;
setTimeout(fn: () => void, ms: number): unknown;
clearTimeout(handle: unknown): void;
};
const realClock: Clock = {
now: () => Date.now(),
setTimeout: (fn, ms) => { const t = setTimeout(fn, ms); t.unref?.(); return t; },
clearTimeout: handle => clearTimeout(handle as ReturnType<typeof setTimeout>),
};
export type ClientConfig = {
/** Sustained requests per second per upstream host, for this whole egress IP. */
requestsPerSecond: number;
burst: number;
concurrencyPerHost: number;
/** Requests per UTC day for this whole egress IP. */
dailyCap: number;
/** Nodes sharing one egress IP; the rate and daily cap are divided between them. */
nodesPerIp: number;
queueLimit: number;
/** Longest wait in the queue before a request fails, per lane. */
maxWaitMs: Record<Lane, number>;
breakerThreshold: number;
breakerMs: number;
maxBackoffMs: number;
maxlag: number;
/** Retries of a single request after a maxlag reply (HTTP 200/503 with MediaWiki-API-Error: maxlag). */
maxlagRetries: number;
};
export const DEFAULT_CONFIG: ClientConfig = {
requestsPerSecond: 1, burst: 3, concurrencyPerHost: 1, dailyCap: 20_000, nodesPerIp: 1, queueLimit: 64,
maxWaitMs: { mint: 10_000, interactive: 5_000, background: 120_000 },
breakerThreshold: 3, breakerMs: 10 * 60_000, maxBackoffMs: 10 * 60_000, maxlag: 5, maxlagRetries: 2,
};
/** Unthrottled settings for tests and tools that inject their own fetch. */
export const UNTHROTTLED: Partial<ClientConfig> = {
requestsPerSecond: 1e9, burst: 1e9, concurrencyPerHost: 1e9, dailyCap: Number.MAX_SAFE_INTEGER, queueLimit: 1e9,
breakerThreshold: Number.MAX_SAFE_INTEGER, maxlagRetries: 0, maxBackoffMs: 0,
};
function positive(value: string | undefined, fallback: number, integer = false): number {
if (value === undefined || value.trim() === '') return fallback;
const n = Number(value);
if (!Number.isFinite(n) || n <= 0 || (integer && !Number.isSafeInteger(n))) {
console.warn(`[wikimedia] ignoring invalid setting ${JSON.stringify(value)}; using ${fallback}`);
return fallback;
}
return n;
}
/** Operator settings. Documented in docs/night2/WIKIMEDIA.md. */
export function configFromEnv(env: Record<string, string | undefined> = process.env): ClientConfig {
return { ...DEFAULT_CONFIG,
requestsPerSecond: positive(env.WIKIDECK_WIKIMEDIA_RPS, DEFAULT_CONFIG.requestsPerSecond),
burst: positive(env.WIKIDECK_WIKIMEDIA_BURST, DEFAULT_CONFIG.burst),
concurrencyPerHost: positive(env.WIKIDECK_WIKIMEDIA_CONCURRENCY, DEFAULT_CONFIG.concurrencyPerHost, true),
dailyCap: positive(env.WIKIDECK_WIKIMEDIA_DAILY_CAP, DEFAULT_CONFIG.dailyCap, true),
nodesPerIp: positive(env.WIKIDECK_WIKIMEDIA_NODES_PER_IP, DEFAULT_CONFIG.nodesPerIp, true),
};
}
/** Thrown for every refusal or transport failure: queue full, cap reached, circuit open, timeout, network. */
export class WikimediaUnavailable extends Error {
readonly reason: string;
readonly status?: number;
constructor(reason: string, status?: number) {
super(`Wikimedia unavailable: ${reason}`);
this.name = 'WikimediaUnavailable';
this.reason = reason; this.status = status;
}
}
export type CachePolicy = {
ttlMs: number;
/** Serve a stale entry at once and revalidate it on the background lane. */
staleWhileRevalidate?: boolean;
/** Largest body stored, in bytes. */
maxBytes?: number;
};
export type RequestOptions = {
lane: Lane;
/** Starts when the request is dispatched, not while it waits in the queue. */
timeoutMs: number;
headers?: Record<string, string>;
/** Omit for mint data: the request then bypasses the cache completely. */
cache?: CachePolicy;
};
export type ClientResponse = { response: Response; signal: AbortSignal; fromCache: boolean };
type Waiter = {
lane: Lane; host: string; enqueuedAt: number;
run: () => void; fail: (error: Error) => void; timer?: unknown;
};
type HostState = {
tokens: number; refilledAt: number; inFlight: number;
pausedUntil: number; breakerUntil: number; consecutive429: number; backoffExponent: number;
};
export type ClientStatus = {
day: string; requestsToday: number; dailyCap: number; requestsPerSecond: number; queued: number;
counters: Record<string, number>;
hosts: Record<string, { inFlight: number; pausedForMs: number; circuitOpen: boolean; consecutive429: number }>;
};
function parseRetryAfter(value: string | null, now: number): number | undefined {
if (!value) return undefined;
if (/^\d+$/.test(value.trim())) return Number(value.trim()) * 1000;
const at = Date.parse(value);
return Number.isNaN(at) ? undefined : Math.max(0, at - now);
}
function isActionApi(url: URL): boolean {
return url.pathname === '/w/api.php' && /(^|\.)(wikipedia|wikidata|wikimedia)\.org$/.test(url.hostname);
}
export class WikimediaClient {
readonly config: ClientConfig;
private readonly fetcher: typeof globalThis.fetch;
private readonly clock: Clock;
private readonly userAgent: () => string;
private cache: WikimediaCache | undefined;
private readonly hosts = new Map<string, HostState>();
private readonly queues = new Map<Lane, Waiter[]>(LANES.map(lane => [lane, []]));
private readonly revalidating = new Set<string>();
private pumpTimer: unknown;
private pumpAt = Infinity;
private day = '';
private requestsToday = 0;
readonly counters: Record<string, number> = {};
constructor(options: { fetch?: typeof globalThis.fetch; config?: Partial<ClientConfig>; clock?: Clock;
cache?: WikimediaCache; userAgent?: () => string } = {}) {
this.fetcher = options.fetch ?? ((...args) => globalThis.fetch(...args));
this.config = { ...DEFAULT_CONFIG, ...options.config,
maxWaitMs: { ...DEFAULT_CONFIG.maxWaitMs, ...options.config?.maxWaitMs } };
this.clock = options.clock ?? realClock;
this.userAgent = options.userAgent ?? (() => wikimediaUserAgent(defaultUserAgent()));
this.cache = options.cache;
}
/** A pass-through client for injected fetchers (tests): no rate limit, no breaker, no cache. */
static unthrottled(fetcher: typeof globalThis.fetch): WikimediaClient {
return new WikimediaClient({ fetch: fetcher, config: UNTHROTTLED });
}
attachCache(cache: WikimediaCache | undefined): void { this.cache = cache; }
private count(name: string): void { this.counters[name] = (this.counters[name] ?? 0) + 1; }
private get rate(): number { return this.config.requestsPerSecond / this.config.nodesPerIp; }
private get cap(): number { return Math.max(1, Math.floor(this.config.dailyCap / this.config.nodesPerIp)); }
private host(name: string): HostState {
let state = this.hosts.get(name);
if (!state) {
state = { tokens: this.config.burst, refilledAt: this.clock.now(), inFlight: 0,
pausedUntil: 0, breakerUntil: 0, consecutive429: 0, backoffExponent: 0 };
this.hosts.set(name, state);
}
return state;
}
private refill(state: HostState, now: number): void {
state.tokens = Math.min(this.config.burst, state.tokens + (now - state.refilledAt) / 1000 * this.rate);
state.refilledAt = now;
}
/** Daily budget by lane: background stops at 80 %, interactive at 100 %, mint has a 10 % reserve. */
private overCap(lane: Lane): boolean {
const today = new Date(this.clock.now()).toISOString().slice(0, 10);
if (today !== this.day) { this.day = today; this.requestsToday = 0; }
const limit = lane === 'background' ? this.cap * 0.8 : lane === 'interactive' ? this.cap : this.cap * 1.1;
return this.requestsToday >= limit;
}
status(): ClientStatus {
const now = this.clock.now();
return { day: this.day, requestsToday: this.requestsToday, dailyCap: this.cap, requestsPerSecond: this.rate,
queued: LANES.reduce((n, lane) => n + this.queues.get(lane)!.length, 0), counters: { ...this.counters },
hosts: Object.fromEntries([...this.hosts].map(([name, s]) => [name,
{ inFlight: s.inFlight, pausedForMs: Math.max(0, s.pausedUntil - now), circuitOpen: s.breakerUntil > now,
consecutive429: s.consecutive429 }])) };
}
async request(input: URL, options: RequestOptions): Promise<ClientResponse> {
const url = new URL(input);
if (url.protocol !== 'https:') throw new RangeError('Wikimedia requests must use HTTPS');
if (isActionApi(url) && !url.searchParams.has('maxlag')) url.searchParams.append('maxlag', String(this.config.maxlag));
const key = options.cache && this.cache ? cacheKey(url) : undefined;
const entry = key ? this.cache?.get(key) : undefined;
const now = this.clock.now();
if (entry && entry.expiresAt > now) {
this.count('cache.hit');
return { response: entry.toResponse(), signal: new AbortController().signal, fromCache: true };
}
if (entry && options.cache?.staleWhileRevalidate) {
this.count('cache.stale');
this.revalidate(url, key!, options);
return { response: entry.toResponse(), signal: new AbortController().signal, fromCache: true };
}
try {
return await this.network(url, options, key);
} catch (error) {
// Prefer a stale copy over an error for anything that is allowed to use the cache.
if (entry && error instanceof WikimediaUnavailable) {
this.count('cache.staleOnError');
return { response: entry.toResponse(), signal: new AbortController().signal, fromCache: true };
}
throw error;
}
}
private revalidate(url: URL, key: string, options: RequestOptions): void {
if (this.revalidating.has(key)) return;
this.revalidating.add(key);
void this.network(url, { ...options, lane: 'background' }, key)
.then(({ response }) => response.body?.cancel(), () => this.count('cache.revalidateFailed'))
.finally(() => this.revalidating.delete(key));
}
private async network(url: URL, options: RequestOptions, key: string | undefined): Promise<ClientResponse> {
for (let attempt = 0; ; attempt++) {
const result = await this.dispatch(url, options, key);
if (result !== 'maxlag') return result;
if (attempt >= this.config.maxlagRetries) throw new WikimediaUnavailable('maxlag');
}
}
/** Wait for a slot, send one request, and classify the reply. */
private async dispatch(url: URL, options: RequestOptions, key: string | undefined): Promise<ClientResponse | 'maxlag'> {
const hostName = url.hostname;
const wait = this.acquire(hostName, options.lane);
if (wait) await wait; // No await on the fast path: an idle host dispatches synchronously.
const state = this.host(hostName);
const headers = new Headers({ Accept: 'application/json', 'Accept-Encoding': 'gzip', ...options.headers,
'User-Agent': this.userAgent() });
const entry = key ? this.cache?.get(key) : undefined;
if (entry?.etag) headers.set('If-None-Match', entry.etag);
if (entry?.lastModified) headers.set('If-Modified-Since', entry.lastModified);
const signal = AbortSignal.timeout(options.timeoutMs);
this.requestsToday++;
this.count(`lane.${options.lane}`);
let response: Response;
try {
response = await this.fetcher(url, { signal, redirect: 'error', headers });
} catch (error) {
this.release(state);
this.count('error.network');
throw new WikimediaUnavailable(signal.aborted ? 'timeout' : `network: ${(error as Error).message}`);
}
const now = this.clock.now();
const retryAfter = parseRetryAfter(response.headers.get('retry-after'), now);
const apiError = response.headers.get('mediawiki-api-error');
if (apiError === 'maxlag') {
await response.body?.cancel();
this.count('maxlag');
this.pause(state, Math.min(this.config.maxBackoffMs, retryAfter ?? 5000), false);
this.release(state);
return 'maxlag';
}
if (response.status === 429 || response.status === 503) {
await response.body?.cancel();
this.count(`http.${response.status}`);
if (response.status === 429) state.consecutive429++;
const backoff = Math.min(this.config.maxBackoffMs, 2 ** state.backoffExponent * 1000 * (1 + Math.random() * 0.25));
state.backoffExponent = Math.min(state.backoffExponent + 1, 20);
if (state.consecutive429 >= this.config.breakerThreshold) {
this.pause(state, this.config.breakerMs, true);
this.count('breaker.open');
} else {
this.pause(state, Math.min(this.config.maxBackoffMs, Math.max(retryAfter ?? 0, backoff)), false);
}
this.release(state);
throw new WikimediaUnavailable(`HTTP ${response.status}`, response.status);
}
state.consecutive429 = 0;
state.backoffExponent = 0;
this.release(state);
if (response.status === 304 && entry && key) {
this.count('cache.revalidated');
this.cache?.touch(key, now + options.cache!.ttlMs);
return { response: entry.toResponse(), signal, fromCache: true };
}
if (key && this.cache && response.status === 200 && !response.redirected && options.cache &&
/^application\/json(?:\s*;|\s*$)/i.test(response.headers.get('content-type') ?? '')) {
// Store the raw body; callers re-validate it exactly as they would a network reply.
const body = await this.readLimited(response, options.cache.maxBytes ?? 1024 * 1024, signal);
const stored = this.cache.put(key, { status: response.status, body,
contentType: response.headers.get('content-type') ?? '', etag: response.headers.get('etag'),
lastModified: response.headers.get('last-modified'), expiresAt: now + options.cache.ttlMs, storedAt: now });
return { response: stored.toResponse(), signal, fromCache: false };
}
if (response.status >= 500) this.count(`http.${response.status}`);
return { response, signal, fromCache: false };
}
private async readLimited(response: Response, maxBytes: number, signal: AbortSignal): Promise<Buffer> {
const length = response.headers.get('content-length');
if (length !== null && (!/^\d+$/.test(length) || Number(length) > maxBytes) || !response.body) {
await response.body?.cancel();
throw new WikimediaUnavailable('oversized or empty response');
}
const reader = response.body.getReader();
const chunks: Uint8Array[] = [];
let size = 0;
try {
for (;;) {
if (signal.aborted) throw new WikimediaUnavailable('timeout');
const { done, value } = await reader.read();
if (done) break;
size += value.byteLength;
if (size > maxBytes) throw new WikimediaUnavailable('oversized response');
chunks.push(value);
}
return Buffer.concat(chunks);
} catch (error) {
throw error instanceof WikimediaUnavailable ? error : new WikimediaUnavailable('read failed');
} finally {
await reader.cancel().catch(() => {});
reader.releaseLock();
}
}
private pause(state: HostState, ms: number, breaker: boolean): void {
const until = this.clock.now() + ms;
if (until > state.pausedUntil) {
if (state.pausedUntil <= this.clock.now()) {
console.warn(`[wikimedia] ${breaker ? 'circuit open' : 'pausing'} for ${Math.ceil(ms / 1000)} s`);
}
state.pausedUntil = until;
}
if (breaker) { state.consecutive429 = 0; state.breakerUntil = until; }
}
private release(state: HostState): void {
state.inFlight--;
this.pump();
}
/** Undefined when a slot was taken at once; otherwise a promise for the slot. Throws on refusal. */
private acquire(hostName: string, lane: Lane): Promise<void> | undefined {
const state = this.host(hostName);
const now = this.clock.now();
if (this.overCap(lane)) { this.count('refused.cap'); throw new WikimediaUnavailable('daily cap reached'); }
// A paused host refuses at once when the pause outlasts this lane's patience.
if (state.pausedUntil - now > this.config.maxWaitMs[lane]) {
this.count('refused.paused');
throw new WikimediaUnavailable(state.breakerUntil > now ? 'circuit open' : 'host paused');
}
this.refill(state, now);
if (state.pausedUntil <= now && state.inFlight < this.config.concurrencyPerHost && state.tokens >= 1 &&
!LANES.some(l => this.queues.get(l)!.some(w => w.host === hostName))) {
state.tokens -= 1;
state.inFlight++;
return undefined;
}
const queued = LANES.reduce((n, l) => n + this.queues.get(l)!.length, 0);
if (queued >= this.config.queueLimit) {
// Background work is shed first; a mint request evicts the newest background waiter.
const background = this.queues.get('background')!;
if (lane === 'background' || background.length === 0) {
this.count('refused.queue');
throw new WikimediaUnavailable('queue full');
}
const evicted = background.pop()!;
this.clock.clearTimeout(evicted.timer);
evicted.fail(new WikimediaUnavailable('queue full'));
}
return new Promise<void>((resolve, reject) => {
const waiter: Waiter = { lane, host: hostName, enqueuedAt: now,
run: () => { this.clock.clearTimeout(waiter.timer); resolve(); },
fail: error => reject(error) };
waiter.timer = this.clock.setTimeout(() => {
const queue = this.queues.get(lane)!;
const index = queue.indexOf(waiter);
if (index >= 0) { queue.splice(index, 1); this.count('refused.wait'); reject(new WikimediaUnavailable('queue wait')); }
}, this.config.maxWaitMs[lane]);
this.queues.get(lane)!.push(waiter);
this.pump();
});
}
/** Start every waiter that may run now, highest lane first; otherwise arm a timer for the next slot. */
private pump(): void {
const now = this.clock.now();
let next = Infinity;
for (const lane of LANES) {
const queue = this.queues.get(lane)!;
for (let i = 0; i < queue.length;) {
const waiter = queue[i];
// A host is served strictly by priority: lower lanes wait while a higher lane waits for the same host.
if (LANES.slice(0, LANES.indexOf(lane)).some(l => this.queues.get(l)!.some(w => w.host === waiter.host))) { i++; continue; }
const state = this.host(waiter.host);
this.refill(state, now);
if (state.pausedUntil > now) { next = Math.min(next, state.pausedUntil); i++; continue; }
if (state.inFlight >= this.config.concurrencyPerHost) { i++; continue; }
if (state.tokens < 1) { next = Math.min(next, now + (1 - state.tokens) / this.rate * 1000); i++; continue; }
state.tokens -= 1;
state.inFlight++;
queue.splice(i, 1);
waiter.run();
}
}
if (next < Infinity && next < this.pumpAt) {
if (this.pumpTimer !== undefined) this.clock.clearTimeout(this.pumpTimer);
this.pumpAt = next;
this.pumpTimer = this.clock.setTimeout(() => { this.pumpTimer = undefined; this.pumpAt = Infinity; this.pump(); },
Math.max(1, Math.ceil(next - now)));
}
}
}
/** The process-wide client. `configureWikimedia()` (server start-up) attaches the disk cache. */
export const wikimediaClient = new WikimediaClient({ config: configFromEnv() });
+36
View File
@@ -0,0 +1,36 @@
/**
* Node start-up wiring for the polite Wikimedia client: the disk cache, the User-Agent warning,
* the mining prefetch pool and an ops status snapshot. See docs/night2/WIKIMEDIA.md.
*/
import { resolve } from 'node:path';
import { WikimediaCache } from './cache.ts';
import { wikimediaClient } from './client.ts';
import { MintTemplatePool } from './pool.ts';
import { warnIfNoContact } from '../wikimedia-user-agent.ts';
import { wikipediaArticles } from '../wiki.ts';
import { wikipediaEnrichment } from '../enrichment.ts';
export { wikimediaClient } from './client.ts';
/** Serves every mining template; shared by all miners of this node. */
export const mintPool = new MintTemplatePool({ articles: wikipediaArticles, enrichment: wikipediaEnrichment });
let cache: WikimediaCache | undefined;
/** Call once at start-up. Without it the client still works, with in-memory caching only. */
export function configureWikimedia(options: { dataDir: string }): void {
warnIfNoContact();
if (cache) return;
try {
cache = new WikimediaCache(resolve(options.dataDir, 'wikimedia-cache.sqlite'));
wikimediaClient.attachCache(cache);
} catch (error) {
console.warn(`[wikimedia] disk cache disabled: ${(error as Error).message}`);
}
}
export function closeWikimedia(): void { wikimediaClient.attachCache(undefined); cache?.close(); cache = undefined; }
export function wikimediaStatus() {
return { client: wikimediaClient.status(), cacheEntries: cache?.size() ?? null, mintPool: mintPool.status() };
}
+88
View File
@@ -0,0 +1,88 @@
/**
* Mining-template prefetch pool (data review §5, "Mining load").
*
* Without it every miner costs 3+ Wikimedia requests per template, all in a burst at each new tip.
* The pool keeps a few fresh article + stats snapshots ready, and serves the SAME pair to every
* template built on the same tip (miners race for one block anyway). Wikimedia load is then about
* one pair (3 requests) per block, whatever the number of miners.
*
* Mint data stays transport-only: each pair comes from the mint-path reads (`random()` and
* `getFresh()`, byte-identical parameters, never the disk cache), and the snapshot is stamped when it
* is fetched, so `capturedAt` stays true. Pairs older than `maxAgeMs` are discarded, never served.
* The pool refills only after it is used, so an idle node makes no requests.
*/
import type { Article } from '../../shared/protocol.ts';
import { snapshotStats, type MintStatsSnapshot, type PageEnrichment } from '../../shared/enrichment.ts';
export type MintPair = { article: Article; statsSnapshot: MintStatsSnapshot };
type Entry = MintPair & { fetchedAt: number };
type Sources = {
articles: { random(): Promise<Article> };
enrichment: { getFresh(pageId: number): Promise<PageEnrichment> };
};
export class MintTemplatePool {
private readonly sources: Sources;
private readonly size: number;
private readonly maxAgeMs: number;
private readonly now: () => number;
private readonly ready: Entry[] = [];
private assigned: { tip: string; entry: Entry } | undefined;
private filling: Promise<void> | undefined;
private direct: Promise<Entry> | undefined;
readonly counters = { served: 0, shared: 0, fetched: 0, expired: 0, failed: 0 };
constructor(sources: Sources, options: { size?: number; maxAgeMs?: number; now?: () => number } = {}) {
this.sources = sources;
this.size = options.size ?? 2;
this.maxAgeMs = options.maxAgeMs ?? 5 * 60_000;
this.now = options.now ?? Date.now;
if (!Number.isSafeInteger(this.size) || this.size < 0 || this.size > 16 ||
!Number.isFinite(this.maxAgeMs) || this.maxAgeMs <= 0) throw new RangeError('Invalid mint pool options');
}
private fresh(entry: Entry): boolean { return this.now() - entry.fetchedAt < this.maxAgeMs; }
private async fetchOne(): Promise<Entry> {
const article = await this.sources.articles.random();
const metrics = await this.sources.enrichment.getFresh(article.pageId);
this.counters.fetched++;
return { article, statsSnapshot: snapshotStats(metrics), fetchedAt: this.now() };
}
/** The article and snapshot for a template on `tip`. Rejects when Wikimedia is unavailable. */
async take(tip: string): Promise<MintPair> {
let entry: Entry | undefined;
if (this.assigned && this.assigned.tip === tip && this.fresh(this.assigned.entry)) {
entry = this.assigned.entry;
this.counters.shared++;
} else {
while (this.ready.length && !this.fresh(this.ready[0])) { this.ready.shift(); this.counters.expired++; }
entry = this.ready.shift();
if (!entry) {
// Cold: fetch directly, and let concurrent templates share that one fetch.
this.direct ??= this.fetchOne().finally(() => { this.direct = undefined; });
try { entry = await this.direct; }
catch (error) { this.counters.failed++; throw error; }
if (this.assigned && this.assigned.tip === tip && this.fresh(this.assigned.entry)) entry = this.assigned.entry;
}
this.assigned = { tip, entry };
}
this.counters.served++;
this.refill();
return structuredClone({ article: entry.article, statsSnapshot: entry.statsSnapshot });
}
/** Top the pool up one pair at a time, in the background. Exposed for tests. */
refill(): Promise<void> {
this.filling ??= (async () => {
while (this.ready.filter(e => this.fresh(e)).length < this.size) {
try { this.ready.push(await this.fetchOne()); }
catch { this.counters.failed++; break; } // The next take() tries again; no retry loop.
}
})().finally(() => { this.filling = undefined; });
return this.filling;
}
status() { return { ready: this.ready.length, size: this.size, maxAgeMs: this.maxAgeMs, ...this.counters }; }
}
-908
View File
@@ -1,908 +0,0 @@
/**
* WikiDeck Duel engine (off-chain, pure, deterministic). Spec: docs/game/DUEL_API.md.
* No stack, sorcery speed only, atomic actions, clone-on-apply.
*/
import { Rng } from '../game/rng.ts';
import { DEFAULT_DUEL_RULESET, parseDuelRuleset } from './ruleset.ts';
import type {
CardInstance, CardRef, ConnectedTier, DuelAction, DuelCard, DuelResult, DuelRuleset, DuelState, EffectSpec, EndReason,
Keyword, LogEvent, Permanent, PermanentView, PlayerState, PlayerView, Seat, SideView, TargetRef, TargetSpec,
} from './types.ts';
import { CARD_TYPES, KEYWORDS } from './types.ts';
type LogBody = LogEvent extends infer E ? (E extends LogEvent ? Omit<E, 'seq' | 'turn'> : never) : never;
type Source = Permanent | CardInstance;
const other = (seat: Seat): Seat => (seat === 0 ? 1 : 0);
const pw = (card: DuelCard) => card.power ?? 0;
const tg = (card: DuelCard) => card.toughness ?? 0;
const CHOSEN: ReadonlySet<TargetSpec> = new Set(['figure', 'figureOrLocation', 'ownFigure', 'opponentFigure', 'anyTarget']);
const NO_KIN = new Set(['Q5', 'Q16521']);
const MAX_COMBOS = 64;
function targetOf(e: EffectSpec): TargetSpec {
if (e.target) return e.target;
switch (e.op) {
case 'damage': return 'anyTarget';
case 'damageEach': return 'each';
case 'pump': return 'self';
case 'drain': return 'opponent';
case 'attach': return 'ownFigure';
case 'fight': return 'opponentFigure';
case 'destroy': case 'bounce': case 'tap': return 'opponentFigure';
default: return 'controller';
}
}
const isEntry = (e: EffectSpec) => e.trigger === 'onPlay' || e.trigger === 'onEnter';
// ---------------------------------------------------------------------------
// Decks
// ---------------------------------------------------------------------------
export function validateDeck(rules: DuelRuleset, cards: DuelCard[]): void {
if (!Array.isArray(cards)) throw new TypeError('Deck must be an array');
if (cards.length !== rules.deckSize) throw new RangeError(`Deck must have exactly ${rules.deckSize} cards (has ${cards.length})`);
const ids = new Set<string>();
const pages = new Map<number, number>();
for (const card of cards) {
if (!card || typeof card.cardId !== 'string' || !card.cardId) throw new TypeError('Card needs a cardId');
if (card.token) throw new RangeError(`Tokens can't be in decks (${card.cardId})`);
if (ids.has(card.cardId)) throw new RangeError(`Duplicate card ${card.cardId}`);
ids.add(card.cardId);
if (card.pageId !== null) {
const n = (pages.get(card.pageId) ?? 0) + 1;
if (n > rules.maxCopiesPerPage) throw new RangeError(`More than ${rules.maxCopiesPerPage} copies of page ${card.pageId}`);
pages.set(card.pageId, n);
}
if (!CARD_TYPES.includes(card.type)) throw new RangeError(`Bad card type ${card.type}`);
if (!Number.isInteger(card.cost) || card.cost < 0 || card.cost > 20) throw new RangeError(`Bad cost on ${card.cardId}`);
for (const n of [pw(card), tg(card)]) if (!Number.isInteger(n) || n < 0 || n > 50) throw new RangeError(`Bad stats on ${card.cardId}`);
if (card.type === 'Figure' && tg(card) < 1) throw new RangeError(`Figure ${card.cardId} needs toughness ≥ 1`);
if (!Array.isArray(card.keywords)) throw new TypeError(`Bad keywords on ${card.cardId}`);
// Trending is granted by the host at match start (applyLive) and doesn't count toward the limit.
if (card.keywords.filter(k => k !== 'Trending').length > rules.maxKeywords) throw new RangeError(`Too many keywords on ${card.cardId}`);
for (const k of card.keywords) if (!KEYWORDS.includes(k)) throw new RangeError(`Unknown keyword ${k}`);
if (!Array.isArray(card.effects) || card.effects.length > 4) throw new RangeError(`Bad effects on ${card.cardId}`);
if (card.territory !== undefined && (!Number.isInteger(card.territory) || card.territory < 0 || card.territory > 5)) throw new RangeError(`Bad territory on ${card.cardId}`);
}
}
// ---------------------------------------------------------------------------
// Creation
// ---------------------------------------------------------------------------
function emptyPlayer(rules: DuelRuleset, library: CardInstance[]): PlayerState {
return {
life: rules.startingLife, citations: 0, baseCitations: 0, library, hand: [], graveyard: [], mulligansTaken: 0, ready: false,
locationsThisTurn: 0, bankSeconds: rules.bankSeconds, timeouts: 0, fatigue: 0,
};
}
export function createDuel(rules: DuelRuleset, decks: [DuelCard[], DuelCard[]], seed: string): DuelState {
const r = parseDuelRuleset(rules);
const rng = Rng.fromSeed(seed);
const firstSeat = rng.int(2) as Seat;
const players = ([0, 1] as Seat[]).map(seat => {
const instances = decks[seat].map((card, i) => ({ uid: `${seat}:${i}`, owner: seat, card: structuredClone(card) }));
return emptyPlayer(r, rng.shuffle(instances));
}) as [PlayerState, PlayerState];
const state: DuelState = {
engine: 'duel', rules: r, seed, rng: rng.state, turn: 0, active: firstSeat, firstSeat, phase: 'mulligan',
players, battlefield: [], combat: { attackers: [], blocks: {} }, log: [], result: null, version: 0, nextToken: 0, nextEntry: 0,
};
log(state, { kind: 'start', firstSeat });
for (const p of state.players) p.hand = p.library.splice(0, r.openingHand);
return state;
}
// ---------------------------------------------------------------------------
// Queries
// ---------------------------------------------------------------------------
export function toAct(state: DuelState): Seat | null {
if (state.result) return null;
if (state.phase === 'mulligan') {
for (const seat of [0, 1] as Seat[]) if (!state.players[seat].ready) return seat;
return null;
}
return state.phase === 'block' ? other(state.active) : state.active;
}
export const isOver = (state: DuelState) => state.result !== null;
export const result = (state: DuelState): DuelResult | null => state.result;
export const turnOf = (state: DuelState) => state.turn;
const find = (state: DuelState, uid: string) => state.battlefield.find(p => p.uid === uid);
export function maxCitations(state: DuelState, seat: Seat): number {
const territory = state.battlefield.filter(p => p.controller === seat && p.card.type === 'Location').reduce((n, p) => n + (p.card.territory ?? 0), 0);
return state.players[seat].baseCitations + Math.min(state.rules.territoryCap, territory);
}
/** Connected tier between two cards (DUEL_DESIGN §6), best tier first, limited to rules.connectedTiers. */
export function tierBetween(rules: DuelRuleset, a: DuelCard, b: DuelCard): { tier: ConnectedTier; via: string } | null {
const ca = a.connect, cb = b.connect;
if (!ca || !cb) return null;
const on = (t: ConnectedTier) => rules.connectedTiers.includes(t);
const linked = (b.pageId !== null && ca.links.includes(b.pageId)) || (a.pageId !== null && cb.links.includes(a.pageId));
const cited = (!!cb.qid && !!ca.cites?.includes(cb.qid)) || (!!ca.qid && !!cb.cites?.includes(ca.qid));
if (on('Linked') && (linked || cited)) return { tier: 'Linked', via: cited ? 'claim' : 'link' };
if (on('Compatriot')) { const c = ca.countries.find(x => cb.countries.includes(x)); if (c !== undefined) return { tier: 'Compatriot', via: `country: ${c}` }; }
if (on('Contemporary') && a.type !== b.type) { const c = ca.centuries.find(x => cb.centuries.includes(x)); if (c !== undefined) return { tier: 'Contemporary', via: `century: ${c}` }; }
if (on('Kin')) { const c = ca.p31.find(x => !NO_KIN.has(x) && cb.p31.includes(x)); if (c !== undefined) return { tier: 'Kin', via: `instance of: ${c}` }; }
return null;
}
function connectionOf(state: DuelState, perm: Permanent): { with: Permanent; tier: ConnectedTier; via: string; bonus: number } | null {
if (perm.card.type !== 'Figure' || !perm.card.connect) return null;
let best: { with: Permanent; tier: ConnectedTier; via: string; bonus: number } | null = null;
for (const mate of state.battlefield) {
if (mate.uid === perm.uid || mate.controller !== perm.controller) continue;
const t = tierBetween(state.rules, perm.card, mate.card);
if (!t) continue;
const bonus = t.tier === 'Linked' ? state.rules.linkedBonus : state.rules.connectedBonus;
if (!best || bonus > best.bonus) best = { with: mate, tier: t.tier, via: t.via, bonus };
}
return best;
}
/** Does a static effect of `src` apply to `perm`? Conditions use printed power (no feedback). */
function staticApplies(state: DuelState, src: Permanent, e: EffectSpec, perm: Permanent): boolean {
const fig = perm.card.type === 'Figure';
const t = targetOf(e);
const hit = t === 'attached' ? src.attachedTo === perm.uid
: t === 'self' ? src.uid === perm.uid
: t === 'ownFigures' ? fig && perm.controller === src.controller
: t === 'opponentFigures' ? fig && perm.controller !== src.controller
: t === 'each' ? fig || perm.card.type === 'Location' : false;
if (!hit) return false;
const c = e.condition;
if (!c) return true;
if (c.maxPower !== undefined && pw(perm.card) > c.maxPower) return false;
if (c.samePortal) {
const anchorUid = src.card.type === 'Doctrine' ? src.attachedTo : src.uid;
const anchor = anchorUid ? find(state, anchorUid) : undefined;
if (!anchor || perm.uid === anchor.uid || perm.card.portal !== anchor.card.portal) return false;
}
if (c.connected && !tierBetween(state.rules, perm.card, src.card)) return false;
return true;
}
function statics(state: DuelState, perm: Permanent): { p: number; t: number; grants: Keyword[] } {
let p = 0, t = 0;
const grants: Keyword[] = [];
for (const src of state.battlefield) {
for (const e of src.card.effects) {
if (e.trigger !== 'static' || !staticApplies(state, src, e, perm)) continue;
if (e.op === 'pump') { p += e.power ?? e.amount ?? 0; t += e.toughness ?? e.amount ?? 0; }
if (e.grant) grants.push(...e.grant);
}
}
return { p, t, grants };
}
export function keywordsOf(state: DuelState, perm: Permanent): Keyword[] {
const out = new Set<Keyword>([...perm.card.keywords, ...perm.permKeywords, ...perm.tempKeywords, ...statics(state, perm).grants]);
return KEYWORDS.filter(k => out.has(k));
}
const hasKw = (state: DuelState, perm: Permanent, k: Keyword) => keywordsOf(state, perm).includes(k);
export function statsOfPermanent(state: DuelState, perm: Permanent): { power: number; toughness: number } {
if (perm.card.type === 'Location') {
return { power: Math.max(0, perm.tempPower), toughness: tg(perm.card) > 0 ? tg(perm.card) + perm.tempToughness : 0 };
}
if (perm.card.type !== 'Figure') return { power: 0, toughness: 0 };
const s = statics(state, perm);
const c = connectionOf(state, perm)?.bonus ?? 0;
const kws = new Set([...perm.card.keywords, ...perm.permKeywords, ...perm.tempKeywords, ...s.grants]);
let local = 0;
if (kws.has('Local') && perm.card.connect?.countries.length) {
const mine = perm.card.connect.countries;
if (state.battlefield.some(l => l.controller === perm.controller && l.card.type === 'Location' && l.card.connect?.countries.some(x => mine.includes(x)))) local = 1;
}
const trend = kws.has('Trending') ? state.rules.trendingPower : 0;
const extra = perm.counters + c + local;
return {
power: Math.max(0, pw(perm.card) + extra + perm.permPower + perm.tempPower + s.p + trend),
toughness: tg(perm.card) + extra + perm.permToughness + perm.tempToughness + s.t,
};
}
export function statsOf(state: DuelState, uid: string): { power: number; toughness: number } {
const perm = find(state, uid);
if (!perm) throw new RangeError(`No permanent ${uid}`);
return statsOfPermanent(state, perm);
}
/** Figures, and Locations with a Garrison, can block and can be destroyed by damage. */
const isBody = (perm: Permanent) => perm.card.type === 'Figure' || (perm.card.type === 'Location' && tg(perm.card) > 0);
function canBlock(state: DuelState, blocker: Permanent, attacker: Permanent): boolean {
const ka = keywordsOf(state, attacker);
const kb = blocker.card.type === 'Figure' ? keywordsOf(state, blocker) : [];
if (ka.includes('Featured') && !kb.some(k => k === 'Featured' || k === 'Translated' || k === 'Multilingual')) return false;
if (ka.includes('Multilingual') && kb.includes('Local')) return false;
return true;
}
function candidates(state: DuelState, seat: Seat, card: DuelCard, e: EffectSpec): TargetRef[] {
const t = targetOf(e);
const out: TargetRef[] = [];
const shielded = (p: Permanent) => p.controller !== seat && (card.type === 'Event' || card.type === 'Doctrine') && hasKw(state, p, 'Protected');
for (const p of state.battlefield) {
const fig = p.card.type === 'Figure';
const ok = t === 'figure' ? fig
: t === 'figureOrLocation' || t === 'anyTarget' ? fig || (p.card.type === 'Location' && tg(p.card) > 0)
: t === 'ownFigure' ? fig && p.controller === seat
: t === 'opponentFigure' ? fig && p.controller !== seat : false;
if (!ok || shielded(p)) continue;
if (e.condition?.maxPower !== undefined && pw(p.card) > e.condition.maxPower) continue;
out.push({ kind: 'card', uid: p.uid });
}
if (t === 'anyTarget') out.push({ kind: 'player', seat: other(seat) }, { kind: 'player', seat });
return out;
}
/** All target combinations for playing `card` now (capped), [] if it can't be played, [[]] if it needs none. */
function targetCombos(state: DuelState, seat: Seat, card: DuelCard): TargetRef[][] {
const chosen = card.effects.filter(e => isEntry(e) && CHOSEN.has(targetOf(e)));
if (!chosen.length) return [[]];
const lists = chosen.map(e => candidates(state, seat, card, e));
const permanent = card.type !== 'Event' && card.type !== 'Doctrine';
if (lists.some(l => !l.length)) return permanent ? [[]] : [];
let combos: TargetRef[][] = [[]];
for (const list of lists) {
const next: TargetRef[][] = [];
for (const c of combos) for (const t of list) {
if (t.kind === 'card' && c.some(x => x.kind === 'card' && x.uid === t.uid)) continue;
if (next.length < MAX_COMBOS) next.push([...c, t]);
}
combos = next;
}
return combos.length ? combos : permanent ? [[]] : [];
}
export function legalActions(state: DuelState, seat: Seat): DuelAction[] {
if (toAct(state) !== seat) return [];
const p = state.players[seat];
switch (state.phase) {
case 'mulligan':
return p.mulligansTaken < state.rules.mulligans ? [{ type: 'keep' }, { type: 'mulligan' }] : [{ type: 'keep' }];
case 'main': {
const out: DuelAction[] = [];
for (const inst of p.hand) {
if (inst.card.cost > p.citations) continue;
if (inst.card.type === 'Location' && p.locationsThisTurn >= state.rules.locationsPerTurn) continue;
for (const targets of targetCombos(state, seat, inst.card)) {
out.push(targets.length ? { type: 'play', uid: inst.uid, targets } : { type: 'play', uid: inst.uid });
}
}
out.push({ type: 'endPhase' });
return out;
}
case 'attack': {
const out: DuelAction[] = [];
for (const perm of state.battlefield) {
if (perm.controller !== seat || perm.card.type !== 'Figure' || perm.tapped || state.combat.attackers.includes(perm.uid)) continue;
if (perm.sick && !hasKw(state, perm, 'Trending')) continue;
out.push({ type: 'toggleAttacker', uid: perm.uid });
}
out.push({ type: 'confirmAttack' });
return out;
}
case 'block':
return [...blockOptions(state), { type: 'confirmBlocks' }];
default:
return [];
}
}
function blockOptions(state: DuelState): DuelAction[] {
const defender = other(state.active);
const out: DuelAction[] = [];
const blocked = new Set(Object.values(state.combat.blocks));
for (const b of state.battlefield) {
if (b.controller !== defender || b.tapped || !isBody(b) || state.combat.blocks[b.uid]) continue;
for (const aUid of state.combat.attackers) {
if (blocked.has(aUid)) continue;
const a = find(state, aUid);
if (a && canBlock(state, b, a)) out.push({ type: 'block', blocker: b.uid, attacker: aUid });
}
}
return out;
}
// ---------------------------------------------------------------------------
// Mutation helpers (operate on a private clone)
// ---------------------------------------------------------------------------
function log(state: DuelState, body: LogBody): void {
state.log.push({ seq: state.log.length, turn: state.turn, ...body } as LogEvent);
}
function ref(state: DuelState, inst: Source): CardRef {
const out: CardRef = { uid: inst.uid, cardId: inst.card.cardId, title: inst.card.title, pageId: inst.card.pageId, type: inst.card.type };
const perm = find(state, inst.uid);
if (perm && isBody(perm)) Object.assign(out, statsOfPermanent(state, perm));
return out;
}
const life = (state: DuelState): [number, number] => [state.players[0].life, state.players[1].life];
function finish(state: DuelState, winner: Seat | null, reason: EndReason): void {
if (state.result) return;
state.result = { winner, reason };
state.phase = 'over';
log(state, { kind: 'gameOver', winner, reason, life: life(state) });
}
function draw(state: DuelState, seat: Seat, n: number): void {
const p = state.players[seat];
for (let i = 0; i < n && !state.result; i++) {
if (!p.library.length) {
const mode = state.rules.libraryEmpty;
if (mode === 'reshuffle' && p.graveyard.length) {
const rng = new Rng(state.rng);
p.library = rng.shuffle(p.graveyard);
p.graveyard = [];
state.rng = rng.state;
} else if (mode === 'fatigue') {
p.fatigue += 1;
log(state, { kind: 'fatigue', seat, amount: p.fatigue });
loseLife(state, seat, p.fatigue, null, false);
continue;
} else {
finish(state, other(seat), 'library');
return;
}
}
const inst = p.library.shift()!;
p.hand.push(inst);
log(state, { kind: 'draw', seat, card: ref(state, inst) });
}
}
function loseLife(state: DuelState, seat: Seat, amount: number, source: Source | null, combat: boolean): void {
if (amount <= 0) return;
const p = state.players[seat];
p.life -= amount;
log(state, { kind: 'playerDamage', source: source ? ref(state, source) : null, seat, amount, life: p.life, combat });
}
function gainLife(state: DuelState, seat: Seat, amount: number, source: Source): void {
if (amount <= 0) return;
const p = state.players[seat];
p.life += amount;
log(state, { kind: 'lifeGain', source: ref(state, source), seat, amount, life: p.life });
}
/** Returns damage actually dealt. */
function dealDamage(state: DuelState, source: Source, target: TargetRef, amount: number, combat: boolean): number {
if (amount <= 0) return 0;
if (target.kind === 'player') { loseLife(state, target.seat, amount, source, combat); return amount; }
const perm = find(state, target.uid);
if (!perm || !isBody(perm)) return 0;
let dmg = amount;
if (!perm.shielded && hasKw(state, perm, 'Well-sourced')) { perm.shielded = true; dmg -= 1; }
if (dmg <= 0) return 0;
perm.damage += dmg;
log(state, { kind: combat ? 'combatDamage' : 'effectDamage', source: ref(state, source), target: ref(state, perm), amount: dmg });
return dmg;
}
function leave(state: DuelState, uid: string): Permanent | null {
const i = state.battlefield.findIndex(p => p.uid === uid);
if (i < 0) return null;
const [perm] = state.battlefield.splice(i, 1);
delete state.combat.blocks[uid];
for (const [b, a] of Object.entries(state.combat.blocks)) if (a === uid) delete state.combat.blocks[b];
state.combat.attackers = state.combat.attackers.filter(a => a !== uid);
return perm;
}
const toInstance = (perm: Permanent): CardInstance => ({ uid: perm.uid, owner: perm.owner, card: perm.card });
/** Destroy a set of permanents at once, then fire their triggers (active player's first, then entry order). */
function destroyAll(state: DuelState, uids: string[]): void {
const gone: Permanent[] = [];
for (const uid of uids) {
const perm = find(state, uid);
if (!perm) continue;
const card = ref(state, perm);
leave(state, uid);
if (!perm.card.token) state.players[perm.owner].graveyard.push(toInstance(perm));
log(state, { kind: 'destroyed', card, owner: perm.owner });
gone.push(perm);
}
gone.sort((a, b) => Number(a.controller !== state.active) - Number(b.controller !== state.active) || a.entered - b.entered);
for (const perm of gone) {
if (state.result) return;
for (const e of perm.card.effects) if (e.trigger === 'onDestroyed') resolveEffect(state, perm, perm.controller, e, undefined, undefined);
if (perm.card.keywords.includes('Legacy') || perm.permKeywords.includes('Legacy')) draw(state, perm.controller, 1);
}
}
function bounce(state: DuelState, uid: string): void {
const perm = find(state, uid);
if (!perm) return;
const card = ref(state, perm);
leave(state, uid);
if (!perm.card.token) state.players[perm.owner].hand.push(toInstance(perm));
log(state, { kind: 'bounced', card, owner: perm.owner });
}
/** State-based checks to a fixpoint: lethal damage, orphaned Doctrines, life totals; then Connected logging. */
function settle(state: DuelState): void {
for (let guard = 0; guard < 100 && !state.result; guard++) {
const dead = state.battlefield.filter(p => {
if (p.card.type === 'Doctrine') return !p.attachedTo || !find(state, p.attachedTo);
if (!isBody(p)) return false;
const { toughness } = statsOfPermanent(state, p);
return toughness <= 0 || p.damage >= toughness;
}).map(p => p.uid);
if (!dead.length) break;
destroyAll(state, dead);
}
if (state.result) return;
const lost = ([0, 1] as Seat[]).filter(s => state.players[s].life <= 0);
if (lost.length === 2) finish(state, null, 'life');
else if (lost.length === 1) finish(state, other(lost[0]), 'life');
if (!state.result) logConnections(state);
}
function logConnections(state: DuelState): void {
for (const perm of state.battlefield) {
if (perm.connectedLogged) continue;
const c = connectionOf(state, perm);
if (!c) continue;
perm.connectedLogged = true;
log(state, { kind: 'connected', seat: perm.controller, card: ref(state, perm), with: ref(state, c.with), tier: c.tier, via: c.via, bonus: c.bonus });
}
}
/** Rivalry (log only): when a card enters, note its best Connected tie to an opposing permanent, once. */
function logRivalry(state: DuelState, perm: Permanent): void {
let best: { with: Permanent; tier: ConnectedTier; via: string } | null = null;
for (const o of state.battlefield) {
if (o.controller === perm.controller) continue;
const t = tierBetween(state.rules, perm.card, o.card);
if (t && (!best || state.rules.connectedTiers.indexOf(t.tier) < state.rules.connectedTiers.indexOf(best.tier))) best = { with: o, ...t };
}
if (best) log(state, { kind: 'rivalry', card: ref(state, perm), with: ref(state, best.with), tier: best.tier, via: best.via });
}
function autoTargets(state: DuelState, source: Source, seat: Seat, t: TargetSpec): TargetRef[] {
const cards = (pred: (p: Permanent) => boolean) => state.battlefield.filter(pred).map(p => ({ kind: 'card', uid: p.uid }) as TargetRef);
switch (t) {
case 'controller': return [{ kind: 'player', seat }];
case 'opponent': return [{ kind: 'player', seat: other(seat) }];
case 'self': return find(state, source.uid) ? [{ kind: 'card', uid: source.uid }] : [];
case 'attached': {
const at = find(state, source.uid)?.attachedTo;
return at && find(state, at) ? [{ kind: 'card', uid: at }] : [];
}
case 'each': return cards(p => p.card.type === 'Figure' || p.card.type === 'Location');
case 'ownFigures': return cards(p => p.card.type === 'Figure' && p.controller === seat);
case 'opponentFigures': return cards(p => p.card.type === 'Figure' && p.controller !== seat);
default: return [];
}
}
function makeToken(state: DuelState, seat: Seat, source: Source, e: EffectSpec): void {
const spec = e.token;
if (!spec) return;
const n = e.amount ?? 1;
for (let i = 0; i < n; i++) {
if (e.condition?.maxTokens !== undefined) {
const count = state.battlefield.filter(p => p.controller === seat && p.card.token).length;
if (count >= e.condition.maxTokens) return;
}
const id = state.nextToken++;
const card: DuelCard = {
cardId: `token:${id}`, pageId: null, title: spec.name, type: 'Figure', portal: spec.portal, cost: 0,
power: spec.power, toughness: spec.toughness, keywords: [], effects: [], rulesText: '', flavor: '', token: true,
};
const perm = enter(state, { uid: `t:${id}`, owner: seat, card }, seat);
log(state, { kind: 'tokenCreated', seat, card: ref(state, perm), source: ref(state, source) });
}
}
/** Resolve one effect. `chosen` is this effect's target (chosen effects); `prev` the previous chosen target (fight). */
function resolveEffect(state: DuelState, source: Source, seat: Seat, e: EffectSpec, chosen: TargetRef | undefined, prev: TargetRef | undefined): void {
if (state.result || e.trigger === 'static') return;
const t = targetOf(e);
let targets: TargetRef[];
if (CHOSEN.has(t)) {
if (!chosen) return; // fizzles (no target, or chosen targets in automatic triggers)
const still = chosen.kind === 'player' ? t === 'anyTarget'
: candidates(state, seat, source.card, e).some(x => x.kind === 'card' && x.uid === chosen.uid);
if (!still) return;
targets = [chosen];
} else {
targets = autoTargets(state, source, seat, t);
}
const needs = !['draw', 'gainLife', 'createToken'].includes(e.op);
if (needs && !targets.length) return;
log(state, { kind: 'effect', seat, source: ref(state, source), op: e.op, ...(e.template ? { template: e.template } : {}), targets });
const amount = e.amount ?? 0;
const permOf = (x: TargetRef) => (x.kind === 'card' ? find(state, x.uid) : undefined);
switch (e.op) {
case 'damage': case 'damageEach':
for (const x of targets) dealDamage(state, source, x, amount, false);
break;
case 'createToken': makeToken(state, seat, source, e); break;
case 'pump': {
const p = e.power ?? amount, q = e.toughness ?? amount;
for (const x of targets) {
const perm = permOf(x);
if (!perm) continue;
if (e.duration === 'permanent') { perm.permPower += p; perm.permToughness += q; if (e.grant) perm.permKeywords.push(...e.grant); }
else { perm.tempPower += p; perm.tempToughness += q; if (e.grant) perm.tempKeywords.push(...e.grant); }
}
break;
}
case 'fight': {
const a = prev?.kind === 'card' ? find(state, prev.uid) : find(state, source.uid);
const b = permOf(targets[0]);
if (!a || !b || a.uid === b.uid) break;
const [ap, bp] = [statsOfPermanent(state, a).power, statsOfPermanent(state, b).power];
dealDamage(state, a, { kind: 'card', uid: b.uid }, ap, false);
dealDamage(state, b, { kind: 'card', uid: a.uid }, bp, false);
break;
}
case 'draw': draw(state, t === 'opponent' ? other(seat) : seat, e.amount ?? 1); break;
case 'attach': {
const perm = find(state, source.uid);
const to = permOf(targets[0]);
if (perm && to) { perm.attachedTo = to.uid; log(state, { kind: 'adopt', seat, card: ref(state, perm), to: ref(state, to) }); }
break;
}
case 'gainLife': gainLife(state, seat, amount, source); break;
case 'drain':
for (const x of targets) {
const victim = x.kind === 'player' ? x.seat : permOf(x)?.controller;
if (victim === undefined) continue;
loseLife(state, victim, amount, source, false);
if (victim !== seat) gainLife(state, seat, amount, source);
}
break;
case 'destroy': destroyAll(state, targets.filter(x => x.kind === 'card').map(x => (x as { uid: string }).uid)); break;
case 'bounce': for (const x of targets) if (x.kind === 'card') bounce(state, x.uid); break;
case 'tap': for (const x of targets) { const perm = permOf(x); if (perm) perm.tapped = true; } break;
}
}
function enter(state: DuelState, inst: CardInstance, seat: Seat): Permanent {
const perm: Permanent = {
...inst, controller: seat, tapped: false, sick: true, damage: 0, counters: 0, permPower: 0, permToughness: 0,
tempPower: 0, tempToughness: 0, tempKeywords: [], permKeywords: [], shielded: false, entered: state.nextEntry++, connectedLogged: false,
};
state.battlefield.push(perm);
return perm;
}
/** Run a card's entry effects, feeding chosen targets in order. */
function runEntry(state: DuelState, source: Source, seat: Seat, targets: TargetRef[]): void {
let i = 0;
let prev: TargetRef | undefined;
for (const e of source.card.effects) {
if (!isEntry(e) || state.result) continue;
if (CHOSEN.has(targetOf(e))) {
const chosen = targets[i++];
resolveEffect(state, source, seat, e, chosen, prev);
prev = chosen;
} else {
resolveEffect(state, source, seat, e, undefined, prev);
}
}
}
// ---------------------------------------------------------------------------
// Turn structure
// ---------------------------------------------------------------------------
function totalPower(state: DuelState, seat: Seat): number {
return state.battlefield.filter(p => p.controller === seat && p.card.type === 'Figure').reduce((n, p) => n + statsOfPermanent(state, p).power, 0);
}
function startTurn(state: DuelState, seat: Seat): void {
if (state.rules.turnCap !== null && state.turn >= state.rules.turnCap) {
const [a, b] = life(state);
const [pa, pb] = [totalPower(state, 0), totalPower(state, 1)];
const winner: Seat = a !== b ? (a > b ? 0 : 1) : pa !== pb ? (pa > pb ? 0 : 1) : other(state.firstSeat);
finish(state, winner, 'turnCap');
return;
}
state.turn += 1;
state.active = seat;
state.phase = 'main';
state.combat = { attackers: [], blocks: {} };
const p = state.players[seat];
p.locationsThisTurn = 0;
p.baseCitations = Math.min(state.rules.maxCitations, p.baseCitations + state.rules.citationsPerTurn);
p.citations = maxCitations(state, seat);
if (state.turn === 2 && seat !== state.firstSeat) p.citations += state.rules.secondPlayerCitations;
for (const perm of state.battlefield) {
perm.shielded = false;
if (perm.controller === seat) { perm.tapped = false; perm.sick = false; }
}
log(state, { kind: 'turnStart', seat, citations: p.citations, life: life(state) });
const mine = state.battlefield.filter(x => x.controller === seat).sort((a, b) => a.entered - b.entered);
for (const perm of mine) {
if (!find(state, perm.uid) || state.result) continue;
if (perm.card.keywords.includes('Stub') && perm.counters < state.rules.stubMax) perm.counters += 1;
for (const e of perm.card.effects) if (e.trigger === 'startOfTurn' && find(state, perm.uid)) resolveEffect(state, perm, seat, e, undefined, undefined);
}
settle(state);
if (state.result) return;
if (!(state.turn === 1 && !state.rules.firstPlayerDraws)) draw(state, seat, 1);
settle(state);
}
function endTurn(state: DuelState): void {
for (const perm of state.battlefield) { perm.damage = 0; perm.tempPower = 0; perm.tempToughness = 0; perm.tempKeywords = []; }
state.combat = { attackers: [], blocks: {} };
const seat = state.active;
const p = state.players[seat];
while (p.hand.length > state.rules.handSize) {
let worst = p.hand.length - 1;
for (let i = p.hand.length - 1; i >= 0; i--) if (p.hand[i].card.cost > p.hand[worst].card.cost) worst = i;
const [inst] = p.hand.splice(worst, 1);
p.graveyard.push(inst);
log(state, { kind: 'discard', seat, card: ref(state, inst) });
}
settle(state);
if (!state.result) startTurn(state, other(seat));
}
function play(state: DuelState, seat: Seat, uid: string, targets: TargetRef[]): void {
const p = state.players[seat];
const [inst] = p.hand.splice(p.hand.findIndex(c => c.uid === uid), 1);
p.citations -= inst.card.cost;
if (inst.card.type === 'Event') {
log(state, { kind: 'cast', seat, card: ref(state, inst), targets });
runEntry(state, inst, seat, targets);
p.graveyard.push(inst);
settle(state);
return;
}
if (inst.card.type === 'Location') p.locationsThisTurn += 1;
const perm = enter(state, inst, seat);
if (inst.card.type !== 'Doctrine') log(state, { kind: 'deploy', seat, card: ref(state, perm) });
logRivalry(state, perm);
runEntry(state, perm, seat, targets);
settle(state);
}
function confirmAttack(state: DuelState): void {
const seat = state.active;
if (!state.combat.attackers.length) { endTurn(state); return; }
const attackers = state.combat.attackers.map(uid => find(state, uid)).filter((x): x is Permanent => !!x);
for (const a of attackers) a.tapped = true;
log(state, { kind: 'attack', seat, attackers: attackers.map(a => ref(state, a)) });
for (const a of attackers) for (const e of a.card.effects) if (e.trigger === 'onAttack' && find(state, a.uid)) resolveEffect(state, a, seat, e, undefined, undefined);
settle(state);
if (state.result) return;
if (!state.combat.attackers.length) { endTurn(state); return; }
state.phase = 'block';
if (!blockOptions(state).length) resolveCombat(state);
}
function resolveCombat(state: DuelState): void {
const defender = other(state.active);
const pairs = Object.entries(state.combat.blocks)
.map(([b, a]) => ({ blocker: find(state, b), attacker: find(state, a) }))
.filter((x): x is { blocker: Permanent; attacker: Permanent } => !!x.blocker && !!x.attacker);
if (pairs.length) log(state, { kind: 'block', seat: defender, blocks: pairs.map(x => ({ blocker: ref(state, x.blocker), attacker: ref(state, x.attacker) })) });
// Simultaneous: compute every amount first, then apply.
const hits: { source: Permanent; target: TargetRef; amount: number }[] = [];
const blockerOf = new Map(pairs.map(x => [x.attacker.uid, x.blocker]));
for (const uid of state.combat.attackers) {
const a = find(state, uid);
if (!a) continue;
const ap = statsOfPermanent(state, a).power;
const b = blockerOf.get(uid);
if (b) {
hits.push({ source: a, target: { kind: 'card', uid: b.uid }, amount: ap });
hits.push({ source: b, target: { kind: 'card', uid: a.uid }, amount: statsOfPermanent(state, b).power });
} else {
hits.push({ source: a, target: { kind: 'player', seat: defender }, amount: ap });
}
}
const toPlayer: Permanent[] = [];
for (const h of hits) if (dealDamage(state, h.source, h.target, h.amount, true) > 0 && h.target.kind === 'player') toPlayer.push(h.source);
for (const a of toPlayer) {
for (const e of a.card.effects) if (e.trigger === 'onCombatDamageToPlayer') resolveEffect(state, a, a.controller, e, undefined, undefined);
for (const d of state.battlefield.filter(x => x.card.type === 'Doctrine' && x.attachedTo === a.uid)) {
for (const e of d.card.effects) if (e.trigger === 'onCombatDamageToPlayer') resolveEffect(state, d, d.controller, e, undefined, undefined);
}
}
settle(state);
if (!state.result) endTurn(state);
}
// ---------------------------------------------------------------------------
// applyAction
// ---------------------------------------------------------------------------
/**
* Structural clone for clone-on-apply. DuelCards, CardInstances, log events and the
* ruleset are never mutated after creation, so they are shared between states;
* Permanents, players and combat are copied. (structuredClone was 85% of runtime.)
*/
function cloneState(s: DuelState): DuelState {
const player = (p: PlayerState): PlayerState => ({ ...p, library: p.library.slice(), hand: p.hand.slice(), graveyard: p.graveyard.slice() });
return {
...s,
players: [player(s.players[0]), player(s.players[1])],
battlefield: s.battlefield.map(p => ({ ...p, tempKeywords: p.tempKeywords.slice(), permKeywords: p.permKeywords.slice() })),
combat: { attackers: s.combat.attackers.slice(), blocks: { ...s.combat.blocks } },
log: s.log.slice(),
result: s.result ? { ...s.result } : null,
};
}
function normalize(action: DuelAction): DuelAction {
// Canonical key order so deep-equal against legalActions is stable.
switch (action.type) {
case 'play': {
const targets = (action.targets ?? []).map(t => (t.kind === 'card' ? { kind: 'card' as const, uid: t.uid } : { kind: 'player' as const, seat: t.seat }));
return targets.length ? { type: 'play', uid: action.uid, targets } : { type: 'play', uid: action.uid };
}
case 'toggleAttacker': return { type: 'toggleAttacker', uid: action.uid };
case 'block': return { type: 'block', blocker: action.blocker, attacker: action.attacker };
default: return { type: action.type } as DuelAction;
}
}
export function applyAction(input: DuelState, seat: Seat, action: DuelAction): DuelState {
if (input.result) throw new Error('Match is over');
if (seat !== 0 && seat !== 1) throw new RangeError('Bad seat');
if (!action || typeof action !== 'object' || typeof (action as { type?: unknown }).type !== 'string') throw new TypeError('Bad action');
if (action.type === 'concede') {
const state = cloneState(input);
state.version += 1;
log(state, { kind: 'concede', seat });
finish(state, other(seat), 'concede');
return state;
}
if (toAct(input) !== seat) throw new Error('Not your turn to act');
if (action.type === 'timeout') {
const state = cloneState(input);
state.version += 1;
log(state, { kind: 'timeout', seat });
const p = state.players[seat];
p.timeouts += 1;
if (p.timeouts >= state.rules.timeoutsToConcede) { finish(state, other(seat), 'timeout'); return state; }
switch (state.phase) {
case 'mulligan': keep(state, seat); break;
case 'main': case 'attack': state.combat.attackers = []; endTurn(state); break;
case 'block': resolveCombat(state); break;
}
return state;
}
const a = normalize(action);
const key = JSON.stringify(a);
if (!legalActions(input, seat).some(x => JSON.stringify(x) === key)) throw new Error(`Illegal action ${JSON.stringify(action)}`);
const state = cloneState(input);
state.version += 1;
state.players[seat].timeouts = 0;
switch (a.type) {
case 'keep': keep(state, seat); break;
case 'mulligan': mulligan(state, seat); break;
case 'play': play(state, seat, a.uid, a.targets ?? []); break;
case 'endPhase': state.phase = 'attack'; break;
case 'toggleAttacker': state.combat.attackers.push(a.uid); break;
case 'confirmAttack': confirmAttack(state); break;
case 'block': state.combat.blocks[a.blocker] = a.attacker; break;
case 'confirmBlocks': resolveCombat(state); break;
}
return state;
}
function keep(state: DuelState, seat: Seat): void {
const p = state.players[seat];
for (let i = 0; i < p.mulligansTaken && p.hand.length; i++) p.library.push(p.hand.pop()!);
p.ready = true;
log(state, { kind: 'keep', seat, handSize: p.hand.length });
if (state.players[0].ready && state.players[1].ready) startTurn(state, state.firstSeat);
}
function mulligan(state: DuelState, seat: Seat): void {
const p = state.players[seat];
const rng = new Rng(state.rng);
p.library = rng.shuffle([...p.library, ...p.hand]);
state.rng = rng.state;
p.hand = p.library.splice(0, state.rules.openingHand);
p.mulligansTaken += 1;
log(state, { kind: 'mulligan', seat });
}
// ---------------------------------------------------------------------------
// Views, log, replay, serialisation
// ---------------------------------------------------------------------------
function side(state: DuelState, seat: Seat): SideView {
const p = state.players[seat];
return {
seat, life: p.life, citations: p.citations, maxCitations: maxCitations(state, seat), handSize: p.hand.length,
librarySize: p.library.length, graveyard: structuredClone(p.graveyard), bankSeconds: p.bankSeconds, ready: p.ready, mulligansTaken: p.mulligansTaken,
};
}
export function viewFor(state: DuelState, seat: Seat): PlayerView {
const battlefield: PermanentView[] = state.battlefield.map(perm => {
const { power, toughness } = statsOfPermanent(state, perm);
const c = connectionOf(state, perm);
return {
uid: perm.uid, owner: perm.owner, controller: perm.controller, card: structuredClone(perm.card), tapped: perm.tapped, sick: perm.sick,
damage: perm.damage, counters: perm.counters, ...(perm.attachedTo ? { attachedTo: perm.attachedTo } : {}), power, toughness,
keywords: keywordsOf(state, perm), ...(c ? { connected: { with: c.with.uid, tier: c.tier, via: c.via, bonus: c.bonus } } : {}),
};
});
return {
engine: 'duel', seat, turn: state.turn, active: state.active, phase: state.phase, toAct: toAct(state),
you: { ...side(state, seat), hand: structuredClone(state.players[seat].hand) },
opponent: side(state, other(seat)),
battlefield, combat: structuredClone(state.combat), result: state.result, version: state.version, rules: structuredClone(state.rules),
};
}
export function logFor(state: DuelState, seat: Seat): LogEvent[] {
return state.log.map(e => {
if (e.kind === 'draw' && e.seat !== seat) return { seq: e.seq, turn: e.turn, kind: 'draw', seat: e.seat } as LogEvent;
return structuredClone(e);
});
}
export function replay(rules: DuelRuleset, decks: [DuelCard[], DuelCard[]], seed: string, actions: { seat: Seat; action: DuelAction }[]): DuelState {
let state = createDuel(rules, decks, seed);
for (const { seat, action } of actions) state = applyAction(state, seat, action);
return state;
}
export const serialize = (state: DuelState): string => JSON.stringify(state);
export function deserialize(text: string): DuelState {
const value = JSON.parse(text) as DuelState;
if (!value || value.engine !== 'duel') throw new TypeError('Not a duel state');
return value;
}
// ---------------------------------------------------------------------------
// Host binding: structurally an EnginePort<DuelRuleset, DuelCard, DuelState, DuelAction>
// (server/game/engine-port.ts). shared/ must not import server/, so host-pvp types it there.
// ---------------------------------------------------------------------------
export const duelEnginePort = {
defaultRuleset: DEFAULT_DUEL_RULESET,
parseRuleset: (value: unknown) => parseDuelRuleset(value),
deckSize: (rules: DuelRuleset) => rules.deckSize,
starterDeck(_rules?: DuelRuleset, _seed?: string): DuelCard[] { throw new Error('Duel has no starter decks: every card must be owned (BRIEF decision 2)'); },
previewPool(_rules?: DuelRuleset, _seed?: string, _size?: number): DuelCard[] { throw new Error('Duel draft pools come from both players’ collections, not previews'); },
validateDeck,
cardPower: (card: DuelCard) => pw(card),
withPower: (card: DuelCard, power: number): DuelCard => ({ ...card, power: Math.max(0, Math.round(power)) }),
cardPageId: (card: DuelCard) => card.pageId,
cardSummary: (card: DuelCard) => ({
cardId: card.cardId, pageId: card.pageId, title: card.title, type: card.type, portal: card.portal, cost: card.cost,
power: card.power, toughness: card.toughness, keywords: card.keywords, rulesText: card.rulesText,
}),
createMatch: (rules: DuelRuleset, decks: [DuelCard[], DuelCard[]], seed: string) => createDuel(rules, decks, seed),
legalActions,
applyAction,
concedeAction: (_seat: Seat): DuelAction => ({ type: 'concede' }),
timeoutAction: (_seat: Seat): DuelAction => ({ type: 'timeout' }),
turnSeconds: (rules: DuelRuleset) => rules.turnSeconds,
turnOf,
/** Trending mode (DUEL_DESIGN §4): a frozen live ▲ ≥ +25% grants Trending (haste, +trendingPower/+0 in play). */
applyLive(_rules: DuelRuleset, card: DuelCard, live: { trendPercent: number | null }): DuelCard {
if (live.trendPercent === null || live.trendPercent < 25 || card.type !== 'Figure' || card.keywords.includes('Trending')) return card;
return { ...card, keywords: [...card.keywords, 'Trending'] };
},
isOver,
result: (state: DuelState) => state.result ?? { winner: null, reason: 'in progress' },
viewFor,
logFor,
};
-40
View File
@@ -1,40 +0,0 @@
/**
* WikiDeck Duel public API. Spec: docs/game/DUEL_API.md.
* Contracts PR: types and ruleset only. engine.ts (createDuel, legalActions,
* applyAction, viewFor, logFor, replay, ...) lands in the engine alpha PR and
* implements DuelEngineApi below; build against that interface until then.
*/
export * from './types.ts';
export * from './ruleset.ts';
export * from './engine.ts';
import type { DuelAction, DuelCard, DuelResult, DuelRuleset, DuelState, LogEvent, PlayerView, Seat } from './types.ts';
/** The engine's function surface (implemented by shared/duel/engine.ts). All pure; inputs are never mutated. */
export interface DuelEngineApi {
/** Throws if the deck is illegal: size ≠ deckSize, duplicate cardId, > maxCopiesPerPage of one pageId, bad stats, > maxKeywords. */
validateDeck(rules: DuelRuleset, cards: DuelCard[]): void;
/** Shuffle both decks from `seed`, deal opening hands, phase 'mulligan'. */
createDuel(rules: DuelRuleset, decks: [DuelCard[], DuelCard[]], seed: string): DuelState;
/** Atomic legal actions for `seat`; [] if `seat` is not the one to act (concede is always accepted by applyAction but not listed). */
legalActions(state: DuelState, seat: Seat): DuelAction[];
/** Returns a new state. Throws on an illegal action. */
applyAction(state: DuelState, seat: Seat, action: DuelAction): DuelState;
/** Seat that must act now, or null when over. Mulligan: the lowest seat not yet ready. */
toAct(state: DuelState): Seat | null;
isOver(state: DuelState): boolean;
result(state: DuelState): DuelResult | null;
viewFor(state: DuelState, seat: Seat): PlayerView;
/** Per-seat log: the opponent's draws (and other hidden-zone cards) are masked. */
logFor(state: DuelState, seat: Seat): LogEvent[];
/** Deterministic: same inputs → identical state (and log). */
replay(rules: DuelRuleset, decks: [DuelCard[], DuelCard[]], seed: string, actions: { seat: Seat; action: DuelAction }[]): DuelState;
serialize(state: DuelState): string;
deserialize(text: string): DuelState;
/** Current power/toughness of a permanent after counters, buffs, statics and Connected. */
statsOf(state: DuelState, uid: string): { power: number; toughness: number };
}
import * as engine from './engine.ts';
/** Compile-time check that engine.ts implements the contract. */
export const duelEngine: DuelEngineApi = engine;
-78
View File
@@ -1,78 +0,0 @@
/**
* Duel ruleset: every rule number as data (BRIEF "Game rules core").
* parseDuelRuleset() is the only way untrusted JSON (mode files, peer hosts)
* becomes a DuelRuleset. Night 1 Edit War rulesets are rejected cleanly.
*/
import { z } from 'zod';
import type { DuelRuleset } from './types.ts';
const int = (min: number, max: number) => z.number().int().min(min).max(max);
export const DuelRulesetSchema = z.object({
engine: z.literal('duel'),
version: z.string().min(1).max(16),
deckSize: int(1, 60),
maxCopiesPerPage: int(1, 60),
startingLife: int(1, 100),
openingHand: int(0, 10),
handSize: int(1, 15),
mulligans: int(0, 3),
firstPlayerDraws: z.boolean(),
secondPlayerCitations: int(0, 5),
maxCitations: int(1, 20),
citationsPerTurn: int(1, 5),
territoryCap: int(0, 10),
locationsPerTurn: int(0, 10),
libraryEmpty: z.enum(['lose', 'reshuffle', 'fatigue']),
maxKeywords: int(0, 4),
stubMax: int(0, 10),
trendingPower: int(0, 5),
connectedTiers: z.array(z.enum(['Linked', 'Compatriot', 'Contemporary', 'Kin'])).max(4)
.refine(v => new Set(v).size === v.length, 'Duplicate tier'),
connectedBonus: int(0, 5),
linkedBonus: int(0, 5),
turnSeconds: int(5, 600),
bankSeconds: int(0, 3600),
timeoutsToConcede: int(1, 20),
turnCap: int(2, 200).nullable(),
}).strict();
export const DEFAULT_DUEL_RULESET: DuelRuleset = Object.freeze({
engine: 'duel',
version: 'duel-1',
deckSize: 40,
maxCopiesPerPage: 4,
startingLife: 20,
openingHand: 7,
handSize: 7,
mulligans: 1,
firstPlayerDraws: false,
secondPlayerCitations: 1, // balance v1 (#64): first player 60.1% → 54.7%
maxCitations: 10,
citationsPerTurn: 1,
territoryCap: 2,
locationsPerTurn: 1,
libraryEmpty: 'lose',
maxKeywords: 2,
stubMax: 2,
trendingPower: 1,
connectedTiers: ['Linked', 'Compatriot', 'Contemporary', 'Kin'],
connectedBonus: 1,
linkedBonus: 2,
turnSeconds: 60,
bankSeconds: 300,
timeoutsToConcede: 3,
turnCap: 40, // balance v1 (#64): 3.9% of games decided at the cap
}) as DuelRuleset;
/** Validate a full ruleset. Throws (ZodError) on anything invalid or unknown. */
export function parseDuelRuleset(value: unknown): DuelRuleset {
return DuelRulesetSchema.parse(value) as DuelRuleset;
}
/** Merge a partial override (a mode file's `rules`) onto the defaults, then validate. */
export function mergeDuelRuleset(overrides: unknown, base: DuelRuleset = DEFAULT_DUEL_RULESET): DuelRuleset {
if (overrides === undefined || overrides === null) return parseDuelRuleset(structuredClone(base));
if (typeof overrides !== 'object' || Array.isArray(overrides)) throw new TypeError('Ruleset overrides must be an object');
return parseDuelRuleset({ ...structuredClone(base), ...(overrides as Record<string, unknown>) });
}
-398
View File
@@ -1,398 +0,0 @@
/**
* WikiDeck Duel: the Magic-style 40-card engine (off-chain). Contract types.
* Spec: docs/game/DUEL_API.md (rules: docs/game/DUEL_DESIGN.md). Owner: duel-core (claude/wikideck#55).
*
* Everything here is plain JSON data: DuelState round-trips through
* JSON.stringify/parse with no loss (no Maps, classes or functions).
* shared/game/ (Edit War) is a separate engine and is not affected.
*/
import type { RngState } from '../game/rng.ts';
export type Seat = 0 | 1;
export const SEATS: readonly Seat[] = [0, 1];
// ---------------------------------------------------------------------------
// Cards (derived by duel-cards in shared/duel/derive.ts; the engine never re-derives)
// ---------------------------------------------------------------------------
export type CardType = 'Figure' | 'Location' | 'Event' | 'Institution' | 'Doctrine';
export const CARD_TYPES: readonly CardType[] = ['Figure', 'Location', 'Event', 'Institution', 'Doctrine'];
export type Portal = 'Arts' | 'Earth' | 'History' | 'Science' | 'Society' | 'Neutral';
export const PORTALS: readonly Portal[] = ['Arts', 'Earth', 'History', 'Science', 'Society', 'Neutral'];
/** Card keywords (DUEL_DESIGN §4), ≤ ruleset.maxKeywords per card. 'Trending' is granted by Trending mode only. */
export type Keyword = 'Protected' | 'Featured' | 'Legacy' | 'Multilingual' | 'Translated' | 'Well-sourced' | 'Stub' | 'Local' | 'Trending';
export const KEYWORDS: readonly Keyword[] = ['Protected', 'Featured', 'Legacy', 'Multilingual', 'Translated', 'Well-sourced', 'Stub', 'Local', 'Trending'];
/** Connected tiers (DUEL_DESIGN §6). The engine computes them from `connect`. */
export type ConnectedTier = 'Linked' | 'Compatriot' | 'Contemporary' | 'Kin';
export const CONNECTED_TIERS: readonly ConnectedTier[] = ['Linked', 'Compatriot', 'Contemporary', 'Kin'];
/** Frozen claims used for Connected (no I/O during a match). */
export interface ConnectKeys {
/** P17/P27 values (Q-ids or labels; compared as strings). */
countries: string[];
/** From P569/P570/P571/P580/P585, e.g. 19 for the 1800s. */
centuries: number[];
/** Outlink pageIds (cached), plus pageIds the card's claims cite. */
links: number[];
/** P31 Q-ids. Q5 (human) and Q16521 (taxon) never count for Kin. */
p31: string[];
/** The card's own Q-id, and the Q-ids its claims cite: Linked if either cites the other (DUEL_DESIGN §6). */
qid?: string;
cites?: string[];
}
/** When an effect runs. Sorcery speed only: there is no stack and no response window. */
export type EffectTrigger =
| 'onPlay' // an Event resolves (on a permanent: same as onEnter)
| 'onEnter' // this permanent enters the battlefield
| 'onAttack' // this Figure is declared as an attacker (resolves at confirmAttack)
| 'onDestroyed' // this permanent is destroyed (after all combat damage)
| 'startOfTurn' // its controller's refresh step, in entry order
| 'onCombatDamageToPlayer' // this Figure (or the Figure this Doctrine is attached to) deals combat damage to a player
| 'static'; // continuous while on the battlefield
/** Effect ops the interpreter knows. duel-cards builds the 12 templates (DUEL_DESIGN §5) from these. */
export type EffectOp =
| 'damage' // `amount` to each target (Figure, Location or player)
| 'damageEach' // `amount` to every Figure and Location (target narrows: 'ownFigures' / 'opponentFigures')
| 'createToken' // `amount` (default 1) copies of `token` under the controller; condition.maxTokens limits
| 'pump' // +power/+toughness (negative allowed); duration 'turn' | 'permanent'; as static: while the source is in play
| 'fight' // the previous chosen target (else this permanent) and this target deal damage equal to their power to each other
| 'draw' // controller draws `amount`
| 'attach' // Doctrine: attach to the target Figure (defines the Doctrine's target)
| 'gainLife' // controller gains `amount` Notability
| 'drain' // opponent loses `amount`, controller gains `amount`
| 'destroy' // destroy target permanent
| 'bounce' // return target permanent to its owner's hand
| 'tap'; // tap target permanent
export type TargetSpec =
// chosen (each consumes one entry of play.targets, in effect order)
| 'figure' // any Figure
| 'figureOrLocation' // any Figure or Location
| 'ownFigure' // a Figure you control
| 'opponentFigure' // a Figure you don't control
| 'anyTarget' // any Figure, Location or player
// automatic
| 'each' // every Figure and Location
| 'self' // this permanent
| 'attached' // the Figure this Doctrine is attached to
| 'ownFigures' // every Figure you control
| 'opponentFigures' // every Figure you don't control
| 'opponent' // the opposing player
| 'controller'; // you
export interface TokenSpec { name: string; power: number; toughness: number; portal: Portal }
export interface EffectSpec {
trigger: EffectTrigger;
op: EffectOp;
/** Default per op: damage → 'anyTarget'; pump → 'self'; draw/gainLife/createToken → 'controller'; drain → 'opponent'. */
target?: TargetSpec;
amount?: number;
/** pump: +power/+toughness (default: amount/amount). */
power?: number;
toughness?: number;
/** pump/grant outside static: 'turn' (default) or 'permanent'. */
duration?: 'turn' | 'permanent';
token?: TokenSpec;
condition?: {
/** Only affects Figures with printed power ≤ maxPower. */
maxPower?: number;
/** Only Figures of the source's Portal (for a Doctrine: the attached Figure's Portal), excluding that card itself. */
samePortal?: boolean;
/** Only Figures Connected to the source. */
connected?: boolean;
/** createToken: only if the controller controls fewer than this many tokens. */
maxTokens?: number;
};
/** Keywords granted to the affected Figures (static: while in play; otherwise for `duration`). */
grant?: Keyword[];
/** Template id (T1–T12) for rules text and log wording. */
template?: string;
}
/**
* A card as the engine sees it, frozen at match start. The host builds these
* from the chain via duel-cards' derive(); the engine trusts them.
*/
export interface DuelCard {
/** Unique within one deck: the NFT tokenId (or a sample id in sims; `token:` ids for tokens). */
cardId: string;
pageId: number | null;
revisionId?: number;
title: string;
/** Wikidata description ("American actor (1909–1994)"), for log first mentions. */
description?: string;
type: CardType;
/** e.g. 'conflict', 'election', 'organisation', 'pathology'. */
subtype?: string | null;
portal: Portal;
cost: number;
/** Figures: the body. Locations: power 0, toughness = Garrison (0 = can't block). Others: null or 0. */
power: number | null;
toughness: number | null;
keywords: Keyword[];
/** Locations: +N max Citations while controlled (all sources together ≤ territoryCap). */
territory?: number;
effects: EffectSpec[];
/** Deterministic rules text (shared/duel/text.ts). Display only. */
rulesText: string;
/** First sentence of article.extract (else description). Display only. */
flavor: string;
connect?: ConnectKeys;
rarity?: 'Common' | 'Rare' | 'Epic' | 'Legendary';
deriveVersion?: string;
/** Tokens are created in play and never in decks. */
token?: boolean;
}
// ---------------------------------------------------------------------------
// Ruleset (all numbers are data; parseDuelRuleset in ruleset.ts validates)
// ---------------------------------------------------------------------------
export type LibraryEmpty = 'lose' | 'reshuffle' | 'fatigue';
export interface DuelRuleset {
engine: 'duel';
version: string;
deckSize: number; // 40
maxCopiesPerPage: number; // 4
startingLife: number; // 20 Notability
openingHand: number; // 7
handSize: number; // 7: hand limit at end of turn (DUEL_DESIGN `handLimit`); excess discarded highest cost first
mulligans: number; // 1: shuffle, draw openingHand, bottom one per mulligan taken
firstPlayerDraws: boolean; // false
secondPlayerCitations: number; // 1 (balance v1): extra Citations on the second player's first turn (designer's "Second Opinion" lever)
maxCitations: number; // 10
citationsPerTurn: number; // 1
territoryCap: number; // 2
locationsPerTurn: number; // 1
libraryEmpty: LibraryEmpty; // 'lose'
maxKeywords: number; // 2
stubMax: number; // 2: max Stub counters
trendingPower: number; // 1: Trending gets +N/+0
connectedTiers: ConnectedTier[]; // all four; cut tiers by removing them
connectedBonus: number; // 1
linkedBonus: number; // 2
turnSeconds: number; // 60
bankSeconds: number; // 300
timeoutsToConcede: number; // 3 consecutive timeouts lose
/** 40 (balance v1); null = none. After this many turns in total, higher Notability wins, then higher power in play, then the second player. */
turnCap: number | null;
}
// ---------------------------------------------------------------------------
// State
// ---------------------------------------------------------------------------
/** 'mulligan' → 'main' → 'attack' → ('block' if attackers can be blocked) → next turn. Refresh, draw, damage and end are automatic. */
export type Phase = 'mulligan' | 'main' | 'attack' | 'block' | 'over';
/** One physical card in a match. `uid` is unique per match ("0:12" = seat 0, deck index 12; "t:3" = token); actions use uids. */
export interface CardInstance {
uid: string;
owner: Seat;
card: DuelCard;
}
export interface Permanent extends CardInstance {
controller: Seat;
tapped: boolean;
/** "Pending review": entered this turn, can't attack. */
sick: boolean;
damage: number;
/** Stub +1/+1 counters. */
counters: number;
/** Permanent pump modifiers. */
permPower: number;
permToughness: number;
/** Until-end-of-turn modifiers. */
tempPower: number;
tempToughness: number;
/** Keywords granted until end of turn / permanently by non-static effects. */
tempKeywords: Keyword[];
permKeywords: Keyword[];
/** Doctrines: uid of the Figure they're attached to. */
attachedTo?: string;
/** Well-sourced: prevention already used this turn. */
shielded: boolean;
/** Entry order (trigger order). */
entered: number;
/** Connected bonus already logged. */
connectedLogged: boolean;
}
export interface PlayerState {
life: number;
/** Citations available this turn. */
citations: number;
/** Base max (rises by citationsPerTurn to maxCitations). */
baseCitations: number;
library: CardInstance[]; // top = index 0. Hidden.
hand: CardInstance[]; // hidden from the opponent
graveyard: CardInstance[]; // "Deleted", public
mulligansTaken: number;
ready: boolean;
locationsThisTurn: number;
/** Remaining time bank in seconds (the host decrements it). */
bankSeconds: number;
/** Consecutive timeouts. */
timeouts: number;
/** libraryEmpty 'fatigue': damage of the last empty draw. */
fatigue: number;
}
export interface CombatState {
attackers: string[];
/** blocker uid → attacker uid. */
blocks: Record<string, string>;
}
export type EndReason = 'life' | 'library' | 'concede' | 'timeout' | 'turnCap';
export interface DuelResult {
winner: Seat | null; // null = draw
reason: EndReason;
}
export interface DuelState {
engine: 'duel';
rules: DuelRuleset;
seed: string;
rng: RngState;
/** 1-based, counts both players' turns. 0 during mulligans. */
turn: number;
active: Seat;
firstSeat: Seat;
phase: Phase;
players: [PlayerState, PlayerState];
battlefield: Permanent[];
combat: CombatState;
log: LogEvent[];
result: DuelResult | null;
/** Increments on every applied action (host polling: unchanged → 204). */
version: number;
/** Counters for token uids and entry order. */
nextToken: number;
nextEntry: number;
}
// ---------------------------------------------------------------------------
// Actions (atomic; legalActions enumerates them; [] for the seat not to act)
// ---------------------------------------------------------------------------
export type TargetRef = { kind: 'card'; uid: string } | { kind: 'player'; seat: Seat };
export type DuelAction =
| { type: 'keep' }
| { type: 'mulligan' }
/** targets: one per chosen-target effect, in effect order (Doctrine: its 'attach' target). Omitted when none. */
| { type: 'play'; uid: string; targets?: TargetRef[] }
| { type: 'endPhase' } // main → attack
| { type: 'toggleAttacker'; uid: string } // declare one more attacker (monotonic)
| { type: 'confirmAttack' } // no attackers → end turn
| { type: 'block'; blocker: string; attacker: string } // defender, monotonic
| { type: 'confirmBlocks' } // damage, then end of turn
| { type: 'concede' } // any time, either seat (not listed)
| { type: 'timeout' }; // host-only, for the seat that must act (not listed)
// ---------------------------------------------------------------------------
// Log (encyclopedic; clients get logFor(state, seat); state.log is omniscient)
// ---------------------------------------------------------------------------
/** A card in the log. power/toughness: current values right after the event (Figures and Garrisons on the battlefield). */
export interface CardRef { uid: string; cardId: string; title: string; pageId: number | null; type: CardType; power?: number; toughness?: number }
export type LogEvent = { seq: number; turn: number } & (
| { kind: 'start'; firstSeat: Seat }
| { kind: 'mulligan'; seat: Seat }
| { kind: 'keep'; seat: Seat; handSize: number }
| { kind: 'turnStart'; seat: Seat; citations: number; life: [number, number] }
/** card is omitted in the opponent's logFor (hidden draw). */
| { kind: 'draw'; seat: Seat; card?: CardRef }
/** A Figure, Location or Institution entered. */
| { kind: 'deploy'; seat: Seat; card: CardRef }
/** An Event resolved. */
| { kind: 'cast'; seat: Seat; card: CardRef; targets: TargetRef[] }
/** A Doctrine was attached. */
| { kind: 'adopt'; seat: Seat; card: CardRef; to: CardRef }
| { kind: 'tokenCreated'; seat: Seat; card: CardRef; source: CardRef }
| { kind: 'connected'; seat: Seat; card: CardRef; with: CardRef; tier: ConnectedTier; via: string; bonus: number }
/** Rivalry (log only): two opposing permanents are Connected. */
| { kind: 'rivalry'; card: CardRef; with: CardRef; tier: ConnectedTier; via: string }
/** Any resolved effect (wording key: template). Damage and life changes follow as their own events. */
| { kind: 'effect'; seat: Seat; source: CardRef; op: EffectOp; template?: string; targets: TargetRef[] }
| { kind: 'attack'; seat: Seat; attackers: CardRef[] }
| { kind: 'block'; seat: Seat; blocks: { blocker: CardRef; attacker: CardRef }[] }
/** Damage to a permanent from an effect / from combat. */
| { kind: 'effectDamage'; source: CardRef; target: CardRef; amount: number }
| { kind: 'combatDamage'; source: CardRef; target: CardRef; amount: number }
/** Damage or life loss to a player. source null = fatigue. */
| { kind: 'playerDamage'; source: CardRef | null; seat: Seat; amount: number; life: number; combat: boolean }
| { kind: 'lifeGain'; source: CardRef; seat: Seat; amount: number; life: number }
| { kind: 'destroyed'; card: CardRef; owner: Seat }
| { kind: 'bounced'; card: CardRef; owner: Seat }
| { kind: 'discard'; seat: Seat; card: CardRef }
| { kind: 'fatigue'; seat: Seat; amount: number }
| { kind: 'concede'; seat: Seat }
| { kind: 'timeout'; seat: Seat }
| { kind: 'gameOver'; winner: Seat | null; reason: EndReason; life: [number, number] }
);
export type LogKind = LogEvent['kind'];
// ---------------------------------------------------------------------------
// Views (what one seat may see; bots and UI get only this)
// ---------------------------------------------------------------------------
export interface PermanentView {
uid: string;
owner: Seat;
controller: Seat;
card: DuelCard;
tapped: boolean;
sick: boolean;
damage: number;
counters: number;
attachedTo?: string;
/** Current values after counters, pumps, statics, Connected and Trending. */
power: number;
toughness: number;
keywords: Keyword[];
/** Active Connected bonus, if any (for the card face). */
connected?: { with: string; tier: ConnectedTier; via: string; bonus: number };
}
export interface SideView {
seat: Seat;
life: number;
citations: number;
maxCitations: number;
handSize: number;
librarySize: number;
graveyard: CardInstance[];
bankSeconds: number;
ready: boolean;
mulligansTaken: number;
}
export interface PlayerView {
engine: 'duel';
seat: Seat;
turn: number;
active: Seat;
phase: Phase;
/** Seat that must act now (null when over). */
toAct: Seat | null;
you: SideView & { hand: CardInstance[] };
opponent: SideView;
battlefield: PermanentView[];
combat: CombatState;
result: DuelResult | null;
version: number;
rules: DuelRuleset;
}
-161
View File
@@ -1,161 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { DEFAULT_DUEL_RULESET, applyAction, createDuel, legalActions, statsOf, toAct, viewFor } from '../shared/duel/index.ts';
import type { DuelAction, DuelCard, DuelState, Seat } from '../shared/duel/index.ts';
import { fig } from './duel-core-fixtures.ts';
/**
* Scenario harness: both players start with exactly `hands` in hand (rest of the
* library is vanilla filler), seat 0 goes first, lots of Citations.
*/
function scenario(hands: [DuelCard[], DuelCard[]], over: Partial<typeof DEFAULT_DUEL_RULESET> = {}): DuelState {
const rules = { ...DEFAULT_DUEL_RULESET, deckSize: 20, openingHand: 0, citationsPerTurn: 5, ...over };
const filler = (seat: number) => Array.from({ length: 20 }, (_, i) => fig(`f${seat}-${i}`, 1, 1));
const decks = [0, 1].map(seat => [...hands[seat], ...filler(seat)].slice(0, rules.deckSize)) as [DuelCard[], DuelCard[]];
let s = structuredClone(createDuel(rules, decks, 'scenario'));
s.firstSeat = 0; s.active = 0;
for (const seat of [0, 1] as Seat[]) {
const p = s.players[seat];
const ids = new Set(hands[seat].map(c => c.cardId));
p.hand = p.library.filter(c => ids.has(c.card.cardId));
p.library = p.library.filter(c => !ids.has(c.card.cardId));
}
s = applyAction(s, 0, { type: 'keep' });
return applyAction(s, 1, { type: 'keep' });
}
const uid = (s: DuelState, id: string) => [...s.players[0].hand, ...s.players[1].hand, ...s.battlefield].find(c => c.card.cardId === id)!.uid;
const act = (s: DuelState, a: DuelAction) => applyAction(s, toAct(s)!, a);
const pass = (s: DuelState) => act(act(s, { type: 'endPhase' }), { type: 'confirmAttack' });
const ev = (id: string, effects: DuelCard['effects'], type: DuelCard['type'] = 'Event'): DuelCard => ({ ...fig(id, 0, 0), type, effects });
test('createToken, maxTokens, and tokens vanish', () => {
const election = ev('election', [{ trigger: 'onPlay', op: 'createToken', amount: 2, token: { name: 'Elected Member', power: 1, toughness: 1, portal: 'Society' } }]);
let s = scenario([[election], []]);
s = act(s, { type: 'play', uid: uid(s, 'election') });
const tokens = s.battlefield.filter(p => p.card.token);
assert.equal(tokens.length, 2);
assert.equal(s.log.filter(e => e.kind === 'tokenCreated').length, 2);
assert.ok(tokens.every(t => t.sick));
});
test('T6 pump then fight uses both chosen targets', () => {
const sport = ev('final', [
{ trigger: 'onPlay', op: 'pump', target: 'ownFigure', power: 2, toughness: 0 },
{ trigger: 'onPlay', op: 'fight', target: 'opponentFigure' },
]);
let s = scenario([[fig('mine', 2, 4), sport], [fig('theirs', 3, 3)]]);
s = act(s, { type: 'play', uid: uid(s, 'mine') });
s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'theirs') });
s = pass(s);
const plays = legalActions(s, 0).filter(a => a.type === 'play' && a.uid === uid(s, 'final'));
assert.deepEqual(plays, [{ type: 'play', uid: uid(s, 'final'), targets: [{ kind: 'card', uid: uid(s, 'mine') }, { kind: 'card', uid: uid(s, 'theirs') }] }]);
s = act(s, plays[0]);
assert.ok(!s.battlefield.some(p => p.card.cardId === 'theirs'), '4 power kills a 3/3');
assert.equal(s.battlefield.find(p => p.card.cardId === 'mine')!.damage, 3);
assert.equal(statsOf(s, uid(s, 'mine')).power, 4, 'pump lasts until end of turn');
});
test('Doctrine attaches, buffs, and is Deleted when its Figure leaves', () => {
const adopt = ev('monoid', [
{ trigger: 'onPlay', op: 'attach', target: 'ownFigure' },
{ trigger: 'static', op: 'pump', target: 'attached', power: 2, toughness: 2, grant: ['Featured'] },
], 'Doctrine');
const kill = ev('war', [{ trigger: 'onPlay', op: 'damage', target: 'figureOrLocation', amount: 5 }]);
let s = scenario([[fig('host', 1, 1), adopt], [kill]]);
s = act(s, { type: 'play', uid: uid(s, 'host') });
s = act(s, { type: 'play', uid: uid(s, 'monoid'), targets: [{ kind: 'card', uid: uid(s, 'host') }] });
assert.deepEqual(statsOf(s, uid(s, 'host')), { power: 3, toughness: 3 });
assert.ok(viewFor(s, 0).battlefield.find(p => p.card.cardId === 'host')!.keywords.includes('Featured'));
assert.equal(s.log.filter(e => e.kind === 'adopt').length, 1);
s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'war'), targets: [{ kind: 'card', uid: uid(s, 'host') }] });
assert.equal(s.battlefield.length, 0, 'Figure destroyed, Doctrine Deleted with it');
assert.equal(s.players[0].graveyard.length, 2);
});
test('Doctrine with no legal target is not playable; Protected blocks opposing Events', () => {
const adopt = ev('monoid', [{ trigger: 'onPlay', op: 'attach', target: 'ownFigure' }], 'Doctrine');
const kill = ev('war', [{ trigger: 'onPlay', op: 'damage', target: 'figure', amount: 5 }]);
let s = scenario([[adopt, fig('pol', 2, 2, ['Protected'])], [kill]]);
assert.ok(!legalActions(s, 0).some(a => a.type === 'play' && a.uid === uid(s, 'monoid')));
s = act(s, { type: 'play', uid: uid(s, 'pol') });
s = pass(s);
assert.ok(!legalActions(s, 1).some(a => a.type === 'play' && a.uid === uid(s, 'war')), 'Protected Figure is the only target');
});
test('Well-sourced prevents 1; Stub grows to 2; Legacy draws', () => {
const ping = ev('ping', [{ trigger: 'onPlay', op: 'damage', target: 'figure', amount: 2 }]);
let s = scenario([[fig('ws', 1, 2, ['Well-sourced']), fig('stub', 1, 1, ['Stub']), fig('leg', 1, 1, ['Legacy'])], [ping, ev('ping2', ping.effects)]]);
s = act(s, { type: 'play', uid: uid(s, 'ws') });
s = act(s, { type: 'play', uid: uid(s, 'stub') });
s = act(s, { type: 'play', uid: uid(s, 'leg') });
s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'ping'), targets: [{ kind: 'card', uid: uid(s, 'ws') }] });
assert.equal(s.battlefield.find(p => p.card.cardId === 'ws')!.damage, 1, 'first point prevented');
const hand0 = s.players[0].hand.length;
s = act(s, { type: 'play', uid: uid(s, 'ping2'), targets: [{ kind: 'card', uid: uid(s, 'leg') }] });
assert.equal(s.players[0].hand.length, hand0 + 1, 'Legacy drew');
for (let i = 0; i < 6; i++) s = pass(s);
assert.deepEqual(statsOf(s, uid(s, 'stub')), { power: 3, toughness: 3 }, 'Stub capped at 2 counters');
});
test('one Location per turn; Territory raises max Citations up to the cap', () => {
const loc = (id: string): DuelCard => ({ ...fig(id, 0, 2), type: 'Location', territory: 1 });
let s = scenario([[loc('l1'), loc('l2'), loc('l3')], []], { citationsPerTurn: 1, maxCitations: 10 });
s = act(s, { type: 'play', uid: uid(s, 'l1') });
assert.ok(!legalActions(s, 0).some(a => a.type === 'play'), 'second Location not allowed (and no Citations left)');
assert.equal(viewFor(s, 0).you.maxCitations, 2);
s = pass(s); s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'l2') });
s = pass(s); s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'l3') });
assert.equal(viewFor(s, 0).you.maxCitations, 3 + 2, 'base 3 + Territory capped at 2');
});
test('Garrison Location blocks; Featured needs a Featured/Translated/Multilingual blocker', () => {
const garrison: DuelCard = { ...fig('fort', 0, 3), type: 'Location' };
let s = scenario([[fig('grunt', 2, 2), fig('flyer', 2, 2, ['Featured'])], [garrison, fig('reach', 1, 3, ['Translated'])]]);
s = act(s, { type: 'play', uid: uid(s, 'grunt') });
s = act(s, { type: 'play', uid: uid(s, 'flyer') });
s = pass(s);
s = act(s, { type: 'play', uid: uid(s, 'fort') });
s = act(s, { type: 'play', uid: uid(s, 'reach') });
s = pass(s);
s = act(s, { type: 'endPhase' });
s = act(s, { type: 'toggleAttacker', uid: uid(s, 'grunt') });
s = act(s, { type: 'toggleAttacker', uid: uid(s, 'flyer') });
s = act(s, { type: 'confirmAttack' });
const blocks = legalActions(s, 1).filter(a => a.type === 'block');
assert.deepEqual(blocks.map(b => b.type === 'block' ? `${b.blocker}>${b.attacker}` : ''), [
`${uid(s, 'fort')}>${uid(s, 'grunt')}`, `${uid(s, 'reach')}>${uid(s, 'grunt')}`, `${uid(s, 'reach')}>${uid(s, 'flyer')}`,
]);
s = act(s, { type: 'block', blocker: uid(s, 'fort'), attacker: uid(s, 'grunt') });
s = act(s, { type: 'block', blocker: uid(s, 'reach'), attacker: uid(s, 'flyer') });
s = act(s, { type: 'confirmBlocks' });
assert.equal(s.players[1].life, 20);
assert.equal(s.battlefield.length, 4, 'nobody died: 2 vs 3 toughness, 1 vs 2');
});
test('three consecutive timeouts lose; turnCap decides by Notability', () => {
let s = scenario([[], []]);
for (let i = 0; i < 5 && !s.result; i++) s = applyAction(s, toAct(s)!, { type: 'timeout' });
assert.deepEqual(s.result, { winner: 1, reason: 'timeout' }, 'seat 0 timed out on turns 1, 3, 5');
let c = scenario([[fig('a', 3, 3)], []], { turnCap: 4 });
c = act(c, { type: 'play', uid: uid(c, 'a') });
c = pass(c); c = pass(c);
c = act(c, { type: 'endPhase' });
c = act(c, { type: 'toggleAttacker', uid: uid(c, 'a') });
c = act(c, { type: 'confirmAttack' });
c = pass(c);
assert.deepEqual(c.result, { winner: 0, reason: 'turnCap' });
});
test('secondPlayerCitations gives the second player extra Citations on their first turn only', () => {
let s = scenario([[], []], { citationsPerTurn: 1, secondPlayerCitations: 1 });
assert.equal(s.players[0].citations, 1);
s = pass(s);
assert.equal(s.players[1].citations, 2);
s = pass(s); s = pass(s);
assert.equal(s.players[1].citations, 2, 'turn 4: base 2, no bonus');
});
-209
View File
@@ -1,209 +0,0 @@
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' };
-110
View File
@@ -1,110 +0,0 @@
// Test-only deck builder for duel-core tests. NOT the real derivation (that is duel-cards'
// shared/duel/derive.ts): a crude, deterministic mapping from docs/game/card-sample.json
// so engine tests have 236 varied real cards. Zero network calls.
import { readFileSync } from 'node:fs';
import { Rng } from '../shared/game/rng.ts';
import type { CardType, DuelCard, EffectSpec, Keyword, Portal } from '../shared/duel/index.ts';
interface SampleCard {
pageId: number; title: string; description?: string; revisionId?: number; pageBytes: number; languageCount: number;
categories: string[]; stats: { attention: number; depth: number; reach: number }; faction: string;
instanceOf?: { id: string; label: string }[];
}
const sample = JSON.parse(readFileSync(new URL('../docs/game/card-sample.json', import.meta.url), 'utf8')) as { cards: SampleCard[] };
/** Effect templates shaped like DUEL_DESIGN §5 (k = magnitude). */
const EVENTS = (k: number): EffectSpec[][] => [
[{ trigger: 'onPlay', op: 'damage', target: 'figureOrLocation', amount: k + 1, template: 'T3' }],
[{ trigger: 'onPlay', op: 'damageEach', target: 'each', amount: Math.max(1, k - 1), template: 'T4' }],
[{ trigger: 'onPlay', op: 'createToken', amount: k, token: { name: 'Elected Member', power: 1, toughness: 1, portal: 'Society' }, template: 'T5' }],
[{ trigger: 'onPlay', op: 'pump', target: 'ownFigure', power: k, toughness: 0, template: 'T6' }, { trigger: 'onPlay', op: 'fight', target: 'opponentFigure', template: 'T6' }],
[{ trigger: 'onPlay', op: 'draw', amount: k, template: 'T7' }],
[{ trigger: 'onPlay', op: 'damage', target: 'anyTarget', amount: k, template: 'X1' }],
];
const INSTITUTIONS = (k: number, cost: number): EffectSpec[][] => [
cost >= 4
? [{ trigger: 'startOfTurn', op: 'createToken', token: { name: 'Staff Member', power: 1, toughness: 1, portal: 'Society' }, condition: { maxTokens: 3 }, template: 'T8' }]
: [{ trigger: 'onEnter', op: 'createToken', token: { name: 'Staff Member', power: 1, toughness: 1, portal: 'Society' }, template: 'T8' }],
[{ trigger: 'onEnter', op: 'draw', amount: 1, template: 'T9' }, { trigger: 'static', op: 'pump', target: 'ownFigures', power: 1, toughness: 1, condition: { connected: true }, template: 'T9' }],
[{ trigger: 'static', op: 'pump', target: 'ownFigures', power: 1, toughness: 0, condition: { maxPower: 2 }, template: 'T10' }],
];
const DOCTRINES = (k: number): EffectSpec[][] => [
[{ trigger: 'onPlay', op: 'attach', target: 'ownFigure', template: 'T11' }, { trigger: 'static', op: 'pump', target: 'attached', power: k, toughness: k, template: 'T11' }],
[{ trigger: 'onPlay', op: 'attach', target: 'ownFigure', template: 'T11' }, { trigger: 'static', op: 'pump', target: 'attached', power: k, toughness: k, grant: ['Well-sourced'], template: 'T11' }],
[{ trigger: 'onPlay', op: 'attach', target: 'ownFigure', template: 'T11' }, { trigger: 'static', op: 'pump', target: 'attached', power: k, toughness: k, template: 'T11' }, { trigger: 'onCombatDamageToPlayer', op: 'draw', amount: 1, template: 'T11' }],
[{ trigger: 'onPlay', op: 'attach', target: 'figure', template: 'T12' }, { trigger: 'static', op: 'pump', target: 'attached', power: -k, toughness: -k, template: 'T12' }],
];
const PORTAL: Record<string, Portal> = {
People: 'Society', Places: 'Earth', Nature: 'Earth', Structures: 'Arts', Works: 'Arts', Events: 'History', Organisations: 'Society',
'Ideas & Tech': 'Science', Sport: 'Society', Other: 'Neutral',
};
function typeOf(c: SampleCard, i: number): CardType {
if (c.instanceOf?.some(x => x.id === 'Q5') || c.faction === 'Nature') return 'Figure';
if (c.faction === 'Places' || c.faction === 'Structures') return i % 2 ? 'Location' : 'Figure';
if (c.faction === 'Events') return 'Event';
if (c.faction === 'Works' || c.faction === 'Organisations') return 'Institution';
return (['Event', 'Doctrine', 'Figure', 'Institution'] as CardType[])[i % 4];
}
const COUNTRIES = ['Q30', 'Q145', 'Q142', 'Q183', 'Q668', 'Q17', 'Q902', 'Q252'];
export function toDuelCard(c: SampleCard, i: number): DuelCard {
const type = typeOf(c, i);
const fame = Math.round((c.stats.attention + c.stats.depth + c.stats.reach) / 3);
const cost = 1 + [26, 39, 51, 63, 78].filter(t => fame >= t).length;
const k = Math.ceil(cost / 2);
const keywords: Keyword[] = [];
if (type === 'Figure') {
const add = (kw: Keyword, ok: boolean) => { if (ok && keywords.length < 2) keywords.push(kw); };
add('Protected', c.categories.includes('Living people'));
add('Featured', c.stats.attention >= 85);
add('Legacy', c.categories.some(x => /^\d+ deaths$/.test(x)));
add('Multilingual', c.stats.reach >= 90);
add('Translated', c.stats.reach >= 75 && c.stats.reach < 90);
add('Well-sourced', c.stats.depth >= 85);
add('Stub', c.pageBytes < 3000 || c.categories.some(x => /stubs$/.test(x)));
add('Local', c.stats.reach === 0);
}
const budget = [3, 4, 6, 8, 10, 12][cost - 1] - keywords.filter(x => x !== 'Stub' && x !== 'Translated' && x !== 'Local').length;
const a = c.stats.attention + 1, d = c.stats.depth + 1;
const power = type === 'Figure' ? Math.min(budget - 1, Math.max(1, Math.round(budget * a / (a + d)))) : 0;
const toughness = type === 'Figure' ? Math.max(1, budget - power) : type === 'Location' ? 1 + Math.floor(c.stats.depth / 25) : 0;
const effects: EffectSpec[] =
type === 'Event' ? EVENTS(k)[i % 6]
: type === 'Doctrine' ? DOCTRINES(k)[i % 4]
: type === 'Institution' ? INSTITUTIONS(k, cost)[i % 3]
: [];
return {
cardId: `sample-${c.pageId}`, pageId: c.pageId, revisionId: c.revisionId, title: c.title, description: c.description, type,
portal: PORTAL[c.faction] ?? 'Neutral', cost: type === 'Location' ? Math.ceil(cost / 2) : cost, power, toughness, keywords,
...(type === 'Location' ? { territory: 1 } : {}), effects, rulesText: '', flavor: '',
connect: {
countries: [COUNTRIES[c.pageId % COUNTRIES.length]].filter((_, j) => (c.pageId >> 3) % 3 !== 0 || j > 0),
centuries: [17 + (c.pageId % 5)],
links: [],
p31: (c.instanceOf ?? []).map(x => x.id),
},
};
}
export const POOL: DuelCard[] = sample.cards.map(toDuelCard);
// A few direct links so the Linked tier gets exercised.
for (let i = 0; i + 1 < POOL.length; i += 9) {
const b = POOL[i + 1];
if (b.pageId !== null) POOL[i].connect!.links.push(b.pageId);
}
/** `size` distinct pool cards; cardIds suffixed per seat so the two decks never share ids. */
export function randomDeck(seed: string, seat: 0 | 1, size = 40): DuelCard[] {
const rng = Rng.fromSeed(seed);
return rng.shuffle(POOL).slice(0, size).map(c => ({ ...structuredClone(c), cardId: `${c.cardId}#${seat}` }));
}
/** Minimal vanilla Figure for hand-built scenarios. */
export function fig(id: string, power: number, toughness: number, keywords: Keyword[] = [], extra: Partial<DuelCard> = {}): DuelCard {
return { cardId: id, pageId: null, title: id, type: 'Figure', portal: 'Society', cost: 1, power, toughness, keywords, effects: [], rulesText: '', flavor: '', ...extra };
}
-41
View File
@@ -1,41 +0,0 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { DEFAULT_DUEL_RULESET, mergeDuelRuleset, parseDuelRuleset, CARD_TYPES, KEYWORDS } from '../shared/duel/index.ts';
import { DEFAULT_RULESET } from '../shared/game/index.ts';
test('duel default ruleset matches the BRIEF', () => {
const rules = parseDuelRuleset({ ...DEFAULT_DUEL_RULESET });
assert.equal(rules.deckSize, 40);
assert.equal(rules.startingLife, 20);
assert.equal(rules.maxCitations, 10);
assert.equal(rules.handSize, 7);
assert.equal(rules.openingHand, 7);
assert.equal(rules.mulligans, 1);
assert.equal(rules.libraryEmpty, 'lose');
assert.equal(rules.firstPlayerDraws, false);
assert.equal(rules.turnSeconds, 60);
assert.equal(rules.bankSeconds, 300);
assert.equal(rules.turnCap, 40, 'balance v1 (#64)');
assert.equal(rules.secondPlayerCitations, 1, 'balance v1 (#64)');
assert.equal(rules.territoryCap, 2);
});
test('duel ruleset merges overrides and rejects unknown keys', () => {
assert.equal(mergeDuelRuleset({ deckSize: 20 }).deckSize, 20);
assert.equal(mergeDuelRuleset({ turnCap: 30 }).turnCap, 30);
assert.throws(() => mergeDuelRuleset({ topicCount: 3 }));
assert.throws(() => mergeDuelRuleset({ libraryEmpty: 'shrug' }));
assert.throws(() => mergeDuelRuleset({ deckSize: 0 }));
assert.throws(() => mergeDuelRuleset([1]));
});
test('Night 1 Edit War rulesets are rejected cleanly', () => {
assert.throws(() => parseDuelRuleset(DEFAULT_RULESET));
});
test('card types and keywords are the BRIEF set', () => {
assert.deepEqual([...CARD_TYPES], ['Figure', 'Location', 'Event', 'Institution', 'Doctrine']);
for (const k of ['Protected', 'Featured', 'Translated', 'Legacy', 'Stub', 'Well-sourced', 'Multilingual', 'Local']) {
assert.ok((KEYWORDS as readonly string[]).includes(k), k);
}
});
+1 -1
View File
@@ -54,7 +54,7 @@ test('fixed HTTPS sources, encoded title, hidden categories, lower bounds and pl
assert.equal(init.redirect, 'error');
assert.ok(init.signal instanceof AbortSignal);
assert.equal(new Headers(init.headers).get('accept'), 'application/json');
assert.match(new Headers(init.headers).get('user-agent')!, /Wikideck/);
assert.match(new Headers(init.headers).get('user-agent')!, /WikiDeck/i);
assert.equal(new Headers(init.headers).has('origin'), false); // Server-side requests need no CORS workaround.
}
assert.equal(result.title, title);
+17 -9
View File
@@ -2,7 +2,7 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import { WikipediaArticleSource } from '../server/wiki.ts';
import { WikipediaEnrichment } from '../server/enrichment.ts';
import { ARTICLE_USER_AGENT, ENRICHMENT_USER_AGENT, wikimediaUserAgent } from '../server/wikimedia-user-agent.ts';
import { PROJECT_URL, defaultUserAgent, wikimediaUserAgent } from '../server/wikimedia-user-agent.ts';
function capture() {
const agents: string[] = [];
@@ -12,9 +12,10 @@ function capture() {
};
return { agents, fetcher };
}
async function agentsFor(env: string | undefined) {
const previous = process.env.WIKIDECK_USER_AGENT;
async function agentsFor(env: string | undefined, contact?: string) {
const previous = process.env.WIKIDECK_USER_AGENT, previousContact = process.env.WIKIDECK_OPERATOR_CONTACT;
if (env === undefined) delete process.env.WIKIDECK_USER_AGENT; else process.env.WIKIDECK_USER_AGENT = env;
if (contact === undefined) delete process.env.WIKIDECK_OPERATOR_CONTACT; else process.env.WIKIDECK_OPERATOR_CONTACT = contact;
try {
const article = capture(), enrichment = capture();
await assert.rejects(new WikipediaArticleSource(article.fetcher).random());
@@ -22,15 +23,22 @@ async function agentsFor(env: string | undefined) {
return { article: article.agents, enrichment: enrichment.agents };
} finally {
if (previous === undefined) delete process.env.WIKIDECK_USER_AGENT; else process.env.WIKIDECK_USER_AGENT = previous;
if (previousContact === undefined) delete process.env.WIKIDECK_OPERATOR_CONTACT;
else process.env.WIKIDECK_OPERATOR_CONTACT = previousContact;
}
}
test('default Wikimedia user agents are unchanged when WIKIDECK_USER_AGENT is unset', async () => {
assert.equal(ARTICLE_USER_AGENT, 'Wikideck/0.1 (experimental Wikipedia article cards)');
assert.equal(ENRICHMENT_USER_AGENT, 'Wikideck/0.1 (Wikipedia off-chain page enrichment)');
const { article, enrichment } = await agentsFor(undefined);
assert.ok(article.length > 0 && article.every(a => a === ARTICLE_USER_AGENT));
assert.ok(enrichment.length > 0 && enrichment.every(a => a === ENRICHMENT_USER_AGENT));
test('default Wikimedia user agent names the project, the operator contact and node', async () => {
const { article, enrichment } = await agentsFor(undefined, 'ops@example.org');
const expected = `WikiDeck/0.1.0 (+${PROJECT_URL}; ops@example.org) node/${process.versions.node}`;
assert.equal(defaultUserAgent({ WIKIDECK_OPERATOR_CONTACT: 'ops@example.org' }), expected);
assert.ok(article.length > 0 && article.every(a => a === expected));
assert.ok(enrichment.length > 0 && enrichment.every(a => a === expected));
assert.equal(defaultUserAgent({ PUBLIC_ORIGIN: 'https://node.example' }),
`WikiDeck/0.1.0 (+${PROJECT_URL}; https://node.example) node/${process.versions.node}`);
assert.equal(defaultUserAgent({}), `WikiDeck/0.1.0 (+${PROJECT_URL}) node/${process.versions.node}`);
// Unsafe contacts are dropped rather than breaking the header's comment syntax.
assert.equal(defaultUserAgent({ WIKIDECK_OPERATOR_CONTACT: 'a); evil(' }), defaultUserAgent({}));
});
test('WIKIDECK_USER_AGENT overrides both Wikimedia clients', async () => {
+4 -1
View File
@@ -56,13 +56,16 @@ test('random article fetch rejects unsafe response types, redirects, and oversiz
() => new Response('{}', { headers: { 'Content-Type': 'text/html' } }),
() => new Response(null, { status: 302, headers: { Location: 'https://evil.example' } }),
() => new Response(' '.repeat(512 * 1024 + 1), { headers: { 'Content-Type': 'application/json' } }),
() => { throw new Error('offline'); },
];
for (const fail of failures) {
const { fetcher, calls } = mockFetch(() => fail());
await assert.rejects(new WikipediaArticleSource(fetcher).random());
assert.equal(calls.length, 3);
}
// A network failure (or throttling) stops at once instead of retrying against an unreachable upstream.
const { fetcher, calls } = mockFetch(() => { throw new Error('offline'); });
await assert.rejects(new WikipediaArticleSource(fetcher).random(), /Wikimedia unavailable/);
assert.equal(calls.length, 1);
});
test('bad page records are skipped while later valid candidates are considered', async () => {
+236
View File
@@ -0,0 +1,236 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { WikimediaClient, WikimediaUnavailable, configFromEnv, type ClientConfig } from '../server/wikimedia/client.ts';
import { WikimediaCache, cacheKey } from '../server/wikimedia/cache.ts';
import { warnIfNoContact } from '../server/wikimedia-user-agent.ts';
import { FakeClock, flush, json, recorder } from './wikimedia-helpers.ts';
const API = 'https://en.wikipedia.org/w/api.php?action=query&format=json&pageids=1';
const REST = 'https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/x';
function setup(handler: Parameters<typeof recorder>[1] = () => json({ ok: true }), config: Partial<ClientConfig> = {},
cache?: WikimediaCache) {
const clock = new FakeClock();
const rec = recorder(clock, handler);
const client = new WikimediaClient({ fetch: rec.fetcher, clock, config, cache, userAgent: () => 'Test/1 (+x; y)' });
return { clock, client, ...rec };
}
test('token bucket: burst of 3, then 1 request per second, one in flight per host', async () => {
const { clock, client, calls } = setup();
const jobs = Array.from({ length: 6 }, () => client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 })
.then(r => r.response.body?.cancel()));
await clock.advance(0);
assert.equal(calls.length, 3);
await clock.advance(999);
assert.equal(calls.length, 3);
await clock.advance(1);
assert.equal(calls.length, 4);
await clock.advance(2000);
await Promise.all(jobs);
assert.deepEqual(calls.map(c => c.at - calls[0].at), [0, 0, 0, 1000, 2000, 3000]);
// Different hosts have separate buckets.
await client.request(new URL(REST), { lane: 'interactive', timeoutMs: 1000 });
assert.equal(calls.at(-1)!.url.hostname, 'wikimedia.org');
});
test('only one request is in flight per host, even with tokens to spare', async () => {
const releases: (() => void)[] = [];
const { clock, client, calls } = setup(() => new Promise(resolve => releases.push(() => resolve(json({})))));
const a = client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 });
const b = client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 });
await clock.advance(0);
assert.equal(calls.length, 1);
releases.shift()!();
await a;
await clock.advance(0);
assert.equal(calls.length, 2);
releases.shift()!();
await b;
});
test('priority lanes: mint, then interactive, then background', async () => {
const { clock, client, calls } = setup(url => json({ lane: url.searchParams.get('n') }), { burst: 1 });
await client.request(new URL(`${API}&n=warm`), { lane: 'background', timeoutMs: 1000 }); // Uses the only token.
const order: string[] = [];
const go = (lane: 'mint' | 'interactive' | 'background', n: string) =>
client.request(new URL(`${API}&n=${n}`), { lane, timeoutMs: 1000 }).then(() => order.push(n));
const jobs = [go('background', 'b1'), go('interactive', 'i1'), go('background', 'b2'), go('mint', 'm1'), go('interactive', 'i2')];
await clock.advance(5000);
await Promise.all(jobs);
assert.deepEqual(order, ['m1', 'i1', 'i2', 'b1', 'b2']);
assert.equal(calls.length, 6);
});
test('maxlag=5 is added to Action API requests only, and a maxlag reply waits for Retry-After', async () => {
let lagged = 1;
const { clock, client, calls } = setup(url => url.pathname === '/w/api.php' && lagged-- > 0
? json({ error: { code: 'maxlag' } }, { 'MediaWiki-API-Error': 'maxlag', 'Retry-After': '5' })
: json({ ok: true }));
const job = client.request(new URL(API), { lane: 'background', timeoutMs: 1000 });
await clock.advance(0);
assert.equal(calls.length, 1);
assert.equal(calls[0].url.searchParams.get('maxlag'), '5');
await clock.advance(4999);
assert.equal(calls.length, 1);
await clock.advance(1);
const { response } = await job;
assert.deepEqual(await response.json(), { ok: true });
assert.equal(calls.length, 2);
await client.request(new URL(REST), { lane: 'background', timeoutMs: 1000 });
assert.equal(calls[2].url.searchParams.has('maxlag'), false);
assert.equal(calls[0].headers.get('user-agent'), 'Test/1 (+x; y)');
});
test('429 honours Retry-After by pausing the host; three in a row open the circuit for 10 minutes', async () => {
let status = 429;
const { clock, client, calls } = setup(() => status === 200 ? json({}) : new Response('slow down',
{ status, headers: { 'Retry-After': '2' } }));
const req = (lane: 'mint' | 'background' = 'background') => client.request(new URL(API), { lane, timeoutMs: 1000 });
await assert.rejects(req(), (e: unknown) => e instanceof WikimediaUnavailable && e.status === 429);
// The next request waits out Retry-After (2 s) instead of going straight back.
const second = req().catch(e => e);
await clock.advance(1999);
assert.equal(calls.length, 1);
await clock.advance(1);
assert.equal(((await second) as WikimediaUnavailable).status, 429);
const third = req().catch(e => e);
await clock.advance(10_000);
assert.equal(((await third) as WikimediaUnavailable).status, 429);
assert.equal(calls.length, 3);
assert.equal(client.status().hosts['en.wikipedia.org'].circuitOpen, true);
// Open circuit: refused at once without touching Wikimedia, even on the mint lane.
status = 200;
await assert.rejects(req('mint'), /circuit open/);
assert.equal(calls.length, 3);
await clock.advance(10 * 60_000);
await req('mint');
assert.equal(calls.length, 4);
assert.equal(client.status().counters['breaker.open'], 1);
});
test('a success resets the consecutive-429 count', async () => {
const replies = [429, 200, 429, 200, 429];
const { clock, client } = setup(() => {
const status = replies.shift()!;
return status === 200 ? json({}) : new Response('', { status });
});
for (let i = 0; i < 5; i++) {
const job = client.request(new URL(API), { lane: 'background', timeoutMs: 1000 }).catch(() => {});
await clock.advance(60_000);
await job;
}
assert.equal(client.status().hosts['en.wikipedia.org'].circuitOpen, false);
});
test('daily cap: background stops at 80 %, interactive at 100 %, mint keeps a 10 % reserve; resets at UTC midnight', async () => {
const { clock, client } = setup(() => json({}), { dailyCap: 10, requestsPerSecond: 1000, burst: 1000 });
const req = (lane: 'mint' | 'interactive' | 'background') => client.request(new URL(API), { lane, timeoutMs: 1000 });
for (let i = 0; i < 8; i++) await req('background');
await assert.rejects(req('background'), /daily cap/);
await req('interactive'); await req('interactive');
await assert.rejects(req('interactive'), /daily cap/);
await req('mint');
await assert.rejects(req('mint'), /daily cap/);
await clock.advance(24 * 3600_000);
await req('background');
});
test('nodes sharing one egress IP split the rate and the daily cap', () => {
const config = configFromEnv({ WIKIDECK_WIKIMEDIA_RPS: '0.9', WIKIDECK_WIKIMEDIA_NODES_PER_IP: '3',
WIKIDECK_WIKIMEDIA_DAILY_CAP: '3000' });
const client = new WikimediaClient({ fetch: async () => json({}), config });
assert.ok(Math.abs(client.status().requestsPerSecond - 0.3) < 1e-9);
assert.equal(client.status().dailyCap, 1000);
assert.equal(configFromEnv({ WIKIDECK_WIKIMEDIA_RPS: 'abc' }).requestsPerSecond, 1);
});
test('a full queue sheds background work first and a mint request evicts a background waiter', async () => {
const { clock, client } = setup(() => json({}), { queueLimit: 2, burst: 1 });
await client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 });
const b1 = client.request(new URL(API), { lane: 'background', timeoutMs: 1000 }).then(() => 'ok', e => e.message);
const b2 = client.request(new URL(API), { lane: 'background', timeoutMs: 1000 }).then(() => 'ok', e => e.message);
await assert.rejects(client.request(new URL(API), { lane: 'background', timeoutMs: 1000 }), /queue full/);
const mint = client.request(new URL(API), { lane: 'mint', timeoutMs: 1000 });
await clock.advance(5000);
await mint;
assert.deepEqual([await b1, await b2], ['ok', 'Wikimedia unavailable: queue full']);
});
test('waiting longer than the lane allows fails instead of queueing forever', async () => {
const { clock, client } = setup(() => json({}), { burst: 1, requestsPerSecond: 0.01, maxWaitMs: { interactive: 3000 } as ClientConfig['maxWaitMs'] });
await client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 });
const late = client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 }).catch(e => e);
await clock.advance(3000);
assert.match(String(await late), /queue wait/);
});
test('disk cache: ETag revalidation with If-None-Match, 304 refresh, stale-while-revalidate on the background lane', async () => {
const cache = new WikimediaCache(':memory:');
let body = { v: 1 };
const { clock, client, calls } = setup((_url, headers) => headers.get('if-none-match') === '"v1"' && body.v === 1
? new Response(null, { status: 304 }) : json(body, { ETag: `"v${body.v}"` }), {}, cache);
const policy = { ttlMs: 1000, staleWhileRevalidate: false };
const read = async (lane: 'interactive' | 'background' = 'interactive', cachePolicy = policy) =>
(await (await client.request(new URL(API), { lane, timeoutMs: 1000, cache: cachePolicy })).response.json());
assert.deepEqual(await read(), { v: 1 });
assert.deepEqual(await read(), { v: 1 });
assert.equal(calls.length, 1); // Fresh hit.
await clock.advance(1000);
assert.deepEqual(await read(), { v: 1 });
assert.equal(calls.length, 2);
assert.equal(calls[1].headers.get('if-none-match'), '"v1"'); // Conditional request, answered 304.
assert.equal(client.status().counters['cache.revalidated'], 1);
// Stale-while-revalidate: the stale copy is served at once; the refresh happens on the background lane.
await clock.advance(1000);
body = { v: 2 };
assert.deepEqual(await read('interactive', { ttlMs: 1000, staleWhileRevalidate: true }), { v: 1 });
await clock.advance(0); await flush();
assert.equal(calls.length, 3);
assert.equal(client.status().counters['lane.background'], 1);
assert.deepEqual(await read(), { v: 2 });
assert.equal(calls.length, 3);
// The key ignores maxlag and parameter order.
assert.equal(cacheKey(new URL('https://h.org/p?b=2&a=1&maxlag=5')), cacheKey(new URL('https://h.org/p?a=1&b=2')));
});
test('requests without a cache policy (mint) neither read nor write the cache', async () => {
const cache = new WikimediaCache(':memory:');
const { client, calls } = setup(() => json({ n: calls.length }), {}, cache);
await client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000, cache: { ttlMs: 60_000 } });
const minted = await client.request(new URL(API), { lane: 'mint', timeoutMs: 1000 });
assert.equal(minted.fromCache, false);
assert.equal(calls.length, 2);
assert.equal(cache.size(), 1);
});
test('a stale copy is served when the upstream refuses, and non-JSON replies are never cached', async () => {
const cache = new WikimediaCache(':memory:');
let fail = false, html = false;
const { clock, client } = setup(() => fail ? new Response('', { status: 429 })
: html ? new Response('<html>', { headers: { 'Content-Type': 'text/html' } }) : json({ v: 1 }), {}, cache);
const policy = { ttlMs: 1000 };
await client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000, cache: policy });
await clock.advance(2000);
fail = true;
const stale = await client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000, cache: policy });
assert.equal(stale.fromCache, true);
fail = false; html = true;
await clock.advance(60_000);
await client.request(new URL(`${API}&other=1`), { lane: 'interactive', timeoutMs: 1000, cache: policy });
assert.equal(cache.size(), 1);
});
test('start-up warns when there is no operator contact', () => {
const messages: string[] = [];
assert.equal(warnIfNoContact({}, m => messages.push(m)), true);
assert.match(messages[0], /WIKIDECK_OPERATOR_CONTACT/);
assert.equal(warnIfNoContact({ WIKIDECK_OPERATOR_CONTACT: 'ops@example.org' }, m => messages.push(m)), false);
assert.equal(warnIfNoContact({ WIKIDECK_USER_AGENT: 'X/1 (+https://x; y)' }, m => messages.push(m)), false);
assert.equal(messages.length, 1);
});
test('only HTTPS is allowed', async () => {
const { client } = setup();
await assert.rejects(client.request(new URL('http://en.wikipedia.org/w/api.php'), { lane: 'mint', timeoutMs: 1 }), RangeError);
});
+52
View File
@@ -0,0 +1,52 @@
// Shared fakes for tests/wikimedia-*.test.ts: a manual clock and a recording fetch. Zero network.
import type { Clock } from '../server/wikimedia/client.ts';
export class FakeClock implements Clock {
time: number;
private timers: { at: number; fn: () => void; id: number }[] = [];
private nextId = 1;
constructor(start = Date.parse('2026-11-17T12:00:00Z')) { this.time = start; }
now(): number { return this.time; }
setTimeout(fn: () => void, ms: number): unknown {
const id = this.nextId++;
this.timers.push({ at: this.time + Math.max(0, ms), fn, id });
return id;
}
clearTimeout(handle: unknown): void { this.timers = this.timers.filter(t => t.id !== handle); }
/** Advance time, firing due timers in order and letting promise chains settle between them. */
async advance(ms: number): Promise<void> {
const target = this.time + ms;
await flush();
for (;;) {
this.timers.sort((a, b) => a.at - b.at || a.id - b.id);
const next = this.timers[0];
if (!next || next.at > target) break;
this.timers.shift();
this.time = Math.max(this.time, next.at);
next.fn();
await flush();
}
this.time = target;
await flush();
}
}
export async function flush(): Promise<void> {
for (let i = 0; i < 10; i++) await new Promise(resolve => setImmediate(resolve));
}
export function json(value: unknown, headers: Record<string, string> = {}, status = 200): Response {
return new Response(JSON.stringify(value), { status, headers: { 'Content-Type': 'application/json; charset=utf-8', ...headers } });
}
export type Call = { url: URL; headers: Headers; at: number };
export function recorder(clock: { now(): number }, handler: (url: URL, headers: Headers) => Response | Promise<Response>) {
const calls: Call[] = [];
const fetcher: typeof fetch = async (input, init) => {
const url = new URL(String(input));
const headers = new Headers(init?.headers);
calls.push({ url, headers, at: clock.now() });
return handler(url, headers);
};
return { calls, fetcher };
}
+104
View File
@@ -0,0 +1,104 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { WikipediaEnrichment } from '../server/enrichment.ts';
import { WikipediaArticleSource, randomArticlesUrl } from '../server/wiki.ts';
import { WikimediaClient } from '../server/wikimedia/client.ts';
import { WikimediaCache } from '../server/wikimedia/cache.ts';
import { UNTHROTTLED } from '../server/wikimedia/client.ts';
import { json } from './wikimedia-helpers.ts';
const JANUARY = new Date('2025-01-15T23:59:59Z');
const MONTHS = ['2024-07', '2024-08', '2024-09', '2024-10', '2024-11', '2024-12'];
const metadata = (pageId = 736) => ({ query: { pages: [{ pageid: pageId, ns: 0, title: 'Albert Einstein', lastrevid: 1234,
length: 100_000, pageprops: { wikibase_item: 'Q937' }, categories: [], langlinks: [{ lang: 'de' }] }] } });
const views = (counts = [10, 20, 30, 40, 50, 100]) => ({ items: MONTHS.map((month, i) => ({ timestamp: `${month.replace('-', '')}0100`,
views: counts[i], project: 'en.wikipedia', access: 'all-access', agent: 'user', granularity: 'monthly' })) });
function service(viewsReply: () => Response | Promise<Response>, cache?: WikimediaCache) {
const urls: URL[] = [];
const fetcher: typeof fetch = async input => {
const url = new URL(String(input));
urls.push(url);
return url.hostname === 'en.wikipedia.org' ? json(metadata(Number(url.searchParams.get('pageids')))) : viewsReply();
};
const client = new WikimediaClient({ fetch: fetcher, config: UNTHROTTLED, cache });
return { urls, client, enrichment: new WikipediaEnrichment({ client, now: () => JANUARY }) };
}
test('mint fix: a 429, 5xx, timeout or network failure on pageviews fails the mint read instead of pinning attention: null', async () => {
const failures: (() => Response | Promise<Response>)[] = [
() => new Response('', { status: 429, headers: { 'Retry-After': '0' } }),
() => new Response('', { status: 503 }),
() => new Response('', { status: 500 }),
() => new Response('<html>busy</html>', { headers: { 'Content-Type': 'text/html' } }),
() => { throw new TypeError('fetch failed'); },
() => Promise.reject(new DOMException('The operation timed out.', 'TimeoutError')),
];
for (const failure of failures) {
const { enrichment } = service(failure);
await assert.rejects(enrichment.getFresh(736), /unavailable/);
// Display reads keep their old behaviour: metadata with unavailable views.
const shown = await enrichment.get(736);
assert.equal(shown.pageviews.status, 'unavailable');
assert.equal(shown.stats.attention, null);
}
});
test('mint records unavailable views only for a genuine 404 or missing data', async () => {
for (const reply of [() => json({ type: 'not found' }, {}, 404), () => json({ items: [] }), () => json({ items: views().items.slice(1) })]) {
const { enrichment } = service(reply);
const minted = await enrichment.getFresh(736);
assert.equal(minted.pageviews.status, 'unavailable');
assert.equal(minted.stats.attention, null);
}
const { enrichment } = service(() => json(views()));
assert.equal((await enrichment.getFresh(736)).stats.attention, 33);
});
test('mint reads never use the disk cache or join a display read', async () => {
const cache = new WikimediaCache(':memory:');
let count = 100;
const { enrichment, urls } = service(() => json(views([1, 1, 1, 1, 1, count++])), cache);
const shown = await enrichment.get(736);
assert.equal(cache.size(), 2);
const [display, minted] = await Promise.all([enrichment.get(736), enrichment.getFresh(736)]);
assert.equal(display.pageviews.months[5].views, shown.pageviews.months[5].views);
assert.equal(minted.pageviews.months[5].views, 101); // Fetched again, not the cached 100.
assert.equal(urls.length, 4);
// A later display read (memory cache cleared by a new service) is served from disk: no upstream call.
const second = new WikipediaEnrichment({ client: new WikimediaClient({ fetch: async () => { throw new Error('no network'); },
config: UNTHROTTLED, cache }), now: () => JANUARY });
assert.equal((await second.get(736)).pageviews.months[5].views, 100);
});
test('golden mint URLs: request parameters are byte-identical apart from the added maxlag', async () => {
assert.equal(randomArticlesUrl().toString(), 'https://en.wikipedia.org/w/api.php?action=query&format=json&formatversion=2'
+ '&generator=random&grnnamespace=0&grnlimit=10&prop=extracts%7Cpageimages%7Cinfo%7Ccategories&exintro=1&explaintext=1'
+ '&exchars=900&piprop=thumbnail&pithumbsize=800&cllimit=20');
const urls: string[] = [];
const fetcher: typeof fetch = async input => {
const url = new URL(String(input));
urls.push(url.toString());
if (url.searchParams.get('generator') === 'random') return json({ query: { pages: [{ pageid: 9, ns: 0, title: 'T',
extract: 'An extract that is comfortably longer than forty characters.', lastrevid: 2,
thumbnail: { source: 'https://upload.wikimedia.org/a.jpg' } }] } });
return url.hostname === 'en.wikipedia.org' ? json(metadata(9)) : json(views());
};
const client = new WikimediaClient({ fetch: fetcher, config: UNTHROTTLED });
const article = await new WikipediaArticleSource(client).random();
await new WikipediaEnrichment({ client, now: () => JANUARY }).getFresh(article.pageId);
assert.deepEqual(urls, [
`${randomArticlesUrl()}&maxlag=5`,
'https://en.wikipedia.org/w/api.php?action=query&format=json&formatversion=2&pageids=9'
+ '&prop=info%7Ccategories%7Cpageprops%7Cdescription%7Clanglinks&clshow=%21hidden&cllimit=50&lllimit=500'
+ '&ppprop=wikibase_item&maxlag=5',
'https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia.org/all-access/user/Albert_Einstein/monthly/2024070100/2024120100',
]);
});
test('a throttled upstream stops random() at once instead of retrying three times', async () => {
let calls = 0;
const source = new WikipediaArticleSource(async () => { calls++; return new Response('', { status: 429 }); });
await assert.rejects(source.random(), /HTTP 429/);
assert.equal(calls, 1);
});
+74
View File
@@ -0,0 +1,74 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { MintTemplatePool } from '../server/wikimedia/pool.ts';
import type { Article } from '../shared/protocol.ts';
import type { PageEnrichment } from '../shared/enrichment.ts';
function article(pageId: number): Article {
return { pageId, title: `Page ${pageId}`, extract: 'An extract that is comfortably longer than forty characters.',
image: 'https://upload.wikimedia.org/a.jpg', url: `https://en.wikipedia.org/?curid=${pageId}`, revisionId: pageId + 1,
source: 'live', category: 'Culture' };
}
function enrichment(pageId: number): PageEnrichment {
return { pageId, title: `Page ${pageId}`, description: null, wikidataId: null, revisionId: pageId + 1, pageBytes: 10,
languageCount: 1, languagesTruncated: false, categories: [], categoriesTruncated: false, fetchedAt: '2026-11-17T00:00:00Z',
pageviews: { status: 'available', months: [], latestMonth: '2026-10', total: 1, dailyAverage: 1, trendPercent: null },
stats: { attention: 1, depth: 1, reach: 1 } };
}
function fixture(options: { size?: number; maxAgeMs?: number } = {}) {
let next = 1, now = 0, fail = false;
const log: string[] = [];
const sources = {
articles: { random: async () => { log.push('random'); if (fail) throw new Error('Wikimedia unavailable: HTTP 429'); return article(next++); } },
enrichment: { getFresh: async (id: number) => { log.push(`fresh:${id}`); return enrichment(id); } },
};
const pool = new MintTemplatePool(sources, { now: () => now, ...options });
return { pool, log, advance: (ms: number) => { now += ms; }, setFail: (value: boolean) => { fail = value; } };
}
test('every template on one tip shares one article; a new tip gets a different one', async () => {
const { pool, log } = fixture({ size: 1 });
const first = await Promise.all(Array.from({ length: 20 }, () => pool.take('tip-a')));
assert.ok(first.every(pair => pair.article.pageId === first[0].article.pageId));
await pool.refill();
const requestsForTwentyMiners = log.length;
assert.equal(requestsForTwentyMiners, 4); // One direct pair, one prefetched pair: 2 calls each.
const second = await pool.take('tip-b');
assert.notEqual(second.article.pageId, first[0].article.pageId);
assert.equal(second.article.pageId, 2); // Served from the prefetched pair, no wait.
await pool.refill();
assert.equal(log.length, 6); // ...and that use alone triggers the next prefetch.
// The stats snapshot comes from a mint-path read of the same page.
assert.equal(second.statsSnapshot.revisionId, second.article.pageId + 1);
});
test('pairs older than maxAge are dropped, never served', async () => {
const { pool, advance } = fixture({ size: 1, maxAgeMs: 1000 });
const a = await pool.take('t1');
await pool.refill();
advance(1000);
const b = await pool.take('t1'); // Same tip, but its pair expired; the ready pair expired too.
assert.notEqual(b.article.pageId, a.article.pageId);
assert.equal(pool.status().expired, 1);
});
test('an idle pool makes no requests, and a failure rejects the template without a retry loop', async () => {
const { pool, log, setFail } = fixture({ size: 2 });
assert.equal(log.length, 0);
setFail(true);
await assert.rejects(pool.take('t1'), /429/);
await pool.refill();
assert.equal(log.filter(entry => entry === 'random').length, 2); // The direct attempt and one refill attempt.
setFail(false);
assert.ok((await pool.take('t1')).article.pageId > 0);
});
test('callers get copies they cannot mutate', async () => {
const { pool } = fixture();
const a = await pool.take('t');
a.article.title = 'changed';
assert.equal((await pool.take('t')).article.title.startsWith('Page'), true);
assert.throws(() => new MintTemplatePool({ articles: { random: async () => article(1) },
enrichment: { getFresh: async () => enrichment(1) } }, { size: 99 }), RangeError);
});