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(); + }); });