refactor(samples/tic-tac-toe): simplify Client implementation

Remove generic EntityMap and TriggerMap types in favor of explicit
selector methods. This simplifies the Client class by removing
unnecessary interface implementations and conditional logic in
selectors.
This commit is contained in:
hypercross 2026-04-25 17:58:16 +08:00
parent 03db1e8a06
commit a0b2003c65
1 changed files with 9 additions and 25 deletions

View File

@ -1,7 +1,7 @@
import { Part } from "@/core/part";
import { createRegion, Region } from "@/core/region";
import { createPromptDef, IGameContext } from "@/core/game";
import { BaseGameClient, GameClient } from "@/core/game-client";
import { BaseGameClient } from "@/core/game-client";
import { createMiddlewareChain } from "@/utils/middleware";
const BOARD_SIZE = 3;
@ -186,14 +186,7 @@ export async function placePiece(
});
}
type EntityMap = { parts: TicTacToePart; board: Region };
type TriggerMap = {
turn: { player: PlayerType; turn: number };
};
export class Client
extends BaseGameClient<TicTacToeState>
implements GameClient<EntityMap, TriggerMap>
{
export class Client extends BaseGameClient<TicTacToeState> {
private onTurn = createMiddlewareChain(
async (ctx: { player: PlayerType; turn: number }) => {
return await turn(this._context, ctx.player, ctx.turn);
@ -225,22 +218,13 @@ export class Client
return game.value;
}
select<K extends keyof EntityMap>(
type: K,
id: string,
callback: (t: EntityMap[K]) => void,
): () => void {
if (type === "parts")
return this.assignSelector((state) => state.parts[id], callback);
return function () {};
selectPart(id: string, callback: (t: TicTacToePart) => void): () => void {
return this.assignSelector((state) => state.parts[id], callback);
}
use<K extends keyof TriggerMap>(
trigger: K,
callback: (ctx: TriggerMap[K], next: () => Promise<any>) => Promise<any>,
): () => void {
if (trigger === "turn") {
return this.onTurn.use(callback);
}
return function () {};
selectTurn(callback: (turns: number) => void): () => void {
return this.assignSelector((s) => s.turn, callback);
}
selectCurrentPlayer(callback: (player: PlayerType) => void) {
return this.assignSelector((s) => s.currentPlayer, callback);
}
}