Update Database.php

Fixed chieftain training issue: when player reinforced their chiefs to other villages they could train more so no more unlimited chieftains :)
This commit is contained in:
lietuvis10
2025-12-14 15:27:46 +02:00
committed by GitHub
parent 04061dc772
commit 038465fe9a
+33 -25
View File
@@ -7147,68 +7147,72 @@ References: User ID/Message ID, Mode
$maxslots -= (3 - floor(($palace - 5) / 5)); $maxslots -= (3 - floor(($palace - 5) / 5));
} }
$q = "SELECT (u10+u20+u30) as R1, (u9+u19+u29) as R2 FROM " . TB_PREFIX . "units WHERE vref = ". (int) $village->wid; // Units at home
$q = "SELECT (u10+u20+u30) as R1, (u9+u19+u29) as R2
FROM " . TB_PREFIX . "units
WHERE vref = " . (int)$village->wid;
$result = mysqli_query($this->dblink,$q); $result = mysqli_query($this->dblink,$q);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC); $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$settlers = $row['R1']; $settlers = (int)$row['R1'];
$chiefs = $row['R2']; $chiefs = (int)$row['R2'];
// Movements
$settlers += 3 * count($this->getMovement(5, $village->wid, 0)); $settlers += 3 * count($this->getMovement(5, $village->wid, 0));
$current_movement = $this->getMovement(3, $village->wid, 0); $current_movement = $this->getMovement(3, $village->wid, 0);
if(!empty($current_movement)) { if(!empty($current_movement)) {
foreach($current_movement as $build) { foreach($current_movement as $build) {
$settlers += $build['t10']; $settlers += (int)$build['t10'];
$chiefs += $build['t9']; $chiefs += (int)$build['t9'];
} }
} }
$current_movement = $this->getMovement(4, $village->wid, 1); $current_movement = $this->getMovement(4, $village->wid, 1);
if(!empty($current_movement)) { if(!empty($current_movement)) {
foreach($current_movement as $build) { foreach($current_movement as $build) {
$settlers += $build['t10']; $settlers += (int)$build['t10'];
$chiefs += $build['t9']; $chiefs += (int)$build['t9'];
} }
} }
$q = "SELECT (u10+u20+u30) FROM " . TB_PREFIX . "enforcement WHERE `from` = ".(int) $village->wid; // FIX: Count ALL reinforcements properly (SUM over ALL rows)
$q = "SELECT COALESCE(SUM(u10+u20+u30),0) AS s
FROM " . TB_PREFIX . "enforcement
WHERE `from` = " . (int)$village->wid;
$result = mysqli_query($this->dblink,$q); $result = mysqli_query($this->dblink,$q);
$row = mysqli_fetch_row($result); $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if(!empty($row)) { $settlers += (int)$row['s'];
foreach($row as $reinf) {
$settlers += $reinf[0];
}
}
$q = "SELECT (u9+u19+u29) FROM " . TB_PREFIX . "enforcement WHERE `from` = ".(int) $village->wid; $q = "SELECT COALESCE(SUM(u9+u19+u29),0) AS c
FROM " . TB_PREFIX . "enforcement
WHERE `from` = " . (int)$village->wid;
$result = mysqli_query($this->dblink,$q); $result = mysqli_query($this->dblink,$q);
$row = mysqli_fetch_row($result); $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if(!empty($row)) { $chiefs += (int)$row['c'];
foreach($row as $reinf) {
$chiefs += $reinf[0];
}
}
// Training queue (your existing logic)
$trainlist = $technology->getTrainingList(4); $trainlist = $technology->getTrainingList(4);
if(!empty($trainlist)) { if(!empty($trainlist)) {
foreach($trainlist as $train) { foreach($trainlist as $train) {
if($train['unit'] % 10 == 0) { if($train['unit'] % 10 == 0) {
$settlers += $train['amt']; $settlers += (int)$train['amt'];
} }
if($train['unit'] % 10 == 9) { if($train['unit'] % 10 == 9) {
$chiefs += $train['amt']; $chiefs += (int)$train['amt'];
} }
} }
} }
// Trapped troops
$trappedTroops = $this->getPrisoners($village->wid, 1); $trappedTroops = $this->getPrisoners($village->wid, 1);
if(!empty($trappedTroops)){ if(!empty($trappedTroops)){
foreach($trappedTroops as $trapped){ foreach($trappedTroops as $trapped){
$settlers += $trapped['t10']; $settlers += (int)$trapped['t10'];
$chiefs += $trapped['t9']; $chiefs += (int)$trapped['t9'];
} }
} }
// Slot math (unchanged, but clamp to 0 to avoid negatives)
$settlerslots = ($maxslots * 3) - ($chiefs * 3) - $settlers; $settlerslots = ($maxslots * 3) - ($chiefs * 3) - $settlers;
$chiefslots = $maxslots - $chiefs - floor(($settlers + 2) / 3); $chiefslots = $maxslots - $chiefs - floor(($settlers + 2) / 3);
@@ -7216,9 +7220,13 @@ References: User ID/Message ID, Mode
$chiefslots = 0; $chiefslots = 0;
} }
if ($settlerslots < 0) $settlerslots = 0;
if ($chiefslots < 0) $chiefslots = 0;
return ["chiefs" => $chiefslots, "settlers" => $settlerslots]; return ["chiefs" => $chiefslots, "settlers" => $settlerslots];
} }
/** /**
* Calculates how much artifacts affect troops speed, cranny efficency, etc. * Calculates how much artifacts affect troops speed, cranny efficency, etc.
* *