Repository navigation
Implement donation troops/gold between human players after forming an alliance - #2450
Conversation
WalkthroughMoves alliance-reply processing from the per-tick phase into the execution's Changes
Sequence Diagram(s)sequenceDiagram
participant Init as Init()
participant Exec as AllianceRequestReplyExecution
participant Tick as Tick()
participant Donate as DonateExecution
rect rgb(220,235,255)
Note over Init,Exec: New — reply processed immediately in init()
Init->>Exec: processReply()
Exec->>Exec: applyAllianceState()
end
rect rgb(220,255,220)
Note over Tick,Donate: Subsequent tick/donate observe applied state
Tick->>Donate: runDonate()
Donate->>Exec: checkAlliance()
Exec-->>Donate: allow/transfer
end
rect rgb(255,235,235)
Note over Init,Tick: Old — reply deferred to tick (race possible)
Init->>Tick: deferReply()
Tick->>Exec: processReply()
Tick->>Donate: runDonate() (may race)
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks✅ Passed checks (3 passed)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
tests/AllianceDonation.test.ts (2)
76-77: Consider checkingisFriendlyfor consistency.Test 1 (lines 55-56) verifies both
isAlliedWithandisFriendlystatus, but this test only checksisAlliedWith. Consider addingisFriendlychecks for consistency across tests.Apply this diff if you want consistent assertions:
expect(player1.isAlliedWith(player2)).toBeTruthy(); expect(player2.isAlliedWith(player1)).toBeTruthy(); + expect(player1.isFriendly(player2)).toBeTruthy(); + expect(player2.isFriendly(player1)).toBeTruthy();
118-119: Consider checkingisFriendlyfor consistency.Same as the previous test - test 3 (lines 98-99) includes
isFriendlychecks, but this test omits them. Adding them would make the test suite more consistent.Apply this diff if you want consistent assertions:
expect(player1.isAlliedWith(player2)).toBeTruthy(); expect(player2.isAlliedWith(player1)).toBeTruthy(); + expect(player1.isFriendly(player2)).toBeTruthy(); + expect(player2.isFriendly(player1)).toBeTruthy();
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (2)
src/core/execution/alliance/AllianceRequestReplyExecution.ts(2 hunks)tests/AllianceDonation.test.ts(1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: scottanderson
Repo: openfrontio/OpenFrontIO PR: 1752
File: src/core/game/Game.ts:750-752
Timestamp: 2025-08-12T00:31:50.144Z
Learning: In the OpenFrontIO codebase, changes to the PlayerInteraction interface (like adding canDonateGold and canDonateTroops flags) do not require corresponding updates to src/core/Schemas.ts or server serialization code.
📚 Learning: 2025-08-12T00:31:50.144Z
Learnt from: scottanderson
Repo: openfrontio/OpenFrontIO PR: 1752
File: src/core/game/Game.ts:750-752
Timestamp: 2025-08-12T00:31:50.144Z
Learning: In the OpenFrontIO codebase, changes to the PlayerInteraction interface (like adding canDonateGold and canDonateTroops flags) do not require corresponding updates to src/core/Schemas.ts or server serialization code.
Applied to files:
tests/AllianceDonation.test.ts
📚 Learning: 2025-10-20T20:15:28.858Z
Learnt from: sambokai
Repo: openfrontio/OpenFrontIO PR: 2225
File: src/core/execution/FakeHumanExecution.ts:51-51
Timestamp: 2025-10-20T20:15:28.858Z
Learning: In src/core/execution/FakeHumanExecution.ts, game balance constants like MIRV_COOLDOWN_TICKS, MIRV_HESITATION_ODDS, VICTORY_DENIAL_TEAM_THRESHOLD, VICTORY_DENIAL_INDIVIDUAL_THRESHOLD, and STEAMROLL_CITY_GAP_MULTIPLIER are experimental tuning parameters subject to frequent change during balance testing. Do not flag changes to these values as issues or compare them against previous values.
Applied to files:
tests/AllianceDonation.test.ts
🧬 Code graph analysis (1)
tests/AllianceDonation.test.ts (5)
src/core/game/Game.ts (1)
Game(664-748)tests/util/Setup.ts (2)
setup(23-81)playerInfo(83-85)src/core/execution/alliance/AllianceRequestExecution.ts (1)
AllianceRequestExecution(9-72)src/core/execution/alliance/AllianceRequestReplyExecution.ts (1)
AllianceRequestReplyExecution(3-57)src/core/execution/DonateGoldExecution.ts (1)
DonateGoldExecution(4-51)
🔇 Additional comments (4)
tests/AllianceDonation.test.ts (2)
12-41: Test setup looks good.The setup correctly initializes two players with different resource levels and enables donation flags. Good foundation for the test scenarios.
129-157: Excellent race condition test - validates the core fix.This test directly validates the bug fix by adding both alliance acceptance and donation in the same turn. The two
executeNextTick()calls ensure the alliance is created during initialization (first tick) before the donation executes (second tick), preventing the race condition.src/core/execution/alliance/AllianceRequestReplyExecution.ts (2)
23-43: Clean fix - alliance creation now happens immediately.Moving the alliance reply processing to
init()ensures the alliance is created before anytick()calls in the same turn. This elegantly prevents the race condition where donations would check alliance status before it existed.The logic remains unchanged, just relocated from
tick()toinit(). Well done keeping the fix minimal and focused.
46-48: Good practice to keeptick()as documented no-op.Leaving
tick()as an empty method with a clear comment is better than removing it entirely. The comment makes the intent obvious: this execution completes during initialization.
Removed unnecessary comments and updated logic to process alliance replies immediately in init() to prevent race conditions.
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/core/execution/alliance/AllianceRequestReplyExecution.ts(1 hunks)tests/AllianceDonation.test.ts(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- tests/AllianceDonation.test.ts
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: scottanderson
Repo: openfrontio/OpenFrontIO PR: 1752
File: src/core/game/Game.ts:750-752
Timestamp: 2025-08-12T00:31:50.144Z
Learning: In the OpenFrontIO codebase, changes to the PlayerInteraction interface (like adding canDonateGold and canDonateTroops flags) do not require corresponding updates to src/core/Schemas.ts or server serialization code.
🪛 GitHub Actions: 🧪 CI
src/core/execution/alliance/AllianceRequestReplyExecution.ts
[warning] 1-1: Code style issues found. Prettier reported formatting problems. Run 'npx prettier --write' to fix.
🔇 Additional comments (1)
src/core/execution/alliance/AllianceRequestReplyExecution.ts (1)
13-42: Good fix for the race condition.Moving the alliance acceptance logic to
init()ensures the alliance is created immediately when the execution is added, before other executions run theirtick()methods in the same turn. This directly fixes the race condition where donations could fail when added in the same turn as alliance acceptance.The logic is sound: proper null checks, early returns, and proper cleanup at the end.
…etween-human-players-after-forming-an-alliance
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (1)
src/core/execution/alliance/AllianceRequestReplyExecution.ts (1)
44-45: Verify the Prettier formatting.The previous review flagged a formatting issue here. Run
npx prettier --write src/core/execution/alliance/AllianceRequestReplyExecution.tsto ensure the empty method conforms to the project's style.
🧹 Nitpick comments (1)
src/core/execution/alliance/AllianceRequestReplyExecution.ts (1)
13-42: Core fix looks good; the alliance now forms immediately.Moving the acceptance logic to
init()correctly addresses the race condition where donations could fail if executed in the same tick. The error handling is sound.Optionally, consider flattening the nested structure with early returns for readability:
init(mg: Game, ticks: number): void { if (!mg.hasPlayer(this.requestorID)) { console.warn( `AllianceRequestReplyExecution requester ${this.requestorID} not found`, ); this.active = false; return; } this.requestor = mg.player(this.requestorID); if (this.requestor.isFriendly(this.recipient)) { console.warn("already allied"); + this.active = false; + return; - } else { - const request = this.requestor - .outgoingAllianceRequests() - .find((ar) => ar.recipient() === this.recipient); - if (request === undefined) { - console.warn("no alliance request found"); - } else { - if (this.accept) { - request.accept(); - this.requestor.updateRelation(this.recipient, 100); - this.recipient.updateRelation(this.requestor, 100); - } else { - request.reject(); - } - } } + + const request = this.requestor + .outgoingAllianceRequests() + .find((ar) => ar.recipient() === this.recipient); + if (request === undefined) { + console.warn("no alliance request found"); + this.active = false; + return; + } + + if (this.accept) { + request.accept(); + this.requestor.updateRelation(this.recipient, 100); + this.recipient.updateRelation(this.requestor, 100); + } else { + request.reject(); + } + this.active = false; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/core/execution/alliance/AllianceRequestReplyExecution.ts(1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: scottanderson
Repo: openfrontio/OpenFrontIO PR: 1752
File: src/core/game/Game.ts:750-752
Timestamp: 2025-08-12T00:31:50.144Z
Learning: In the OpenFrontIO codebase, changes to the PlayerInteraction interface (like adding canDonateGold and canDonateTroops flags) do not require corresponding updates to src/core/Schemas.ts or server serialization code.
📚 Learning: 2025-10-18T17:54:01.311Z
Learnt from: sambokai
Repo: openfrontio/OpenFrontIO PR: 2225
File: src/core/execution/FakeHumanExecution.ts:172-173
Timestamp: 2025-10-18T17:54:01.311Z
Learning: In src/core/execution/FakeHumanExecution.ts, MIRVs and ground attacks should not be mutually exclusive. The considerMIRV() method should not short-circuit maybeAttack() - both actions can occur in the same tick.
Applied to files:
src/core/execution/alliance/AllianceRequestReplyExecution.ts
…-players-after-forming-an-alliance
…-players-after-forming-an-alliance
… alliance (openfrontio#2450) Resolves openfrontio#2448 Hi team, I've implemented and locally tested the alliance-related changes (including unit tests and some manual simulation with multiple browser profiles). Unfortunately I wasn't able to perform full end-to-end testing on the live game server with two separate machines/accounts. If someone on the team (or another contributor) can verify the alliance flow with two real players, that would be greatly appreciated before merging. Happy to hop on a call or provide any clarification needed. Thanks! ## Description: Fixed a race condition bug where donations (troops/gold) between human players failed after forming an alliance. The issue was caused by a one-tick delay in `AllianceRequestReplyExecution`: the alliance acceptance logic ran in `tick()` instead of `init()`, meaning the alliance wasn't created until the tick after the execution was added. If a donation execution was added in the same turn as the alliance acceptance, it would fail the `isFriendly()` check because the alliance didn't exist yet. **Root cause:** When human players formed alliances via reply, the execution model delayed alliance creation by one tick, while bots called `accept()` directly without this delay. **Solution:** Moved alliance acceptance logic from `tick()` to `init()` in `AllianceRequestReplyExecution.ts`, ensuring immediate alliance creation and eliminating race conditions with donations. **Changes:** - Modified `src/core/execution/alliance/AllianceRequestReplyExecution.ts` to process alliance replies in `init()` instead of `tick()` - Added comprehensive test suite `tests/AllianceDonation.test.ts` with 5 test cases covering donation scenarios after alliance formation (reply and mutual request flows) - All existing tests pass (323 total) ## Please complete the following: - [x] I have added screenshots for all UI updates (N/A - backend logic fix only) - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file (N/A - no user-facing text changes) - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced Discord: loacky GitHub: @LoackyBit --------- Co-authored-by: Evan <evanpelle@gmail.com>
Resolves #2448
Hi team,
I've implemented and locally tested the alliance-related changes (including unit tests and some manual simulation with multiple browser profiles).
Unfortunately I wasn't able to perform full end-to-end testing on the live game server with two separate machines/accounts.
If someone on the team (or another contributor) can verify the alliance flow with two real players, that would be greatly appreciated before merging. Happy to hop on a call or provide any clarification needed.
Thanks!
Description:
Fixed a race condition bug where donations (troops/gold) between human players failed after forming an alliance. The issue was caused by a one-tick delay in
AllianceRequestReplyExecution: the alliance acceptance logic ran intick()instead ofinit(), meaning the alliance wasn't created until the tick after the execution was added. If a donation execution was added in the same turn as the alliance acceptance, it would fail theisFriendly()check because the alliance didn't exist yet.Root cause: When human players formed alliances via reply, the execution model delayed alliance creation by one tick, while bots called
accept()directly without this delay.Solution: Moved alliance acceptance logic from
tick()toinit()inAllianceRequestReplyExecution.ts, ensuring immediate alliance creation and eliminating race conditions with donations.Changes:
src/core/execution/alliance/AllianceRequestReplyExecution.tsto process alliance replies ininit()instead oftick()tests/AllianceDonation.test.tswith 5 test cases covering donation scenarios after alliance formation (reply and mutual request flows)Please complete the following:
Discord: loacky
GitHub: @LoackyBit