fix: traitor bug when attacking immediately after initiating an alliance (#2044)

This PR fixes a critical race condition bug where players could
unintentionally receive the traitor debuff when alliance requests were
accepted mid-attack.

Critical Bug Fixes #1866

**Root Cause:**
Players could bypass UI alliance checks ( isFriendly() ) by accepting
alliances and immediately attacking after that, causing the server to
treat the attack as betrayal
Solution: Added server-side alliance validation in
AttackExecution.init()
This ensures attacks on allies are blocked at the server level.

- Once Bots and Nations decide to attack, they breaks the alliance. I
added maybeConsiderBetrayal(), which currently always returns true. I’ll
add proper logic for alliance-breaking soon on another PR; this didn’t
exist in the code before.

- [x] I have added screenshots for all UI updates
- [x] 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
regression is found:

abodcraft1

---------

Co-authored-by: evanpelle <evanpelle@gmail.com>
This commit is contained in:
Abdallah Bahrawi
2025-09-13 19:21:21 +03:00
committed by evanpelle
parent 633556effe
commit 6f96788406
7 changed files with 343 additions and 89 deletions
+29 -26
View File
@@ -16,8 +16,6 @@ import { FlatBinaryHeap } from "./utils/FlatBinaryHeap"; // adjust path if neede
const malusForRetreat = 25;
export class AttackExecution implements Execution {
private breakAlliance = false;
private wasAlliedAtInit = false; // Store alliance state at initialization
private active: boolean = true;
private toConquer = new FlatBinaryHeap();
@@ -62,6 +60,24 @@ export class AttackExecution implements Execution {
? mg.terraNullius()
: mg.player(this._targetID);
if (this._owner === this.target) {
console.error(`Player ${this._owner} cannot attack itself`);
this.active = false;
return;
}
// ALLIANCE CHECK — block attacks on friendly (ally or same team)
if (this.target.isPlayer()) {
const targetPlayer = this.target as Player;
if (this._owner.isFriendly(targetPlayer)) {
console.warn(
`${this._owner.displayName()} cannot attack ${targetPlayer.displayName()} because they are friendly (allied or same team)`,
);
this.active = false;
return;
}
}
if (this.target && this.target.isPlayer()) {
const targetPlayer = this.target as Player;
if (
@@ -70,15 +86,10 @@ export class AttackExecution implements Execution {
) {
// Don't let bots embargo since they can't trade anyway.
targetPlayer.addEmbargo(this._owner, true);
this.rejectIncomingAllianceRequests(targetPlayer);
}
}
if (this._owner === this.target) {
console.error(`Player ${this._owner} cannot attack itself`);
this.active = false;
return;
}
if (this.target.isPlayer()) {
if (
this.mg.config().numSpawnPhaseTurns() +
@@ -148,11 +159,6 @@ export class AttackExecution implements Execution {
}
if (this.target.isPlayer()) {
// Store the alliance state at initialization time to prevent race conditions
this.wasAlliedAtInit = this._owner.isAlliedWith(this.target);
if (this.wasAlliedAtInit) {
this.breakAlliance = true;
}
this.target.updateRelation(this._owner, -80);
}
}
@@ -221,20 +227,8 @@ export class AttackExecution implements Execution {
return;
}
const alliance = targetPlayer
? this._owner.allianceWith(targetPlayer)
: null;
if (this.breakAlliance && alliance !== null) {
this.breakAlliance = false;
this._owner.breakAlliance(alliance);
}
if (
targetPlayer &&
this._owner.isAlliedWith(targetPlayer) &&
!this.wasAlliedAtInit
) {
if (targetPlayer && this._owner.isFriendly(targetPlayer)) {
// In this case a new alliance was created AFTER the attack started.
// We should retreat to avoid the attacker becoming a traitor.
this.retreat();
return;
}
@@ -295,6 +289,15 @@ export class AttackExecution implements Execution {
}
}
private rejectIncomingAllianceRequests(target: Player) {
const request = this._owner
.incomingAllianceRequests()
.find((ar) => ar.requestor() === target);
if (request !== undefined) {
request.reject();
}
}
private addNeighbors(tile: TileRef) {
if (this.attack === null) {
throw new Error("Attack not initialized");
+7
View File
@@ -69,6 +69,13 @@ export class BotExecution implements Execution {
if (toAttack !== null) {
const odds = this.bot.isFriendly(toAttack) ? 6 : 3;
if (this.random.chance(odds)) {
// Check and break alliance before attacking if needed
const alliance = this.bot.allianceWith(toAttack);
if (alliance !== null) {
this.bot.breakAlliance(alliance);
}
this.behavior.sendAttack(toAttack);
return;
}
+34 -1
View File
@@ -161,6 +161,30 @@ export class FakeHumanExecution implements Execution {
this.maybeAttack();
}
/**
* TODO: Implement strategic betrayal logic
* Currently this just breaks alliances without strategic consideration.
* Future implementation should consider:
* - Relative strength (troop count, territory size) compared to target
* - Risk vs reward of betrayal
* - Potential impact on relations with other players
* - Timing (don't betray when already fighting other enemies)
* - Strategic value of target's territory
* - If target is distracted
*/
private maybeConsiderBetrayal(target: Player): boolean {
if (this.player === null) throw new Error("not initialized");
const alliance = this.player.allianceWith(target);
if (!alliance) return false;
this.player.breakAlliance(alliance);
// Successfully broken an alliance
return true;
}
private maybeAttack() {
if (this.player === null || this.behavior === null) {
throw new Error("not initialized");
@@ -208,6 +232,7 @@ export class FakeHumanExecution implements Execution {
const toAttack = this.random.chance(2)
? enemies[0]
: this.random.randElement(enemies);
if (this.shouldAttack(toAttack)) {
this.behavior.sendAttack(toAttack);
return;
@@ -228,9 +253,17 @@ export class FakeHumanExecution implements Execution {
private shouldAttack(other: Player): boolean {
if (this.player === null) throw new Error("not initialized");
if (this.player.isOnSameTeam(other)) {
return false;
}
// Consider betrayal for allies
if (this.player.isAlliedWith(other)) {
const canProceed = this.maybeConsiderBetrayal(other);
return canProceed;
}
if (this.player.isFriendly(other)) {
if (this.shouldDiscourageAttack(other)) {
return this.random.chance(200);
@@ -396,7 +429,7 @@ export class FakeHumanExecution implements Execution {
private maybeSendBoatAttack(other: Player) {
if (this.player === null) throw new Error("not initialized");
if (this.player.isOnSameTeam(other)) return;
if (this.player.isFriendly(other)) return;
const closest = closestTwoTiles(
this.mg,
Array.from(this.player.borderTiles()).filter((t) =>
+4 -2
View File
@@ -230,7 +230,9 @@ export class BotBehavior {
}
sendAttack(target: Player | TerraNullius) {
if (target.isPlayer() && this.player.isOnSameTeam(target)) return;
// Skip attacking friendly targets (allies or teammates) - decision to break alliances should be made by caller
if (target.isPlayer() && this.player.isFriendly(target)) return;
const maxTroops = this.game.config().maxTroops(this.player);
const reserveRatio = target.isPlayer()
? this.reserveRatio
@@ -242,7 +244,7 @@ export class BotBehavior {
new AttackExecution(
troops,
this.player,
target.isPlayer() ? target.id() : null,
target.isPlayer() ? target.id() : this.game.terraNullius().id(),
),
);
}