Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/core/execution/NukeExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
28 changes: 27 additions & 1 deletion tests/AllianceRequestExecution.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
});
});
Loading