2026-04-19 15:54:05 +08:00
|
|
|
import { MutableSignal, mutableSignal } from "@/utils/mutable-signal";
|
2026-04-02 10:48:20 +08:00
|
|
|
import {
|
2026-04-19 15:54:05 +08:00
|
|
|
Command,
|
|
|
|
|
CommandRegistry,
|
|
|
|
|
CommandResult,
|
|
|
|
|
CommandRunnerContextExport,
|
|
|
|
|
CommandSchema,
|
|
|
|
|
createCommandRegistry,
|
|
|
|
|
createCommandRunnerContext,
|
|
|
|
|
parseCommandSchema,
|
2026-04-02 15:59:27 +08:00
|
|
|
} from "@/utils/command";
|
2026-04-19 15:54:05 +08:00
|
|
|
import { PromptValidator } from "@/utils/command/command-runner";
|
|
|
|
|
import { Mulberry32RNG, ReadonlyRNG, RNG } from "@/utils/rng";
|
2026-04-02 10:26:42 +08:00
|
|
|
|
2026-04-19 15:54:05 +08:00
|
|
|
export interface IGameContext<TState extends Record<string, unknown> = {}> {
|
|
|
|
|
get value(): TState;
|
|
|
|
|
get rng(): ReadonlyRNG;
|
|
|
|
|
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: <TResult, TArgs extends any[] = any[]>(
|
|
|
|
|
def: PromptDef<TArgs>,
|
|
|
|
|
validator: PromptValidator<TResult, TArgs>,
|
|
|
|
|
currentPlayer?: string | null,
|
|
|
|
|
) => Promise<TResult>;
|
|
|
|
|
|
|
|
|
|
// test only
|
|
|
|
|
_state: MutableSignal<TState>;
|
|
|
|
|
_commands: CommandRunnerContextExport<IGameContext<TState>>;
|
|
|
|
|
_rng: RNG;
|
2026-04-02 10:26:42 +08:00
|
|
|
}
|
2026-04-19 15:54:05 +08:00
|
|
|
export type IGameContextExport<TState extends Record<string, unknown> = {}> =
|
|
|
|
|
Omit<IGameContext<TState>, "_state" | "_commands" | "_rng">;
|
2026-04-02 10:26:42 +08:00
|
|
|
|
2026-04-19 15:54:05 +08:00
|
|
|
export function createGameContext<TState extends Record<string, unknown> = {}>(
|
|
|
|
|
commandRegistry: CommandRegistry<IGameContext<TState>>,
|
|
|
|
|
initialState?: TState | (() => TState),
|
2026-04-02 12:48:29 +08:00
|
|
|
): IGameContext<TState> {
|
2026-04-19 15:54:05 +08:00
|
|
|
const stateValue =
|
|
|
|
|
typeof initialState === "function"
|
|
|
|
|
? initialState()
|
|
|
|
|
: (initialState ?? ({} as TState));
|
|
|
|
|
const state = mutableSignal(stateValue);
|
|
|
|
|
let commands: CommandRunnerContextExport<IGameContext<TState>> = null as any;
|
2026-04-02 12:48:29 +08:00
|
|
|
|
2026-04-19 15:54:05 +08:00
|
|
|
const context: IGameContext<TState> = {
|
|
|
|
|
get value(): TState {
|
|
|
|
|
return state.value;
|
|
|
|
|
},
|
|
|
|
|
get rng() {
|
|
|
|
|
return this._rng;
|
|
|
|
|
},
|
|
|
|
|
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(def, validator, currentPlayer) {
|
|
|
|
|
return commands.prompt(
|
|
|
|
|
def.schema,
|
|
|
|
|
validator,
|
|
|
|
|
def.hintText,
|
|
|
|
|
currentPlayer,
|
|
|
|
|
);
|
|
|
|
|
},
|
2026-04-04 18:29:33 +08:00
|
|
|
|
2026-04-19 15:54:05 +08:00
|
|
|
_state: state,
|
|
|
|
|
_commands: commands,
|
|
|
|
|
_rng: new Mulberry32RNG(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context._commands = commands = createCommandRunnerContext(
|
|
|
|
|
commandRegistry,
|
|
|
|
|
context,
|
|
|
|
|
);
|
2026-04-02 10:48:20 +08:00
|
|
|
|
2026-04-19 15:54:05 +08:00
|
|
|
return context;
|
2026-04-06 15:47:33 +08:00
|
|
|
}
|
2026-04-19 15:54:05 +08:00
|
|
|
|
|
|
|
|
export type PromptDef<TArgs extends any[] = any[]> = {
|
|
|
|
|
schema: CommandSchema;
|
|
|
|
|
hintText?: string;
|
|
|
|
|
};
|
|
|
|
|
export function createPromptDef<TArgs extends any[] = any[]>(
|
|
|
|
|
schema: CommandSchema | string,
|
|
|
|
|
hintText?: string,
|
|
|
|
|
): PromptDef<TArgs> {
|
|
|
|
|
schema = typeof schema === "string" ? parseCommandSchema(schema) : schema;
|
|
|
|
|
return { schema, hintText };
|
2026-04-06 15:47:33 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-19 15:54:05 +08:00
|
|
|
export function createGameCommandRegistry<
|
|
|
|
|
TState extends Record<string, unknown> = {},
|
|
|
|
|
>() {
|
|
|
|
|
return createCommandRegistry<IGameContext<TState>>();
|
|
|
|
|
}
|