import {Command, CommandSchema, parseCommand, parseCommandSchema} from "../utils/command"; export type RuleState = 'running' | 'yielded' | 'waiting' | 'done'; export type RuleContext = { type: string; schema?: CommandSchema; generator: Generator; parent?: RuleContext; children: RuleContext[]; state: RuleState; resolution?: T; } export type RuleDef = { schema: CommandSchema; create: (cmd: Command) => Generator; }; export type RuleRegistry = Map>; export function createRule( schemaStr: string, fn: (cmd: Command) => Generator ): RuleDef { return { schema: parseCommandSchema(schemaStr, ''), create: fn as RuleDef['create'], }; } function parseYieldedSchema(value: string | CommandSchema): CommandSchema { if (typeof value === 'string') { return parseCommandSchema(value, ''); } return value; } function pushContextToGame(game: GameContextLike, ctx: RuleContext) { game.contexts.value = [...game.contexts.value, { value: ctx } as any]; game.addRuleContext(ctx); } function discardChildren(game: GameContextLike, parent: RuleContext) { for (const child of parent.children) { game.removeRuleContext(child); 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; } } parent.children = []; parent.state = 'yielded'; } 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 invokeRule( game: GameContextLike, command: Command, ruleDef: RuleDef, parent?: RuleContext ): RuleContext { const ctx: RuleContext = { type: ruleDef.schema.name, schema: undefined, generator: ruleDef.create(command), parent, children: [], state: 'running', resolution: undefined, }; if (parent) { discardChildren(game, parent); parent.children.push(ctx as RuleContext); parent.state = 'waiting'; } pushContextToGame(game, ctx as RuleContext); const result = ctx.generator.next(); if (result.done) { ctx.resolution = result.value; ctx.state = 'done'; } else { ctx.schema = parseYieldedSchema(result.value); ctx.state = 'yielded'; } return ctx; } export function dispatchCommand(game: GameContextLike, input: string): RuleContext | undefined { const command = parseCommand(input); if (game.rules.has(command.name)) { const ruleDef = game.rules.get(command.name)!; const parent = findYieldedParent(game); return invokeRule(game, command, ruleDef, parent); } for (let i = game.ruleContexts.length - 1; i >= 0; i--) { const ctx = game.ruleContexts[i]; if (ctx.state === 'yielded' && ctx.schema) { if (validateYieldedSchema(command, ctx.schema)) { 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; } function findYieldedParent(game: GameContextLike): RuleContext | undefined { for (let i = game.ruleContexts.length - 1; i >= 0; i--) { const ctx = game.ruleContexts[i]; if (ctx.state === 'yielded') { return ctx; } } return undefined; } type GameContextLike = { rules: RuleRegistry; ruleContexts: RuleContext[]; contexts: { value: any[] }; addRuleContext: (ctx: RuleContext) => void; removeRuleContext: (ctx: RuleContext) => void; };