From 7dc27047b6461f3ce21d31e5162f1b98a1a372d1 Mon Sep 17 00:00:00 2001 From: Ivor Barhansky Date: Tue, 16 Jan 2024 09:13:15 +0200 Subject: [PATCH] Optimising Stat()::class. --- _incl_data/class/Battle.php | 17 ++++++-------- _incl_data/class/BotPriemLogic.php | 8 +++---- _incl_data/class/Item/Data/Bonuses.php | 11 ++++----- _incl_data/class/Item/Data/Requirements.php | 8 +++---- _incl_data/class/Model/Constant/Stat.php | 14 ------------ _incl_data/class/User/InfoBox.php | 15 ++++++------- _incl_data/class/User/Item.php | 3 ++- _incl_data/class/User/Stats.php | 25 +++++++++------------ inf.php | 7 +++--- 9 files changed, 43 insertions(+), 65 deletions(-) diff --git a/_incl_data/class/Battle.php b/_incl_data/class/Battle.php index e8860f71..20010da1 100644 --- a/_incl_data/class/Battle.php +++ b/_incl_data/class/Battle.php @@ -118,9 +118,10 @@ class Battle private Finish $finish; - public function __construct(private Stat $statnames = new Stat()) + public function __construct(private readonly Stat $statnames = new Stat()) { ignore_user_abort(true); + $this->statnames->getBonus(); $this->finish = new Finish($this); $this->prm = [1 => new BattlePriem(act:1, typeOf:5), @@ -540,19 +541,17 @@ class Battle //Действие эффекта $tr = ''; - $statBonusNames = $this->statnames->getBonusNames(); - $statSysBonusNames = array_keys($statBonusNames); $x = 0; $ed = Conversion::dataStringToArray($eff[$i]['data']); - while ($x < count($statSysBonusNames)) { - $n = $statSysBonusNames[$x]; + while ($x < count($this->statnames->sysBonusNames)) { + $n = $this->statnames->sysBonusNames[$x]; if (isset($ed['add_' . $n]) && $n != 'pog') { $z = ''; if ($ed['add_' . $n] > 0) { $z = '+'; } - $tr .= '
' . $statBonusNames[$n] . ': ' . $z . $ed['add_' . $n]; + $tr .= '
' . $this->statnames->bonusNames[$n] . ': ' . $z . $ed['add_' . $n]; } $x++; } @@ -5266,10 +5265,8 @@ JS; $animalBonusData = Conversion::dataStringToArray($animalBonusDataString); $bonusString = ''; - $statBonusNames = $this->statnames->getBonusNames(); - $statBonusSysNames = array_keys($statBonusNames); - foreach ($statBonusSysNames as $itemBonusSysName) { + foreach ($this->statnames->sysBonusNames as $itemBonusSysName) { if ( !isset($animalBonusData['add_' . $itemBonusSysName]) || $animalBonusData['add_' . $itemBonusSysName] <= 0 @@ -5282,7 +5279,7 @@ JS; 'mib2' => 'Броня корпуса: +' . $animalBonusData['add_' . $itemBonusSysName] . ', ', 'mib3' => 'Броня пояса: +' . $animalBonusData['add_' . $itemBonusSysName] . ', ', 'mib4' => 'Броня ног: +' . $animalBonusData['add_' . $itemBonusSysName] . ', ', - default => $statBonusNames[$itemBonusSysName] . ': +' . $animalBonusData['add_' . $itemBonusSysName] . ', ', + default => $this->statnames->bonusNames[$itemBonusSysName] . ': +' . $animalBonusData['add_' . $itemBonusSysName] . ', ', }; } diff --git a/_incl_data/class/BotPriemLogic.php b/_incl_data/class/BotPriemLogic.php index 1add1841..c78d80f2 100644 --- a/_incl_data/class/BotPriemLogic.php +++ b/_incl_data/class/BotPriemLogic.php @@ -108,6 +108,7 @@ class BotPriemLogic global $u; $notr = 0; $statnames = new Stat(); + $statnames->getRequirement(); $tr = Conversion::dataStringToArray($pl['tr']); $d2 = Conversion::dataStringToArray($pl['date2']); @@ -130,13 +131,10 @@ class BotPriemLogic } } - $statRequirementNames = $statnames->getRequirementNames(); - $statSysRequirementNames = array_keys($statRequirementNames); - $x = 0; - while ($x < count($statSysRequirementNames)) { - $n = $statSysRequirementNames[$x]; + while ($x < count($statnames->sysRequirementNames)) { + $n = $statnames->sysRequirementNames[$x]; if (isset($tr['tr_' . $n])) { if ($n == 'lvl') { if ($tr['tr_' . $n] > BotLogic::$bot['level']) { diff --git a/_incl_data/class/Item/Data/Bonuses.php b/_incl_data/class/Item/Data/Bonuses.php index 63ccfc35..ca6486dd 100644 --- a/_incl_data/class/Item/Data/Bonuses.php +++ b/_incl_data/class/Item/Data/Bonuses.php @@ -12,13 +12,14 @@ class Bonuses public function __construct(array $data) { $this->stat = new Stat(); + $this->stat->getBonus(); foreach ($data as $bonusName => $value) { - if (!$this->stat->getBonusNames()[$bonusName]) { + if (!$this->stat->bonusNames[$bonusName]) { $this->result[$bonusName] = $value; //fixme на период отладки для отлова забытых //continue; } - $this->result[$this->stat->getBonusNames()[$bonusName]] = $value; + $this->result[$this->stat->bonusNames[$bonusName]] = $value; } $this->armor($data); } @@ -58,10 +59,10 @@ class Bonuses public function addZonb(): void { - if (isset($this->result[$this->stat->getBonusNames()['zonb']])) { - $this->result[$this->stat->getBonusNames()['zonb']]++; + if (isset($this->result[$this->stat->bonusNames['zonb']])) { + $this->result[$this->stat->bonusNames['zonb']]++; } else { - $this->result[$this->stat->getBonusNames()['zonb']] = 1; + $this->result[$this->stat->bonusNames['zonb']] = 1; } } } diff --git a/_incl_data/class/Item/Data/Requirements.php b/_incl_data/class/Item/Data/Requirements.php index fddde405..6851f55a 100644 --- a/_incl_data/class/Item/Data/Requirements.php +++ b/_incl_data/class/Item/Data/Requirements.php @@ -20,13 +20,13 @@ class Requirements private int $alignValue = 0; private array $result = []; - private Stat $stat; public function __construct(array $data) { - $this->stat = new Stat(); + $statnames = new Stat(); + $statnames->getRequirement(); foreach ($data as $requirementName => $value) { - if (!$this->stat->getRequirementNames()[$requirementName]) { + if (!$statnames->requirementNames[$requirementName]) { $this->result[$requirementName] = $value; //fixme на период отладки для отлова забытых //continue; } @@ -49,7 +49,7 @@ class Requirements //$this->result[$this->stat->getRequirementNames()[$requirementName]] = $value; $this->result[$requirementName] = [ - 'name' => $this->stat->getRequirementNames()[$requirementName], + 'name' => $statnames->requirementNames[$requirementName], 'value' => $value, ]; } diff --git a/_incl_data/class/Model/Constant/Stat.php b/_incl_data/class/Model/Constant/Stat.php index d2827aeb..3232da3b 100644 --- a/_incl_data/class/Model/Constant/Stat.php +++ b/_incl_data/class/Model/Constant/Stat.php @@ -17,13 +17,6 @@ class Stat extends Constant parent::__construct(); } - public function getBonusNames(): array - { - $filtered = $this->filterByCell(StatFilterCellName::Bonus); - $this->sysBonusNames = array_keys($filtered); - return $filtered; - } - private function filterByCell(StatFilterCellName $cellName): array { $result = []; @@ -36,13 +29,6 @@ class Stat extends Constant return $result; } - public function getRequirementNames(): array - { - $filtered = $this->filterByCell(StatFilterCellName::Requirement); - $this->sysRequirementNames = array_keys($filtered); - return $filtered; - } - /** * Создаёт и заполняет массивы bonusNames и sysBonusNames. * @return void diff --git a/_incl_data/class/User/InfoBox.php b/_incl_data/class/User/InfoBox.php index 758f9be6..b4b2b9cc 100644 --- a/_incl_data/class/User/InfoBox.php +++ b/_incl_data/class/User/InfoBox.php @@ -18,6 +18,7 @@ class InfoBox public function __construct(private readonly User $user, private readonly Stat $statname = new Stat()) { $this->info = $user->info; + $this->statname->getBonus(); } public function getInfoPers($uid, $i1, $sn = 0, $ivv = 0): array @@ -384,17 +385,16 @@ class InfoBox //Действие эффекта $tr = ''; - $t = array_keys($this->statname->getBonusNames()); $x = 0; $ed = Conversion::dataStringToArray($e['data']); - while ($x < count($t)) { - $n = $t[$x]; + while ($x < count($this->statname->sysBonusNames)) { + $n = $this->statname->sysBonusNames[$x]; if (isset($ed['add_' . $n])) { $z = ''; if ($ed['add_' . $n] > 0) { $z = '+'; } - $tr .= '
' . $this->statname->getBonusNames()[$n] . ': ' . $z . $ed['add_' . $n]; + $tr .= '
' . $this->statname->bonusNames[$n] . ': ' . $z . $ed['add_' . $n]; } $x++; } @@ -708,16 +708,15 @@ class InfoBox $lvar .= '
Урон: ' . $po['sv_yron_min'] . '-' . $po['sv_yron_max']; } - $t = array_keys($this->statname->getBonusNames()); $x = 0; - while ($x < count($t)) { - $n = $t[$x]; + while ($x < count($this->statname->sysBonusNames)) { + $n = $this->statname->sysBonusNames[$x]; if (isset($po['add_' . $n])) { $z = '+'; if ($po['add_' . $n] < 0) { $z = ''; } - $lvar .= '
' . $this->statname->getBonusNames()[$n] . ': ' . $z . $po['add_' . $n]; + $lvar .= '
' . $this->statname->bonusNames[$n] . ': ' . $z . $po['add_' . $n]; } $x++; } diff --git a/_incl_data/class/User/Item.php b/_incl_data/class/User/Item.php index 55c6fed2..5421d90f 100644 --- a/_incl_data/class/User/Item.php +++ b/_incl_data/class/User/Item.php @@ -31,8 +31,9 @@ class Item ] = $item; $this->durability = $iMax - $iNow; $stat = new Stat(); + $stat->getBonus(); $dataArr = Conversion::dataStringToArray(strtolower($this->data)); - foreach (array_keys($stat->getBonusNames()) as $bonusName) { + foreach ($stat->sysBonusNames as $bonusName) { if (!isset($dataArr["add_$bonusName"])) { continue; } diff --git a/_incl_data/class/User/Stats.php b/_incl_data/class/User/Stats.php index 7f2c8680..6019a67e 100644 --- a/_incl_data/class/User/Stats.php +++ b/_incl_data/class/User/Stats.php @@ -14,16 +14,17 @@ class Stats private User $u; /** Список разрешённых для бонусов системных названий статов - * @var array + * @var Stat */ - private array $sysNames; + private Stat $sysNames; private array $statsKeys = []; public function __construct(User $user) { $this->u = $user; - $this->sysNames = (new Stat())->getBonusNames(); + $this->sysNames = new Stat(); + $this->sysNames->getBonus(); } @@ -137,7 +138,8 @@ class Stats } // Как? Нахуя 4 переменные разные для одного и того же? - $st = array_fill_keys(array_keys($this->sysNames), 0); + + $st = array_fill_keys($this->sysNames->sysBonusNames, 0); $sti = $st; $s_v = $st; $s_vi = $st; @@ -310,7 +312,7 @@ class Stats } $sti = Conversion::dataStringToArray($com); - foreach ($this->sysNames as $stat) { + foreach ($this->sysNames->sysBonusNames as $stat) { if (!isset($sti[$stat])) { continue; } @@ -596,7 +598,7 @@ class Stats //Добавляем статы от данного предмета if (!isset($data['restart_stats'])) { - foreach ($this->sysNames as $stat) { + foreach ($this->sysNames->sysBonusNames as $stat) { if (!isset($data['add_' . $stat])) { continue; } @@ -605,13 +607,6 @@ class Stats } else { $reitm[] = $data; } - - foreach ($this->sysNames as $stat) { - if (!isset($data['sv_' . $stat])) { - continue; - } - $s_v[$stat] += (int)$data['sv_' . $stat]; - } } //Сохраненные хар-ки и умения @@ -662,7 +657,7 @@ class Stats foreach ($efs as $data) { $sts = Conversion::dataStringToArray($data); foreach ($sts as $paramName => $value) { - if (!in_array($paramName, $this->sysNames, true)) { // есть ли параметр в разрешенных? + if (!in_array($paramName, $this->sysNames->sysBonusNames, true)) { // есть ли параметр в разрешенных? continue; } $st[$paramName] += $value; @@ -672,7 +667,7 @@ class Stats private function addValuesToAllArrays(array $sts, array &$st, array &$sti, array &$s_v, array &$s_vi): void { - foreach ($this->sysNames as $stat) { + foreach ($this->sysNames->sysBonusNames as $stat) { if (!isset($sts['add_' . $stat])) { continue; } diff --git a/inf.php b/inf.php index fd899981..55fe4950 100644 --- a/inf.php +++ b/inf.php @@ -27,7 +27,8 @@ $c['inf'] = true; include_once('_incl_data/class/__db_connect.php'); $u = User::start(); -$bonusstatnames = (new Stat())->getBonusNames(); +$statnames = new Stat(); +$statnames->getBonus(); const LOWERCASE = 3; const UPPERCASE = 1; @@ -349,7 +350,7 @@ if (isset($uer)) { sysBonusNames as $stat) { if (empty($st[$stat]) || empty($u->is[$stat])) { continue; } @@ -357,7 +358,7 @@ if (isset($uer)) { $st[$stat] = '+' . $st[$stat]; } - echo '• ' . $bonusstatnames[$stat] . ': ' . $st[$stat] . '
'; + echo '• ' . $statnames->bonusNames[$stat] . ': ' . $st[$stat] . '
'; } ?>