refactor: async boop
This commit is contained in:
+56
-32
@@ -16,7 +16,7 @@ export type PlayerType = 'white' | 'black';
|
||||
export type PieceType = 'kitten' | 'cat';
|
||||
export type WinnerType = PlayerType | 'draw' | null;
|
||||
|
||||
type BoopPart = Part<{ player: PlayerType; pieceType: PieceType }>;
|
||||
export type BoopPart = Part<{ player: PlayerType; pieceType: PieceType }>;
|
||||
|
||||
type PieceSupply = { supply: number; placed: number };
|
||||
|
||||
@@ -120,7 +120,7 @@ registration.add('turn <player>', async function(cmd) {
|
||||
const pieceType = type === 'cat' ? 'cat' : 'kitten';
|
||||
|
||||
placePiece(this.context, row, col, turnPlayer, pieceType);
|
||||
applyBoops(this.context, row, col, pieceType);
|
||||
await applyBoops(this.context, row, col, pieceType);
|
||||
|
||||
const graduatedLines = checkGraduation(this.context, turnPlayer);
|
||||
if (graduatedLines.length > 0) {
|
||||
@@ -192,8 +192,7 @@ export function placePiece(host: MutableSignal<BoopState>, row: number, col: num
|
||||
decrementSupply(playerData, pieceType);
|
||||
}
|
||||
|
||||
export function applyBoops(host: MutableSignal<BoopState>, placedRow: number, placedCol: number, placedType: PieceType) {
|
||||
const board = getBoardRegion(host);
|
||||
export async function applyBoops(host: MutableSignal<BoopState>, placedRow: number, placedCol: number, placedType: PieceType) {
|
||||
const pieces = host.value.pieces;
|
||||
const piecesArray = Object.values(pieces);
|
||||
|
||||
@@ -216,39 +215,49 @@ export function applyBoops(host: MutableSignal<BoopState>, placedRow: number, pl
|
||||
}
|
||||
}
|
||||
|
||||
for (const { part, dr, dc } of piecesToBoop) {
|
||||
const [r, c] = part.position;
|
||||
const newRow = r + dr;
|
||||
const newCol = c + dc;
|
||||
await host.produceAsync(state => {
|
||||
const board = state.board;
|
||||
const currentPieces = state.pieces;
|
||||
|
||||
if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) {
|
||||
const pt = part.pieceType;
|
||||
const pl = part.player;
|
||||
const playerData = getPlayer(host, pl);
|
||||
removePieceFromBoard(host, part);
|
||||
incrementSupply(playerData, pt);
|
||||
continue;
|
||||
for (const { part, dr, dc } of piecesToBoop) {
|
||||
const [r, c] = part.position;
|
||||
const newRow = r + dr;
|
||||
const newCol = c + dc;
|
||||
|
||||
if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) {
|
||||
const pt = part.pieceType;
|
||||
const pl = part.player;
|
||||
const playerData = state.players[pl];
|
||||
// Remove piece from board
|
||||
board.childIds = board.childIds.filter(id => id !== part.id);
|
||||
delete board.partMap[part.position.join(',')];
|
||||
delete currentPieces[part.id];
|
||||
playerData[pt].placed--;
|
||||
playerData[pt].supply++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if target cell is occupied
|
||||
const targetPosKey = `${newRow},${newCol}`;
|
||||
if (board.partMap[targetPosKey]) continue;
|
||||
|
||||
// Move piece to new position
|
||||
delete board.partMap[part.position.join(',')];
|
||||
part.position = [newRow, newCol];
|
||||
board.partMap[targetPosKey] = part.id;
|
||||
}
|
||||
|
||||
if (isCellOccupied(host, newRow, newCol)) continue;
|
||||
|
||||
part.position = [newRow, newCol];
|
||||
board.partMap = Object.fromEntries(
|
||||
board.childIds.map(id => {
|
||||
const p = pieces[id];
|
||||
return [p.position.join(','), id];
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function removePieceFromBoard(host: MutableSignal<BoopState>, part: BoopPart) {
|
||||
const board = getBoardRegion(host);
|
||||
const playerData = getPlayer(host, part.player);
|
||||
board.childIds = board.childIds.filter(id => id !== part.id);
|
||||
delete board.partMap[part.position.join(',')];
|
||||
delete host.value.pieces[part.id];
|
||||
playerData[part.pieceType].placed--;
|
||||
host.produce(state => {
|
||||
const board = state.board;
|
||||
const playerData = state.players[part.player];
|
||||
board.childIds = board.childIds.filter(id => id !== part.id);
|
||||
delete board.partMap[part.position.join(',')];
|
||||
delete state.pieces[part.id];
|
||||
playerData[part.pieceType].placed--;
|
||||
});
|
||||
}
|
||||
|
||||
const DIRECTIONS: [number, number][] = [
|
||||
@@ -363,3 +372,18 @@ export function checkWinner(host: MutableSignal<BoopState>): WinnerType {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// 命令构建器
|
||||
export const commands = {
|
||||
play: (player: PlayerType, row: number, col: number, type?: PieceType) =>
|
||||
`play ${player} ${row} ${col}${type ? ` ${type}` : ''}`,
|
||||
turn: (player: PlayerType) => `turn ${player}`,
|
||||
graduate: (row: number, col: number) => `graduate ${row} ${col}`,
|
||||
} as const;
|
||||
|
||||
// 导出游戏模块
|
||||
export const gameModule = {
|
||||
createInitialState,
|
||||
registry,
|
||||
commands,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user