refactor: clean up boop implementation structure

This commit is contained in:
2026-04-07 15:43:17 +08:00
parent 10393f45b6
commit 91c993b223
18 changed files with 485 additions and 317 deletions
+40
View File
@@ -0,0 +1,40 @@
import {BoopPart, PieceType, PlayerType, RegionType} from "@/samples/boop/types";
import {BoopState} from "@/samples/boop/state";
import {BoopGame} from "@/samples/boop/types-extensions";
/**
* 在区域中查找棋子
*/
export function findPartInRegion(
ctx: BoopGame | BoopState,
regionId: keyof BoopGame['value']['regions'],
type: PieceType,
player?: PlayerType
): BoopPart | null {
const state = getState(ctx);
if(!regionId){
return Object.values(state.pieces).find(part => match(regionId, part, type, player)) || null;
}
const id = state.regions[regionId].childIds.find(id => match(regionId, state.pieces[id], type, player));
return id ? state.pieces[id] || null : null;
}
/**
* 在指定位置查找棋子
*/
export function findPartAtPosition(ctx: BoopGame | BoopState, row: number, col: number): BoopPart | null {
const state = getState(ctx);
const id = state.regions.board.partMap[`${row},${col}`];
return id ? state.pieces[id] || null : null;
}
function match(regionId: RegionType, part: BoopPart, type: PieceType, player?: PlayerType){
return regionId === part.regionId && part.type === type && (!player || part.player === player);
}
function getState(ctx: BoopGame | BoopState): BoopState {
if('value' in ctx){
return ctx.value;
}
return ctx;
}