refactor: improve rule handling

This commit is contained in:
2026-04-02 00:44:29 +08:00
parent ff9d9bd9a1
commit e06dc8ecba
4 changed files with 184 additions and 226 deletions
+23 -16
View File
@@ -1,6 +1,6 @@
import { GameContextInstance } from '../core/context';
import type { GameContextLike, RuleContext } from '../core/rule';
import { createRule, type InvokeYield, type RuleYield } from '../core/rule';
import type { RuleEngineHost, RuleContext } from '../core/rule';
import { createRule, type InvokeYield, type SchemaYield } from '../core/rule';
import type { Command } from '../utils/command';
import type { Part } from '../core/part';
import type { Region } from '../core/region';
@@ -17,19 +17,26 @@ type TurnResult = {
winner: 'X' | 'O' | 'draw' | null;
};
function getBoardRegion(game: GameContextLike) {
return game.regions.get('board');
type TicTacToeHost = RuleEngineHost & {
pushContext: (context: Context) => any;
latestContext: <T>(type: string) => { value: T } | undefined;
regions: { add: (...entities: any[]) => void; get: (id: string) => { value: { children: any[] } } };
parts: { add: (...entities: any[]) => void; get: (id: string) => any; collection: { value: Record<string, { value: Part }> } };
};
function getBoardRegion(host: TicTacToeHost) {
return host.regions.get('board');
}
function isCellOccupied(game: GameContextLike, row: number, col: number): boolean {
const board = getBoardRegion(game);
function isCellOccupied(host: TicTacToeHost, 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
);
}
function checkWinner(game: GameContextLike): 'X' | 'O' | 'draw' | null {
const parts = Object.values(game.parts.collection.value).map((s: { value: Part }) => s.value);
function checkWinner(host: TicTacToeHost): '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);
const oPositions = parts.filter((_: Part, i: number) => i % 2 === 1).map((p: Part) => p.position);
@@ -59,8 +66,8 @@ function hasWinningLine(positions: number[][]): boolean {
);
}
function placePiece(game: GameContextLike, row: number, col: number, moveCount: number) {
const board = getBoardRegion(game);
function placePiece(host: TicTacToeHost, row: number, col: number, moveCount: number) {
const board = getBoardRegion(host);
const piece: Part = {
id: `piece-${moveCount}`,
sides: 1,
@@ -68,14 +75,14 @@ function placePiece(game: GameContextLike, row: number, col: number, moveCount:
region: board,
position: [row, col],
};
game.parts.add(piece);
board.value.children.push(game.parts.get(piece.id));
host.parts.add(piece);
board.value.children.push(host.parts.get(piece.id));
}
const playSchema = 'play <player> <row:number> <col:number>';
const playSchema: SchemaYield = { type: 'schema', value: 'play <player> <row:number> <col:number>' };
export function createSetupRule() {
return createRule('start', function*() {
return createRule('start', function*(this: TicTacToeHost) {
this.pushContext({
type: 'tic-tac-toe',
currentPlayer: 'X',
@@ -101,7 +108,7 @@ export function createSetupRule() {
rule: 'turn',
command: { name: 'turn', params: [currentPlayer], flags: {}, options: {} } as Command,
};
const ctx = yield yieldValue as RuleYield;
const ctx = yield yieldValue;
turnResult = (ctx as RuleContext<TurnResult>).resolution;
if (turnResult?.winner) break;
@@ -117,7 +124,7 @@ export function createSetupRule() {
}
export function createTurnRule() {
return createRule('turn <player>', function*(cmd) {
return createRule('turn <player>', function*(this: TicTacToeHost, cmd) {
while (true) {
const received = yield playSchema;
if ('resolution' in received) continue;