2026-04-02 15:36:32 +08:00
|
|
|
|
import type { Command, CommandSchema } from './types';
|
2026-04-03 14:10:42 +08:00
|
|
|
|
import { parseCommand } from './command-parse';
|
|
|
|
|
|
import { applyCommandSchema } from './command-validate';
|
2026-04-02 08:58:11 +08:00
|
|
|
|
|
2026-04-02 09:05:47 +08:00
|
|
|
|
export type PromptEvent = {
|
|
|
|
|
|
schema: CommandSchema;
|
2026-04-04 10:30:00 +08:00
|
|
|
|
/** 当前等待输入的玩家 */
|
|
|
|
|
|
currentPlayer: string | null;
|
2026-04-03 14:10:42 +08:00
|
|
|
|
/**
|
2026-04-02 19:08:14 +08:00
|
|
|
|
* 尝试提交命令
|
2026-04-03 14:10:42 +08:00
|
|
|
|
* @param commandOrInput Command 对象或命令字符串
|
2026-04-02 19:08:14 +08:00
|
|
|
|
* @returns null - 验证成功,Promise 已 resolve
|
|
|
|
|
|
* @returns string - 验证失败,返回错误消息,Promise 未 resolve
|
|
|
|
|
|
*/
|
2026-04-03 14:10:42 +08:00
|
|
|
|
tryCommit: (commandOrInput: Command | string) => string | null;
|
2026-04-02 19:08:14 +08:00
|
|
|
|
/** 取消 prompt,Promise 被 reject */
|
|
|
|
|
|
cancel: (reason?: string) => void;
|
2026-04-02 09:05:47 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export type CommandRunnerEvents = {
|
|
|
|
|
|
prompt: PromptEvent;
|
2026-04-04 00:59:40 +08:00
|
|
|
|
/** 当 prompt 结束(tryCommit 成功或 cancel)时触发 */
|
|
|
|
|
|
promptEnd: void;
|
2026-04-02 09:05:47 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2026-04-02 10:38:31 +08:00
|
|
|
|
export type CommandResult<T=unknown> = {
|
|
|
|
|
|
success: true;
|
|
|
|
|
|
result: T;
|
|
|
|
|
|
} | {
|
|
|
|
|
|
success: false;
|
|
|
|
|
|
error: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-06 11:07:11 +08:00
|
|
|
|
export type PromptValidator<TResult,TArgs extends any[]=any[]> = (...params: TArgs) => TResult;
|
2026-04-04 21:38:07 +08:00
|
|
|
|
|
2026-04-02 08:58:11 +08:00
|
|
|
|
export type CommandRunnerContext<TContext> = {
|
|
|
|
|
|
context: TContext;
|
2026-04-02 10:38:31 +08:00
|
|
|
|
run: <T=unknown>(input: string) => Promise<CommandResult<T>>;
|
2026-04-04 18:29:33 +08:00
|
|
|
|
runParsed: <T=unknown>(command: Command) => Promise<CommandResult<T>>;
|
2026-04-06 11:07:11 +08:00
|
|
|
|
prompt: <TResult,TArgs extends any[]=any[]>(schema: CommandSchema | string, validator: PromptValidator<TResult,TArgs>, currentPlayer?: string | null) => Promise<TResult>;
|
2026-04-02 09:05:47 +08:00
|
|
|
|
on: <T extends keyof CommandRunnerEvents>(event: T, listener: (e: CommandRunnerEvents[T]) => void) => void;
|
|
|
|
|
|
off: <T extends keyof CommandRunnerEvents>(event: T, listener: (e: CommandRunnerEvents[T]) => void) => void;
|
2026-04-02 08:58:11 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export type CommandRunnerHandler<TContext, TResult> = (
|
|
|
|
|
|
this: CommandRunnerContext<TContext>,
|
|
|
|
|
|
command: Command
|
|
|
|
|
|
) => Promise<TResult>;
|
|
|
|
|
|
|
|
|
|
|
|
export type CommandRunner<TContext, TResult = unknown> = {
|
|
|
|
|
|
schema: CommandSchema;
|
|
|
|
|
|
run: CommandRunnerHandler<TContext, TResult>;
|
|
|
|
|
|
};
|
2026-04-06 10:06:04 +08:00
|
|
|
|
|
|
|
|
|
|
export type CommandFunction<TContext> = (ctx: TContext, ...args: any[]) => Promise<unknown>;
|
|
|
|
|
|
export type CommandDef<TContext,TFunc extends CommandFunction<TContext>> = {
|
|
|
|
|
|
schema: string | CommandSchema,
|
|
|
|
|
|
run: TFunc;
|
|
|
|
|
|
}
|