boardgame-core/src/core/game-host.ts

159 lines
5.0 KiB
TypeScript
Raw Normal View History

2026-04-04 00:54:19 +08:00
import { ReadonlySignal, signal, Signal } from '@preact/signals-core';
2026-04-04 10:30:00 +08:00
import type { CommandSchema, CommandRegistry, CommandResult, PromptEvent } from '@/utils/command';
2026-04-04 00:54:19 +08:00
import type { MutableSignal } from '@/utils/mutable-signal';
import { createGameContext } from './game';
export type GameHostStatus = 'created' | 'running' | 'disposed';
export interface GameHostOptions {
autoStart?: boolean;
}
export class GameHost<TState extends Record<string, unknown>> {
readonly state: ReadonlySignal<TState>;
readonly commands: ReturnType<typeof createGameContext<TState>>['commands'];
readonly status: ReadonlySignal<GameHostStatus>;
readonly activePromptSchema: ReadonlySignal<CommandSchema | null>;
2026-04-04 10:30:00 +08:00
readonly activePromptPlayer: ReadonlySignal<string | null>;
2026-04-04 00:54:19 +08:00
private _state: MutableSignal<TState>;
private _status: Signal<GameHostStatus>;
private _activePromptSchema: Signal<CommandSchema | null>;
2026-04-04 10:30:00 +08:00
private _activePromptPlayer: Signal<string | null>;
2026-04-04 00:54:19 +08:00
private _createInitialState: () => TState;
private _setupCommand: string;
private _eventListeners: Map<'setup' | 'dispose', Set<() => void>>;
private _isDisposed = false;
constructor(
registry: CommandRegistry<MutableSignal<TState>>,
createInitialState: () => TState,
setupCommand: string,
options?: GameHostOptions
) {
this._createInitialState = createInitialState;
this._setupCommand = setupCommand;
this._eventListeners = new Map();
const initialState = createInitialState();
const context = createGameContext(registry, initialState);
this._state = context.state;
this.commands = context.commands;
this._status = new Signal<GameHostStatus>('created');
this.status = this._status;
this._activePromptSchema = new Signal<CommandSchema | null>(null);
this.activePromptSchema = this._activePromptSchema;
2026-04-04 10:30:00 +08:00
this._activePromptPlayer = new Signal<string | null>(null);
this.activePromptPlayer = this._activePromptPlayer;
2026-04-04 00:54:19 +08:00
this.state = this._state;
this._setupPromptTracking();
if (options?.autoStart !== false) {
this._status.value = 'running';
}
}
private _setupPromptTracking() {
2026-04-04 10:30:00 +08:00
let currentPromptEvent: PromptEvent | null = null;
2026-04-04 00:54:19 +08:00
2026-04-04 10:30:00 +08:00
this.commands.on('prompt', (e) => {
currentPromptEvent = e as PromptEvent;
this._activePromptSchema.value = currentPromptEvent.schema;
this._activePromptPlayer.value = currentPromptEvent.currentPlayer;
2026-04-04 00:59:40 +08:00
});
2026-04-04 00:54:19 +08:00
2026-04-04 00:59:40 +08:00
this.commands.on('promptEnd', () => {
2026-04-04 10:30:00 +08:00
currentPromptEvent = null;
this._activePromptSchema.value = null;
this._activePromptPlayer.value = null;
2026-04-04 00:54:19 +08:00
});
2026-04-04 10:30:00 +08:00
// Initial state
this._activePromptSchema.value = null;
this._activePromptPlayer.value = null;
2026-04-04 00:54:19 +08:00
}
onInput(input: string): string | null {
if (this._isDisposed) {
return 'GameHost is disposed';
}
return this.commands._tryCommit(input);
}
async setup(setupCommand: string): Promise<void> {
if (this._isDisposed) {
throw new Error('GameHost is disposed');
}
this.commands._cancel();
const initialState = this._createInitialState();
this._state.value = initialState as any;
// Start the setup command but don't wait for it to complete
// The command will run in the background and prompt for input
this.commands.run(setupCommand).catch(() => {
// Command may be cancelled or fail, which is expected
});
this._status.value = 'running';
this._emitEvent('setup');
}
dispose(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this.commands._cancel();
this._status.value = 'disposed';
// Emit dispose event BEFORE clearing listeners
this._emitEvent('dispose');
this._eventListeners.clear();
}
on(event: 'setup' | 'dispose', listener: () => void): () => void {
if (!this._eventListeners.has(event)) {
this._eventListeners.set(event, new Set());
}
this._eventListeners.get(event)!.add(listener);
return () => {
this._eventListeners.get(event)?.delete(listener);
};
}
private _emitEvent(event: 'setup' | 'dispose') {
const listeners = this._eventListeners.get(event);
if (listeners) {
for (const listener of listeners) {
listener();
}
}
}
}
export function createGameHost<TState extends Record<string, unknown>>(
module: {
registry: CommandRegistry<MutableSignal<TState>>;
createInitialState: () => TState;
},
setupCommand: string,
options?: GameHostOptions
): GameHost<TState> {
return new GameHost(
module.registry,
module.createInitialState,
setupCommand,
options
);
}