boardgame-core/src/core/rule.ts

298 lines
9.4 KiB
TypeScript
Raw Normal View History

2026-04-01 22:31:07 +08:00
import {Command, CommandSchema, parseCommand, parseCommandSchema} from "../utils/command";
import { defineSchema, type ParseError } from 'inline-schema';
2026-04-01 22:20:38 +08:00
export type RuleState = 'running' | 'yielded' | 'waiting' | 'invoking' | 'done';
export type InvokeYield = {
type: 'invoke';
rule: string;
command: Command;
};
export type RuleYield = string | CommandSchema | InvokeYield;
2026-04-01 22:20:38 +08:00
export type RuleContext<T = unknown> = {
type: string;
schema?: CommandSchema;
generator: Generator<RuleYield, T, Command | RuleContext<unknown>>;
2026-04-01 22:20:38 +08:00
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: (this: GameContextLike, cmd: Command) => Generator<RuleYield, T, Command | RuleContext<unknown>>;
2026-04-01 22:20:38 +08:00
};
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: (this: GameContextLike, cmd: Command) => Generator<RuleYield, T, Command | RuleContext<unknown>>
2026-04-01 22:20:38 +08:00
): 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
function isInvokeYield(value: RuleYield): value is InvokeYield {
return typeof value === 'object' && value !== null && 'type' in value && (value as InvokeYield).type === 'invoke';
}
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
function parseCommandWithSchema(command: Command, schema: CommandSchema): Command {
const parsedParams: unknown[] = [...command.params];
for (let i = 0; i < command.params.length; i++) {
const paramSchema = schema.params[i]?.schema;
if (paramSchema && typeof command.params[i] === 'string') {
try {
parsedParams[i] = paramSchema.parse(command.params[i] as string);
} catch {
// keep original value
}
}
}
const parsedOptions: Record<string, unknown> = { ...command.options };
for (const [key, value] of Object.entries(command.options)) {
const optSchema = schema.options.find(o => o.name === key || o.short === key);
if (optSchema?.schema && typeof value === 'string') {
try {
parsedOptions[key] = optSchema.schema.parse(value);
} catch {
// keep original value
}
}
}
return { ...command, params: parsedParams, options: parsedOptions };
}
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];
2026-04-01 22:55:59 +08:00
game.addRuleContext(ctx);
2026-04-01 22:20:38 +08:00
}
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) {
2026-04-01 22:55:59 +08:00
game.removeRuleContext(child);
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;
}
function invokeChildRule(
2026-04-01 22:20:38 +08:00
game: GameContextLike,
ruleName: string,
2026-04-01 22:20:38 +08:00
command: Command,
parent: RuleContext<unknown>
): RuleContext<unknown> {
const ruleDef = game.rules.get(ruleName)!;
const ctx: RuleContext<unknown> = {
2026-04-01 22:20:38 +08:00
type: ruleDef.schema.name,
schema: undefined,
generator: ruleDef.create.call(game, command),
2026-04-01 22:20:38 +08:00
parent,
children: [],
state: 'running',
resolution: undefined,
2026-04-01 17:34:21 +08:00
};
parent.children.push(ctx);
pushContextToGame(game, ctx);
return stepGenerator(game, ctx);
}
function resumeInvokingParent(
game: GameContextLike,
childCtx: RuleContext<unknown>
): RuleContext<unknown> | undefined {
const parent = childCtx.parent;
if (!parent || parent.state !== 'invoking') return undefined;
parent.children = parent.children.filter(c => c !== childCtx);
const result = parent.generator.next(childCtx);
if (result.done) {
(parent as RuleContext<unknown>).resolution = result.value;
(parent as RuleContext<unknown>).state = 'done';
const resumed = resumeInvokingParent(game, parent);
return resumed ?? parent;
} else if (isInvokeYield(result.value)) {
(parent as RuleContext<unknown>).state = 'invoking';
const childCtx2 = invokeChildRule(game, result.value.rule, result.value.command, parent);
return childCtx2;
} else {
(parent as RuleContext<unknown>).schema = parseYieldedSchema(result.value);
(parent as RuleContext<unknown>).state = 'yielded';
2026-04-01 22:20:38 +08:00
}
return parent;
}
2026-04-01 13:36:16 +08:00
function stepGenerator<T>(
game: GameContextLike,
ctx: RuleContext<T>
): RuleContext<T> {
2026-04-01 22:20:38 +08:00
const result = ctx.generator.next();
2026-04-01 22:20:38 +08:00
if (result.done) {
ctx.resolution = result.value;
ctx.state = 'done';
const resumed = resumeInvokingParent(game, ctx as RuleContext<unknown>);
if (resumed) return resumed as RuleContext<T>;
} else if (isInvokeYield(result.value)) {
const childRuleDef = game.rules.get(result.value.rule);
if (childRuleDef) {
ctx.state = 'invoking';
const childCtx = invokeChildRule(game, result.value.rule, result.value.command, ctx as RuleContext<unknown>);
return childCtx as RuleContext<T>;
} else {
ctx.schema = parseYieldedSchema('');
ctx.state = 'yielded';
}
2026-04-01 22:20:38 +08:00
} 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
}
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.call(game, command),
parent,
children: [],
state: 'running',
resolution: undefined,
};
if (parent) {
discardChildren(game, parent);
parent.children.push(ctx as RuleContext<unknown>);
parent.state = 'waiting';
}
pushContextToGame(game, ctx as RuleContext<unknown>);
return stepGenerator(game, ctx);
}
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';
const resumed = resumeInvokingParent(game, ctx);
return resumed ?? ctx;
} else if (isInvokeYield(result.value)) {
ctx.state = 'invoking';
const childCtx = invokeChildRule(game, result.value.rule, result.value.command, ctx);
return childCtx;
2026-04-01 22:20:38 +08:00
} 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;
}
export type GameContextLike = {
2026-04-01 22:20:38 +08:00
rules: RuleRegistry;
ruleContexts: RuleContext<unknown>[];
contexts: { value: any[] };
2026-04-01 22:55:59 +08:00
addRuleContext: (ctx: RuleContext<unknown>) => void;
removeRuleContext: (ctx: RuleContext<unknown>) => void;
parts: {
collection: { value: Record<string, any> };
add: (...entities: any[]) => void;
remove: (...ids: string[]) => void;
get: (id: string) => any;
};
regions: {
collection: { value: Record<string, any> };
add: (...entities: any[]) => void;
remove: (...ids: string[]) => void;
get: (id: string) => any;
};
pushContext: (context: any) => any;
popContext: () => void;
latestContext: <T>(type: string) => any | undefined;
2026-04-01 22:20:38 +08:00
};