Refactor(Automation): TO-DO list items 7-9 [#266] (#278)

This commit is contained in:
Ferywir
2026-06-24 17:21:30 +02:00
committed by GitHub
parent e13586a2d7
commit 49ce36fd99
+86 -77
View File
@@ -387,7 +387,7 @@ class Automation {
} }
private function buildComplete() { private function buildComplete() {
global $database, $technology, $bid18, $bid10, $bid11, $bid38, $bid39; global $database;
$time = time(); $time = time();
// IDs of villages that were affected by this building completion update, // IDs of villages that were affected by this building completion update,
@@ -435,29 +435,15 @@ class Automation {
// update capacity if we updated a warehouse or a granary // update capacity if we updated a warehouse or a granary
if (in_array($indi['type'], [10, 11, 38, 39])) { if (in_array($indi['type'], [10, 11, 38, 39])) {
$fieldDbName = (in_array($indi['type'], [10, 38]) ? 'maxstore' : 'maxcrop'); [$fieldDbName, $max] = $this->updateStorageCapacity($indi['type'], $level, $villageData);
$max = $villageData[$fieldDbName];
if($level == 1 && $max == STORAGE_BASE) $max = STORAGE_BASE;
if ($level != 1) $max -= ${'bid'.$indi['type']}[$level - 1]['attri'] * STORAGE_MULTIPLIER;
$max += ${'bid'.$indi['type']}[$level]['attri'] * STORAGE_MULTIPLIER;
$fieldsToSet[$fieldDbName] = $max; $fieldsToSet[$fieldDbName] = $max;
} }
// if we updated Embassy, update maximum members that the alliance can take // if we updated Embassy, update maximum members that the alliance can take
if($indi['type'] == 18) Automation::updateMax($villageOwner); if($indi['type'] == 18) Automation::updateMax($villageOwner);
if ($indi['type'] == 40 && ($indi['level'] % 5 == 0 || $indi['level'] > 95) && $indi['level'] != 100) { // World Wonder completion handling (Natar attacks, winner lock, last-upgrade time)
$this->startNatarAttack($indi['level'], $indi['wid'], $indi['timestamp']); if ($indi['type'] == 40) $this->completeWorldWonder($indi);
}
//now can't be more than one winner if ww to level 100 is build by 2 users or more on same time
if ($indi['type'] == 40 && $indi['level'] == 100) {
mysqli_query($database->dblink,"TRUNCATE ".TB_PREFIX."bdata");
}
// TODO: find out what exactly these conditions are for // TODO: find out what exactly these conditions are for
// no special military conditioning for Teutons and Gauls // no special military conditioning for Teutons and Gauls
@@ -469,12 +455,6 @@ class Automation {
else $loopconUpdates[$indi['wid']] = ' AND field < 19'; else $loopconUpdates[$indi['wid']] = ' AND field < 19';
} }
// Update ww last finish upgrade
if ($indi['type'] == 40) {
$qW = "UPDATE ".TB_PREFIX."fdata set ww_lastupdate = ".time()." where vref = ".(int) $indi['wid'];
$database->query($qW);
}
$dbIdsToDelete[] = (int) $indi['id']; $dbIdsToDelete[] = (int) $indi['id'];
} }
@@ -508,6 +488,47 @@ class Automation {
} }
} }
/**
* Recompute a warehouse/granary capacity after a build completion.
* Returns [$fieldDbName, $max]: the vdata column to update and its new value.
*/
private function updateStorageCapacity($type, $level, $villageData) {
global $bid10, $bid11, $bid38, $bid39;
$fieldDbName = (in_array($type, [10, 38]) ? 'maxstore' : 'maxcrop');
$max = $villageData[$fieldDbName];
if($level == 1 && $max == STORAGE_BASE) $max = STORAGE_BASE;
if ($level != 1) $max -= ${'bid'.$type}[$level - 1]['attri'] * STORAGE_MULTIPLIER;
$max += ${'bid'.$type}[$level]['attri'] * STORAGE_MULTIPLIER;
return [$fieldDbName, $max];
}
/**
* Handle the side effects of completing a World Wonder (type 40) build:
* launch the Natar attack waves, lock out further winners at level 100,
* and record the last upgrade time.
*/
private function completeWorldWonder($indi) {
global $database;
if (($indi['level'] % 5 == 0 || $indi['level'] > 95) && $indi['level'] != 100) {
$this->startNatarAttack($indi['level'], $indi['wid'], $indi['timestamp']);
}
//now can't be more than one winner if ww to level 100 is build by 2 users or more on same time
if ($indi['level'] == 100) {
mysqli_query($database->dblink,"TRUNCATE ".TB_PREFIX."bdata");
}
// Update ww last finish upgrade
$qW = "UPDATE ".TB_PREFIX."fdata set ww_lastupdate = ".time()." where vref = ".(int) $indi['wid'];
$database->query($qW);
}
private function startNatarAttack($level, $vid, $time) { private function startNatarAttack($level, $vid, $time) {
global $database; global $database;
@@ -515,7 +536,46 @@ class Automation {
// I took the data from my first ww (first .org world) // I took the data from my first ww (first .org world)
// TODO: get the algo from the real travian with the 100 biggest offs // TODO: get the algo from the real travian with the 100 biggest offs
$troops = [5 => [[3412, 2814, 4156, 3553, 9, 0], [35, 0, 77, 33, 17, 10]], // select the troops^^
$troops = $this->getNatarTroopTable();
if (isset($troops[$level])) $units = $troops[$level];
else return false;
// get the capital village from the natars
$query = mysqli_query($database->dblink,'SELECT `wref` FROM `' . TB_PREFIX . 'vdata` WHERE `owner` = 3 and `capital` = 1 LIMIT 1') or die(mysqli_error($database->dblink));
$row = mysqli_fetch_assoc($query);
// start the attacks
$endtime = $time + round(86400 / INCREASE_SPEED);
// -.-
$vid = (int) $vid;
mysqli_query($database->dblink,'INSERT INTO `' . TB_PREFIX . 'ww_attacks` (`vid`, `attack_time`) VALUES (' . $vid . ', ' . $endtime . ')');
mysqli_query($database->dblink,'INSERT INTO `' . TB_PREFIX . 'ww_attacks` (`vid`, `attack_time`) VALUES (' . $vid . ', ' . ($endtime + 1) . ')');
// two waves: the second one targets the WW (catapult target 40) one second later
$this->launchNatarWave($row['wref'], $vid, $units[0], 0, $time, $endtime);
$this->launchNatarWave($row['wref'], $vid, $units[1], 40, $time, $endtime + 1);
}
/**
* Fire a single Natar attack wave at a World Wonder village.
* $unitRow is one troop row [t2, t3, t5, t6, t7, t8]; $ctar1 is the
* catapult target slot (0 for the first wave, 40 for the second).
*/
private function launchNatarWave($source, $vid, $unitRow, $ctar1, $time, $arrival) {
global $database;
$ref = $database->addAttack($source, 0, $unitRow[0], $unitRow[1], 0, $unitRow[2], $unitRow[3], $unitRow[4], $unitRow[5], 0, 0, 0, 3, $ctar1, 0, 0, 0, 20, 20, 0, 20, 20, 20, 20);
$database->addMovement(3, $source, $vid, $ref, $time, $arrival);
}
/**
* Hard-coded Natar offensive waves indexed by World Wonder level.
* Each level holds two waves, each a troop row [t2, t3, t5, t6, t7, t8].
*/
private function getNatarTroopTable() {
return [5 => [[3412, 2814, 4156, 3553, 9, 0], [35, 0, 77, 33, 17, 10]],
10 => [[4314, 3688, 5265, 4621, 13, 0], [65, 0, 175, 77, 28, 17]], 10 => [[4314, 3688, 5265, 4621, 13, 0], [65, 0, 175, 77, 28, 17]],
15 => [[4645, 4267, 5659, 5272, 15, 0], [99, 0, 305, 134, 40, 25]], 15 => [[4645, 4267, 5659, 5272, 15, 0], [99, 0, 305, 134, 40, 25]],
20 => [[6207, 5881, 7625, 7225, 22, 0], [144, 0, 456, 201, 56, 36]], 20 => [[6207, 5881, 7625, 7225, 22, 0], [144, 0, 456, 201, 56, 36]],
@@ -538,30 +598,6 @@ class Automation {
97 => [[32940, 37099, 40968, 44235, 150, 0], [794, 0, 2755, 1219, 289, 190]], 97 => [[32940, 37099, 40968, 44235, 150, 0], [794, 0, 2755, 1219, 289, 190]],
98 => [[33521, 37691, 41686, 44953, 152, 0], [812, 0, 2816, 1246, 296, 194]], 98 => [[33521, 37691, 41686, 44953, 152, 0], [812, 0, 2816, 1246, 296, 194]],
99 => [[36251, 40861, 45089, 48714, 165, 0], [872, 0, 3025, 1338, 317, 208]]]; 99 => [[36251, 40861, 45089, 48714, 165, 0], [872, 0, 3025, 1338, 317, 208]]];
// select the troops^^
if (isset($troops[$level])) $units = $troops[$level];
else return false;
// get the capital village from the natars
$query = mysqli_query($database->dblink,'SELECT `wref` FROM `' . TB_PREFIX . 'vdata` WHERE `owner` = 3 and `capital` = 1 LIMIT 1') or die(mysqli_error($database->dblink));
$row = mysqli_fetch_assoc($query);
// start the attacks
$endtime = $time + round(86400 / INCREASE_SPEED);
// -.-
$vid = (int) $vid;
mysqli_query($database->dblink,'INSERT INTO `' . TB_PREFIX . 'ww_attacks` (`vid`, `attack_time`) VALUES (' . $vid . ', ' . $endtime . ')');
mysqli_query($database->dblink,'INSERT INTO `' . TB_PREFIX . 'ww_attacks` (`vid`, `attack_time`) VALUES (' . $vid . ', ' . ($endtime + 1) . ')');
// wave 1
$ref = $database->addAttack($row['wref'], 0, $units[0][0], $units[0][1], 0, $units[0][2], $units[0][3], $units[0][4], $units[0][5], 0, 0, 0, 3, 0, 0, 0, 0, 20, 20, 0, 20, 20, 20, 20);
$database->addMovement(3, $row['wref'], $vid, $ref, $time, $endtime);
// wave 2
$ref2 = $database->addAttack($row['wref'], 0, $units[1][0], $units[1][1], 0, $units[1][2], $units[1][3], $units[1][4], $units[1][5], 0, 0, 0, 3, 40, 0, 0, 0, 20, 20, 0, 20, 20, 20, 20, ['vid' => $vid, 'endtime' => ($endtime + 1)]);
$database->addMovement(3, $row['wref'], $vid, $ref2, $time, $endtime + 1);
} }
private function checkWWAttacks() { private function checkWWAttacks() {
@@ -2905,7 +2941,7 @@ class Automation {
// PROCESARE ATACURI COMPLETE - functie critica, pastrata 100% compatibila // PROCESARE ATACURI COMPLETE - functie critica, pastrata 100% compatibila
// Aceasta functie gestioneaza toate atacurile care ajung la destinatie // Aceasta functie gestioneaza toate atacurile care ajung la destinatie
// Include: batalii, capcane, evaziune erou, distrugere cladiri, cuceriri // Include: batalii, capcane, evaziune erou, distrugere cladiri, cuceriri
global $bid19, $bid23, $bid34, $u99, $database, $battle, $technology, $units; global $bid23, $database, $battle, $technology, $units;
$time = time(); $time = time();
$dataarray = $this->fetchCompletedAttacks($time); $dataarray = $this->fetchCompletedAttacks($time);
@@ -2972,7 +3008,6 @@ class Automation {
$this->handleEvasion($data, $DefenderID, $DefenderUnit, $targettribe, $vt['evasion'], $vt['maxevasion'], $vt['gold'], $vt['cannotsend'], $dataarray[$data_num]['attack_type']); $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;
$defUnits = $this->buildDefenderUnits($data['to']); $defUnits = $this->buildDefenderUnits($data['to']);
$Defender = $defUnits['Defender']; $Defender = $defUnits['Defender'];
$enforDefender = $defUnits['enforDefender']; $enforDefender = $defUnits['enforDefender'];
@@ -3020,7 +3055,6 @@ class Automation {
$stonemason = $ot['stonemason']; $stonemason = $ot['stonemason'];
// defence units gathered — extracted to buildDefenderUnits() [#155] // defence units gathered — extracted to buildDefenderUnits() [#155]
$rom = $ger = $gal = $nat = $natar = 0;
$defUnits = $this->buildDefenderUnits($data['to']); $defUnits = $this->buildDefenderUnits($data['to']);
$Defender = $defUnits['Defender']; $Defender = $defUnits['Defender'];
$enforDefender = $defUnits['enforDefender']; $enforDefender = $defUnits['enforDefender'];
@@ -3056,11 +3090,6 @@ class Automation {
$defspy = $enforDefender['u4'] > 0 || $enforDefender['u14'] > 0 || $enforDefender['u23'] > 0 || $enforDefender['u44'] > 0; $defspy = $enforDefender['u4'] > 0 || $enforDefender['u14'] > 0 || $enforDefender['u23'] > 0 || $enforDefender['u44'] > 0;
if(PEACE == 0 || $targettribe == 4 || $targettribe == 5 || $scout){ if(PEACE == 0 || $targettribe == 4 || $targettribe == 5 || $scout){
if($targettribe == 1) $def_spy = $enforDefender['u4'];
elseif($targettribe == 2) $def_spy = $enforDefender['u14'];
elseif($targettribe == 3) $def_spy = $enforDefender['u23'];
elseif($targettribe == 5) $def_spy = $enforDefender['u44'];
// trapper resolution + prisoners — extracted to calculateTrappedUnits() [#155] // trapper resolution + prisoners — extracted to calculateTrappedUnits() [#155]
$trapResult = $this->calculateTrappedUnits($data, $Defender, $Attacker, $NatarCapital, $scout, $start, $end); $trapResult = $this->calculateTrappedUnits($data, $Defender, $Attacker, $NatarCapital, $scout, $start, $end);
for($i = 1; $i <= 11; $i++) ${'traped'.$i} = $trapResult['traped'][$i]; for($i = 1; $i <= 11; $i++) ${'traped'.$i} = $trapResult['traped'][$i];
@@ -3148,12 +3177,8 @@ class Automation {
$unitssend_att = ''.$data['t1'].','.$data['t2'].','.$data['t3'].','.$data['t4'].','.$data['t5'].','.$data['t6'].','.$data['t7'].','.$data['t8'].','.$data['t9'].','.$data['t10'].''; $unitssend_att = ''.$data['t1'].','.$data['t2'].','.$data['t3'].','.$data['t4'].','.$data['t5'].','.$data['t6'].','.$data['t7'].','.$data['t8'].','.$data['t9'].','.$data['t10'].'';
$herosend_att = $data['t11']; $herosend_att = $data['t11'];
if ($herosend_att > 0) $unitssend_att_check = $unitssend_att.','.$data['t11'];
else $unitssend_att_check = $unitssend_att;
//reinforcement report (pre-casualty defender data) — extracted to collectReinforcementReport() [#155] //reinforcement report (pre-casualty defender data) — extracted to collectReinforcementReport() [#155]
$reinfReport = $this->collectReinforcementReport($data, $targettribe, $Defender); $reinfReport = $this->collectReinforcementReport($data, $targettribe, $Defender);
$DefenderEnf = $reinfReport['DefenderEnf'];
$DefenderHeroesTotArray = $reinfReport['DefenderHeroesTotArray']; $DefenderHeroesTotArray = $reinfReport['DefenderHeroesTotArray'];
$DefenderHeroesDeadArray = $reinfReport['DefenderHeroesDeadArray']; $DefenderHeroesDeadArray = $reinfReport['DefenderHeroesDeadArray'];
$unitssend_def = $reinfReport['unitssend_def']; $unitssend_def = $reinfReport['unitssend_def'];
@@ -3177,7 +3202,6 @@ class Automation {
$alldead = []; $alldead = [];
for($i = 1; $i <= 50; $i++) $alldead[$i] = 0; for($i = 1; $i <= 50; $i++) $alldead[$i] = 0;
$heroAttackDead = $dead11;
//kill own defence — extracted to applyOwnDefenceCasualties() [#155] //kill own defence — extracted to applyOwnDefenceCasualties() [#155]
$owndead = $this->applyOwnDefenceCasualties($data, $targettribe, $battlepart); $owndead = $this->applyOwnDefenceCasualties($data, $targettribe, $battlepart);
@@ -3232,10 +3256,6 @@ class Automation {
$unitsdead_att = $dead1.','.$dead2.','.$dead3.','.$dead4.','.$dead5.','.$dead6.','.$dead7.','.$dead8.','.$dead9.','.$dead10; $unitsdead_att = $dead1.','.$dead2.','.$dead3.','.$dead4.','.$dead5.','.$dead6.','.$dead7.','.$dead8.','.$dead9.','.$dead10;
$unitstraped_att = $traped1.','.$traped2.','.$traped3.','.$traped4.','.$traped5.','.$traped6.','.$traped7.','.$traped8.','.$traped9.','.$traped10.','.$traped11; $unitstraped_att = $traped1.','.$traped2.','.$traped3.','.$traped4.','.$traped5.','.$traped6.','.$traped7.','.$traped8.','.$traped9.','.$traped10.','.$traped11;
if($herosend_att > 0) $unitsdead_att_check = $unitsdead_att.','.$dead11;
else $unitsdead_att_check = $unitsdead_att;
//top 10 attack and defence update //top 10 attack and defence update
$totaldead_att = $dead1 + $dead2 + $dead3 + $dead4 + $dead5 + $dead6 + $dead7 + $dead8 + $dead9 + $dead10 + $dead11; $totaldead_att = $dead1 + $dead2 + $dead3 + $dead4 + $dead5 + $dead6 + $dead7 + $dead8 + $dead9 + $dead10 + $dead11;
$totalattackdead += $totaldead_att; $totalattackdead += $totaldead_att;
@@ -3349,16 +3369,13 @@ class Automation {
unset( unset(
$Attacker $Attacker
,$Defender ,$Defender
,$DefenderEnf
,$DefenderHeroesTotArray ,$DefenderHeroesTotArray
,$DefenderHeroesDeadArray ,$DefenderHeroesDeadArray
,$DefenderHeroesTot ,$DefenderHeroesTot
,$DefenderHeroesDead ,$DefenderHeroesDead
,$enforce
,$unitssend_att ,$unitssend_att
,$unitssend_def ,$unitssend_def
,$battlepart ,$battlepart
,$unitlist
,$unitsdead_def ,$unitsdead_def
,$dead ,$dead
,$steal ,$steal
@@ -3366,9 +3383,6 @@ class Automation {
,$data ,$data
,$data2 ,$data2
,$to ,$to
,$artifact
,$artifactBig
,$canclaim
,$data_fail ,$data_fail
,$owntribe ,$owntribe
,$unitsdead_att ,$unitsdead_att
@@ -3382,13 +3396,8 @@ class Automation {
,$totaldead_att ,$totaldead_att
,$totaltraped_att ,$totaltraped_att
,$totaldead_def ,$totaldead_def
,$unitsdead_att_check
,$totalattackdead ,$totalattackdead
,$enforce1
,$defheroowner
,$enforceowner
,$defheroxp ,$defheroxp
,$reinfheroxp
,$AttackerWref ,$AttackerWref
,$DefenderWref ,$DefenderWref
,$troopsdead1 ,$troopsdead1