forked from WikiDeck/wikideck
79 lines
2.6 KiB
TypeScript
79 lines
2.6 KiB
TypeScript
/**
|
|
* Duel ruleset: every rule number as data (BRIEF "Game rules core").
|
|
* parseDuelRuleset() is the only way untrusted JSON (mode files, peer hosts)
|
|
* becomes a DuelRuleset. Night 1 Edit War rulesets are rejected cleanly.
|
|
*/
|
|
import { z } from 'zod';
|
|
import type { DuelRuleset } from './types.ts';
|
|
|
|
const int = (min: number, max: number) => z.number().int().min(min).max(max);
|
|
|
|
export const DuelRulesetSchema = z.object({
|
|
engine: z.literal('duel'),
|
|
version: z.string().min(1).max(16),
|
|
deckSize: int(1, 60),
|
|
maxCopiesPerPage: int(1, 60),
|
|
startingLife: int(1, 100),
|
|
openingHand: int(0, 10),
|
|
handSize: int(1, 15),
|
|
mulligans: int(0, 3),
|
|
firstPlayerDraws: z.boolean(),
|
|
secondPlayerCitations: int(0, 5),
|
|
maxCitations: int(1, 20),
|
|
citationsPerTurn: int(1, 5),
|
|
territoryCap: int(0, 10),
|
|
locationsPerTurn: int(0, 10),
|
|
libraryEmpty: z.enum(['lose', 'reshuffle', 'fatigue']),
|
|
maxKeywords: int(0, 4),
|
|
stubMax: int(0, 10),
|
|
trendingPower: int(0, 5),
|
|
connectedTiers: z.array(z.enum(['Linked', 'Compatriot', 'Contemporary', 'Kin'])).max(4)
|
|
.refine(v => new Set(v).size === v.length, 'Duplicate tier'),
|
|
connectedBonus: int(0, 5),
|
|
linkedBonus: int(0, 5),
|
|
turnSeconds: int(5, 600),
|
|
bankSeconds: int(0, 3600),
|
|
timeoutsToConcede: int(1, 20),
|
|
turnCap: int(2, 200).nullable(),
|
|
}).strict();
|
|
|
|
export const DEFAULT_DUEL_RULESET: DuelRuleset = Object.freeze({
|
|
engine: 'duel',
|
|
version: 'duel-1',
|
|
deckSize: 40,
|
|
maxCopiesPerPage: 4,
|
|
startingLife: 20,
|
|
openingHand: 7,
|
|
handSize: 7,
|
|
mulligans: 1,
|
|
firstPlayerDraws: false,
|
|
secondPlayerCitations: 0,
|
|
maxCitations: 10,
|
|
citationsPerTurn: 1,
|
|
territoryCap: 2,
|
|
locationsPerTurn: 1,
|
|
libraryEmpty: 'lose',
|
|
maxKeywords: 2,
|
|
stubMax: 2,
|
|
trendingPower: 1,
|
|
connectedTiers: ['Linked', 'Compatriot', 'Contemporary', 'Kin'],
|
|
connectedBonus: 1,
|
|
linkedBonus: 2,
|
|
turnSeconds: 60,
|
|
bankSeconds: 300,
|
|
timeoutsToConcede: 3,
|
|
turnCap: null,
|
|
}) as DuelRuleset;
|
|
|
|
/** Validate a full ruleset. Throws (ZodError) on anything invalid or unknown. */
|
|
export function parseDuelRuleset(value: unknown): DuelRuleset {
|
|
return DuelRulesetSchema.parse(value) as DuelRuleset;
|
|
}
|
|
|
|
/** Merge a partial override (a mode file's `rules`) onto the defaults, then validate. */
|
|
export function mergeDuelRuleset(overrides: unknown, base: DuelRuleset = DEFAULT_DUEL_RULESET): DuelRuleset {
|
|
if (overrides === undefined || overrides === null) return parseDuelRuleset(structuredClone(base));
|
|
if (typeof overrides !== 'object' || Array.isArray(overrides)) throw new TypeError('Ruleset overrides must be an object');
|
|
return parseDuelRuleset({ ...structuredClone(base), ...(overrides as Record<string, unknown>) });
|
|
}
|