Files
wikideck/server/game/engine-binding.ts
T
claudeandClaude Opus 5.5 7fef46e38e Peer-hosted game modes: mode schema, node API, host credit, three example modes
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>
2026-11-16 23:00:17 -05:00

46 lines
2.2 KiB
TypeScript

/** Binds the mode host to the real engine through its public API (shared/game/index.ts). */
import { DEFAULT_RULESET, parseRuleset, starterDeck, starterCard, STARTER_POOL, validateDeck, createMatch,
legalActions, applyAction, isOver, result, viewFor, Rng,
type Action, type GameCard, type MatchState, type Ruleset } from '../../shared/game/index.ts';
import type { EnginePort, Seat } from './engine-port.ts';
/**
* Deterministic pool of preview (non-NFT) cards for draft modes: seeded shuffles of the
* starter pool, one copy per round, never exceeding the ruleset's per-page copy limit.
*/
export function previewPool(rules: Ruleset, seed: string, size: number): GameCard[] {
const rng = Rng.fromSeed(`draft-pool:${seed}`);
const pool: GameCard[] = [];
for (let copy = 0; pool.length < size && copy < rules.deck.maxCopiesPerPage; copy++) {
for (const spec of rng.shuffle(STARTER_POOL)) { if (pool.length >= size) break; pool.push(starterCard(spec, copy)); }
}
return pool;
}
export const engineBinding: EnginePort<Ruleset, GameCard, MatchState, Action> = {
defaultRuleset: DEFAULT_RULESET,
parseRuleset,
deckSize: rules => rules.deck.min,
starterDeck: (rules, seed) => starterDeck(seed, rules),
previewPool,
validateDeck: (rules, cards) => {
const problems = validateDeck(rules, cards);
if (problems.length) throw new Error(problems.slice(0, 3).join('; '));
},
cardPower: card => card.power,
withPower: (card, power) => ({ ...card, power }),
cardPageId: card => card.pageId,
cardSummary: card => ({ id: card.id, name: card.name, pageId: card.pageId, category: card.category,
cost: card.cost, power: card.power, keyword: card.keyword }),
createMatch: (rules, decks, seed) => createMatch(rules, decks, seed),
legalActions: (state, seat) => legalActions(state, seat),
applyAction: (state, seat: Seat, action) => {
if (action.player !== seat) throw new Error('Action for the wrong seat');
return applyAction(state, action);
},
concedeAction: seat => ({ type: 'concede', player: seat }),
isOver,
result: state => { const r = result(state); return { winner: r?.winner ?? null, reason: r?.reason ?? 'complete' }; },
viewFor: (state, seat) => viewFor(state, seat),
};