import {Command, CommandSchema, parseCommand, parseCommandSchema, validateCommand} 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.ruleContexts.push(ctx); } function discardChildren(game: GameContextLike, parent: RuleContext) { for (const child of parent.children) { const idx = game.ruleContexts.indexOf(child); if (idx !== -1) game.ruleContexts.splice(idx, 1); 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 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)!; 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; } type GameContextLike = { rules: RuleRegistry; ruleContexts: RuleContext[]; contexts: { value: any[] }; };