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