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>
56 lines
3.1 KiB
TypeScript
56 lines
3.1 KiB
TypeScript
/**
|
|
* Operator configuration for hosted modes. Everything comes from the operator's own
|
|
* environment and files; nothing here is learned from peers.
|
|
*
|
|
* WIKIDECK_MODES comma list of built-in mode ids and/or JSON file paths
|
|
* (default "classic"; "none" hosts nothing but still discovers)
|
|
* WIKIDECK_MODES_DIR optional directory: every *.json in it is a mode
|
|
* WIKIDECK_GAME_OPERATOR name shown as "Hosted by ..." (default: the origin's host name)
|
|
* WIKIDECK_GAME_NOTE optional short note shown with the host
|
|
* WIKIDECK_GAME_PEERS peer origins whose modes to list (default: WIKIDECK_PEERS)
|
|
* PUBLIC_ORIGIN this node's public origin (the host identity)
|
|
*/
|
|
import { readFileSync, readdirSync } from 'node:fs';
|
|
import { join, resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { HostIdentitySchema, MAX_MODES_PER_NODE, parseModeJson, type HostIdentity, type ModeDocument } from '../../shared/game/modes.ts';
|
|
import { parsePeerOrigins } from './discovery.ts';
|
|
|
|
export const BUILTIN_MODES_DIR = fileURLToPath(new URL('./modes/', import.meta.url));
|
|
export const BUILTIN_MODE_IDS = ['classic', 'trending', 'draft'] as const;
|
|
|
|
export function loadModes(env: NodeJS.ProcessEnv = process.env): ModeDocument[] {
|
|
const modes: ModeDocument[] = [];
|
|
const spec = (env.WIKIDECK_MODES ?? 'classic').trim();
|
|
if (spec !== 'none') for (const item of spec.split(',').map(s => s.trim()).filter(Boolean)) {
|
|
if ((BUILTIN_MODE_IDS as readonly string[]).includes(item)) modes.push(readMode(join(BUILTIN_MODES_DIR, `${item}.json`)));
|
|
else if (item.endsWith('.json')) modes.push(readMode(resolve(item)));
|
|
else throw new Error(`WIKIDECK_MODES: unknown built-in mode "${item.slice(0, 40)}" (use ${BUILTIN_MODE_IDS.join(', ')} or a .json path)`);
|
|
}
|
|
if (env.WIKIDECK_MODES_DIR) {
|
|
const dir = resolve(env.WIKIDECK_MODES_DIR);
|
|
for (const f of readdirSync(dir).filter(f => f.endsWith('.json')).sort()) modes.push(readMode(join(dir, f)));
|
|
}
|
|
if (modes.length > MAX_MODES_PER_NODE) throw new Error(`At most ${MAX_MODES_PER_NODE} modes per node`);
|
|
const ids = new Set<string>();
|
|
for (const m of modes) { if (ids.has(m.id)) throw new Error(`Duplicate mode id ${m.id}`); ids.add(m.id); }
|
|
return modes;
|
|
}
|
|
|
|
function readMode(path: string): ModeDocument {
|
|
try { return parseModeJson(readFileSync(path, 'utf8')); }
|
|
catch (e) { throw new Error(`Invalid mode file ${path}: ${(e as Error).message.slice(0, 300)}`); }
|
|
}
|
|
|
|
export function hostIdentity(env: NodeJS.ProcessEnv = process.env): HostIdentity {
|
|
const origin = new URL(env.PUBLIC_ORIGIN ?? `http://127.0.0.1:${env.PORT ?? 3001}`).origin;
|
|
return HostIdentitySchema.parse({ origin, operator: (env.WIKIDECK_GAME_OPERATOR ?? new URL(origin).host).slice(0, 48),
|
|
...(env.WIKIDECK_GAME_NOTE ? { note: env.WIKIDECK_GAME_NOTE.slice(0, 120) } : {}) });
|
|
}
|
|
|
|
export function gamePeers(env: NodeJS.ProcessEnv = process.env): string[] {
|
|
const self = hostIdentity(env).origin;
|
|
return parsePeerOrigins(env.WIKIDECK_GAME_PEERS ?? env.WIKIDECK_PEERS, env.WIKIDECK_ALLOW_PRIVATE_PEERS === '1')
|
|
.filter(o => o !== self);
|
|
}
|