Files
wikideck/shared/game/ruleset.ts
T
claudeandClaude Opus 5.5 f3b6097640 Engine core: align with DESIGN.md draft (Edit War)
mulberry32 uint32 seed with the §4 draw order, Topics with stats and Portals,
staggered reveal, initiative and underdog rules, queue/undo/commit/timeout
actions, §9 flat RulesConfig with zod bounds, viewFor hidden info, replay.
cards.ts left to engine-b; placeholder loaners in starter.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 22:55:51 -05:00

112 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Ruleset (DESIGN §9 RulesConfig): 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; its bounds keep matches short and
* guarantee termination.
*/
import { z } from 'zod';
import { KEYWORDS, PORTALS } from './keywords.ts';
import { STATS, type Ruleset } from './types.ts';
const int = (min: number, max: number) => z.number().int().min(min).max(max);
const id = z.string().regex(/^[a-z0-9][a-z0-9-]{0,31}$/);
const params = z.record(z.string().regex(/^[A-Za-z0-9]{1,24}$/), z.number().finite().min(-100).max(100))
.refine(value => Object.keys(value).length <= 8, 'At most 8 params');
const paramTable = z.record(id, params).refine(value => Object.keys(value).length <= 32, 'At most 32 entries');
export const TopicSchema = z.object({
id, name: z.string().min(1).max(32), stat: z.enum(STATS as [string, ...string[]]), portal: id.nullable(),
}).strict();
export const RulesetSchema = z.object({
version: z.string().min(1).max(16),
deckSize: int(1, 30),
maxCopiesPerPage: int(1, 30),
maxLoaners: int(0, 30),
openingHand: int(0, 10),
handLimit: int(1, 12),
mulligan: z.literal(false), // not implemented in v1
turns: int(1, 12),
turnSeconds: int(5, 120),
afkTurns: int(1, 12),
energyByTurn: z.array(int(0, 20)).min(1).max(12),
bonusEnergy: int(0, 10),
underdog: z.boolean(),
topicCount: int(1, 5),
distinctStats: z.boolean(),
revealSchedule: z.array(int(1, 12)).max(5),
laneCapacity: int(1, 8),
topicPool: z.array(TopicSchema).min(1).max(32),
costThresholds: z.array(int(0, 100)).length(5),
powerByCost: z.array(int(0, 30)).length(7),
onTopicBonus: int(0, 10),
trendUp: z.number().finite().min(-1000).max(1000),
trendDown: z.number().finite().min(-1000).max(1000),
factionSynergyCap: int(0, 12),
verifyStats: z.boolean(),
percentiles: id,
starterDecks: id,
keywordParams: paramTable,
portalParams: paramTable,
}).strict().superRefine((rules, ctx) => {
const issue = (message: string) => ctx.addIssue({ code: 'custom', message });
if (rules.turns * rules.turnSeconds > 240) issue('turns × turnSeconds must be ≤ 240');
if (rules.energyByTurn.length < rules.turns) issue('energyByTurn needs one entry per turn');
if (rules.revealSchedule.length !== rules.topicCount) issue('revealSchedule needs one entry per Topic');
if (rules.openingHand > rules.handLimit) issue('openingHand > handLimit');
if (rules.trendDown > rules.trendUp) issue('trendDown > trendUp');
if (rules.costThresholds.some((value, i) => i > 0 && value < rules.costThresholds[i - 1])) issue('costThresholds must ascend');
if (rules.distinctStats) {
if (rules.topicCount > STATS.length) issue('distinctStats allows at most 3 Topics');
const stats = new Set(rules.topicPool.map(topic => topic.stat));
if (stats.size < rules.topicCount) issue('topicPool lacks enough distinct stats');
} else if (rules.topicPool.length < rules.topicCount) issue('topicPool smaller than topicCount');
const topicIds = new Set<string>();
for (const topic of rules.topicPool) {
if (topicIds.has(topic.id)) issue(`Duplicate topic id: ${topic.id}`);
topicIds.add(topic.id);
if (topic.portal && !Object.hasOwn(PORTALS, topic.portal)) issue(`Unknown portal: ${topic.portal}`);
}
for (const key of Object.keys(rules.keywordParams)) if (!Object.hasOwn(KEYWORDS, key)) issue(`Unknown keyword: ${key}`);
for (const key of Object.keys(rules.portalParams)) if (!Object.hasOwn(PORTALS, key)) issue(`Unknown portal: ${key}`);
});
/** Validate untrusted ruleset JSON. Throws a ZodError with readable issues. */
export function parseRuleset(input: unknown): Ruleset {
return RulesetSchema.parse(input) as Ruleset;
}
/** DESIGN §3: 9 Topics, each stat 3 times: one plain and two with Portals (portals are placeholders until §10.3). */
export const DEFAULT_TOPIC_POOL: Ruleset['topicPool'] = [
{ id: 'front-page', name: 'Front Page', stat: 'attention', portal: null },
{ id: 'trending', name: 'Trending', stat: 'attention', portal: 'spotlight' },
{ id: 'viral', name: 'Viral', stat: 'attention', portal: 'paywall' },
{ id: 'deep-dive', name: 'Deep Dive', stat: 'depth', portal: null },
{ id: 'archive', name: 'Archive', stat: 'depth', portal: 'spotlight' },
{ id: 'citation-needed', name: 'Citation Needed', stat: 'depth', portal: 'paywall' },
{ id: 'world-tour', name: 'World Tour', stat: 'reach', portal: null },
{ id: 'translation', name: 'Translation', stat: 'reach', portal: 'spotlight' },
{ id: 'interwiki', name: 'Interwiki', stat: 'reach', portal: 'paywall' },
];
/** DESIGN §9 defaults. */
export const DEFAULT_RULESET: Ruleset = {
version: '1.0',
deckSize: 12, maxCopiesPerPage: 2, maxLoaners: 12,
openingHand: 3, handLimit: 7, mulligan: false,
turns: 6, turnSeconds: 25, afkTurns: 2,
energyByTurn: [1, 2, 3, 4, 5, 6], bonusEnergy: 0, underdog: true,
topicCount: 3, distinctStats: true, revealSchedule: [1, 2, 3], laneCapacity: 4,
topicPool: DEFAULT_TOPIC_POOL,
costThresholds: [15, 35, 55, 75, 90],
powerByCost: [0, 1, 2, 4, 5, 7, 9],
onTopicBonus: 2,
trendUp: 25, trendDown: -25,
factionSynergyCap: 3,
verifyStats: true,
percentiles: 'card-sample-2026-11-16',
starterDecks: 'default',
keywordParams: { rally: { amount: 1 }, anchor: { amount: 2 } },
portalParams: { spotlight: { amount: 1 }, paywall: { maxCost: 3 } },
};