diff --git a/src/core/game.ts b/src/core/game.ts index 4028293..35b1e31 100644 --- a/src/core/game.ts +++ b/src/core/game.ts @@ -10,15 +10,17 @@ import { PromptEvent } from "../utils/command"; import {AsyncQueue} from "../utils/async-queue"; +import {signal, Signal} from "@preact/signals-core"; -export interface IGameContext { +export interface IGameContext = {} > { parts: ReturnType>; regions: ReturnType>; commands: CommandRunnerContextExport>; prompts: AsyncQueue; + state: Signal } -export function createGameContext( +export function createGameContext = {} >( commandRegistry: CommandRegistry>, initialState?: TState | (() => TState) ): IGameContext { @@ -32,7 +34,7 @@ export function createGameContext( regions, prompts, commands: null!, - state, + state: signal(state), } as IGameContext ctx.commands = createCommandRunnerContext(commandRegistry, ctx); @@ -45,7 +47,7 @@ export function createGameContext( * so that we can do `import * as tictactoe from './tic-tac-toe.ts';\n\n createGameContextFromModule(tictactoe);` * @param module */ -export function createGameContextFromModule( +export function createGameContextFromModule = {} >( module: { registry: CommandRegistry>, createInitialState: () => TState @@ -54,7 +56,7 @@ export function createGameContextFromModule( return createGameContext(module.registry, module.createInitialState); } -export function createGameCommand( +export function createGameCommand = {} , TResult = unknown>( schema: CommandSchema | string, run: (this: CommandRunnerContext>, command: Command) => Promise ): CommandRunner, TResult> { diff --git a/src/samples/tic-tac-toe.ts b/src/samples/tic-tac-toe.ts index 63d9a8c..05423e5 100644 --- a/src/samples/tic-tac-toe.ts +++ b/src/samples/tic-tac-toe.ts @@ -1,5 +1,5 @@ import { IGameContext, createGameCommand } from '../core/game'; -import { createCommandRegistry, type CommandRegistry, registerCommand } from '../utils/command'; +import { createCommandRegistry, registerCommand } from '../utils/command'; import type { Part } from '../core/part'; export type TicTacToeState = { @@ -77,7 +77,7 @@ export function placePiece(host: TicTacToeContext, row: number, col: number, mov board.value.children.push(host.parts.get(piece.id)); } -const setup = createGameCommand( +const setup = createGameCommand( 'setup', async function() { this.context.regions.add({ @@ -106,7 +106,7 @@ const setup = createGameCommand( +const turn = createGameCommand( 'turn ', async function(cmd) { const [turnPlayer, turnNumber] = cmd.params as [string, number];