diff --git a/src/core/game.ts b/src/core/game.ts index 5f1c46e..9902e43 100644 --- a/src/core/game.ts +++ b/src/core/game.ts @@ -1,4 +1,4 @@ -import {entity, Entity} from "@/utils/entity"; +import {MutableSignal, mutableSignal} from "@/utils/mutable-signal"; import { Command, CommandRegistry, @@ -12,16 +12,16 @@ import { } from "@/utils/command"; export interface IGameContext = {} > { - state: Entity; - commands: CommandRunnerContextExport>; + state: MutableSignal; + commands: CommandRunnerContextExport>; } export function createGameContext = {} >( - commandRegistry: CommandRegistry>, + commandRegistry: CommandRegistry>, initialState?: TState | (() => TState) ): IGameContext { const stateValue = typeof initialState === 'function' ? initialState() : initialState ?? {} as TState; - const state = entity('state', stateValue); + const state = mutableSignal(stateValue); const commands = createCommandRunnerContext(commandRegistry, state); return { @@ -36,7 +36,7 @@ export function createGameContext = {} >( */ export function createGameContextFromModule = {} >( module: { - registry: CommandRegistry>, + registry: CommandRegistry>, createInitialState: () => TState }, ): IGameContext { @@ -44,12 +44,12 @@ export function createGameContextFromModule = {} >() { - const registry = createCommandRegistry>(); + const registry = createCommandRegistry>(); return { registry, add( schema: CommandSchema | string, - run: (this: CommandRunnerContext>, command: Command) => Promise + run: (this: CommandRunnerContext>, command: Command) => Promise ){ createGameCommand(registry, schema, run); return this; @@ -58,9 +58,9 @@ export function createGameCommandRegistry } export function createGameCommand = {} , TResult = unknown>( - registry: CommandRegistry>, + registry: CommandRegistry>, schema: CommandSchema | string, - run: (this: CommandRunnerContext>, command: Command) => Promise + run: (this: CommandRunnerContext>, command: Command) => Promise ) { registerCommand(registry, { schema: typeof schema === 'string' ? parseCommandSchema(schema) : schema, diff --git a/src/index.ts b/src/index.ts index 29f1c50..b42b63c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,8 +20,8 @@ export { parseCommand, parseCommandSchema, validateCommand, parseCommandWithSche export type { CommandRunner, CommandRunnerHandler, CommandRunnerContext, PromptEvent, CommandRunnerEvents } from './utils/command'; export { createCommandRegistry, registerCommand, unregisterCommand, hasCommand, getCommand, runCommand, runCommandParsed, createCommandRunnerContext, type CommandRegistry, type CommandRunnerContextExport } from './utils/command'; -export type { Entity } from './utils/entity'; -export { createEntityCollection, entity } from './utils/entity'; +export type { MutableSignal } from './utils/mutable-signal'; +export { mutableSignal, createEntityCollection } from './utils/mutable-signal'; export type { RNG } from './utils/rng'; export { createRNG, Mulberry32RNG } from './utils/rng'; diff --git a/src/samples/boop/index.ts b/src/samples/boop/index.ts index ab9c8c9..3ed793a 100644 --- a/src/samples/boop/index.ts +++ b/src/samples/boop/index.ts @@ -1,4 +1,4 @@ -import {createGameCommandRegistry, Part, Entity, createRegion} from '@/index'; +import {createGameCommandRegistry, Part, MutableSignal, createRegion} from '@/index'; const BOARD_SIZE = 6; const MAX_PIECES_PER_PLAYER = 8; @@ -48,7 +48,7 @@ export type BoopState = ReturnType; const registration = createGameCommandRegistry(); export const registry = registration.registry; -export function getPlayer(host: Entity, player: PlayerType): Player { +export function getPlayer(host: MutableSignal, player: PlayerType): Player { return host.value.players[player]; } @@ -152,23 +152,23 @@ function isValidMove(row: number, col: number): boolean { return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE; } -export function getBoardRegion(host: Entity) { +export function getBoardRegion(host: MutableSignal) { return host.value.board; } -export function isCellOccupied(host: Entity, row: number, col: number): boolean { +export function isCellOccupied(host: MutableSignal, row: number, col: number): boolean { const board = getBoardRegion(host); return board.partMap[`${row},${col}`] !== undefined; } -export function getPartAt(host: Entity, row: number, col: number): BoopPart | null { +export function getPartAt(host: MutableSignal, row: number, col: number): BoopPart | null { const board = getBoardRegion(host); const partId = board.partMap[`${row},${col}`]; if (!partId) return null; return host.value.pieces.find(p => p.id === partId) || null; } -export function placePiece(host: Entity, row: number, col: number, player: PlayerType, pieceType: PieceType) { +export function placePiece(host: MutableSignal, row: number, col: number, player: PlayerType, pieceType: PieceType) { const board = getBoardRegion(host); const playerData = getPlayer(host, player); const count = playerData[pieceType].placed + 1; @@ -188,7 +188,7 @@ export function placePiece(host: Entity, row: number, col: number, pl decrementSupply(playerData, pieceType); } -export function applyBoops(host: Entity, placedRow: number, placedCol: number, placedType: PieceType) { +export function applyBoops(host: MutableSignal, placedRow: number, placedCol: number, placedType: PieceType) { const board = getBoardRegion(host); const pieces = host.value.pieces; @@ -237,7 +237,7 @@ export function applyBoops(host: Entity, placedRow: number, placedCol } } -export function removePieceFromBoard(host: Entity, part: BoopPart) { +export function removePieceFromBoard(host: MutableSignal, part: BoopPart) { const board = getBoardRegion(host); const playerData = getPlayer(host, part.player); board.childIds = board.childIds.filter(id => id !== part.id); @@ -297,7 +297,7 @@ export function hasWinningLine(positions: number[][]): boolean { return false; } -export function checkGraduation(host: Entity, player: PlayerType): number[][][] { +export function checkGraduation(host: MutableSignal, player: PlayerType): number[][][] { const pieces = host.value.pieces; const posSet = new Set(); @@ -316,7 +316,7 @@ export function checkGraduation(host: Entity, player: PlayerType): nu return winningLines; } -export function processGraduation(host: Entity, player: PlayerType, lines: number[][][]) { +export function processGraduation(host: MutableSignal, player: PlayerType, lines: number[][][]) { const allPositions = new Set(); for (const line of lines) { for (const [r, c] of line) { @@ -338,12 +338,12 @@ export function processGraduation(host: Entity, player: PlayerType, l incrementSupply(playerData, 'cat', count); } -export function countPiecesOnBoard(host: Entity, player: PlayerType): number { +export function countPiecesOnBoard(host: MutableSignal, player: PlayerType): number { const pieces = host.value.pieces; return pieces.filter(p => p.player === player).length; } -export function checkWinner(host: Entity): WinnerType { +export function checkWinner(host: MutableSignal): WinnerType { const pieces = host.value.pieces; for (const player of ['white', 'black'] as PlayerType[]) { diff --git a/src/samples/tic-tac-toe.ts b/src/samples/tic-tac-toe.ts index add32eb..76e1b93 100644 --- a/src/samples/tic-tac-toe.ts +++ b/src/samples/tic-tac-toe.ts @@ -1,4 +1,4 @@ -import {createGameCommandRegistry, Part, Entity, createRegion, moveToRegion} from '@/index'; +import {createGameCommandRegistry, Part, MutableSignal, createRegion, moveToRegion} from '@/index'; const BOARD_SIZE = 3; const MAX_TURNS = BOARD_SIZE * BOARD_SIZE; @@ -90,7 +90,7 @@ function isValidMove(row: number, col: number): boolean { return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE; } -export function isCellOccupied(host: Entity, row: number, col: number): boolean { +export function isCellOccupied(host: MutableSignal, row: number, col: number): boolean { const board = host.value.board; return board.partMap[`${row},${col}`] !== undefined; } @@ -103,7 +103,7 @@ export function hasWinningLine(positions: number[][]): boolean { ); } -export function checkWinner(host: Entity): WinnerType { +export function checkWinner(host: MutableSignal): WinnerType { const parts = Object.values(host.value.parts); const xPositions = parts.filter((p: TicTacToePart) => p.player === 'X').map((p: TicTacToePart) => p.position); @@ -116,7 +116,7 @@ export function checkWinner(host: Entity): WinnerType { return null; } -export function placePiece(host: Entity, row: number, col: number, player: PlayerType) { +export function placePiece(host: MutableSignal, row: number, col: number, player: PlayerType) { const board = host.value.board; const moveNumber = Object.keys(host.value.parts).length + 1; const piece: TicTacToePart = { diff --git a/src/utils/entity.ts b/src/utils/mutable-signal.ts similarity index 62% rename from src/utils/entity.ts rename to src/utils/mutable-signal.ts index b50993e..8bcceca 100644 --- a/src/utils/entity.ts +++ b/src/utils/mutable-signal.ts @@ -1,8 +1,8 @@ import {Signal, signal, SignalOptions} from "@preact/signals-core"; import {create} from 'mutative'; -export class Entity extends Signal { - public constructor(public readonly id: string, t?: T, options?: SignalOptions) { +export class MutableSignal extends Signal { + public constructor(t?: T, options?: SignalOptions) { super(t, options); } produce(fn: (draft: T) => void) { @@ -10,19 +10,19 @@ export class Entity extends Signal { } } -export function entity(id: string, t?: T, options?: SignalOptions) { - return new Entity(id, t, options); +export function mutableSignal(initial?: T, options?: SignalOptions): MutableSignal { + return new MutableSignal(initial, options); } export type EntityCollection = { - collection: Signal>>; + collection: Signal>>; remove(...ids: string[]): void; add(...entities: (T & {id: string})[]): void; - get(id: string): Entity; + get(id: string): MutableSignal; } export function createEntityCollection(): EntityCollection { - const collection = signal({} as Record>); + const collection = signal({} as Record>); const remove = (...ids: string[]) => { collection.value = Object.fromEntries( Object.entries(collection.value).filter(([id]) => !ids.includes(id)), @@ -32,7 +32,7 @@ export function createEntityCollection(): EntityCollection { const add = (...entities: (T & {id: string})[]) => { collection.value = { ...collection.value, - ...Object.fromEntries(entities.map((e) => [e.id, entity(e.id, e)])), + ...Object.fromEntries(entities.map((e) => [e.id, mutableSignal(e)])), }; }; @@ -44,4 +44,4 @@ export function createEntityCollection(): EntityCollection { add, get } -} \ No newline at end of file +} diff --git a/tests/samples/boop.test.ts b/tests/samples/boop.test.ts index 61a1175..5170e2b 100644 --- a/tests/samples/boop.test.ts +++ b/tests/samples/boop.test.ts @@ -16,7 +16,7 @@ import { PlayerType, getBoardRegion, } from '@/samples/boop'; -import {Entity} from "@/utils/entity"; +import {MutableSignal} from "@/utils/entity"; import {createGameContext} from "@/"; import type { PromptEvent } from '@/utils/command'; @@ -25,7 +25,7 @@ function createTestContext() { return { registry, ctx }; } -function getState(ctx: ReturnType['ctx']): Entity { +function getState(ctx: ReturnType['ctx']): MutableSignal { return ctx.state; } @@ -35,7 +35,7 @@ function waitForPrompt(ctx: ReturnType['ctx']): Promis }); } -function getParts(state: Entity) { +function getParts(state: MutableSignal) { return state.value.pieces; } diff --git a/tests/samples/tic-tac-toe.test.ts b/tests/samples/tic-tac-toe.test.ts index 1051c76..e54ab2c 100644 --- a/tests/samples/tic-tac-toe.test.ts +++ b/tests/samples/tic-tac-toe.test.ts @@ -8,7 +8,7 @@ import { TicTacToeState, WinnerType, PlayerType } from '@/samples/tic-tac-toe'; -import {Entity} from "@/utils/entity"; +import {MutableSignal} from "@/utils/mutable-signal"; import {createGameContext} from "@/"; import type { PromptEvent } from '@/utils/command'; @@ -17,7 +17,7 @@ function createTestContext() { return { registry, ctx }; } -function getState(ctx: ReturnType['ctx']): Entity { +function getState(ctx: ReturnType['ctx']): MutableSignal { return ctx.state; } diff --git a/tests/utils/entity.test.ts b/tests/utils/mutable-signal.test.ts similarity index 84% rename from tests/utils/entity.test.ts rename to tests/utils/mutable-signal.test.ts index 520415c..fbd95ea 100644 --- a/tests/utils/entity.test.ts +++ b/tests/utils/mutable-signal.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest'; -import { createEntityCollection, Entity, entity } from '@/utils/entity'; +import { createEntityCollection, MutableSignal, mutableSignal } from '@/utils/mutable-signal'; type TestEntity = { id: string; @@ -82,16 +82,6 @@ describe('createEntityCollection', () => { expect(collection.get('nonexistent')).toBeUndefined(); }); - it('should have correct accessor id', () => { - const collection = createEntityCollection(); - const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 }; - - collection.add(testEntity); - - const accessor = collection.get('e1'); - expect(accessor.id).toBe('e1'); - }); - it('should handle removing non-existent entity', () => { const collection = createEntityCollection(); const testEntity: TestEntity = { id: 'e1', name: 'Entity 1', value: 10 }; @@ -116,20 +106,19 @@ describe('createEntityCollection', () => { }); }); -describe('Entity', () => { - it('should create entity with id and value', () => { - const e = entity('test', { count: 1 }); - expect(e.id).toBe('test'); - expect(e.value.count).toBe(1); +describe('MutableSignal', () => { + it('should create signal with initial value', () => { + const s = mutableSignal({ count: 1 }); + expect(s.value.count).toBe(1); }); it('should produce immutable updates', () => { - const e = entity('test', { count: 1, items: [1, 2, 3] }); - e.produce(draft => { + const s = mutableSignal({ count: 1, items: [1, 2, 3] }); + s.produce(draft => { draft.count = 2; draft.items.push(4); }); - expect(e.value.count).toBe(2); - expect(e.value.items).toEqual([1, 2, 3, 4]); + expect(s.value.count).toBe(2); + expect(s.value.items).toEqual([1, 2, 3, 4]); }); });