boardgame-core/src/core/game.ts

71 lines
2.5 KiB
TypeScript
Raw Normal View History

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,
type CommandRunner, CommandRunnerContext,
CommandRunnerContextExport, CommandSchema, createCommandRegistry,
2026-04-02 10:48:20 +08:00
createCommandRunnerContext, parseCommandSchema,
PromptEvent
} 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> = {} > {
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>();
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!,
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);
}
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 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,
};
}