From d16accafef06b6a0131b19d0fb1a9dfd34b535f1 Mon Sep 17 00:00:00 2001 From: Daniel Popsuevich <54398565+DanyPops@users.noreply.github.com> Date: Mon, 29 Sep 2025 03:56:09 +0300 Subject: [PATCH] Revoking alliances request during nuke (#2101) Closes #2071 ## Description: - Created Functional Test for scenario - Added an alliance revoker function to 'NukeExection' ## Please complete the following: - [ ] I have added screenshots for all UI updates - [ ] I process any text displayed to the user through translateText() and I've added it to the en.json file - [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 ## Please put your Discord username so you can be contacted if a bug or regression is found: dpop --- src/core/execution/NukeExecution.ts | 16 +++++++++++---- tests/AllianceRequestExecution.test.ts | 28 +++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/core/execution/NukeExecution.ts b/src/core/execution/NukeExecution.ts index b7ac4a32f..1fd9222bb 100644 --- a/src/core/execution/NukeExecution.ts +++ b/src/core/execution/NukeExecution.ts @@ -76,18 +76,26 @@ export class NukeExecution implements Execution { } const threshold = this.mg.config().nukeAllianceBreakThreshold(); - for (const [other, tilesDestroyed] of attacked) { + for (const [attackedPlayer, tilesDestroyed] of attacked) { if ( tilesDestroyed > threshold && this.nuke.type() !== UnitType.MIRVWarhead ) { + // Resolves exploit of alliance breaking in which a pending alliance request + // was accepeted in the middle of an missle attack. + const allianceRequest = attackedPlayer + .incomingAllianceRequests() + .find((ar) => ar.requestor() === this.player); + if (allianceRequest) { + allianceRequest?.reject(); + } // Mirv warheads shouldn't break alliances - const alliance = this.player.allianceWith(other); + const alliance = this.player.allianceWith(attackedPlayer); if (alliance !== null) { this.player.breakAlliance(alliance); } - if (other !== this.player) { - other.updateRelation(this.player, -100); + if (attackedPlayer !== this.player) { + attackedPlayer.updateRelation(this.player, -100); } } } diff --git a/tests/AllianceRequestExecution.test.ts b/tests/AllianceRequestExecution.test.ts index c88edd5a0..d2b6ac916 100644 --- a/tests/AllianceRequestExecution.test.ts +++ b/tests/AllianceRequestExecution.test.ts @@ -1,7 +1,9 @@ import { AllianceRequestExecution } from "../src/core/execution/alliance/AllianceRequestExecution"; import { AllianceRequestReplyExecution } from "../src/core/execution/alliance/AllianceRequestReplyExecution"; -import { Game, Player, PlayerType } from "../src/core/game/Game"; +import { NukeExecution } from "../src/core/execution/NukeExecution"; +import { Game, Player, PlayerType, UnitType } from "../src/core/game/Game"; import { playerInfo, setup } from "./util/Setup"; +import { constructionExecution } from "./util/utils"; let game: Game; let player1: Player; @@ -74,4 +76,28 @@ describe("AllianceRequestExecution", () => { expect(player1.isAlliedWith(player2)).toBeFalsy(); expect(player2.isAlliedWith(player1)).toBeFalsy(); }); + + // Resolves exploit https://github.com/openfrontio/OpenFrontIO/issues/2071 + test("alliance request is revoked immediately if requester launches a nuke", () => { + game.config().nukeAllianceBreakThreshold = () => 0; + // Player 1 sends an alliance request to player 2. + game.addExecution(new AllianceRequestExecution(player1, player2.id())); + game.executeNextTick(); + + expect(player1.outgoingAllianceRequests().length).toBe(1); + expect(player2.incomingAllianceRequests().length).toBe(1); + + // Player 1 Builds a silo & launches a missle at player 2. + constructionExecution(game, player1, 0, 0, UnitType.MissileSilo); + game.addExecution( + new NukeExecution(UnitType.AtomBomb, player1, game.ref(0, 1), null), + ); + game.executeNextTick(); + game.executeNextTick(); + + expect(player1.outgoingAllianceRequests().length).toBe(0); + expect(player2.incomingAllianceRequests().length).toBe(0); + expect(player1.isAlliedWith(player2)).toBeFalsy(); + expect(player2.isAlliedWith(player1)).toBeFalsy(); + }); });