2026-04-01 22:20:38 +08:00
|
|
|
import {Command, CommandSchema, parseCommand, parseCommandSchema, validateCommand} from "../utils/command";
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
schema: parseCommandSchema(schemaStr),
|
|
|
|
|
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') {
|
|
|
|
|
return parseCommandSchema(value);
|
|
|
|
|
}
|
|
|
|
|
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';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)!;
|
|
|
|
|
return invokeRule(game, command, ruleDef);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let i = game.ruleContexts.length - 1; i >= 0; i--) {
|
|
|
|
|
const ctx = game.ruleContexts[i];
|
|
|
|
|
if (ctx.state === 'yielded' && ctx.schema) {
|
|
|
|
|
const validation = validateCommand(command, ctx.schema);
|
|
|
|
|
if (validation.valid) {
|
|
|
|
|
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
|
|
|
|
|
|
|
|
type GameContextLike = {
|
|
|
|
|
rules: RuleRegistry;
|
|
|
|
|
ruleContexts: RuleContext<unknown>[];
|
|
|
|
|
contexts: { value: any[] };
|
|
|
|
|
};
|