Fix parallel bot boat attacks of nations 🐛#4297
Conversation
…attack troop logic
WalkthroughA new private helper ChangesAttack Troop Calculation Centralization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed due to a network error. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/core/execution/utils/AiAttackBehavior.ts (1)
914-961:⚠️ Potential issue | 🟠 MajorAdd a regression test for the parallel bot boat attacks bug.
Per coding guidelines, all changes to
src/core/must include tests. While existing tests cover troop cap and weak attack logic, there is no test for the specific bug fixed in this change: parallel boat attacks against bots should not drain troops to zero. Add a test that verifiesbotAttackTroopsSentis properly tracked across multiple concurrent bot attacks to prevent troops from being exhausted.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/core/execution/utils/AiAttackBehavior.ts` around lines 914 - 961, Add a regression test for the parallel bot boat attacks bug in the AiAttackBehavior class. The test should verify that when calculateAttackTroops is called multiple times in parallel against bot targets, the botAttackTroopsSent counter is properly tracked and deducted from available troops, ensuring that troops are not drained to zero across multiple concurrent attacks. The test should simulate at least two concurrent attack scenarios and assert that the remaining available troops are correctly calculated after accounting for all sent troops.Source: Coding guidelines
🧹 Nitpick comments (1)
src/core/execution/utils/AiAttackBehavior.ts (1)
140-153: 💤 Low valueConsider:
attackWithRandomBoatuses different troop calculation.This method still calculates troops with
this.player.troops() / 5and thetroopSendCap()directly, bypassingcalculateAttackTroops. SinceattackWithRandomBoatis a single-shot action (not parallel bot attacks), this may be fine. But if consistency is desired, this path could also delegate to the helper.Not blocking since the PR's bug (parallel bot boat attacks) happens through
sendBoatAttack, which is now fixed.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/core/execution/utils/AiAttackBehavior.ts` around lines 140 - 153, Consider refactoring the `attackWithRandomBoat` method to use the `calculateAttackTroops` helper method for consistency with other attack methods like `sendBoatAttack`. Currently, the troop calculation is done inline using `Math.min(this.player.troops() / 5, this.troopSendCap())`, but other attack methods delegate this logic to `calculateAttackTroops`. Replace the inline calculation with a call to `calculateAttackTroops` to maintain consistent troop calculation across all attack methods in the class.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@src/core/execution/utils/AiAttackBehavior.ts`:
- Around line 914-961: Add a regression test for the parallel bot boat attacks
bug in the AiAttackBehavior class. The test should verify that when
calculateAttackTroops is called multiple times in parallel against bot targets,
the botAttackTroopsSent counter is properly tracked and deducted from available
troops, ensuring that troops are not drained to zero across multiple concurrent
attacks. The test should simulate at least two concurrent attack scenarios and
assert that the remaining available troops are correctly calculated after
accounting for all sent troops.
---
Nitpick comments:
In `@src/core/execution/utils/AiAttackBehavior.ts`:
- Around line 140-153: Consider refactoring the `attackWithRandomBoat` method to
use the `calculateAttackTroops` helper method for consistency with other attack
methods like `sendBoatAttack`. Currently, the troop calculation is done inline
using `Math.min(this.player.troops() / 5, this.troopSendCap())`, but other
attack methods delegate this logic to `calculateAttackTroops`. Replace the
inline calculation with a call to `calculateAttackTroops` to maintain consistent
troop calculation across all attack methods in the class.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 0c4ee478-0b9e-438b-8da7-78844cbb2cba
📒 Files selected for processing (1)
src/core/execution/utils/AiAttackBehavior.ts
Description:
When a nation attacked multiple bots via boat attacks in parallel, each boat attack computed its troop allocation independently using
player.troops() / 5without subtractingbotAttackTroopsSent. The cumulative troop commitment could exceed the nation's actual troop count, and when the queuedAttackExecutions raninit(), they drained the nation to zero.Planetary Realignment found this bug by accident, here Russia has only 39 troops:
The land attack path already handled this correctly. The bug in
sendBoatAttackwas introduced by #3786, which made nations see and attack enemies across rivers via boats, and changedattackBots()from.neighbors()to.nearby().So the bug was on prod for the entirety of v31.
This fix extracts the shared attack troop calculation (reserve, bot-aware allocation, troopSendCap, isAttackTooWeak, emoji) into a new
calculateAttackTroopsmethod, with a callback for the non-bot troop default (land:player.troops() - targetTroops, boat:player.troops() / 5). Bot targets in both paths now go through the same reserve-aware calculation.Please complete the following:
Please put your Discord username so you can be contacted if a bug or regression is found:
FloPinguin