From 8212811970e9b1c686797f297e929ed838c2b573 Mon Sep 17 00:00:00 2001 From: Sarjuuk Date: Sat, 1 Nov 2025 20:12:41 +0100 Subject: [PATCH] Misc/Cleanup * create function for num range .. creation --- endpoints/itemset/itemset.php | 10 +--------- endpoints/object/object.php | 15 +++++---------- endpoints/spell/spell.php | 17 ++++++++--------- includes/components/SmartAI/SmartAI.class.php | 10 +++------- includes/utilities.php | 12 ++++++++++++ 5 files changed, 29 insertions(+), 35 deletions(-) diff --git a/endpoints/itemset/itemset.php b/endpoints/itemset/itemset.php index 317637b3..a975d734 100644 --- a/endpoints/itemset/itemset.php +++ b/endpoints/itemset/itemset.php @@ -95,15 +95,7 @@ class ItemsetBaseResponse extends TemplateResponse implements ICache // itemLevel if ($min = $this->subject->getField('minLevel')) - { - $foo = Lang::game('level').Lang::main('colon').$min; - $max = $this->subject->getField('maxLevel'); - - if ($min < $max) - $foo .= ' - '.$max; - - $infobox[] = $foo; - } + $infobox[] = Lang::game('level').Lang::main('colon').Util::createNumRange($min, $this->subject->getField('maxLevel'), ' - '); // class if ($cl = Lang::getClassString($this->subject->getField('classMask'), $jsg, Lang::FMT_MARKUP)) diff --git a/endpoints/object/object.php b/endpoints/object/object.php index de643a4d..6b5150ef 100644 --- a/endpoints/object/object.php +++ b/endpoints/object/object.php @@ -148,9 +148,7 @@ class ObjectBaseResponse extends TemplateResponse implements ICache if ($this->subject->getField('lootStack')) { [$min, $max, $restock] = $this->subject->getField('lootStack'); - $buff = Lang::spell('spellModOp', 4).Lang::main('colon').$min; - if ($min < $max) - $buff .= Lang::game('valueDelim').$max; + $buff = Lang::spell('spellModOp', 4).Lang::main('colon').Util::createNumRange($min, $max); // since Veins don't have charges anymore, the timer is questionable $infobox[] = $restock > 1 ? '[tooltip name=restock]'.Lang::gameObject('restock', [Util::formatTime($restock * 1000)]).'[/tooltip][span class=tip tooltip=restock]'.$buff.'[/span]' : $buff; @@ -163,10 +161,7 @@ class ObjectBaseResponse extends TemplateResponse implements ICache $this->extendGlobalIds(Type::ZONE, $zone); $m = Lang::game('meetingStone').'[zone='.$zone.']'; - - $l = $minLevel; - if ($minLevel > 1 && $maxLevel > $minLevel) - $l .= Lang::game('valueDelim').min($maxLevel, MAX_LEVEL); + $l = Util::createNumRange($minLevel, min($maxLevel, MAX_LEVEL)); $infobox[] = $l ? '[tooltip name=meetingstone]'.Lang::game('reqLevel', [$l]).'[/tooltip][span class=tip tooltip=meetingstone]'.$m.'[/span]' : $m; } @@ -181,11 +176,11 @@ class ObjectBaseResponse extends TemplateResponse implements ICache if ($minTime > 1 || $minPlayer || $radius) $buff .= Lang::main('colon').'[ul]'; - if ($minTime > 1) - $buff .= '[li]'.Lang::game('duration').Lang::main('colon').($maxTime > $minTime ? Util::FormatTime($maxTime * 1000, true).' - ' : '').Util::FormatTime($minTime * 1000, true).'[/li]'; + if ($minTime > 1) // sign shenannigans reverse the display order + $buff .= '[li]'.Lang::game('duration').Lang::main('colon').Util::createNumRange(-$maxTime, -$minTime, fn: fn($x) => Util::FormatTime(-$x * 1000, true)).'[/li]'; if ($minPlayer) - $buff .= '[li]'.Lang::main('players').Lang::main('colon').$minPlayer.($maxPlayer > $minPlayer ? ' - '.$maxPlayer : '').'[/li]'; + $buff .= '[li]'.Lang::main('players').Lang::main('colon').Util::createNumRange($minPlayer, $maxPlayer).'[/li]'; if ($radius) $buff .= '[li]'.Lang::spell('range', [$radius]).'[/li]'; diff --git a/endpoints/spell/spell.php b/endpoints/spell/spell.php index d3945836..28bafde2 100644 --- a/endpoints/spell/spell.php +++ b/endpoints/spell/spell.php @@ -1606,7 +1606,7 @@ class SpellBaseResponse extends TemplateResponse implements ICache */ $_nameEffect = $_nameAura = $_nameMV = $_nameMVB = $_markup = ''; - $_icon = $_perfItem = $_footer = []; + $_icon = $_perfItem = $_footer = []; $_footer['value'] = [0, 0]; $valueFmt = '%s'; @@ -1623,7 +1623,7 @@ class SpellBaseResponse extends TemplateResponse implements ICache Type::ITEM, $itemId, $itemEntry ? $this->subject->relItems->getField('name', true) : Util::ucFirst(Lang::game('item')).' #'.$itemId, - ($effBP + 1) . ($effDS > 1 ? '-' . ($effBP + $effDS) : ''), + $this->createNumRange($effBP, $effDS), quality: $itemEntry ? $this->subject->relItems->getField('quality') : '', link: !empty($itemEntry) ); @@ -1654,12 +1654,6 @@ class SpellBaseResponse extends TemplateResponse implements ICache $cndSpell = new SpellList(array(['id', $extraItem['requiredSpecialization']])); if (!$cndSpell->error) { - $num = '+'.($effBP + 1); - if ($extraItem['additionalMaxNum'] > 1) - $num .= '-'.($extraItem['additionalMaxNum'] * ($effBP + $effDS)); - else if ($effDS > 1) - $num .= '-'.($effBP + $effDS); - $_perfItem = array( 'spellId' => $cndSpell->id, 'spellName' => $cndSpell->getField('name', true), @@ -1669,7 +1663,7 @@ class SpellBaseResponse extends TemplateResponse implements ICache Type::ITEM, $this->subject->relItems->id, $this->subject->relItems->getField('name', true), - num: $num, + num: '+'.$this->createNumRange($effBP, $effDS, $extraItem['additionalMaxNum']), quality: $this->subject->relItems->getField('quality') ) ); @@ -2285,6 +2279,11 @@ class SpellBaseResponse extends TemplateResponse implements ICache $this->attributes = $list; } + private function createNumRange(int $bp, int $ds, int $mult = 1) : string + { + return Util::createNumRange($bp + 1, ($bp + $ds) * $mult, '-'); + } + private function generatePath() { $cat = $this->subject->getField('typeCat'); diff --git a/includes/components/SmartAI/SmartAI.class.php b/includes/components/SmartAI/SmartAI.class.php index 1518e426..a9307160 100644 --- a/includes/components/SmartAI/SmartAI.class.php +++ b/includes/components/SmartAI/SmartAI.class.php @@ -21,14 +21,10 @@ trait SmartHelper private function numRange(int $min, int $max, bool $isTime) : string { - if (!$min && !$max) - return ''; + if ($isTime) + return Util::createNumRange($min, $max, ' – ', fn($x) => Util::formatTime($x, true)); - $str = $isTime ? Util::formatTime($min, true) : $min; - if ($max > $min) - $str .= ' – '.($isTime ? Util::formatTime($max, true) : $max); - - return $str; + return Util::createNumRange($min, $max, ' – '); } private function formatTime(int $time, int $_, bool $isMilliSec) : string diff --git a/includes/utilities.php b/includes/utilities.php index 2f3db816..5c1193c4 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -554,6 +554,18 @@ abstract class Util } } + public static function createNumRange(int $min, int $max, string $delim = '', ?callable $fn = null) : string + { + if (!$min && !$max) + return ''; + + $fn ??= fn($x) => $x; + $_min = $fn($min); + $_max = $fn($max); + + return $max > $min ? $_min . ($delim ?: Lang::main('valueDelim')) . $_max : $_min; + } + public static function validateLogin(?string $val) : string { if ($_ = self::validateEmail($val))