fix(core): prevent bots from invading/attacking themselves (#3865) (#4014)

Resolves #4094

## Description:

In Free-For-All (FFA) mode where teams default to 0, player isOnSameTeam
checks returned false for oneself, allowing players to attack
themselves. Consequently, if a bot conquered the targeted tile between
queueing a transport ship action and its actual initialization, the
target became itself, causing the bot to execute a self-invasion.

This fix adds a reflexive check in PlayerImpl.ts's isFriendly method to
always treat oneself as friendly. It also adds a safety guard in
TransportShipExecution.ts's init method to abort ship execution if the
target has shifted to the attacker.

## Please complete the following:

- [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

## Please put your Discord username so you can be contacted if a bug or
regression is found:

barfires
This commit is contained in:
Berk
2026-06-01 06:05:51 +03:00
committed by GitHub
parent b38f8ed1f8
commit f3ba95574c
4 changed files with 83 additions and 0 deletions
@@ -91,6 +91,11 @@ export class TransportShipExecution implements Execution {
}
}
if (this.target === this.attacker) {
this.active = false;
return;
}
if (this.target.isPlayer() && !this.attacker.canAttackPlayer(this.target)) {
this.active = false;
return;
+15
View File
@@ -776,6 +776,9 @@ export class PlayerImpl implements Player {
}
canDonateGold(recipient: Player): boolean {
if (recipient === this) {
return false;
}
if (
!this.isAlive() ||
!recipient.isAlive() ||
@@ -803,6 +806,9 @@ export class PlayerImpl implements Player {
}
canDonateTroops(recipient: Player): boolean {
if (recipient === this) {
return false;
}
if (
!this.isAlive() ||
!recipient.isAlive() ||
@@ -830,6 +836,9 @@ export class PlayerImpl implements Player {
}
donateTroops(recipient: Player, troops: number): boolean {
// Defense-in-depth: canDonateTroops already checks this, but guard here too
// to prevent self-donation if the method is called directly.
if (recipient === this) return false;
if (troops <= 0) return false;
const removed = this.removeTroops(troops);
if (removed === 0) return false;
@@ -847,6 +856,9 @@ export class PlayerImpl implements Player {
}
donateGold(recipient: Player, gold: Gold): boolean {
// Defense-in-depth: canDonateGold already checks this, but guard here too
// to prevent self-donation if the method is called directly.
if (recipient === this) return false;
if (gold <= 0n) return false;
const removed = this.removeGold(gold);
if (removed === 0n) return false;
@@ -969,6 +981,9 @@ export class PlayerImpl implements Player {
}
isFriendly(other: Player, treatAFKFriendly: boolean = false): boolean {
if (other === this) {
return true;
}
if (other.isDisconnected() && !treatAFKFriendly) {
return false;
}