fix: api doc

This commit is contained in:
hypercross 2026-04-04 22:54:58 +08:00
parent f5830ea637
commit b929a126a1
1 changed files with 91 additions and 69 deletions

View File

@ -4,117 +4,139 @@
| 导出 | 说明 | | 导出 | 说明 |
|---|---| |---|---|
| `IGameContext` | 游戏上下文基础接口 |
| `createGameContext(registry, initialState?)` | 创建游戏上下文实例 |
| `createGameContextFromModule(module)` | 从 GameModule 创建游戏上下文 |
| `createGameCommandRegistry<TState>()` | 创建命令注册表 |
| `createGameModule(module)` | 辅助函数,标记 GameModule |
| `GameHost<TState>` | 游戏生命周期管理类 | | `GameHost<TState>` | 游戏生命周期管理类 |
| `createGameHost(module, options?)` | 从 GameModule 创建 GameHost | | `createGameHost(module)` | 从 GameModule 创建 GameHost |
| `GameHostStatus` | 类型: `'created' \| 'running' \| 'disposed'` | | `GameHostStatus` | 类型: `'created' \| 'running' \| 'disposed'` |
| `GameModule` | 游戏模块类型 | | `GameModule` | 游戏模块类型,包含 `registry``createInitialState` |
| `createGameModule(module)` | 辅助函数,标记 GameModule |
| `createGameCommandRegistry<TState>()` | 创建游戏命令注册表 |
## 棋子 (Parts) ### GameHost
| 成员 | 说明 |
|---|---|
| `context: IGameContext<TState>` | 游戏上下文,含状态和命令运行能力 |
| `status: Signal<GameHostStatus>` | 当前状态 |
| `activePromptSchema: Signal<CommandSchema \| null>` | 当前等待的 prompt schema |
| `activePromptPlayer: Signal<string \| null>` | 当前等待的玩家 |
| `setup(setupCommand: string)` | 启动游戏,运行 setup 命令 |
| `onInput(input: string)` | 提交玩家输入到当前 prompt |
| `addInterruption(promise)` / `clearInterruptions()` | 动画中断控制 |
| `on(event, listener)` | 监听 `setup` / `dispose` 事件 |
| `dispose()` | 销毁游戏实例 |
### IGameContext
| 成员 | 说明 |
|---|---|
| `value: TState` | 当前游戏状态 |
| `produce(fn)` | 同步更新状态 |
| `produceAsync(fn)` | 等待动画中断后更新状态 |
| `run(input)` / `runParsed(command)` | 运行命令 |
| `prompt(schema, validator, currentPlayer?)` | 等待玩家输入 |
| `addInterruption(promise)` | 注册动画中断 |
## 棋子与区域
### Part
| 导出 | 说明 | | 导出 | 说明 |
|---|---| |---|---|
| `Part<TMeta>` | 游戏棋子类型 | | `Part<TMeta>` | 棋子类型,含 `id`、`regionId`、`position`、`side` 等 |
| `PartTemplate<TMeta>` | 创建棋子的模板类型 | | `PartTemplate<TMeta>` / `PartPool<TMeta>` | 棋子模板和棋子池类型 |
| `PartPool<TMeta>` | 棋子池类型 |
| `createPart(template, id)` | 创建单个棋子 | | `createPart(template, id)` | 创建单个棋子 |
| `createParts(template, count, idPrefix)` | 批量创建相同棋子 | | `createParts(template, count, idPrefix)` | 批量创建棋子 |
| `createPartPool(template, count, idPrefix)` | 创建棋子池,支持 `draw()` / `return()` / `remaining()` |
| `mergePartPools(...pools)` | 合并棋子池 |
| `createPartsFromTable(items, getId, getCount?)` | 从表格数据创建棋子 | | `createPartsFromTable(items, getId, getCount?)` | 从表格数据创建棋子 |
| `createPartPool(template, count, idPrefix)` | 创建棋子池 | | `findPartById(parts, id)` | 按 ID 查找 |
| `mergePartPools(...pools)` | 合并多个棋子池 | | `getPartAtPosition(parts, regionId, position)` | 获取位置上的棋子 |
| `findPartById(parts, id)` | 按 ID 查找棋子 | | `isCellOccupied(parts, regionId, position)` | 检查格子占用 |
| `isCellOccupied(parts, regionId, position)` | 检查格子是否被占用 | | `flip(part)` / `flipTo(part, side)` / `roll(part, rng)` | 翻面/掷骰 |
| `getPartAtPosition(parts, regionId, position)` | 获取格子上的棋子 |
| `isCellOccupiedByRegion(region, position)` | O(1) 检查格子占用 |
| `getPartAtPositionInRegion(region, parts, position)` | O(1) 获取棋子 |
| `flip(part)` / `flipTo(part, side)` / `roll(part, rng)` | 翻面/随机面 |
## 区域 (Regions) ### Region
| 导出 | 说明 | | 导出 | 说明 |
|---|---| |---|---|
| `Region` / `RegionAxis` | 区域类型 | | `Region` / `RegionAxis` | 区域类型 |
| `createRegion(id, axes)` | 创建区域 | | `createRegion(id, axes)` | 创建区域 |
| `applyAlign(region, parts)` | 紧凑排列 | | `applyAlign(region, parts)` | 按 axis 配置紧凑排列棋子 |
| `shuffle(region, parts, rng)` | 打乱位置 | | `shuffle(region, parts, rng)` | 打乱区域内棋子位置 |
| `moveToRegion(part, sourceRegion?, targetRegion, position?)` | 移动棋子到区域 | | `moveToRegion(part, source?, target, position?)` | 移动棋子到区域 |
| `isCellOccupiedByRegion(region, position)` | O(1) 检查格子占用(使用 region.partMap |
| `getPartAtPositionInRegion(region, parts, position)` | O(1) 获取棋子(使用 region.partMap |
## 命令 (Commands) ## 命令系统
### 基础类型
| 导出 | 说明 | | 导出 | 说明 |
|---|---| |---|---|
| `Command` / `CommandSchema` / `CommandResult` | 命令相关类型 | | `Command` | 解析后的命令对象,含 `name`、`params`、`options`、`flags` |
| `CommandParamSchema` / `CommandOptionSchema` / `CommandFlagSchema` | 命令参数类型 | | `CommandSchema` | 命令 schema 定义 |
| `parseCommand(input)` | 解析命令字符串 | | `CommandResult<T>` | 命令执行结果: `{ success: true, result: T } | { success: false, error: string }` |
| `parseCommandSchema(schema)` | 解析 Schema 字符串 |
| `validateCommand(cmd, schema)` | 验证命令 |
| `parseCommandWithSchema(input, schema)` | 解析并应用 Schema |
| `applyCommandSchema(cmd, schema)` | 应用 Schema 到命令 |
### 命令运行器 ### 注册与运行
| 导出 | 说明 | | 导出 | 说明 |
|---|---| |---|---|
| `CommandRunner<TContext, TResult>` | 命令运行器类型 | | `CommandRegistry<TContext>` | 命令注册表,继承自 `Map<string, CommandRunner>`,含 `register(schema, handler)` 快捷方法 |
| `CommandRunnerHandler<TContext, TResult>` | 命令处理器类型 | | `CommandRunner<TContext, TResult>` | 命令运行器,含 `schema``run` |
| `CommandRunnerContext<TContext>` | 命令运行器上下文类型 | | `CommandRunnerContext<TContext>` | 命令运行器上下文,提供 `run`、`prompt`、`on`、`off` |
| `CommandRunnerContextExport<TContext>` | 导出的命令运行器上下文(含内部方法) | | `PromptEvent` | prompt 事件,含 `schema`、`currentPlayer`、`tryCommit`、`cancel` |
| `CommandRegistry<TContext>` | 命令注册表类型 | | `PromptValidator<T>` | prompt 验证器: `(command) => T`throw 字符串表示验证失败 |
| `PromptEvent` / `CommandRunnerEvents` | 提示事件类型 |
| `PromptValidator<T>` | 提示验证器类型 |
| `createCommandRegistry()` | 创建命令注册表 | | `createCommandRegistry()` | 创建命令注册表 |
| `registerCommand(registry, runner)` | 注册命令运行器 | | `registerCommand(registry, runner)` | 注册命令 |
| `unregisterCommand(registry, name)` | 注销命令 | | `unregisterCommand(registry, name)` / `hasCommand(registry, name)` / `getCommand(registry, name)` | 命令管理 |
| `hasCommand(registry, name)` | 检查命令是否存在 | | `runCommand(registry, context, input)` | 解析并运行命令 |
| `getCommand(registry, name)` | 获取命令 |
| `runCommand(registry, context, input)` | 运行命令 | ### 命令解析
| `runCommandParsed(registry, context, command)` | 运行已解析命令 |
| `createCommandRunnerContext(registry, context)` | 创建命令运行器上下文 | | 导出 | 说明 |
|---|---|
| `parseCommand(input)` | 解析命令字符串为 `Command` 对象 |
| `parseCommandSchema(schemaStr)` | 解析 schema 字符串为 `CommandSchema` |
| `validateCommand(command, schema)` | 验证命令是否符合 schema |
| `parseCommandWithSchema(input, schemaStr)` | 解析并验证命令 |
| `applyCommandSchema(command, schema)` | 应用 schema 到命令,含类型转换 |
### Game Command Registry ### Game Command Registry
游戏专用命令注册表(通过 `createGameCommandRegistry` 创建,类型为 `CommandRegistry<IGameContext<TState>>` 通过 `createGameCommandRegistry()` 创建的注册表有快捷 `register` 方法
| 方法 | 说明 | ```ts
|---|---| const moveCmd = registry.register('move <from> <to>', async (ctx, from, to) => {
| `registry.register(schema, handler)` | 注册命令并返回可调用命令对象 | ctx.produce(state => { /* ... */ });
});
`registry.register` 接受命令 Schema字符串或 `CommandSchema` 对象)和处理器函数,返回一个可调用函数。处理器函数签名为 `(ctx, ...args) => Promise<TResult>` // 作为子命令调用
await moveCmd(ctx, 'A1', 'A2');
```
在 GameModule 中使用 `game.prompt()` 等待玩家输入,验证函数中 `throw` 字符串会触发重新提示,返回非 null 值表示验证通过。子命令可以通过 `await subCommand(game, ...args)` 方式调用。 处理器签名: `(ctx, ...args) => Promise<TResult>`
`game.prompt()` 等待玩家输入,验证器 throw 字符串触发重新提示,返回非 null 值表示通过。
## MutableSignal ## MutableSignal
| 导出 | 说明 | | 导出 | 说明 |
|---|---| |---|---|
| `MutableSignal<T>` | 支持突变和动画中断的响应式信号 | | `MutableSignal<T>` | 支持突变和动画中断的响应式信号,继承 Preact Signal |
| `mutableSignal(initial?, options?)` | 创建 MutableSignal | | `mutableSignal(initial?, options?)` | 创建 MutableSignal |
| `EntityCollection<T>` / `createEntityCollection()` | 实体集合辅助函数 | | `EntityCollection<T>` / `createEntityCollection()` | 实体集合辅助函数,管理 `MutableSignal` 字典 |
### MutableSignal 成员
| 成员 | 说明 | | 成员 | 说明 |
|---|---| |---|---|
| `value: T` | 获取当前值 | | `value: T` | 获取/设置当前值 |
| `produce(fn: (draft: T) => void): void` | 同步不可变更新 | | `produce(fn)` | 同步不可变更新(使用 mutative |
| `addInterruption(promise: Promise<void>): void` | 添加中断 | | `produceAsync(fn)` | 等待所有 interruption 完成后更新状态 |
| `clearInterruptions(): void` | 清除所有中断 | | `addInterruption(promise)` | 注册中断,下一个 produceAsync 会等待 |
| `produceAsync(fn: (draft: T) => void): Promise<void>` | 等待中断完成后更新 | | `clearInterruptions()` | 清除所有未完成的中断 |
### GameHost 中断代理
`GameHost` 直接代理 `addInterruption``clearInterruptions`,供 UI 层使用。
`IGameContext` 提供 `produce()`、`produceAsync()` 和 `addInterruption()` 方法。
详见 [动画与状态更新同步](./animation-sync.md)。 详见 [动画与状态更新同步](./animation-sync.md)。
## 工具 ## 工具
| 导出 | 说明 | | 导出 | 说明 |
|---|---| |---|---|
| `RNG` | 随机数生成器接口 | | `RNG` | 随机数生成器接口: `setSeed`、`getSeed`、`next`、`nextInt` |
| `createRNG(seed?)` | 创建种子 RNG | | `createRNG(seed?)` / `Mulberry32RNG` | Mulberry32 算法 PRNG |
| `Mulberry32RNG` | Mulberry32 算法实现 |