Night 2 (2026-11-17), from the `claude/wikideck` fork's `night/2026-11-17` branch. It follows Night 1 (#22).
## What's in it
- **Duel engine** (fork #72, #80): 40-card decks of cards you own, Notability/Citations, keywords, effects T1–T12, Connected, a hidden-information-safe `viewFor`/`logFor`, and replay. Balance v1 tuning (#84) gives the first player about 51%.
- **Card profiles** (#70, #82): Figure, Location, Event, Institution or Doctrine, plus stats, keywords and rules text, all derived from real article data. Templates only.
- **Design and story** (#66, #69, #83): DUEL_DESIGN.md, plus the match log and the encyclopedic "Battle of X (2026)" recap.
- **PvP** (#74, #77, #81, #76, #85):
- The server checks ownership, and 40-card eligibility is enforced.
- Modes: Classic (ranked), Draft and Trending.
- Invites, quick match, Elo, a ladder and recap pages. Online matches render on the duel board.
- **Duel board UI** (#79): MTG-style card faces and combat declaration, built for 360 px first.
- **Polite Wikimedia client** (#73, #75): throttled, cached and identified requests, the mint-path fix, and a prefetch pool.
- **Android mining** (#78): native RandomX, as a sideload build. The emulator mined a testnet block.
- **Testnet ops** (#67, #68).
PR numbers refer to `claude/wikideck`.
## Status
- **Tests:** 423, of which 422 pass, 0 fail and 1 is skipped. The build is OK (`node:24-alpine`, `--network none`).
- **Testnet:** running this commit (036489c) on r350.
- **Untouched:** no changes to `server/testnet/consensus.ts`, `randomx.ts`, `native/` or the mainnet compose.
- **Review:** night-review reviewed fork #66–#79. #80–#85 came from the agents' unfinished branches and were not reviewed.
- **Known gaps:**
- The PvP end-to-end test hasn't been run (it needs two eom.dev accounts).
- Bot matches aren't reachable from Play.
- Card identity pre-bake hasn't been done.
- The RandomX verifier latch (`server/testnet/randomx.ts:140`) is still open.
This PR is for Eric to merge.
🤖 Generated with [Claude Code](https://claude.com/claude-code)Reviewed-on: #23
Co-authored-by: Claude <claude@eom.dev>
114 lines
6.9 KiB
TypeScript
114 lines
6.9 KiB
TypeScript
// Live IdentitySource for server/game/identity.ts (engineer WS E, data §3).
|
|
//
|
|
// Every request goes through the process-wide polite Wikimedia client on the **background** lane:
|
|
// - the page enrichment (description, wikidataId, categories) via `enrichment.getBackground()`,
|
|
// which also feeds the disk cache card faces use;
|
|
// - one `wbgetentities&props=claims` per batch of at most 50 items;
|
|
// - one `wbgetentities&props=labels` per 50 class ids still missing a label.
|
|
// That is about 2 Wikidata requests per 50 new cards, plus one cached enrichment per card.
|
|
// Outlinks (prop=links, data §4) are not fetched tonight: they cost about 1.2 requests per card,
|
|
// and Connected falls back to its other tiers without them.
|
|
//
|
|
// The profile itself is built by duel-cards' pure `profileFromSources()` (shared/duel/profile.ts, #70),
|
|
// so the frozen identity and what derive() reads cannot drift apart. That module, and the client
|
|
// (server/wikimedia, #73), are injected structurally, so this file has no import from unmerged PRs.
|
|
// Wiring, once both have merged:
|
|
// wikimediaIdentitySource({ client: wikimediaClient, enrichment: wikipediaEnrichment,
|
|
// profile: await import('../../shared/duel/profile.ts') })
|
|
// Any transport failure leaves the affected cards unknown (undefined), so they are retried later,
|
|
// never frozen half-empty.
|
|
import type { PageEnrichment } from '../../shared/enrichment.ts';
|
|
import type { ChainArticle, IdentityFacts, IdentitySource, WikidataClass } from './identity.ts';
|
|
|
|
/** Structural slices of server/wikimedia/client.ts (#73) and shared/duel/profile.ts (#70). */
|
|
export type BackgroundRequest = { lane: 'background'; timeoutMs: number; cache?: { ttlMs: number; maxBytes?: number } };
|
|
export type WikidataEntity = { id?: string; claims?: Record<string, readonly {
|
|
rank?: string; mainsnak?: { snaktype?: string; datavalue?: { type?: string; value?: unknown } } }[]> };
|
|
export type ProfileLike = { wikidataId: string | null; description: string | null; categories: readonly string[];
|
|
instanceOf: readonly WikidataClass[]; subclassOf?: readonly WikidataClass[]; countries?: readonly string[];
|
|
years?: readonly number[]; died?: boolean; links?: readonly number[]; cites?: readonly string[]; fetchedRevisionId?: number };
|
|
export type ProfileBuilder = {
|
|
wikidataEntityParams(qids: readonly string[]): Record<string, string>;
|
|
wikidataLabelParams(qids: readonly string[]): Record<string, string>;
|
|
classIdsOf(entity: WikidataEntity | null | undefined): string[];
|
|
profileFromSources(src: { enrichment: PageEnrichment; entity?: WikidataEntity | null; labels?: Readonly<Record<string, string>> }): ProfileLike;
|
|
};
|
|
export type IdentitySourceDeps = {
|
|
enrichment: { getBackground(pageId: number): Promise<PageEnrichment> };
|
|
client: { request(url: URL, options: BackgroundRequest): Promise<{ response: Response }> };
|
|
profile: ProfileBuilder;
|
|
};
|
|
|
|
const WIKIDATA_API = 'https://www.wikidata.org/w/api.php';
|
|
const MAX_BYTES = 4 * 1024 * 1024;
|
|
const CACHE = { ttlMs: 7 * 24 * 60 * 60 * 1000, maxBytes: MAX_BYTES };
|
|
|
|
async function wikidata(client: IdentitySourceDeps['client'], params: Record<string, string>): Promise<Record<string, unknown>> {
|
|
const url = new URL(WIKIDATA_API);
|
|
for (const [k, v] of Object.entries(params)) url.searchParams.set(k, v);
|
|
const { response } = await client.request(url, { lane: 'background', timeoutMs: 15000, cache: CACHE });
|
|
if (!response.ok) { await response.body?.cancel(); throw new Error(`Wikidata HTTP ${response.status}`); }
|
|
const text = await response.text();
|
|
if (text.length > MAX_BYTES) throw new Error('Wikidata response too large');
|
|
const body = JSON.parse(text) as { entities?: Record<string, unknown> | unknown[] };
|
|
const entities = body.entities ?? {};
|
|
// formatversion=2 returns an object keyed by id; tolerate an array too.
|
|
return Array.isArray(entities)
|
|
? Object.fromEntries(entities.filter(e => e && typeof (e as { id?: unknown }).id === 'string').map(e => [(e as { id: string }).id, e]))
|
|
: entities as Record<string, unknown>;
|
|
}
|
|
|
|
const chunks = <T>(xs: T[], n: number) => Array.from({ length: Math.ceil(xs.length / n) }, (_, i) => xs.slice(i * n, i * n + n));
|
|
|
|
export function wikimediaIdentitySource(deps: IdentitySourceDeps): IdentitySource {
|
|
const { classIdsOf, profileFromSources, wikidataEntityParams, wikidataLabelParams } = deps.profile;
|
|
return {
|
|
kind: 'wikimedia',
|
|
async lookup(articles: ChainArticle[]) {
|
|
const out = new Map<number, IdentityFacts | undefined>(articles.map(a => [a.pageId, undefined]));
|
|
// 1. Enrichment, one page at a time (the client paces it); failures stay unknown.
|
|
const enriched = new Map<number, PageEnrichment>();
|
|
for (const a of articles) {
|
|
try { enriched.set(a.pageId, await deps.enrichment.getBackground(a.pageId)); } catch { /* retry later */ }
|
|
}
|
|
// 2. Claims for every item we know, in batches of ≤ 50.
|
|
const qids = [...new Set([...enriched.values()].map(e => e.wikidataId).filter((q): q is string => !!q && /^Q\d+$/.test(q)))];
|
|
const entities = new Map<string, WikidataEntity>();
|
|
let claimsFailed = false;
|
|
for (const batch of chunks(qids, 50)) {
|
|
try { for (const [id, e] of Object.entries(await wikidata(deps.client, wikidataEntityParams(batch)))) entities.set(id, e as WikidataEntity); }
|
|
catch { claimsFailed = true; }
|
|
}
|
|
// 3. English labels for the P31/P279 classes.
|
|
const classIds = [...new Set([...entities.values()].flatMap(e => classIdsOf(e)))];
|
|
const labels: Record<string, string> = {};
|
|
let labelsFailed = false;
|
|
for (const batch of chunks(classIds, 50)) {
|
|
try {
|
|
for (const [id, e] of Object.entries(await wikidata(deps.client, wikidataLabelParams(batch)))) {
|
|
const v = (e as { labels?: { en?: { value?: unknown } } }).labels?.en?.value;
|
|
if (typeof v === 'string') labels[id] = v;
|
|
}
|
|
} catch { labelsFailed = true; }
|
|
}
|
|
// 4. Freeze only complete profiles: a card whose item couldn't be read stays unknown.
|
|
for (const a of articles) {
|
|
const e = enriched.get(a.pageId);
|
|
if (!e) continue;
|
|
const qid = e.wikidataId && /^Q\d+$/.test(e.wikidataId) ? e.wikidataId : null;
|
|
const entity = qid ? entities.get(qid) : null;
|
|
if (qid && !entity && claimsFailed) continue; // item not read: retry later
|
|
if (entity && labelsFailed) continue; // class labels not read: retry later
|
|
const p = profileFromSources({ enrichment: e, entity: entity ?? null, labels });
|
|
out.set(a.pageId, {
|
|
fetchedRevisionId: p.fetchedRevisionId ?? null, wikidataId: p.wikidataId, description: p.description,
|
|
categories: [...p.categories], instanceOf: [...p.instanceOf], subclassOf: [...(p.subclassOf ?? [])],
|
|
countries: [...(p.countries ?? [])], years: [...(p.years ?? [])], died: p.died === true,
|
|
links: [...(p.links ?? [])], cites: [...(p.cites ?? [])],
|
|
});
|
|
}
|
|
return out;
|
|
},
|
|
};
|
|
}
|