import {createEntityCollection} from "../utils/entity"; import {Part} from "./part"; import {Region} from "./region"; import { Command, CommandRegistry, type CommandRunner, CommandRunnerContext, CommandRunnerContextExport, CommandSchema, createCommandRunnerContext, parseCommandSchema, PromptEvent } from "../utils/command"; import {AsyncQueue} from "../utils/async-queue"; export interface IGameContext { parts: ReturnType>; regions: ReturnType>; commands: CommandRunnerContextExport; prompts: AsyncQueue; } /** * creates a game context. * expects a command registry already registered with commands. * @param commandRegistry */ export function createGameContext(commandRegistry: CommandRegistry) { const parts = createEntityCollection(); const regions = createEntityCollection(); const ctx: IGameContext = { parts, regions, commands: null!, prompts: new AsyncQueue(), }; ctx.commands = createCommandRunnerContext(commandRegistry, ctx); ctx.commands.on('prompt', (prompt: PromptEvent) => ctx.prompts.push(prompt)); return ctx; } export function createGameCommand( schema: CommandSchema | string, run: (this: CommandRunnerContext, command: Command) => Promise ): CommandRunner { return { schema: typeof schema === 'string' ? parseCommandSchema(schema) : schema, run, }; }