From af9254603a64c39df975cd8877580f5c831643b8 Mon Sep 17 00:00:00 2001 From: hypercross Date: Sat, 4 Apr 2026 20:57:58 +0800 Subject: [PATCH] refactor: don't use this --- src/samples/tic-tac-toe.ts | 34 ++++++++++++++------------- src/utils/command/command-registry.ts | 9 +++---- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/samples/tic-tac-toe.ts b/src/samples/tic-tac-toe.ts index 13f859e..7526af9 100644 --- a/src/samples/tic-tac-toe.ts +++ b/src/samples/tic-tac-toe.ts @@ -1,6 +1,6 @@ import { createGameCommandRegistry, Part, createRegion, createPart, isCellOccupied as isCellOccupiedUtil, - registerGameCommand, IGameContext + IGameContext } from '@/index'; const BOARD_SIZE = 3; @@ -37,28 +37,29 @@ export type TicTacToeState = ReturnType; export type TicTacToeGame = IGameContext; export const registry = createGameCommandRegistry(); -registerGameCommand(registry, 'setup', async function() { +async function setup(game: TicTacToeGame) { while (true) { - const currentPlayer = this.value.currentPlayer; - const turnNumber = this.value.turn + 1; - const turnOutput = await this.run<{winner: WinnerType}>(`turn ${currentPlayer} ${turnNumber}`); + const currentPlayer = game.value.currentPlayer; + const turnNumber = game.value.turn + 1; + const turnOutput = await turnCommand(game, currentPlayer, turnNumber); if (!turnOutput.success) throw new Error(turnOutput.error); - this.produce(state => { + game.produce(state => { state.winner = turnOutput.result.winner; if (!state.winner) { state.currentPlayer = state.currentPlayer === 'X' ? 'O' : 'X'; state.turn = turnNumber; } }); - if (this.value.winner) break; + if (game.value.winner) break; } - return this.value; -}); + return game.value; +} +registry.register('setup', setup); -registerGameCommand(registry, 'turn ', async function turn(turnPlayer: PlayerType, turnNumber: number) { - const playCmd = await this.prompt( +async function turn(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) { + const playCmd = await game.prompt( 'play ', (command) => { const [player, row, col] = command.params as [PlayerType, number, number]; @@ -69,23 +70,24 @@ registerGameCommand(registry, 'turn ', async fun if (!isValidMove(row, col)) { return `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`; } - if (isCellOccupied(this, row, col)) { + if (isCellOccupied(game, row, col)) { return `Cell (${row}, ${col}) is already occupied.`; } return null; }, - this.value.currentPlayer + game.value.currentPlayer ); const [player, row, col] = playCmd.params as [PlayerType, number, number]; - placePiece(this, row, col, turnPlayer); + placePiece(game, row, col, turnPlayer); - const winner = checkWinner(this); + const winner = checkWinner(game); if (winner) return { winner }; if (turnNumber >= MAX_TURNS) return { winner: 'draw' as WinnerType }; return { winner: null }; -}); +} +const turnCommand = registry.register('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 24721cd..9ed0e67 100644 --- a/src/utils/command/command-registry.ts +++ b/src/utils/command/command-registry.ts @@ -9,7 +9,7 @@ type CanRunParsed = { runParsed(command: Command): Promise>, } -type CmdFunc = (this: TContext, ...args: any[]) => Promise; +type CmdFunc = (ctx: TContext, ...args: any[]) => Promise; export class CommandRegistry extends Map>{ register = CmdFunc>(schema: CommandSchema | string, run: TFunc) { const parsedSchema = typeof schema === 'string' ? parseCommandSchema(schema) : schema; @@ -17,12 +17,13 @@ export class CommandRegistry extends Map, command: Command){ const params = command.params; - return await run.call(this.context, ...params); + return await run.call(this.context, this.context, ...params); }, }); - type TResult = TFunc extends (this: TContext, ...args: any[]) => Promise ? X : null; - return function(ctx: TContext & CanRunParsed, ...args: Parameters){ + type TParams = TFunc extends (ctx: TContext, ...args: infer X) => Promise ? X : null; + type TResult = TFunc extends (ctx: TContext, ...args: any[]) => Promise ? X : null; + return function(ctx: TContext & CanRunParsed, ...args: TParams){ return ctx.runParsed({ options: {}, params: args,