forked from WikiDeck/wikideck
Pure deterministic off-chain engine in shared/game: createMatch, legalActions, applyAction, isOver, result, viewFor, serialize/deserialize, replay. Placeholder Snap-style lanes rules until DESIGN.md v1; keyword hook registry for engine-b. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
71 lines
3.3 KiB
TypeScript
71 lines
3.3 KiB
TypeScript
/**
|
|
* Ruleset: every rule number as data, so peer-hosted modes can vary them
|
|
* without running code. parseRuleset() is the only way untrusted JSON should
|
|
* become a Ruleset; it enforces bounds that keep matches short and terminating.
|
|
*/
|
|
import { z } from 'zod';
|
|
import { KEYWORDS, LANE_MODIFIERS } from './keywords.ts';
|
|
import type { Ruleset } from './types.ts';
|
|
|
|
const int = (min: number, max: number) => z.number().int().min(min).max(max);
|
|
const params = z.record(z.string().max(32), z.number().finite().min(-100).max(100))
|
|
.refine(value => Object.keys(value).length <= 8, 'Too many params');
|
|
|
|
export const RulesetSchema = z.object({
|
|
id: z.string().regex(/^[a-z0-9][a-z0-9-]{0,47}$/),
|
|
name: z.string().min(1).max(64),
|
|
version: int(1, 1_000_000),
|
|
lanes: int(1, 5),
|
|
laneCapacity: int(1, 8),
|
|
maxTurns: int(1, 12),
|
|
deck: z.object({ min: int(1, 40), max: int(1, 40), maxCopiesPerPage: int(1, 40) }).strict(),
|
|
hand: z.object({ start: int(0, 10), max: int(1, 12), drawPerTurn: int(0, 3) }).strict(),
|
|
energy: z.object({ start: int(0, 20), perTurn: int(0, 5), max: int(1, 20) }).strict(),
|
|
keywords: z.record(z.string(), params).refine(value => Object.keys(value).length <= 32, 'Too many keywords'),
|
|
laneModifiers: z.object({
|
|
pool: z.array(z.string()).max(32),
|
|
revealTurns: z.array(int(1, 12)).max(5),
|
|
params: z.record(z.string(), params),
|
|
}).strict(),
|
|
tieBreak: z.enum(['totalPower', 'draw']),
|
|
}).strict().superRefine((rules, ctx) => {
|
|
const issue = (message: string) => ctx.addIssue({ code: 'custom', message });
|
|
if (rules.deck.min > rules.deck.max) issue('deck.min > deck.max');
|
|
if (rules.hand.start > rules.hand.max) issue('hand.start > hand.max');
|
|
if (rules.energy.start > rules.energy.max) issue('energy.start > energy.max');
|
|
if (rules.laneModifiers.revealTurns.length !== rules.lanes) issue('laneModifiers.revealTurns must have one entry per lane');
|
|
if (rules.laneModifiers.pool.length && rules.laneModifiers.pool.length < rules.lanes) issue('laneModifiers.pool needs at least one modifier per lane (or be empty)');
|
|
for (const id of Object.keys(rules.keywords)) if (!Object.hasOwn(KEYWORDS, id)) issue(`Unknown keyword: ${id}`);
|
|
for (const id of [...rules.laneModifiers.pool, ...Object.keys(rules.laneModifiers.params)])
|
|
if (!Object.hasOwn(LANE_MODIFIERS, id)) issue(`Unknown lane modifier: ${id}`);
|
|
});
|
|
|
|
/** Validate untrusted ruleset JSON. Throws a ZodError with readable issues. */
|
|
export function parseRuleset(input: unknown): Ruleset {
|
|
return RulesetSchema.parse(input) as Ruleset;
|
|
}
|
|
|
|
/** Placeholder base rules (Snap-style lanes) until DESIGN.md v1 lands. */
|
|
export const DEFAULT_RULESET: Ruleset = {
|
|
id: 'wikideck-standard',
|
|
name: 'WikiDeck Standard',
|
|
version: 1,
|
|
lanes: 3,
|
|
laneCapacity: 4,
|
|
maxTurns: 6,
|
|
deck: { min: 12, max: 12, maxCopiesPerPage: 2 },
|
|
hand: { start: 3, max: 7, drawPerTurn: 1 },
|
|
energy: { start: 1, perTurn: 1, max: 6 },
|
|
keywords: { rally: { amount: 1 }, anchor: { amount: 2 } },
|
|
laneModifiers: {
|
|
pool: ['plain', 'portal', 'narrow'],
|
|
revealTurns: [1, 2, 3],
|
|
params: { portal: { categoryIndex: 0, amount: 2 }, narrow: { maxCost: 3 } },
|
|
},
|
|
tieBreak: 'totalPower',
|
|
};
|
|
|
|
export function energyForTurn(rules: Ruleset, turn: number): number {
|
|
return Math.min(rules.energy.max, rules.energy.start + (turn - 1) * rules.energy.perTurn);
|
|
}
|