mirror of
https://github.com/Shadowss/TravianZ.git
synced 2026-08-28 22:27:15 +00:00
Refactor(sendunitsComplete): extract resolveVillageTarget()/resolveOasisTarget() [#155]
Extract the per-branch defender target resolution and battle-environment setup into two private helpers: resolveVillageTarget() and resolveOasisTarget(). Each returns the target owner (tribe/alliance), map info, conquest flag and the battle parameters (wall, armory/blacksmith tech, residence, siege masonry); the village helper also returns the evasion inputs. Both are read-only (no DB writes). The foreach body keeps handleEvasion(), buildDefenderUnits() and buildAttackerUnits() as explicit, ordered calls, so the village and oasis branches are now symmetric orchestration. Behaviour-preserving. The building/tech reads now run inside the helper before handleEvasion(); they read buildings and technology only (never the troops handleEvasion() may move), so the result is unchanged. A few dead locals are dropped (playerunit, wallgid, w; the redundant DefenderUnit/def_ab re-inits). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
committed by
Catalin Novgorodschi
parent
4ba2507f9d
commit
509a4d4137
+192
-76
@@ -2276,45 +2276,24 @@ class Automation {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function sendunitsComplete() {
|
/**
|
||||||
// PROCESARE ATACURI COMPLETE - functie critica, pastrata 100% compatibila
|
* Resolve the defender target context for a VILLAGE attack: target owner
|
||||||
// Aceasta functie gestioneaza toate atacurile care ajung la destinatie
|
* (tribe/alliance), map info, conquest flag, evasion inputs and the battle
|
||||||
// Include: batalii, capcane, evaziune erou, distrugere cladiri, cuceriri
|
* environment (wall, armory/blacksmith tech, residence, siege masonry).
|
||||||
global $bid19, $bid23, $bid34, $u99, $database, $battle, $technology, $units;
|
* Read-only — no DB writes.
|
||||||
|
* Pure behaviour-preserving extraction (refactor for issue #155).
|
||||||
|
*
|
||||||
|
* @param array $data Current attack row.
|
||||||
|
* @param array $dataarray Full batch of completed attacks.
|
||||||
|
* @param int $data_num Index of $data within $dataarray.
|
||||||
|
* @param int $owntribe Attacker tribe.
|
||||||
|
* @return array Target + battle-environment context.
|
||||||
|
*/
|
||||||
|
private function resolveVillageTarget($data, $dataarray, $data_num, $owntribe) {
|
||||||
|
global $database, $units;
|
||||||
|
|
||||||
$time = time();
|
|
||||||
$dataarray = $this->fetchCompletedAttacks($time);
|
|
||||||
$totalattackdead = $data_num = 0;
|
|
||||||
|
|
||||||
if ($dataarray && count($dataarray)) {
|
|
||||||
// preload village data (batched) so the per-attack loop hits the
|
|
||||||
// cache instead of querying row by row
|
|
||||||
$this->preloadBattleData($dataarray);
|
|
||||||
|
|
||||||
// calculate battles
|
|
||||||
foreach($dataarray as $data) {
|
|
||||||
//set base things
|
|
||||||
$totaltraped_att = 0;
|
|
||||||
for($i = 1; $i <= 11; $i++) ${'traped'.$i} = 0;
|
|
||||||
// per-attack context (attacker village/owner) — extracted to resolveAttackContext() [#155]
|
|
||||||
$ctx = $this->resolveAttackContext($data);
|
|
||||||
$isoasis = $ctx['isoasis'];
|
|
||||||
$AttackArrivalTime = $ctx['AttackArrivalTime'];
|
|
||||||
$AttackerWref = $ctx['AttackerWref'];
|
|
||||||
$DefenderWref = $ctx['DefenderWref'];
|
|
||||||
$NatarCapital = $ctx['NatarCapital'];
|
|
||||||
$AttackerID = $ctx['AttackerID'];
|
|
||||||
$Attacker['id'] = $ctx['AttackerID'];
|
|
||||||
$owntribe = $ctx['owntribe'];
|
|
||||||
$ownally = $ctx['ownally'];
|
|
||||||
$from = $ctx['from'];
|
|
||||||
$fromF = $ctx['fromF'];
|
|
||||||
|
|
||||||
//It's a village
|
|
||||||
if ($isoasis == 0){
|
|
||||||
$DefenderUserData = $this->getCachedUser($database->getVillageField($data['to'],"owner"),1);
|
$DefenderUserData = $this->getCachedUser($database->getVillageField($data['to'],"owner"),1);
|
||||||
$Defender['id'] = $DefenderUserData["id"];
|
$DefenderID = $DefenderUserData["id"];
|
||||||
$DefenderID = $Defender['id'];
|
|
||||||
$targettribe = $DefenderUserData["tribe"];
|
$targettribe = $DefenderUserData["tribe"];
|
||||||
$targetally = $DefenderUserData["alliance"];
|
$targetally = $DefenderUserData["alliance"];
|
||||||
$to = $database->getMInfo($data['to']);
|
$to = $database->getMInfo($data['to']);
|
||||||
@@ -2323,12 +2302,10 @@ class Automation {
|
|||||||
$NatarCapital = ($toF['owner'] == 3 && $toF['capital'] == 1);
|
$NatarCapital = ($toF['owner'] == 3 && $toF['capital'] == 1);
|
||||||
if(!isset($to['name']) || empty($to['name'])) $to['name'] = "[?]";
|
if(!isset($to['name']) || empty($to['name'])) $to['name'] = "[?]";
|
||||||
|
|
||||||
$DefenderUnit = [];
|
|
||||||
$DefenderUnit = $database->getUnit($data['to']);
|
$DefenderUnit = $database->getUnit($data['to']);
|
||||||
$evasion = $toF["evasion"];
|
$evasion = $toF["evasion"];
|
||||||
$maxevasion = $DefenderUserData["maxevasion"];
|
$maxevasion = $DefenderUserData["maxevasion"];
|
||||||
$gold = $DefenderUserData["gold"];
|
$gold = $DefenderUserData["gold"];
|
||||||
$playerunit = (($targettribe - 1) * 10);
|
|
||||||
$cannotsend = false;
|
$cannotsend = false;
|
||||||
|
|
||||||
$movements = $database->getMovement(34, $data['to'], 1);
|
$movements = $database->getMovement(34, $data['to'], 1);
|
||||||
@@ -2341,26 +2318,6 @@ class Automation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->handleEvasion($data, $DefenderID, $DefenderUnit, $targettribe, $evasion, $maxevasion, $gold, $cannotsend, $dataarray[$data_num]['attack_type']);
|
|
||||||
|
|
||||||
// defence units gathered — extracted to buildDefenderUnits() [#155]
|
|
||||||
$rom = $ger = $gal = $nat = $natar = 0;
|
|
||||||
$defUnits = $this->buildDefenderUnits($data['to']);
|
|
||||||
$Defender = $defUnits['Defender'];
|
|
||||||
$enforDefender = $defUnits['enforDefender'];
|
|
||||||
$enforcementarray = $defUnits['enforcementarray'];
|
|
||||||
|
|
||||||
// attacker army built — extracted to buildAttackerUnits() [#155]
|
|
||||||
$atkUnits = $this->buildAttackerUnits($dataarray[$data_num], $owntribe, $isoasis);
|
|
||||||
$Attacker = $atkUnits['Attacker'];
|
|
||||||
$start = $atkUnits['start'];
|
|
||||||
$end = $atkUnits['end'];
|
|
||||||
$catp_pic = $atkUnits['catp_pic'];
|
|
||||||
$ram_pic = $atkUnits['ram_pic'];
|
|
||||||
$chief_pic = $atkUnits['chief_pic'];
|
|
||||||
$spy_pic = $atkUnits['spy_pic'];
|
|
||||||
$hero_pic = $atkUnits['hero_pic'];
|
|
||||||
|
|
||||||
//need to set these variables.
|
//need to set these variables.
|
||||||
$def_wall = $database->getFieldLevel($data['to'], 40, false);
|
$def_wall = $database->getFieldLevel($data['to'], 40, false);
|
||||||
$att_tribe = $owntribe;
|
$att_tribe = $owntribe;
|
||||||
@@ -2396,28 +2353,154 @@ class Automation {
|
|||||||
$def_ab[$ud + 8] = $armory['a8'];
|
$def_ab[$ud + 8] = $armory['a8'];
|
||||||
|
|
||||||
//rams attack
|
//rams attack
|
||||||
|
$walllevel = 0;
|
||||||
|
$wallid = 0;
|
||||||
if (($data['t7']) > 0 && $type == 3) {
|
if (($data['t7']) > 0 && $type == 3) {
|
||||||
$basearraywall = $to;
|
$basearraywall = $to;
|
||||||
if (($walllevel = $database->getFieldLevel($basearraywall['wref'], 40, false)) > 0){
|
if (($walllevel = $database->getFieldLevel($basearraywall['wref'], 40, false)) > 0){
|
||||||
$wallgid = $database->getFieldLevel($basearraywall['wref'],"40t");
|
|
||||||
$wallid = 40;
|
$wallid = 40;
|
||||||
$w = 4;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$tblevel = 1;
|
$tblevel = 1;
|
||||||
$stonemason = $database->getFieldLevelInVillage($data['to'], 34);
|
$stonemason = $database->getFieldLevelInVillage($data['to'], 34);
|
||||||
|
|
||||||
}else{ //It's an oasis
|
return [
|
||||||
|
'DefenderID' => $DefenderID, 'targettribe' => $targettribe, 'targetally' => $targetally,
|
||||||
|
'to' => $to, 'toF' => $toF, 'conqureby' => $conqureby, 'NatarCapital' => $NatarCapital,
|
||||||
|
'DefenderUnit' => $DefenderUnit, 'evasion' => $evasion, 'maxevasion' => $maxevasion,
|
||||||
|
'gold' => $gold, 'cannotsend' => $cannotsend,
|
||||||
|
'def_wall' => $def_wall, 'att_tribe' => $att_tribe, 'def_tribe' => $def_tribe,
|
||||||
|
'attpop' => $attpop, 'defpop' => $defpop, 'residence' => $residence, 'def_ab' => $def_ab,
|
||||||
|
'type' => $type, 'scout' => $scout,
|
||||||
|
'att_ab1' => $att_ab1, 'att_ab2' => $att_ab2, 'att_ab3' => $att_ab3, 'att_ab4' => $att_ab4,
|
||||||
|
'att_ab5' => $att_ab5, 'att_ab6' => $att_ab6, 'att_ab7' => $att_ab7, 'att_ab8' => $att_ab8,
|
||||||
|
'walllevel' => $walllevel, 'wallid' => $wallid,
|
||||||
|
'tblevel' => $tblevel, 'stonemason' => $stonemason,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve the defender target context for an OASIS attack: target owner
|
||||||
|
* (tribe/alliance), oasis map info, conquest flag and the (mostly fixed)
|
||||||
|
* battle environment. Read-only — no DB writes.
|
||||||
|
* Pure behaviour-preserving extraction (refactor for issue #155).
|
||||||
|
*
|
||||||
|
* @param array $data Current attack row.
|
||||||
|
* @param array $dataarray Full batch of completed attacks.
|
||||||
|
* @param int $data_num Index of $data within $dataarray.
|
||||||
|
* @param int $owntribe Attacker tribe.
|
||||||
|
* @return array Target + battle-environment context.
|
||||||
|
*/
|
||||||
|
private function resolveOasisTarget($data, $dataarray, $data_num, $owntribe) {
|
||||||
|
global $database;
|
||||||
|
|
||||||
$DefenderUserData = $this->getCachedUser($database->getOasisField($data['to'], "owner"),1);
|
$DefenderUserData = $this->getCachedUser($database->getOasisField($data['to'], "owner"),1);
|
||||||
$Defender['id'] = $DefenderUserData["id"];
|
$DefenderID = $DefenderUserData["id"];
|
||||||
$DefenderID = $Defender['id'];
|
|
||||||
$targettribe = $DefenderUserData["tribe"];
|
$targettribe = $DefenderUserData["tribe"];
|
||||||
$targetally = $DefenderUserData["alliance"];
|
$targetally = $DefenderUserData["alliance"];
|
||||||
$to = $database->getOMInfo($data['to']);
|
$to = $database->getOMInfo($data['to']);
|
||||||
$toF = $database->getOasisV($data['to']);
|
$toF = $database->getOasisV($data['to']);
|
||||||
$conqureby = $toF['conqured'];
|
$conqureby = $toF['conqured'];
|
||||||
|
|
||||||
|
//need to set these variables.
|
||||||
|
$def_wall = $residence = $attpop = 0;
|
||||||
|
$att_tribe = $owntribe;
|
||||||
|
$def_tribe = $targettribe;
|
||||||
|
$defpop = 500;
|
||||||
|
|
||||||
|
//type of attack
|
||||||
|
$type = $dataarray[$data_num]['attack_type'];
|
||||||
|
$scout = ($type == 1) ? 1 : 0;
|
||||||
|
|
||||||
|
$att_ab1 = $att_ab2 = $att_ab3 = $att_ab4 = $att_ab5 = $att_ab6 = $att_ab7 = $att_ab8 = 0;
|
||||||
|
$def_ab = [];
|
||||||
|
$def_ab[31] = $def_ab[32] = $def_ab[33] = $def_ab[34] = $def_ab[35] = $def_ab[36] = $def_ab[37] = $def_ab[38] = 0;
|
||||||
|
|
||||||
|
$walllevel = $tblevel = $stonemason = 0;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'DefenderID' => $DefenderID, 'targettribe' => $targettribe, 'targetally' => $targetally,
|
||||||
|
'to' => $to, 'toF' => $toF, 'conqureby' => $conqureby,
|
||||||
|
'def_wall' => $def_wall, 'att_tribe' => $att_tribe, 'def_tribe' => $def_tribe,
|
||||||
|
'attpop' => $attpop, 'defpop' => $defpop, 'residence' => $residence, 'def_ab' => $def_ab,
|
||||||
|
'type' => $type, 'scout' => $scout,
|
||||||
|
'att_ab1' => $att_ab1, 'att_ab2' => $att_ab2, 'att_ab3' => $att_ab3, 'att_ab4' => $att_ab4,
|
||||||
|
'att_ab5' => $att_ab5, 'att_ab6' => $att_ab6, 'att_ab7' => $att_ab7, 'att_ab8' => $att_ab8,
|
||||||
|
'walllevel' => $walllevel, 'tblevel' => $tblevel, 'stonemason' => $stonemason,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function sendunitsComplete() {
|
||||||
|
// PROCESARE ATACURI COMPLETE - functie critica, pastrata 100% compatibila
|
||||||
|
// Aceasta functie gestioneaza toate atacurile care ajung la destinatie
|
||||||
|
// Include: batalii, capcane, evaziune erou, distrugere cladiri, cuceriri
|
||||||
|
global $bid19, $bid23, $bid34, $u99, $database, $battle, $technology, $units;
|
||||||
|
|
||||||
|
$time = time();
|
||||||
|
$dataarray = $this->fetchCompletedAttacks($time);
|
||||||
|
$totalattackdead = $data_num = 0;
|
||||||
|
|
||||||
|
if ($dataarray && count($dataarray)) {
|
||||||
|
// preload village data (batched) so the per-attack loop hits the
|
||||||
|
// cache instead of querying row by row
|
||||||
|
$this->preloadBattleData($dataarray);
|
||||||
|
|
||||||
|
// calculate battles
|
||||||
|
foreach($dataarray as $data) {
|
||||||
|
//set base things
|
||||||
|
$totaltraped_att = 0;
|
||||||
|
for($i = 1; $i <= 11; $i++) ${'traped'.$i} = 0;
|
||||||
|
// per-attack context (attacker village/owner) — extracted to resolveAttackContext() [#155]
|
||||||
|
$ctx = $this->resolveAttackContext($data);
|
||||||
|
$isoasis = $ctx['isoasis'];
|
||||||
|
$AttackArrivalTime = $ctx['AttackArrivalTime'];
|
||||||
|
$AttackerWref = $ctx['AttackerWref'];
|
||||||
|
$DefenderWref = $ctx['DefenderWref'];
|
||||||
|
$NatarCapital = $ctx['NatarCapital'];
|
||||||
|
$AttackerID = $ctx['AttackerID'];
|
||||||
|
$Attacker['id'] = $ctx['AttackerID'];
|
||||||
|
$owntribe = $ctx['owntribe'];
|
||||||
|
$ownally = $ctx['ownally'];
|
||||||
|
$from = $ctx['from'];
|
||||||
|
$fromF = $ctx['fromF'];
|
||||||
|
|
||||||
|
//It's a village
|
||||||
|
if ($isoasis == 0){
|
||||||
|
// target + battle environment — extracted to resolveVillageTarget() [#155]
|
||||||
|
$vt = $this->resolveVillageTarget($data, $dataarray, $data_num, $owntribe);
|
||||||
|
$DefenderID = $vt['DefenderID'];
|
||||||
|
$targettribe = $vt['targettribe'];
|
||||||
|
$targetally = $vt['targetally'];
|
||||||
|
$to = $vt['to'];
|
||||||
|
$toF = $vt['toF'];
|
||||||
|
$conqureby = $vt['conqureby'];
|
||||||
|
$NatarCapital = $vt['NatarCapital'];
|
||||||
|
$DefenderUnit = $vt['DefenderUnit'];
|
||||||
|
$def_wall = $vt['def_wall'];
|
||||||
|
$att_tribe = $vt['att_tribe'];
|
||||||
|
$def_tribe = $vt['def_tribe'];
|
||||||
|
$attpop = $vt['attpop'];
|
||||||
|
$defpop = $vt['defpop'];
|
||||||
|
$residence = $vt['residence'];
|
||||||
|
$def_ab = $vt['def_ab'];
|
||||||
|
$type = $vt['type'];
|
||||||
|
$scout = $vt['scout'];
|
||||||
|
$att_ab1 = $vt['att_ab1'];
|
||||||
|
$att_ab2 = $vt['att_ab2'];
|
||||||
|
$att_ab3 = $vt['att_ab3'];
|
||||||
|
$att_ab4 = $vt['att_ab4'];
|
||||||
|
$att_ab5 = $vt['att_ab5'];
|
||||||
|
$att_ab6 = $vt['att_ab6'];
|
||||||
|
$att_ab7 = $vt['att_ab7'];
|
||||||
|
$att_ab8 = $vt['att_ab8'];
|
||||||
|
$walllevel = $vt['walllevel'];
|
||||||
|
$wallid = $vt['wallid'];
|
||||||
|
$tblevel = $vt['tblevel'];
|
||||||
|
$stonemason = $vt['stonemason'];
|
||||||
|
|
||||||
|
$this->handleEvasion($data, $DefenderID, $DefenderUnit, $targettribe, $vt['evasion'], $vt['maxevasion'], $vt['gold'], $vt['cannotsend'], $dataarray[$data_num]['attack_type']);
|
||||||
|
|
||||||
// defence units gathered — extracted to buildDefenderUnits() [#155]
|
// defence units gathered — extracted to buildDefenderUnits() [#155]
|
||||||
$rom = $ger = $gal = $nat = $natar = 0;
|
$rom = $ger = $gal = $nat = $natar = 0;
|
||||||
$defUnits = $this->buildDefenderUnits($data['to']);
|
$defUnits = $this->buildDefenderUnits($data['to']);
|
||||||
@@ -2436,20 +2519,53 @@ class Automation {
|
|||||||
$spy_pic = $atkUnits['spy_pic'];
|
$spy_pic = $atkUnits['spy_pic'];
|
||||||
$hero_pic = $atkUnits['hero_pic'];
|
$hero_pic = $atkUnits['hero_pic'];
|
||||||
|
|
||||||
//need to set these variables.
|
}else{ //It's an oasis
|
||||||
$def_wall = $residence = $attpop = 0;
|
// target + battle environment — extracted to resolveOasisTarget() [#155]
|
||||||
$att_tribe = $owntribe;
|
$ot = $this->resolveOasisTarget($data, $dataarray, $data_num, $owntribe);
|
||||||
$def_tribe = $targettribe;
|
$DefenderID = $ot['DefenderID'];
|
||||||
$defpop = 500;
|
$targettribe = $ot['targettribe'];
|
||||||
|
$targetally = $ot['targetally'];
|
||||||
|
$to = $ot['to'];
|
||||||
|
$toF = $ot['toF'];
|
||||||
|
$conqureby = $ot['conqureby'];
|
||||||
|
$def_wall = $ot['def_wall'];
|
||||||
|
$att_tribe = $ot['att_tribe'];
|
||||||
|
$def_tribe = $ot['def_tribe'];
|
||||||
|
$attpop = $ot['attpop'];
|
||||||
|
$defpop = $ot['defpop'];
|
||||||
|
$residence = $ot['residence'];
|
||||||
|
$def_ab = $ot['def_ab'];
|
||||||
|
$type = $ot['type'];
|
||||||
|
$scout = $ot['scout'];
|
||||||
|
$att_ab1 = $ot['att_ab1'];
|
||||||
|
$att_ab2 = $ot['att_ab2'];
|
||||||
|
$att_ab3 = $ot['att_ab3'];
|
||||||
|
$att_ab4 = $ot['att_ab4'];
|
||||||
|
$att_ab5 = $ot['att_ab5'];
|
||||||
|
$att_ab6 = $ot['att_ab6'];
|
||||||
|
$att_ab7 = $ot['att_ab7'];
|
||||||
|
$att_ab8 = $ot['att_ab8'];
|
||||||
|
$walllevel = $ot['walllevel'];
|
||||||
|
$tblevel = $ot['tblevel'];
|
||||||
|
$stonemason = $ot['stonemason'];
|
||||||
|
|
||||||
//type of attack
|
// defence units gathered — extracted to buildDefenderUnits() [#155]
|
||||||
$type = $dataarray[$data_num]['attack_type'];
|
$rom = $ger = $gal = $nat = $natar = 0;
|
||||||
$scout = ($type == 1) ? 1 : 0;
|
$defUnits = $this->buildDefenderUnits($data['to']);
|
||||||
|
$Defender = $defUnits['Defender'];
|
||||||
|
$enforDefender = $defUnits['enforDefender'];
|
||||||
|
$enforcementarray = $defUnits['enforcementarray'];
|
||||||
|
|
||||||
$att_ab1 = $att_ab2 = $att_ab3 = $att_ab4 = $att_ab5 = $att_ab6 = $att_ab7 = $att_ab8 = 0;
|
// attacker army built — extracted to buildAttackerUnits() [#155]
|
||||||
$def_ab[31] = $def_ab[32] = $def_ab[33] = $def_ab[34] = $def_ab[35] = $def_ab[36] = $def_ab[37] = $def_ab[38] = 0;
|
$atkUnits = $this->buildAttackerUnits($dataarray[$data_num], $owntribe, $isoasis);
|
||||||
|
$Attacker = $atkUnits['Attacker'];
|
||||||
$walllevel = $tblevel = $stonemason = 0;
|
$start = $atkUnits['start'];
|
||||||
|
$end = $atkUnits['end'];
|
||||||
|
$catp_pic = $atkUnits['catp_pic'];
|
||||||
|
$ram_pic = $atkUnits['ram_pic'];
|
||||||
|
$chief_pic = $atkUnits['chief_pic'];
|
||||||
|
$spy_pic = $atkUnits['spy_pic'];
|
||||||
|
$hero_pic = $atkUnits['hero_pic'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// attacker/defender populations + village lists — extracted to calculatePopulations() [#155]
|
// attacker/defender populations + village lists — extracted to calculatePopulations() [#155]
|
||||||
|
|||||||
Reference in New Issue
Block a user