mirror of
https://github.com/Shadowss/TravianZ.git
synced 2026-06-28 00:24:23 +00:00
Refactor(sendunitsComplete): extract applyOwnDefenceCasualties() [#155]
Move the in-village defender casualty handling (kill ratio applied to the defender's own troops + hero, persisted via modifyUnit) out of the giant sendunitsComplete() loop into a dedicated private method returning the $owndead map. Pure behaviour-preserving extraction; also drops a dead $u variable that was assigned but never read. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
committed by
Catalin Novgorodschi
parent
580702c5bc
commit
e167322d12
+49
-36
@@ -1710,6 +1710,53 @@ class Automation {
|
||||
return $steal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply battle casualties to the defender's own (in-village) troops, persist
|
||||
* the losses, and return the per-unit dead map used later for points/reports.
|
||||
* Pure behaviour-preserving extraction (refactor for issue #155).
|
||||
*
|
||||
* @param array $data Current attack row (uses 'to').
|
||||
* @param int $targettribe Defender tribe (1-5).
|
||||
* @param array $battlepart Battle result (index 2 = defender kill ratio,
|
||||
* 'deadherodef' = defender hero losses).
|
||||
* @return array Map of dead own troops: keys are the unit index (int) plus 'hero'.
|
||||
*/
|
||||
private function applyOwnDefenceCasualties($data, $targettribe, $battlepart) {
|
||||
global $database;
|
||||
|
||||
$owndead = [];
|
||||
$unitlist = $database->getUnit($data['to'], false);
|
||||
$start = ($targettribe - 1) * 10 + 1;
|
||||
$end = ($targettribe * 10);
|
||||
|
||||
$unitModifications_units = [];
|
||||
$unitModifications_amounts = [];
|
||||
$unitModifications_modes = [];
|
||||
for ($i = $start; $i <= $end; $i++) {
|
||||
if ($unitlist) {
|
||||
$owndead[$i] = round($battlepart[2] * $unitlist['u'.$i]);
|
||||
$unitModifications_units[] = $i;
|
||||
$unitModifications_amounts[] = $owndead[$i];
|
||||
$unitModifications_modes[] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
$owndead['hero'] = 0;
|
||||
|
||||
if ($unitlist) {
|
||||
$owndead['hero'] = (isset($battlepart['deadherodef']) ? $battlepart['deadherodef'] : '');
|
||||
|
||||
$unitModifications_units[] = 'hero';
|
||||
$unitModifications_amounts[] = $owndead['hero'];
|
||||
$unitModifications_modes[] = 0;
|
||||
}
|
||||
|
||||
// modify units in DB
|
||||
$database->modifyUnit($data['to'], $unitModifications_units, $unitModifications_amounts, $unitModifications_modes);
|
||||
|
||||
return $owndead;
|
||||
}
|
||||
|
||||
private function sendunitsComplete() {
|
||||
// PROCESARE ATACURI COMPLETE - functie critica, pastrata 100% compatibila
|
||||
// Aceasta functie gestioneaza toate atacurile care ajung la destinatie
|
||||
@@ -2213,42 +2260,8 @@ class Automation {
|
||||
for($i = 1; $i <= 50; $i++) $alldead[$i] = 0;
|
||||
$heroAttackDead = $dead11;
|
||||
|
||||
//kill own defence
|
||||
$unitlist = $database->getUnit($data['to'], false);
|
||||
$start = ($targettribe-1)*10+1;
|
||||
$end = ($targettribe*10);
|
||||
|
||||
if ($targettribe == 1) $u = "";
|
||||
else $u = $targettribe - 1;
|
||||
|
||||
$unitModifications_units = [];
|
||||
$unitModifications_amounts = [];
|
||||
$unitModifications_modes = [];
|
||||
for ($i = $start; $i <= $end; $i++) {
|
||||
if($i == $end) {
|
||||
$u = $targettribe;
|
||||
}
|
||||
|
||||
if($unitlist){
|
||||
$owndead[$i] = round($battlepart[2] * $unitlist['u'.$i]);
|
||||
$unitModifications_units[] = $i;
|
||||
$unitModifications_amounts[] = $owndead[$i];
|
||||
$unitModifications_modes[] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
$owndead['hero'] = 0;
|
||||
|
||||
if($unitlist){
|
||||
$owndead['hero'] = (isset($battlepart['deadherodef']) ? $battlepart['deadherodef'] : '');
|
||||
|
||||
$unitModifications_units[] = 'hero';
|
||||
$unitModifications_amounts[] = $owndead['hero'];
|
||||
$unitModifications_modes[] = 0;
|
||||
}
|
||||
|
||||
// modify units in DB
|
||||
$database->modifyUnit($data['to'], $unitModifications_units, $unitModifications_amounts, $unitModifications_modes);
|
||||
//kill own defence — extracted to applyOwnDefenceCasualties() [#155]
|
||||
$owndead = $this->applyOwnDefenceCasualties($data, $targettribe, $battlepart);
|
||||
|
||||
//kill other defence in village
|
||||
// ... once again, units could have changed, so we need to reselect
|
||||
|
||||
Reference in New Issue
Block a user