2026-04-03 14:17:36 +08:00
|
|
|
import {MutableSignal, mutableSignal} from "@/utils/mutable-signal";
|
2026-04-02 10:48:20 +08:00
|
|
|
import {
|
|
|
|
|
Command,
|
2026-04-04 18:29:33 +08:00
|
|
|
CommandRegistry, CommandResult,
|
2026-04-04 20:50:17 +08:00
|
|
|
CommandRunnerContext, CommandRunnerContextExport,
|
2026-04-02 14:39:30 +08:00
|
|
|
CommandSchema,
|
|
|
|
|
createCommandRegistry,
|
|
|
|
|
createCommandRunnerContext,
|
|
|
|
|
parseCommandSchema,
|
|
|
|
|
registerCommand
|
2026-04-02 15:59:27 +08:00
|
|
|
} from "@/utils/command";
|
2026-04-04 11:06:41 +08:00
|
|
|
import type { GameModule } from './game-host';
|
2026-04-02 10:26:42 +08:00
|
|
|
|
2026-04-02 12:53:49 +08:00
|
|
|
export interface IGameContext<TState extends Record<string, unknown> = {} > {
|
2026-04-04 18:29:33 +08:00
|
|
|
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>;
|
2026-04-04 20:50:17 +08:00
|
|
|
_commands: CommandRunnerContextExport<IGameContext<TState>>;
|
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-04 18:29:33 +08:00
|
|
|
commandRegistry: CommandRegistry<IGameContext<TState>>,
|
2026-04-02 12:48:29 +08:00
|
|
|
initialState?: TState | (() => TState)
|
|
|
|
|
): IGameContext<TState> {
|
2026-04-02 14:39:30 +08:00
|
|
|
const stateValue = typeof initialState === 'function' ? initialState() : initialState ?? {} as TState;
|
2026-04-03 14:17:36 +08:00
|
|
|
const state = mutableSignal(stateValue);
|
2026-04-04 20:50:17 +08:00
|
|
|
let commands: CommandRunnerContextExport<IGameContext<TState>> = null as any;
|
2026-04-02 12:48:29 +08:00
|
|
|
|
2026-04-04 18:29:33 +08:00
|
|
|
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,
|
2026-04-02 14:39:30 +08:00
|
|
|
};
|
2026-04-04 18:29:33 +08:00
|
|
|
|
|
|
|
|
context._commands = commands = createCommandRunnerContext(commandRegistry, context);
|
|
|
|
|
|
|
|
|
|
return context;
|
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: {
|
2026-04-04 18:29:33 +08:00
|
|
|
registry: CommandRegistry<IGameContext<TState>>,
|
2026-04-02 12:48:29 +08:00
|
|
|
createInitialState: () => TState
|
|
|
|
|
},
|
|
|
|
|
): IGameContext<TState> {
|
|
|
|
|
return createGameContext(module.registry, module.createInitialState);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 14:39:30 +08:00
|
|
|
export function createGameCommandRegistry<TState extends Record<string, unknown> = {} >() {
|
2026-04-04 18:29:33 +08:00
|
|
|
return createCommandRegistry<IGameContext<TState>>();
|
2026-04-02 13:52:15 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 20:50:17 +08:00
|
|
|
type CmdFunc<TState extends Record<string, unknown> = {}> = (this: IGameContext<TState>, ...args: any[]) => Promise<unknown>;
|
|
|
|
|
|
|
|
|
|
export function registerGameCommand<TState extends Record<string, unknown> = {}, TFunc extends CmdFunc<TState> = CmdFunc<TState>>(
|
2026-04-04 18:29:33 +08:00
|
|
|
registry: CommandRegistry<IGameContext<TState>>,
|
2026-04-02 10:48:20 +08:00
|
|
|
schema: CommandSchema | string,
|
2026-04-04 20:50:17 +08:00
|
|
|
run: TFunc
|
2026-04-02 14:11:35 +08:00
|
|
|
) {
|
2026-04-04 20:50:17 +08:00
|
|
|
const parsedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema;
|
2026-04-02 14:11:35 +08:00
|
|
|
registerCommand(registry, {
|
2026-04-04 20:50:17 +08:00
|
|
|
schema: parsedSchema,
|
2026-04-04 18:29:33 +08:00
|
|
|
async run(this: CommandRunnerContext<IGameContext<TState>>, command: Command){
|
|
|
|
|
const params = command.params;
|
|
|
|
|
return await run.call(this.context, ...params);
|
|
|
|
|
},
|
2026-04-02 14:11:35 +08:00
|
|
|
});
|
2026-04-04 20:50:17 +08:00
|
|
|
|
|
|
|
|
return function(game: IGameContext<TState>, ...args: Parameters<TFunc>){
|
|
|
|
|
return game.runParsed({
|
|
|
|
|
options: {},
|
|
|
|
|
params: args,
|
|
|
|
|
flags: {},
|
|
|
|
|
name: parsedSchema.name,
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-04-04 00:54:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { GameHost, createGameHost } from './game-host';
|
2026-04-04 14:05:23 +08:00
|
|
|
export type { GameHostStatus, GameModule } from './game-host';
|
2026-04-04 11:06:41 +08:00
|
|
|
|
|
|
|
|
export function createGameModule<TState extends Record<string, unknown>>(
|
|
|
|
|
module: GameModule<TState>
|
|
|
|
|
): GameModule<TState> {
|
|
|
|
|
return module;
|
|
|
|
|
}
|