2026-04-02 10:26:42 +08:00
|
|
|
import {createEntityCollection} from "../utils/entity";
|
|
|
|
|
import {Part} from "./part";
|
|
|
|
|
import {Region} from "./region";
|
2026-04-02 10:48:20 +08:00
|
|
|
import {
|
|
|
|
|
Command,
|
|
|
|
|
CommandRegistry,
|
|
|
|
|
type CommandRunner, CommandRunnerContext,
|
|
|
|
|
CommandRunnerContextExport, CommandSchema,
|
|
|
|
|
createCommandRunnerContext, parseCommandSchema,
|
|
|
|
|
PromptEvent
|
|
|
|
|
} from "../utils/command";
|
2026-04-02 10:26:42 +08:00
|
|
|
import {AsyncQueue} from "../utils/async-queue";
|
|
|
|
|
|
2026-04-02 12:48:29 +08:00
|
|
|
export interface IGameContext<TState extends {} = {}> {
|
2026-04-02 10:26:42 +08:00
|
|
|
parts: ReturnType<typeof createEntityCollection<Part>>;
|
|
|
|
|
regions: ReturnType<typeof createEntityCollection<Region>>;
|
2026-04-02 12:48:29 +08:00
|
|
|
commands: CommandRunnerContextExport<IGameContext<TState>>;
|
2026-04-02 10:48:20 +08:00
|
|
|
prompts: AsyncQueue<PromptEvent>;
|
2026-04-02 10:26:42 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 12:48:29 +08:00
|
|
|
export function createGameContext<TState extends {} = {}>(
|
|
|
|
|
commandRegistry: CommandRegistry<IGameContext<TState>>,
|
|
|
|
|
initialState?: TState | (() => TState)
|
|
|
|
|
): IGameContext<TState> {
|
2026-04-02 10:26:42 +08:00
|
|
|
const parts = createEntityCollection<Part>();
|
|
|
|
|
const regions = createEntityCollection<Region>();
|
2026-04-02 12:48:29 +08:00
|
|
|
const prompts = new AsyncQueue<PromptEvent>();
|
|
|
|
|
const state: TState = typeof initialState === 'function' ? (initialState as (() => TState))() : (initialState ?? {} as TState);
|
|
|
|
|
|
|
|
|
|
const ctx = {
|
2026-04-02 10:26:42 +08:00
|
|
|
parts,
|
|
|
|
|
regions,
|
2026-04-02 12:48:29 +08:00
|
|
|
prompts,
|
2026-04-02 10:48:20 +08:00
|
|
|
commands: null!,
|
2026-04-02 12:48:29 +08:00
|
|
|
state,
|
|
|
|
|
} as IGameContext<TState>
|
|
|
|
|
|
2026-04-02 10:26:42 +08:00
|
|
|
ctx.commands = createCommandRunnerContext(commandRegistry, ctx);
|
2026-04-02 10:48:20 +08:00
|
|
|
ctx.commands.on('prompt', (prompt: PromptEvent) => ctx.prompts.push(prompt));
|
2026-04-02 10:26:42 +08:00
|
|
|
|
|
|
|
|
return ctx;
|
2026-04-02 10:48:20 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 12:48:29 +08:00
|
|
|
/**
|
|
|
|
|
* so that we can do `import * as tictactoe from './tic-tac-toe.ts';\n\n createGameContextFromModule(tictactoe);`
|
|
|
|
|
* @param module
|
|
|
|
|
*/
|
|
|
|
|
export function createGameContextFromModule<TState extends {} = {}>(
|
|
|
|
|
module: {
|
|
|
|
|
registry: CommandRegistry<IGameContext<TState>>,
|
|
|
|
|
createInitialState: () => TState
|
|
|
|
|
},
|
|
|
|
|
): IGameContext<TState> {
|
|
|
|
|
return createGameContext(module.registry, module.createInitialState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createGameCommand<TState extends {} = {}, TResult = unknown>(
|
2026-04-02 10:48:20 +08:00
|
|
|
schema: CommandSchema | string,
|
2026-04-02 12:48:29 +08:00
|
|
|
run: (this: CommandRunnerContext<IGameContext<TState>>, command: Command) => Promise<TResult>
|
|
|
|
|
): CommandRunner<IGameContext<TState>, TResult> {
|
2026-04-02 10:48:20 +08:00
|
|
|
return {
|
|
|
|
|
schema: typeof schema === 'string' ? parseCommandSchema(schema) : schema,
|
|
|
|
|
run,
|
|
|
|
|
};
|
|
|
|
|
}
|