refactor: clean up boop implementation structure
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user