2026-04-02 13:52:15 +08:00
|
|
|
import {createEntityCollection, entity, Entity, EntityCollection} from "../utils/entity";
|
2026-04-02 10:26:42 +08:00
|
|
|
import {Part} from "./part";
|
|
|
|
|
import {Region} from "./region";
|
2026-04-02 10:48:20 +08:00
|
|
|
import {
|
|
|
|
|
Command,
|
|
|
|
|
CommandRegistry,
|
2026-04-02 14:11:35 +08:00
|
|
|
CommandRunnerContext,
|
2026-04-02 13:52:15 +08:00
|
|
|
CommandRunnerContextExport, CommandSchema, createCommandRegistry,
|
2026-04-02 10:48:20 +08:00
|
|
|
createCommandRunnerContext, parseCommandSchema,
|
2026-04-02 14:11:35 +08:00
|
|
|
PromptEvent, registerCommand
|
2026-04-02 10:48:20 +08:00
|
|
|
} from "../utils/command";
|
2026-04-02 10:26:42 +08:00
|
|
|
import {AsyncQueue} from "../utils/async-queue";
|
|
|
|
|
|
2026-04-02 12:53:49 +08:00
|
|
|
export interface IGameContext<TState extends Record<string, unknown> = {} > {
|
2026-04-02 13:52:15 +08:00
|
|
|
parts: EntityCollection<Part>;
|
|
|
|
|
regions: EntityCollection<Region>;
|
|
|
|
|
state: Entity<TState>;
|
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:53:49 +08:00
|
|
|
export function createGameContext<TState extends Record<string, unknown> = {} >(
|
2026-04-02 12:48:29 +08:00
|
|
|
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>();
|
2026-04-02 13:52:15 +08:00
|
|
|
const state = typeof initialState === 'function' ? initialState() : initialState ?? {} as TState;
|
2026-04-02 12:48:29 +08:00
|
|
|
|
|
|
|
|
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 13:52:15 +08:00
|
|
|
state: entity('gameState', state),
|
2026-04-02 12:48:29 +08:00
|
|
|
} 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
|
|
|
|
|
*/
|
2026-04-02 12:53:49 +08:00
|
|
|
export function createGameContextFromModule<TState extends Record<string, unknown> = {} >(
|
2026-04-02 12:48:29 +08:00
|
|
|
module: {
|
|
|
|
|
registry: CommandRegistry<IGameContext<TState>>,
|
|
|
|
|
createInitialState: () => TState
|
|
|
|
|
},
|
|
|
|
|
): IGameContext<TState> {
|
|
|
|
|
return createGameContext(module.registry, module.createInitialState);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 13:52:15 +08:00
|
|
|
export function createGameCommandRegistry<TState extends Record<string, unknown> = {} >(): CommandRegistry<IGameContext<TState>> {
|
|
|
|
|
return createCommandRegistry<IGameContext<TState>>();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 12:53:49 +08:00
|
|
|
export function createGameCommand<TState extends Record<string, unknown> = {} , TResult = unknown>(
|
2026-04-02 14:11:35 +08:00
|
|
|
registry: CommandRegistry<IGameContext<TState>>,
|
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>
|
2026-04-02 14:11:35 +08:00
|
|
|
) {
|
|
|
|
|
registerCommand(registry, {
|
2026-04-02 10:48:20 +08:00
|
|
|
schema: typeof schema === 'string' ? parseCommandSchema(schema) : schema,
|
|
|
|
|
run,
|
2026-04-02 14:11:35 +08:00
|
|
|
});
|
|
|
|
|
}
|