2026-04-19 15:54:05 +08:00
|
|
|
import { MutableSignal, mutableSignal } from "@/utils/mutable-signal";
|
2026-04-23 15:07:52 +08:00
|
|
|
import { CommandSchema, parseCommandSchema, PromptDef } 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-23 15:07:52 +08:00
|
|
|
import { PromptContext } from "@/utils/command/command-prompt";
|
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;
|
2026-04-22 01:13:39 +08:00
|
|
|
produce(fn: (draft: TState) => undefined): void;
|
|
|
|
|
produceAsync(fn: (draft: TState) => undefined): Promise<void>;
|
2026-04-19 15:54:05 +08:00
|
|
|
prompt: <TResult, TArgs extends any[] = any[]>(
|
|
|
|
|
def: PromptDef<TArgs>,
|
|
|
|
|
validator: PromptValidator<TResult, TArgs>,
|
2026-04-23 15:07:52 +08:00
|
|
|
player?: string,
|
2026-04-19 15:54:05 +08:00
|
|
|
) => Promise<TResult>;
|
|
|
|
|
|
|
|
|
|
// test only
|
|
|
|
|
_state: MutableSignal<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> = {}>(
|
2026-04-23 15:07:52 +08:00
|
|
|
promptContext: PromptContext,
|
2026-04-19 15:54:05 +08:00
|
|
|
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);
|
2026-04-23 15:07:52 +08:00
|
|
|
const { prompt } = promptContext;
|
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;
|
|
|
|
|
},
|
2026-04-22 01:13:39 +08:00
|
|
|
produce(fn: (draft: TState) => undefined) {
|
2026-04-19 15:54:05 +08:00
|
|
|
return state.produce(fn);
|
|
|
|
|
},
|
2026-04-22 01:13:39 +08:00
|
|
|
produceAsync(fn: (draft: TState) => undefined) {
|
2026-04-19 15:54:05 +08:00
|
|
|
return state.produceAsync(fn);
|
|
|
|
|
},
|
2026-04-23 15:07:52 +08:00
|
|
|
prompt,
|
2026-04-04 18:29:33 +08:00
|
|
|
|
2026-04-19 15:54:05 +08:00
|
|
|
_state: state,
|
|
|
|
|
_rng: new Mulberry32RNG(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return context;
|
2026-04-06 15:47:33 +08:00
|
|
|
}
|
2026-04-19 15:54:05 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
}
|