boardgame-core/src/core/game.ts

106 lines
3.7 KiB
TypeScript

import {MutableSignal, mutableSignal} from "@/utils/mutable-signal";
import {
Command,
CommandRegistry, CommandResult,
CommandRunnerContext,
CommandSchema,
createCommandRegistry,
createCommandRunnerContext,
parseCommandSchema,
registerCommand
} from "@/utils/command";
import type { GameModule } from './game-host';
export interface IGameContext<TState extends Record<string, unknown> = {} > {
get value(): TState;
produce(fn: (draft: TState) => void): void;
produceAsync(fn: (draft: TState) => void): Promise<void>;
run<T>(input: string): Promise<CommandResult<T>>;
runParsed<T>(command: Command): Promise<CommandResult<T>>;
prompt(schema: CommandSchema | string, validator?: (command: Command) => string | null, currentPlayer?: string | null): Promise<Command>;
addInterruption(promise: Promise<void>): void;
// test only
_state: MutableSignal<TState>;
_commands: CommandRunnerContext<IGameContext<TState>>;
}
export function createGameContext<TState extends Record<string, unknown> = {} >(
commandRegistry: CommandRegistry<IGameContext<TState>>,
initialState?: TState | (() => TState)
): IGameContext<TState> {
const stateValue = typeof initialState === 'function' ? initialState() : initialState ?? {} as TState;
const state = mutableSignal(stateValue);
let commands: CommandRunnerContext<IGameContext<TState>> = null as any;
const context: IGameContext<TState> = {
get value(): TState {
return state.value;
},
produce(fn) {
return state.produce(fn);
},
produceAsync(fn) {
return state.produceAsync(fn);
},
run<T>(input: string) {
return commands.run<T>(input);
},
runParsed<T>(command: Command) {
return commands.runParsed<T>(command);
},
prompt(schema, validator, currentPlayer) {
return commands.prompt(schema, validator, currentPlayer);
},
addInterruption(promise) {
state.addInterruption(promise);
},
_state: state,
_commands: commands,
};
context._commands = commands = createCommandRunnerContext(commandRegistry, context);
return context;
}
/**
* so that we can do `import * as tictactoe from './tic-tac-toe.ts';\n\n createGameContextFromModule(tictactoe);`
* @param module
*/
export function createGameContextFromModule<TState extends Record<string, unknown> = {} >(
module: {
registry: CommandRegistry<IGameContext<TState>>,
createInitialState: () => TState
},
): IGameContext<TState> {
return createGameContext(module.registry, module.createInitialState);
}
export function createGameCommandRegistry<TState extends Record<string, unknown> = {} >() {
return createCommandRegistry<IGameContext<TState>>();
}
export function registerGameCommand<TState extends Record<string, unknown> = {}, TResult = unknown>(
registry: CommandRegistry<IGameContext<TState>>,
schema: CommandSchema | string,
run: (this: IGameContext<TState>, ...args: any[]) => Promise<TResult>
) {
registerCommand(registry, {
schema: typeof schema === 'string' ? parseCommandSchema(schema) : schema,
async run(this: CommandRunnerContext<IGameContext<TState>>, command: Command){
const params = command.params;
return await run.call(this.context, ...params);
},
});
}
export { GameHost, createGameHost } from './game-host';
export type { GameHostStatus, GameModule } from './game-host';
export function createGameModule<TState extends Record<string, unknown>>(
module: GameModule<TState>
): GameModule<TState> {
return module;
}