forked from WikiDeck/wikideck
57 lines
3.2 KiB
TypeScript
57 lines
3.2 KiB
TypeScript
// testnet-ops (engineer WS E): pre-bake frozen card identities for every card on a chain.
|
|
//
|
|
// node --experimental-strip-types scripts/night2/prebake-identity.ts --node http://192.168.1.32:3101 \
|
|
// --store $WIKIDECK_DATA_DIR/card-identity.sqlite [--cards docs/game/card-sample.json] [--batch 50]
|
|
//
|
|
// - Reads the canonical chain from ONE node (GET /api/summary, then /api/canonical pages), so every
|
|
// card is built from its mint block, never from client JSON.
|
|
// - Facts come ONLY from cached card files (`--cards`, repeatable). This script never calls
|
|
// Wikipedia or Wikidata itself. Live facts must go through the wikimedia team's process-wide client
|
|
// on its `background` lane (server/wikimedia, #62); that IdentitySource plugs into `prebake()` in
|
|
// server/game/identity.ts once it has merged. Until then, unknown cards are left unfrozen and are
|
|
// picked up by a later run.
|
|
// - Idempotent and freeze-at-first-sight: already frozen tokens are skipped and never rewritten.
|
|
import { readFileSync } from 'node:fs';
|
|
import { parseArgs } from 'node:util';
|
|
import { IdentityStore, cachedSource, identityStoreFile, prebake, type ChainArticle } from '../../server/game/identity.ts';
|
|
|
|
const { values } = parseArgs({ options: {
|
|
node: { type: 'string', default: 'http://192.168.1.32:3101' },
|
|
store: { type: 'string', default: identityStoreFile() },
|
|
cards: { type: 'string', multiple: true, default: ['docs/game/card-sample.json'] },
|
|
batch: { type: 'string', default: '50' },
|
|
} });
|
|
const nodeUrl = new URL(values.node!);
|
|
if (!['http:', 'https:'].includes(nodeUrl.protocol)) throw new Error('--node must be an http(s) WikiDeck node URL');
|
|
if (/wikipedia\.org|wikidata\.org|wikimedia\.org/i.test(nodeUrl.hostname)) throw new Error('--node must be a WikiDeck node, not Wikimedia');
|
|
|
|
async function get<T>(path: string): Promise<T> {
|
|
const r = await fetch(new URL(path, nodeUrl), { signal: AbortSignal.timeout(30000), redirect: 'error' });
|
|
if (!r.ok) throw new Error(`${path}: HTTP ${r.status}`);
|
|
return await r.json() as T;
|
|
}
|
|
|
|
type Block = { tokenId: string; article: { pageId: number; title: string; revisionId?: number } };
|
|
async function chainArticles(): Promise<ChainArticle[]> {
|
|
const { tip } = await get<{ tip: string }>('/api/summary');
|
|
const out: ChainArticle[] = [];
|
|
for (let offset: number | null = 0; offset !== null;) {
|
|
const page: { blocks: Block[]; next: number | null } = await get(`/api/canonical?tip=${tip}&offset=${offset}&limit=16`);
|
|
for (const b of page.blocks) if (b.article && b.tokenId)
|
|
out.push({ tokenId: b.tokenId, pageId: b.article.pageId, title: b.article.title, revisionId: b.article.revisionId ?? null });
|
|
offset = page.next;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
const cards = values.cards!.flatMap(f => {
|
|
const d = JSON.parse(readFileSync(f, 'utf8'));
|
|
return Array.isArray(d) ? d : d.cards ?? [];
|
|
});
|
|
const articles = await chainArticles();
|
|
const store = new IdentityStore(values.store!);
|
|
try {
|
|
const report = await prebake(store, cachedSource(cards), articles, { batchSize: Number(values.batch) });
|
|
console.log(JSON.stringify({ node: nodeUrl.origin, chainCards: articles.length, cachedCards: cards.length, ...report, stored: store.count() }));
|
|
} finally { store.close(); }
|