refactor: update api

This commit is contained in:
2026-04-02 12:48:29 +08:00
parent 846badc081
commit 004d49c36f
4 changed files with 128 additions and 66 deletions
+25 -17
View File
@@ -1,7 +1,6 @@
import { IGameContext } from '../core/game';
import {CommandRegistry, CommandRunner, registerCommand} from '../utils/command';
import { IGameContext, createGameCommand } from '../core/game';
import { createCommandRegistry, type CommandRegistry, registerCommand } from '../utils/command';
import type { Part } from '../core/part';
import {createGameCommand} from "../core/game";
export type TicTacToeState = {
currentPlayer: 'X' | 'O';
@@ -9,15 +8,25 @@ export type TicTacToeState = {
moveCount: number;
};
export type TicTacToeContext = IGameContext<TicTacToeState>;
type TurnResult = {
winner: 'X' | 'O' | 'draw' | null;
};
export function getBoardRegion(host: IGameContext) {
export function createInitialState(): TicTacToeState {
return {
currentPlayer: 'X',
winner: null,
moveCount: 0,
};
}
export function getBoardRegion(host: TicTacToeContext) {
return host.regions.get('board');
}
export function isCellOccupied(host: IGameContext, row: number, col: number): boolean {
export function isCellOccupied(host: TicTacToeContext, row: number, col: number): boolean {
const board = getBoardRegion(host);
return board.value.children.some(
(child: { value: { position: number[] } }) => child.value.position[0] === row && child.value.position[1] === col
@@ -43,7 +52,7 @@ export function hasWinningLine(positions: number[][]): boolean {
);
}
export function checkWinner(host: IGameContext): 'X' | 'O' | 'draw' | null {
export function checkWinner(host: TicTacToeContext): 'X' | 'O' | 'draw' | null {
const parts = Object.values(host.parts.collection.value).map((s: { value: Part }) => s.value);
const xPositions = parts.filter((_: Part, i: number) => i % 2 === 0).map((p: Part) => p.position);
@@ -55,7 +64,7 @@ export function checkWinner(host: IGameContext): 'X' | 'O' | 'draw' | null {
return null;
}
export function placePiece(host: IGameContext, row: number, col: number, moveCount: number) {
export function placePiece(host: TicTacToeContext, row: number, col: number, moveCount: number) {
const board = getBoardRegion(host);
const piece: Part = {
id: `piece-${moveCount}`,
@@ -68,7 +77,7 @@ export function placePiece(host: IGameContext, row: number, col: number, moveCou
board.value.children.push(host.parts.get(piece.id));
}
const setup = createGameCommand(
const setup = createGameCommand<TicTacToeContext, { winner: 'X' | 'O' | 'draw' | null }>(
'setup',
async function() {
this.context.regions.add({
@@ -81,23 +90,23 @@ const setup = createGameCommand(
});
let currentPlayer: 'X' | 'O' = 'X';
let turnResult: TurnResult | undefined;
let winner: 'X' | 'O' | 'draw' | null = null;
let turn = 1;
while (true) {
const turnOutput = await this.run<TurnResult>(`turn ${currentPlayer} ${turn++}`);
if (!turnOutput.success) throw new Error(turnOutput.error);
turnResult = turnOutput?.result.winner;
if (turnResult) break;
winner = turnOutput.result.winner;
if (winner) break;
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
return { winner: turnResult };
return { winner };
}
)
const turn = createGameCommand(
const turn = createGameCommand<TicTacToeContext, TurnResult>(
'turn <player> <turn:number>',
async function(cmd) {
const [turnPlayer, turnNumber] = cmd.params as [string, number];
@@ -119,7 +128,6 @@ const turn = createGameCommand(
}
);
export function registerTicTacToeCommands(registry: CommandRegistry<IGameContext>) {
registerCommand(registry, setup);
registerCommand(registry, turn);
}
export const registry = createCommandRegistry<TicTacToeContext>();
registerCommand(registry, setup);
registerCommand(registry, turn);