boardgame-core/src/core/rule.ts

165 lines
4.7 KiB
TypeScript
Raw Normal View History

2026-04-01 22:31:07 +08:00
import {Command, CommandSchema, parseCommand, parseCommandSchema} from "../utils/command";
2026-04-01 22:20:38 +08:00
export type RuleState = 'running' | 'yielded' | 'waiting' | 'done';
export type RuleContext<T = unknown> = {
type: string;
schema?: CommandSchema;
generator: Generator<string | CommandSchema, T, Command>;
parent?: RuleContext<unknown>;
children: RuleContext<unknown>[];
state: RuleState;
2026-04-01 13:36:16 +08:00
resolution?: T;
}
2026-04-01 22:20:38 +08:00
export type RuleDef<T = unknown> = {
schema: CommandSchema;
create: (cmd: Command) => Generator<string | CommandSchema, T, Command>;
};
2026-04-01 17:34:21 +08:00
2026-04-01 22:20:38 +08:00
export type RuleRegistry = Map<string, RuleDef<unknown>>;
2026-04-01 21:44:20 +08:00
2026-04-01 22:20:38 +08:00
export function createRule<T>(
schemaStr: string,
fn: (cmd: Command) => Generator<string | CommandSchema, T, Command>
): RuleDef<T> {
return {
2026-04-01 22:31:07 +08:00
schema: parseCommandSchema(schemaStr, ''),
2026-04-01 22:20:38 +08:00
create: fn as RuleDef<T>['create'],
};
}
2026-04-01 21:44:20 +08:00
2026-04-01 22:20:38 +08:00
function parseYieldedSchema(value: string | CommandSchema): CommandSchema {
if (typeof value === 'string') {
2026-04-01 22:31:07 +08:00
return parseCommandSchema(value, '');
2026-04-01 22:20:38 +08:00
}
return value;
}
2026-04-01 21:44:20 +08:00
2026-04-01 22:20:38 +08:00
function pushContextToGame(game: GameContextLike, ctx: RuleContext<unknown>) {
game.contexts.value = [...game.contexts.value, { value: ctx } as any];
game.ruleContexts.push(ctx);
}
2026-04-01 17:34:21 +08:00
2026-04-01 22:20:38 +08:00
function discardChildren(game: GameContextLike, parent: RuleContext<unknown>) {
for (const child of parent.children) {
const idx = game.ruleContexts.indexOf(child);
if (idx !== -1) game.ruleContexts.splice(idx, 1);
2026-04-01 21:44:20 +08:00
2026-04-01 22:20:38 +08:00
const ctxIdx = game.contexts.value.findIndex((c: any) => c.value === child);
if (ctxIdx !== -1) {
const arr = [...game.contexts.value];
arr.splice(ctxIdx, 1);
game.contexts.value = arr;
2026-04-01 17:34:21 +08:00
}
2026-04-01 22:20:38 +08:00
}
parent.children = [];
parent.state = 'yielded';
}
2026-04-01 22:31:07 +08:00
function validateYieldedSchema(command: Command, schema: CommandSchema): boolean {
const requiredParams = schema.params.filter(p => p.required);
const variadicParam = schema.params.find(p => p.variadic);
if (command.params.length < requiredParams.length) {
return false;
}
if (!variadicParam && command.params.length > schema.params.length) {
return false;
}
const requiredOptions = schema.options.filter(o => o.required);
for (const opt of requiredOptions) {
const hasOption = opt.name in command.options || (opt.short && opt.short in command.options);
if (!hasOption) {
return false;
}
}
return true;
}
2026-04-01 22:20:38 +08:00
function invokeRule<T>(
game: GameContextLike,
command: Command,
ruleDef: RuleDef<T>,
parent?: RuleContext<unknown>
): RuleContext<T> {
const ctx: RuleContext<T> = {
type: ruleDef.schema.name,
schema: undefined,
generator: ruleDef.create(command),
parent,
children: [],
state: 'running',
resolution: undefined,
2026-04-01 17:34:21 +08:00
};
2026-04-01 22:20:38 +08:00
if (parent) {
discardChildren(game, parent);
parent.children.push(ctx as RuleContext<unknown>);
parent.state = 'waiting';
}
pushContextToGame(game, ctx as RuleContext<unknown>);
2026-04-01 13:36:16 +08:00
2026-04-01 22:20:38 +08:00
const result = ctx.generator.next();
if (result.done) {
ctx.resolution = result.value;
ctx.state = 'done';
} else {
ctx.schema = parseYieldedSchema(result.value);
ctx.state = 'yielded';
}
2026-04-01 17:34:21 +08:00
return ctx;
2026-04-01 13:36:16 +08:00
}
2026-04-01 22:20:38 +08:00
export function dispatchCommand(game: GameContextLike, input: string): RuleContext<unknown> | undefined {
const command = parseCommand(input);
if (game.rules.has(command.name)) {
const ruleDef = game.rules.get(command.name)!;
2026-04-01 22:31:07 +08:00
const parent = findYieldedParent(game);
return invokeRule(game, command, ruleDef, parent);
2026-04-01 22:20:38 +08:00
}
for (let i = game.ruleContexts.length - 1; i >= 0; i--) {
const ctx = game.ruleContexts[i];
if (ctx.state === 'yielded' && ctx.schema) {
2026-04-01 22:31:07 +08:00
if (validateYieldedSchema(command, ctx.schema)) {
2026-04-01 22:20:38 +08:00
const result = ctx.generator.next(command);
if (result.done) {
ctx.resolution = result.value;
ctx.state = 'done';
} else {
ctx.schema = parseYieldedSchema(result.value);
ctx.state = 'yielded';
}
return ctx;
}
}
}
return undefined;
2026-04-01 17:34:21 +08:00
}
2026-04-01 22:20:38 +08:00
2026-04-01 22:31:07 +08:00
function findYieldedParent(game: GameContextLike): RuleContext<unknown> | undefined {
for (let i = game.ruleContexts.length - 1; i >= 0; i--) {
const ctx = game.ruleContexts[i];
if (ctx.state === 'yielded') {
return ctx;
}
}
return undefined;
}
2026-04-01 22:20:38 +08:00
type GameContextLike = {
rules: RuleRegistry;
ruleContexts: RuleContext<unknown>[];
contexts: { value: any[] };
};