forked from WikiDeck/wikideck
Modes are inert JSON (shared/game/modes.ts): rule overrides on the engine's DEFAULT_RULESET, re-validated by parseRuleset; card-mapping presets from a fixed list; deck policy and limits. server/game/ publishes this node's modes, discovers configured peers' modes as data (byte cap, timeout, pinned origin), and hosts lobbies, snake drafts and matches with the deterministic engine. Finished matches are off-chain JSONL records with seed, decks and action log (replayable). Per-mode and host leaderboards. Docs in docs/game/HOSTING.md, including security notes and an on-chain host-reward proposal. One mount line in server/testnet/http.ts; no consensus changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
43 lines
2.2 KiB
TypeScript
43 lines
2.2 KiB
TypeScript
/**
|
|
* Entry point for the game-modes API. server/testnet/http.ts mounts it with one line:
|
|
* app.use('/api/game', await (await import('../game/index.ts')).gameRouter({ dataDir, enrichment: wikipediaEnrichment }));
|
|
*/
|
|
import { join } from 'node:path';
|
|
import type express from 'express';
|
|
import { loadModes, hostIdentity, gamePeers } from './config.ts';
|
|
import { ModeDiscovery } from './discovery.ts';
|
|
import { ModeHost } from './host.ts';
|
|
import { createGameRouter } from './router.ts';
|
|
import { MatchStore } from './store.ts';
|
|
import type { EnginePort } from './engine-port.ts';
|
|
import { TrendCache } from './trends.ts';
|
|
|
|
type Enrichment = { get(pageId: number): Promise<{ pageviews: { trendPercent: number | null } }> };
|
|
|
|
export async function gameRouter(options: { dataDir: string | null; enrichment?: Enrichment;
|
|
engine?: EnginePort<any, any, any, any>; env?: NodeJS.ProcessEnv; warmTrends?: boolean }): Promise<express.Router> {
|
|
const env = options.env ?? process.env;
|
|
const engine = options.engine ?? (await import('./engine-binding.ts')).engineBinding;
|
|
const modes = loadModes(env);
|
|
// Trends come from a slowly warmed, cache-only store: never a Wikipedia call per match.
|
|
let trends: TrendCache | undefined;
|
|
if (options.enrichment && modes.some(m => m.cards.preset === 'trending')) {
|
|
const enrichment = options.enrichment;
|
|
trends = new TrendCache(pageId => enrichment.get(pageId));
|
|
for (const card of engine.starterDeck(engine.defaultRuleset, 'trend-warmup')) {
|
|
const id = engine.cardPageId(card); if (id !== null) trends.enqueue(id);
|
|
}
|
|
for (const card of engine.previewPool(engine.defaultRuleset, 'trend-warmup', 64)) {
|
|
const id = engine.cardPageId(card); if (id !== null) trends.enqueue(id);
|
|
}
|
|
if (options.warmTrends !== false) trends.start();
|
|
}
|
|
const host = new ModeHost({ identity: hostIdentity(env), modes, engine,
|
|
store: new MatchStore(options.dataDir ? join(options.dataDir, 'game') : null),
|
|
liveData: trends ? async pageId => trends!.get(pageId) : undefined });
|
|
const discovery = new ModeDiscovery({ peers: gamePeers(env) });
|
|
const timer = setInterval(() => host.sweep(), 5_000);
|
|
timer.unref();
|
|
return createGameRouter(host, discovery);
|
|
}
|