From 2d5200bdb7e20bf773e8ed48abe788129dfaba08 Mon Sep 17 00:00:00 2001 From: hypercross Date: Mon, 6 Apr 2026 09:46:59 +0800 Subject: [PATCH] refactor: renaming api --- README.md | 2 +- src/core/game-host.ts | 12 +++---- src/samples/boop/commands.ts | 12 +++---- src/samples/tic-tac-toe.ts | 6 ++-- src/utils/command/command-registry.ts | 2 +- tests/core/game-host.test.ts | 46 +++++++++++++-------------- tests/samples/tic-tac-toe.test.ts | 4 +-- 7 files changed, 42 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index b26b586..877fe9e 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ import { createGameHost } from 'boardgame-core'; import * as tictactoe from 'boardgame-core/samples/tic-tac-toe'; const host = createGameHost(tictactoe); -await host.setup('setup'); +await host.start('start'); // 监听状态变化 effect(() => { diff --git a/src/core/game-host.ts b/src/core/game-host.ts index 6a95614..0f20223 100644 --- a/src/core/game-host.ts +++ b/src/core/game-host.ts @@ -23,7 +23,7 @@ export class GameHost> { private _activePromptSchema: Signal; private _activePromptPlayer: Signal; private _createInitialState: () => TState; - private _eventListeners: Map<'setup' | 'dispose', Set<() => void>>; + private _eventListeners: Map<'start' | 'dispose', Set<() => void>>; private _isDisposed = false; constructor( @@ -94,7 +94,7 @@ export class GameHost> { this._state.clearInterruptions(); } - setup(setupCommand: string): Promise> { + start(startCommand: string): Promise> { if (this._isDisposed) { throw new Error('GameHost is disposed'); } @@ -104,10 +104,10 @@ export class GameHost> { const initialState = this._createInitialState(); this._state.value = initialState as any; - const promise = this._commands.run(setupCommand); + const promise = this._commands.run(startCommand); this._status.value = 'running'; - this._emitEvent('setup'); + this._emitEvent('start'); return promise; } @@ -126,7 +126,7 @@ export class GameHost> { this._eventListeners.clear(); } - on(event: 'setup' | 'dispose', listener: () => void): () => void { + on(event: 'start' | 'dispose', listener: () => void): () => void { if (!this._eventListeners.has(event)) { this._eventListeners.set(event, new Set()); } @@ -137,7 +137,7 @@ export class GameHost> { }; } - private _emitEvent(event: 'setup' | 'dispose') { + private _emitEvent(event: 'start' | 'dispose') { const listeners = this._eventListeners.get(event); if (listeners) { for (const listener of listeners) { diff --git a/src/samples/boop/commands.ts b/src/samples/boop/commands.ts index e19fee2..9b3e3c6 100644 --- a/src/samples/boop/commands.ts +++ b/src/samples/boop/commands.ts @@ -42,7 +42,7 @@ async function place(game: BoopGame, row: number, col: number, player: PlayerTyp return { row, col, player, type, partId }; } -const placeCommand = registry.register( 'place ', place); +const placeCommand = registry.asCommand( 'place ', place); /** * 执行boop - 推动周围棋子 @@ -85,7 +85,7 @@ async function boop(game: BoopGame, row: number, col: number, type: PieceType) { return { booped }; } -const boopCommand = registry.register('boop ', boop); +const boopCommand = registry.asCommand('boop ', boop); /** * 检查是否有玩家获胜(三个猫连线) @@ -109,7 +109,7 @@ async function checkWin(game: BoopGame): Promise { } return null; } -const checkWinCommand = registry.register('check-win', checkWin); +const checkWinCommand = registry.asCommand('check-win', checkWin); /** * 检查并执行小猫升级(三个小猫连线变成猫) @@ -145,7 +145,7 @@ async function checkGraduates(game: BoopGame){ } }); } -const checkGraduatesCommand = registry.register('check-graduates', checkGraduates); +const checkGraduatesCommand = registry.asCommand('check-graduates', checkGraduates); async function setup(game: BoopGame) { while (true) { @@ -163,7 +163,7 @@ async function setup(game: BoopGame) { return game.value; } -registry.register('setup', setup); +registry.asCommand('setup', setup); async function checkFullBoard(game: BoopGame, turnPlayer: PlayerType){ // 检查8-piece规则: 如果玩家所有8个棋子都在棋盘上且没有获胜,强制升级一个小猫 @@ -238,4 +238,4 @@ async function turn(game: BoopGame, turnPlayer: PlayerType) { await checkFullBoard(game, turnPlayer); return { winner: null }; } -const turnCommand = registry.register('turn ', turn); \ No newline at end of file +const turnCommand = registry.asCommand('turn ', turn); \ No newline at end of file diff --git a/src/samples/tic-tac-toe.ts b/src/samples/tic-tac-toe.ts index cff6288..094492b 100644 --- a/src/samples/tic-tac-toe.ts +++ b/src/samples/tic-tac-toe.ts @@ -36,7 +36,7 @@ export type TicTacToeState = ReturnType; export type TicTacToeGame = IGameContext; export const registry = createGameCommandRegistry(); -async function setup(game: TicTacToeGame) { +async function start(game: TicTacToeGame) { while (true) { const currentPlayer = game.value.currentPlayer; const turnNumber = game.value.turn + 1; @@ -54,7 +54,7 @@ async function setup(game: TicTacToeGame) { return game.value; } -registry.register('setup', setup); +registry.asCommand('start', start); async function turn(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) { const {player, row, col} = await game.prompt( @@ -83,7 +83,7 @@ async function turn(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: num return { winner: null }; } -const turnCommand = registry.register('turn ', turn); +const turnCommand = registry.asCommand('turn ', turn); function isValidMove(row: number, col: number): boolean { return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE; diff --git a/src/utils/command/command-registry.ts b/src/utils/command/command-registry.ts index 4a88a96..5d6e8aa 100644 --- a/src/utils/command/command-registry.ts +++ b/src/utils/command/command-registry.ts @@ -18,7 +18,7 @@ type CanRunParsed = { type CmdFunc = (ctx: TContext, ...args: any[]) => Promise; export class CommandRegistry extends Map>{ - register = CmdFunc>(schema: CommandSchema | string, run: TFunc) { + asCommand = CmdFunc>(schema: CommandSchema | string, run: TFunc) { const parsedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema; registerCommand(this, { schema: parsedSchema, diff --git a/tests/core/game-host.test.ts b/tests/core/game-host.test.ts index 7c85bfd..d790974 100644 --- a/tests/core/game-host.test.ts +++ b/tests/core/game-host.test.ts @@ -59,7 +59,7 @@ describe('GameHost', () => { const { host } = createTestHost(); const promptPromise = waitForPromptEvent(host); - const runPromise = host.context._commands.run('setup'); + const runPromise = host.context._commands.run('start'); const promptEvent = await promptPromise; expect(promptEvent.schema.name).toBe('play'); @@ -68,7 +68,7 @@ describe('GameHost', () => { const error = host.onInput('play X 1 1'); expect(error).toBeNull(); - // Cancel to end the game since setup runs until game over + // Cancel to end the game since start runs until game over const nextPromptPromise = waitForPromptEvent(host); const nextPrompt = await nextPromptPromise; nextPrompt.cancel('test cleanup'); @@ -81,7 +81,7 @@ describe('GameHost', () => { const { host } = createTestHost(); const promptPromise = waitForPromptEvent(host); - const runPromise = host.context._commands.run('setup'); + const runPromise = host.context._commands.run('start'); const promptEvent = await promptPromise; @@ -106,7 +106,7 @@ describe('GameHost', () => { const { host } = createTestHost(); const promptPromise = waitForPromptEvent(host); - const runPromise = host.context._commands.run('setup'); + const runPromise = host.context._commands.run('start'); const promptEvent = await promptPromise; const schema = host.activePromptSchema.value; @@ -125,13 +125,13 @@ describe('GameHost', () => { }); }); - describe('setup', () => { - it('should reset state and run setup command', async () => { + describe('start', () => { + it('should reset state and run start command', async () => { const { host } = createTestHost(); // First setup - make one move let promptPromise = waitForPromptEvent(host); - let runPromise = host.context._commands.run('setup'); + let runPromise = host.context._commands.run('start'); let promptEvent = await promptPromise; // Make a move @@ -146,11 +146,11 @@ describe('GameHost', () => { expect(result.success).toBe(false); // Cancelled expect(Object.keys(host.context._state.value.parts).length).toBe(1); - // Setup listener before calling setup + // Setup listener before calling start const newPromptPromise = waitForPromptEvent(host); // Reset - should reset state and start new game - host.setup('setup'); + host.start('start'); // State should be back to initial expect(host.context._state.value.currentPlayer).toBe('X'); @@ -164,16 +164,16 @@ describe('GameHost', () => { newPrompt.cancel('test end'); }); - it('should cancel active prompt during setup', async () => { + it('should cancel active prompt during start', async () => { const { host } = createTestHost(); const promptPromise = waitForPromptEvent(host); - const runPromise = host.context._commands.run('setup'); + const runPromise = host.context._commands.run('start'); await promptPromise; // Setup should cancel the active prompt and reset state - host.setup('setup'); + host.start('start'); // The original runPromise should be rejected due to cancellation try { @@ -192,7 +192,7 @@ describe('GameHost', () => { const { host } = createTestHost(); host.dispose(); - expect(() => host.setup('setup')).toThrow('GameHost is disposed'); + expect(() => host.start('start')).toThrow('GameHost is disposed'); }); }); @@ -208,7 +208,7 @@ describe('GameHost', () => { const { host } = createTestHost(); const promptPromise = waitForPromptEvent(host); - const runPromise = host.context._commands.run('setup'); + const runPromise = host.context._commands.run('start'); await promptPromise; @@ -233,11 +233,11 @@ describe('GameHost', () => { }); describe('events', () => { - it('should emit setup event', async () => { + it('should emit start event', async () => { const { host } = createTestHost(); let setupCount = 0; - host.on('setup', () => { + host.on('start', () => { setupCount++; }); @@ -245,7 +245,7 @@ describe('GameHost', () => { const promptPromise = waitForPromptEvent(host); // Initial setup via reset - host.setup('setup'); + host.start('start'); expect(setupCount).toBe(1); // State should be running @@ -272,7 +272,7 @@ describe('GameHost', () => { const { host } = createTestHost(); let setupCount = 0; - const unsubscribe = host.on('setup', () => { + const unsubscribe = host.on('start', () => { setupCount++; }); @@ -294,7 +294,7 @@ describe('GameHost', () => { // Make a move const promptPromise = waitForPromptEvent(host); - const runPromise = host.context._commands.run('setup'); + const runPromise = host.context._commands.run('start'); const promptEvent = await promptPromise; promptEvent.tryCommit({ name: 'play', params: ['X', 1, 1], options: {}, flags: {} }); @@ -320,7 +320,7 @@ describe('GameHost', () => { // Start a command that triggers prompt const promptPromise = waitForPromptEvent(host); - const runPromise = host.context._commands.run('setup'); + const runPromise = host.context._commands.run('start'); await promptPromise; @@ -369,7 +369,7 @@ describe('GameHost', () => { }); // Start setup command (runs game loop until completion) - const setupPromise = host.context._commands.run('setup'); + const setupPromise = host.context._commands.run('start'); for (let i = 0; i < moves.length; i++) { // Wait until the next prompt event arrives @@ -415,7 +415,7 @@ describe('GameHost', () => { const { host } = createTestHost(); const promptPromise = waitForPromptEvent(host); - const runPromise = host.context._commands.run('setup'); + const runPromise = host.context._commands.run('start'); const promptEvent = await promptPromise; expect(promptEvent.currentPlayer).toBe('X'); @@ -433,7 +433,7 @@ describe('GameHost', () => { // First prompt - X's turn let promptPromise = waitForPromptEvent(host); - let runPromise = host.context._commands.run('setup'); + let runPromise = host.context._commands.run('start'); let promptEvent = await promptPromise; expect(promptEvent.currentPlayer).toBe('X'); expect(host.activePromptPlayer.value).toBe('X'); diff --git a/tests/samples/tic-tac-toe.test.ts b/tests/samples/tic-tac-toe.test.ts index 53379e6..b1e97d2 100644 --- a/tests/samples/tic-tac-toe.test.ts +++ b/tests/samples/tic-tac-toe.test.ts @@ -176,7 +176,7 @@ describe('TicTacToe - game flow', () => { it('should have setup and turn commands registered', () => { const { registry: reg } = createTestContext(); - expect(reg.has('setup')).toBe(true); + expect(reg.has('start')).toBe(true); expect(reg.has('turn')).toBe(true); }); @@ -184,7 +184,7 @@ describe('TicTacToe - game flow', () => { const { ctx } = createTestContext(); const promptPromise = waitForPrompt(ctx); - const runPromise = ctx.run('setup'); + const runPromise = ctx.run('start'); const promptEvent = await promptPromise; expect(promptEvent).not.toBeNull();