Disconnected player giveaway for team mode

This commit is contained in:
aqw42
2025-05-30 14:02:44 +02:00
parent a43d64549d
commit b441ab2f97
3 changed files with 76 additions and 0 deletions
+51
View File
@@ -4,6 +4,7 @@ import { consolex } from "../Consolex";
import {
Execution,
Game,
GameMode,
MessageType,
Player,
PlayerID,
@@ -75,6 +76,17 @@ export class PlayerExecution implements Execution {
return;
}
if (
this.player.isDisconnected() &&
this.config.gameConfig().gameMode === GameMode.Team
) {
const closestAlly = this.getMoreSharedBorderAlly();
if (closestAlly) {
this.giveaway(closestAlly);
}
return;
}
const popInc = this.config.populationIncreaseRate(this.player);
this.player.addWorkers(popInc * (1 - this.player.targetTroopRatio()));
this.player.addTroops(popInc * this.player.targetTroopRatio());
@@ -252,6 +264,45 @@ export class PlayerExecution implements Execution {
}
}
private getMoreSharedBorderAlly(): Player | null {
if (!this.player) {
return null;
}
const neighbours = this.player!.neighborsBordersSurface().filter((p) =>
p[0].isOnSameTeam(this.player!),
);
if (!neighbours.length) {
return null;
}
return neighbours[0][0];
}
private giveaway(other: Player) {
if (this.mg === null || this.player === null) {
return;
}
for (const tile of this.player.tiles()) {
other.conquer(tile);
}
other.addGold(this.player.gold());
this.player.removeGold(this.player.gold());
other.addTroops(this.player.troops());
this.player.removeTroops(this.player.troops());
other.addWorkers(this.player.workers());
this.player.removeWorkers(this.player.workers());
for (const unit of this.player.units()) {
other.captureUnit(unit);
}
}
private getCapturingPlayer(cluster: Set<TileRef>): Player | null {
if (this.mg === null || this.player === null) {
throw new Error("Not initialized");