Compare commits

...

2 Commits

Author SHA1 Message Date
hypercross a8774f34bc refactor: make stuff peer dependencies 2026-04-06 12:38:24 +08:00
hypercross 49109963bc refactor: hide api 2026-04-06 12:05:11 +08:00
4 changed files with 36 additions and 25 deletions

14
package-lock.json generated
View File

@ -8,19 +8,23 @@
"name": "boardgame-core",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"devDependencies": {
"@preact/signals-core": "^1.5.1",
"inline-schema": "file:../inline-schema",
"mutative": "^1.3.0"
},
"devDependencies": {
"mutative": "^1.3.0",
"tsup": "^8.0.2",
"typescript": "^5.3.3",
"vitest": "^1.3.1"
},
"peerDependencies": {
"@preact/signals-core": "^1.5.1",
"inline-schema": "file:../inline-schema",
"mutative": "^1.3.0"
}
},
"../inline-schema": {
"version": "1.0.0",
"dev": true,
"license": "ISC",
"dependencies": {
"csv-parse": "^5.5.6"
@ -534,6 +538,7 @@
"version": "1.14.0",
"resolved": "https://registry.npmjs.org/@preact/signals-core/-/signals-core-1.14.0.tgz",
"integrity": "sha512-AowtCcCU/33lFlh1zRFf/u+12rfrhtNakj7UpaGEsmMwUKpKWMVvcktOGcwBBNiB4lWrZWc01LhiyyzVklJyaQ==",
"dev": true,
"license": "MIT",
"funding": {
"type": "opencollective",
@ -1511,6 +1516,7 @@
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/mutative/-/mutative-1.3.0.tgz",
"integrity": "sha512-8MJj6URmOZAV70dpFe1YnSppRTKC4DsMkXQiBDFayLcDI4ljGokHxmpqaBQuDWa4iAxWaJJ1PS8vAmbntjjKmQ==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=14.0"

View File

@ -18,12 +18,15 @@
"test:run": "vitest run",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"peerDependencies": {
"@preact/signals-core": "^1.5.1",
"inline-schema": "file:../inline-schema",
"mutative": "^1.3.0"
},
"devDependencies": {
"@preact/signals-core": "^1.5.1",
"inline-schema": "file:../inline-schema",
"mutative": "^1.3.0",
"tsup": "^8.0.2",
"typescript": "^5.3.3",
"vitest": "^1.3.1"

View File

@ -1,23 +1,20 @@
import { ReadonlySignal, Signal } from '@preact/signals-core';
import type {
import {
CommandSchema,
CommandRegistry,
PromptEvent,
CommandRunnerContextExport,
} from '@/utils/command';
import type { MutableSignal } from '@/utils/mutable-signal';
import {createGameCommandRegistry, createGameContext, IGameContext} from './game';
export type GameHostStatus = 'created' | 'running' | 'disposed';
export class GameHost<TState extends Record<string, unknown>, TResult=unknown> {
readonly context: IGameContext<TState>;
readonly state: ReadonlySignal<TState>;
readonly status: ReadonlySignal<GameHostStatus>;
readonly activePromptSchema: ReadonlySignal<CommandSchema | null>;
readonly activePromptPlayer: ReadonlySignal<string | null>;
private _state: MutableSignal<TState>;
private _commands: CommandRunnerContextExport<IGameContext<TState>>;
private _context: IGameContext<TState>;
private _start: (ctx: IGameContext<TState>) => Promise<TResult>;
private _status: Signal<GameHostStatus>;
private _activePromptSchema: Signal<CommandSchema | null>;
@ -35,8 +32,9 @@ export class GameHost<TState extends Record<string, unknown>, TResult=unknown> {
this._eventListeners = new Map();
const initialState = createInitialState();
this.context = createGameContext(registry, initialState);
this._context = createGameContext(registry, initialState);
this._start = start;
this.state = this._context._state;
this._status = new Signal<GameHostStatus>('created');
this.status = this._status;
@ -47,22 +45,19 @@ export class GameHost<TState extends Record<string, unknown>, TResult=unknown> {
this._activePromptPlayer = new Signal<string | null>(null);
this.activePromptPlayer = this._activePromptPlayer;
this._state = this.context._state;
this._commands = this.context._commands;
this._setupPromptTracking();
}
private _setupPromptTracking() {
let currentPromptEvent: PromptEvent | null = null;
this._commands.on('prompt', (e) => {
this._context._commands.on('prompt', (e) => {
currentPromptEvent = e as PromptEvent;
this._activePromptSchema.value = currentPromptEvent.schema;
this._activePromptPlayer.value = currentPromptEvent.currentPlayer;
});
this._commands.on('promptEnd', () => {
this._context._commands.on('promptEnd', () => {
currentPromptEvent = null;
this._activePromptSchema.value = null;
this._activePromptPlayer.value = null;
@ -77,7 +72,7 @@ export class GameHost<TState extends Record<string, unknown>, TResult=unknown> {
if (this._isDisposed) {
return 'GameHost is disposed';
}
return this._commands._tryCommit(input);
return this._context._commands._tryCommit(input);
}
/**
@ -85,7 +80,7 @@ export class GameHost<TState extends Record<string, unknown>, TResult=unknown> {
* @see MutableSignal.addInterruption
*/
addInterruption(promise: Promise<void>): void {
this._state.addInterruption(promise);
this._context._state.addInterruption(promise);
}
/**
@ -93,7 +88,7 @@ export class GameHost<TState extends Record<string, unknown>, TResult=unknown> {
* @see MutableSignal.clearInterruptions
*/
clearInterruptions(): void {
this._state.clearInterruptions();
this._context._state.clearInterruptions();
}
start(): Promise<TResult> {
@ -101,12 +96,12 @@ export class GameHost<TState extends Record<string, unknown>, TResult=unknown> {
throw new Error('GameHost is disposed');
}
this._commands._cancel();
this._context._commands._cancel();
const initialState = this._createInitialState();
this._state.value = initialState as any;
this._context._state.value = initialState as any;
const promise = this._start(this.context);
const promise = this._start(this._context);
this._status.value = 'running';
this._emitEvent('start');
@ -120,7 +115,7 @@ export class GameHost<TState extends Record<string, unknown>, TResult=unknown> {
}
this._isDisposed = true;
this._commands._cancel();
this._context._commands._cancel();
this._status.value = 'disposed';
// Emit dispose event BEFORE clearing listeners

View File

@ -10,11 +10,18 @@ import {
import { createGameHost, GameHost } from '@/core/game-host';
import type { PromptEvent } from '@/utils/command';
import { MutableSignal } from '@/utils/mutable-signal';
import {IGameContext} from "../../src";
type TestGameHost = GameHost<TicTacToeState> & {
_context: IGameContext<TicTacToeState>;
context: IGameContext<TicTacToeState>;
}
function createTestHost() {
const host = createGameHost(
const host: TestGameHost = createGameHost(
{ registry, createInitialState, start }
);
host.context = host._context;
return { host };
}