Fix AI bot turn chain for multiple bots - prevent race conditions

This commit is contained in:
sokol
2026-02-21 21:01:57 +03:00
parent 64c81da166
commit e427f1c68d
3 changed files with 435 additions and 67 deletions

View File

@@ -17,50 +17,63 @@ export class AIBot {
* Execute AI turn - makes ONE move then ends turn
*/
async playTurn() {
console.log(`AI Player ${this.playerId} thinking...`);
// Get all player cells
const playerCells = this.map.getPlayerCells(this.playerId);
console.log(`AI Player ${this.playerId} has ${playerCells.length} cells`);
console.log(`[AI-BOT P${this.playerId}] === Turn started ===`);
console.log(`[AI-BOT P${this.playerId}] Thinking...`);
if (playerCells.length === 0) {
console.log(`AI Player ${this.playerId} has no cells, ending turn`);
try {
// Get all player cells
const playerCells = this.map.getPlayerCells(this.playerId);
console.log(`[AI-BOT P${this.playerId}] Has ${playerCells.length} cells`);
if (playerCells.length === 0) {
console.log(`[AI-BOT P${this.playerId}] No cells, ending turn`);
await this.wait(this.thinkingTime);
this.gameUI.endTurn();
return;
}
// Find all possible moves
const moves = this.findPossibleMoves(playerCells);
console.log(`[AI-BOT P${this.playerId}] Found ${moves.length} possible moves`);
if (moves.length === 0) {
// No moves available, end turn
console.log(`[AI-BOT P${this.playerId}] No valid moves, ending turn`);
await this.wait(this.thinkingTime);
this.gameUI.endTurn();
return;
}
// Sort moves by priority (attack > expand > reinforce)
moves.sort((a, b) => this.movePriority(b) - this.movePriority(a));
// Execute best move
const bestMove = moves[0];
console.log(`[AI-BOT P${this.playerId}] Selected move: from (${bestMove.from.q},${bestMove.from.r}) to (${bestMove.to.q},${bestMove.to.r}), type=${bestMove.type}, attackStr=${bestMove.attackStrength}, defStr=${bestMove.defenseStrength}`);
// Wait for thinking time
await this.wait(this.thinkingTime);
// Execute the move
this.gameUI.selectedCell = bestMove.from;
this.gameUI.currentTarget = bestMove.to;
this.gameUI.executeAttack();
console.log(`[AI-BOT P${this.playerId}] Move executed`);
// End turn after executing move
console.log(`[AI-BOT P${this.playerId}] Calling endTurn()`);
this.gameUI.endTurn();
return;
}
// Find all possible moves
const moves = this.findPossibleMoves(playerCells);
console.log(`AI Player ${this.playerId} found ${moves.length} possible moves`);
if (moves.length === 0) {
// No moves available, end turn
console.log(`AI Player ${this.playerId} has no moves, ending turn`);
console.log(`[AI-BOT P${this.playerId}] === Turn completed ===`);
} catch (error) {
console.error(`[AI-BOT P${this.playerId}] Error during turn:`, error);
// Still end turn on error to prevent game from getting stuck
this.gameUI.endTurn();
return;
throw error; // Re-throw so caller knows there was an error
}
// Sort moves by priority (attack > expand > reinforce)
moves.sort((a, b) => this.movePriority(b) - this.movePriority(a));
// Execute best move
const bestMove = moves[0];
console.log(`AI Player ${this.playerId} selected move: from (${bestMove.from.q},${bestMove.from.r}) to (${bestMove.to.q},${bestMove.to.r}), type=${bestMove.type}`);
// Wait for thinking time
await this.wait(this.thinkingTime);
// Execute the move directly without using executeMove helper
this.gameUI.selectedCell = bestMove.from;
this.gameUI.currentTarget = bestMove.to;
this.gameUI.executeAttack();
console.log(`AI Player ${this.playerId} executed move`);
// End turn after executing move
this.gameUI.endTurn();
}
/**