forked from WikiDeck/wikideck
- keywords.ts: viral, rising, local, encyclopedic, nostalgic, translated (v1); edition, obscure, wildcard (off by default); §10.3 Portals with capacity, allOnTopic and topicBonus hooks; isOnTopic, keywordEnabled, hiddenFrom - ruleset.ts: §10.3 default Topic pool, keyword/portal param defaults - starter.ts: placeholder loaners carry their faction keyword (§10.1) - derive.ts: percentile -> fame -> cost -> power, home, trend, keyword choice - tests: game-keywords, game-derive, game-property, game-golden (§12, todo until core implements the frozen §3-§7 rules) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
6.0 KiB
TypeScript
125 lines
6.0 KiB
TypeScript
/**
|
||
* 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 §10.3: 9 Topics, one plain and two with Portals per stat. A Portal's id is its Topic's id. */
|
||
export const DEFAULT_TOPIC_POOL: Ruleset['topicPool'] = [
|
||
{ id: 'current-events', name: 'Current Events', stat: 'attention', portal: null },
|
||
{ id: 'main-page', name: 'Main Page', stat: 'attention', portal: 'main-page' },
|
||
{ id: 'trending', name: 'Trending', stat: 'attention', portal: 'trending' },
|
||
{ id: 'archive', name: 'Archive', stat: 'depth', portal: null },
|
||
{ id: 'featured-article', name: 'Featured Article', stat: 'depth', portal: 'featured-article' },
|
||
{ id: 'stub', name: 'Stub', stat: 'depth', portal: 'stub' },
|
||
{ id: 'translations', name: 'Translations', stat: 'reach', portal: null },
|
||
{ id: 'interwiki', name: 'Interwiki', stat: 'reach', portal: 'interwiki' },
|
||
{ id: 'disambiguation', name: 'Disambiguation', stat: 'reach', portal: 'disambiguation' },
|
||
];
|
||
|
||
/** DESIGN §10.2 keywordParams defaults. */
|
||
export const DEFAULT_KEYWORD_PARAMS: Ruleset['keywordParams'] = {
|
||
viral: { amount: 1, cap: 2, powerDelta: 0 }, rising: { amount: 1, cap: 2, powerDelta: 0 },
|
||
local: { amount: 1, powerDelta: 0 }, encyclopedic: { amount: 2, powerDelta: 0 },
|
||
nostalgic: { amount: 2, powerDelta: 0 }, translated: { amount: 1, powerDelta: 0 },
|
||
edition: { amount: 2, powerDelta: 0 }, obscure: { powerDelta: 0 }, wildcard: { powerDelta: 0 },
|
||
};
|
||
|
||
/** DESIGN §10.3 portalParams defaults. */
|
||
export const DEFAULT_PORTAL_PARAMS: Ruleset['portalParams'] = {
|
||
'main-page': { amount: 1 }, trending: { factor: 2 }, 'featured-article': { amount: 2 }, stub: { value: 2 }, interwiki: { amount: 1 },
|
||
};
|
||
|
||
/** 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: DEFAULT_KEYWORD_PARAMS,
|
||
portalParams: DEFAULT_PORTAL_PARAMS,
|
||
};
|