From 73a5829974ceca3f02d7925d5cfbd5fa50b1bbd2 Mon Sep 17 00:00:00 2001 From: slawkens Date: Mon, 5 May 2025 21:21:54 +0200 Subject: [PATCH] Better monster images (no image not found anymore) + use cache --- system/compat/base.php | 4 --- system/functions.php | 16 --------- system/libs/pot/OTS_Monster.php | 34 +++++++++++++----- system/pages/monsters.php | 62 +++++++++++++++++++++++++++------ 4 files changed, 76 insertions(+), 40 deletions(-) diff --git a/system/compat/base.php b/system/compat/base.php index d29c9f3a..5a46ecf2 100644 --- a/system/compat/base.php +++ b/system/compat/base.php @@ -74,7 +74,3 @@ function fieldExist($field, $table) global $db; return $db->hasColumn($table, $field); } - -function getCreatureImgPath($creature): string { - return getMonsterImgPath($creature); -} diff --git a/system/functions.php b/system/functions.php index e0ed8f61..1c96d14a 100644 --- a/system/functions.php +++ b/system/functions.php @@ -1572,22 +1572,6 @@ function right($str, $length) { return substr($str, -$length); } -function getMonsterImgPath($monster): string -{ - $monster_path = setting('core.monsters_images_url'); - $monster_gfx_name = trim(strtolower($monster)) . setting('core.monsters_images_extension'); - if (!file_exists($monster_path . $monster_gfx_name)) { - $monster_gfx_name = str_replace(" ", "", $monster_gfx_name); - if (file_exists($monster_path . $monster_gfx_name)) { - return $monster_path . $monster_gfx_name; - } else { - return $monster_path . 'nophoto.png'; - } - } else { - return $monster_path . $monster_gfx_name; - } -} - function between($x, $lim1, $lim2) { if ($lim1 < $lim2) { $lower = $lim1; $upper = $lim2; diff --git a/system/libs/pot/OTS_Monster.php b/system/libs/pot/OTS_Monster.php index 759375f5..ea20c0e1 100644 --- a/system/libs/pot/OTS_Monster.php +++ b/system/libs/pot/OTS_Monster.php @@ -284,7 +284,7 @@ class OTS_Monster extends DOMDocument */ public function getLook() { - $look = array(); + $look = []; $element = $this->documentElement->getElementsByTagName('look')->item(0); @@ -292,14 +292,30 @@ class OTS_Monster extends DOMDocument return $look; } - $look['type'] = $element->getAttribute('type'); - $look['typeex'] = $element->getAttribute('typeex'); - $look['head'] = $element->getAttribute('head'); - $look['body'] = $element->getAttribute('body'); - $look['legs'] = $element->getAttribute('legs'); - $look['feet'] = $element->getAttribute('feet'); - $look['addons'] = $element->getAttribute('addons'); - $look['corpse'] = $element->getAttribute('corpse'); + if ($element->hasAttribute('typeex')) { + $look['typeEx'] = (int) $element->getAttribute('typeex'); + } + if ($element->hasAttribute('type')) { + $look['type'] = (int) $element->getAttribute('type'); + } + if ($element->hasAttribute('head')) { + $look['head'] = (int) $element->getAttribute('head'); + } + if ($element->hasAttribute('body')) { + $look['body'] = (int) $element->getAttribute('body'); + } + if ($element->hasAttribute('legs')) { + $look['legs'] = (int) $element->getAttribute('legs'); + } + if ($element->hasAttribute('feet')) { + $look['feet'] = (int) $element->getAttribute('feet'); + } + if ($element->hasAttribute('addons')) { + $look['addons'] = (int) $element->getAttribute('addons'); + } + if ($element->hasAttribute('corpse')) { + $look['corpse'] = (int) $element->getAttribute('corpse'); + } return $look; } diff --git a/system/pages/monsters.php b/system/pages/monsters.php index 1678291e..a7d94188 100644 --- a/system/pages/monsters.php +++ b/system/pages/monsters.php @@ -16,18 +16,22 @@ defined('MYAAC') or die('Direct access not allowed!'); $title = 'Monsters'; if (empty($_REQUEST['name'])) { - // display list of monsters $preview = setting('core.monsters_images_preview'); - $monsters = Monster::where('hide', '!=', 1)->when(!empty($_REQUEST['boss']), function ($query) { - $query->where('rewardboss', 1); - })->get()->toArray(); - if ($preview) { - foreach($monsters as $key => &$monster) - { - $monster['img_link'] = getMonsterImgPath($monster['name']); + // display list of monsters + $monsters = MyAAC\Cache::remember('monsters', 30 * 60, function () use ($preview) { + $monsters = Monster::where('hide', '!=', 1)->when(!empty($_REQUEST['boss']), function ($query) { + $query->where('rewardboss', 1); + })->get()->toArray(); + + if ($preview) { + foreach($monsters as &$monster) { + $monster['img_link'] = getMonsterImage($monster); + } } - } + + return $monsters; + }); $twig->display('monsters.html.twig', array( 'monsters' => $monsters, @@ -45,7 +49,7 @@ if ($monsterModel && isset($monsterModel->name)) { /** @var array $monster */ $monster = $monsterModel->toArray(); - function sort_by_chance($a, $b) + function sort_by_chance($a, $b): int { if ($a['chance'] == $b['chance']) { return 0; @@ -55,7 +59,7 @@ if ($monsterModel && isset($monsterModel->name)) { $title = $monster['name'] . " - Monsters"; - $monster['img_link']= getMonsterImgPath($monster_name); + $monster['img_link']= getMonsterImage($monster); $voices = json_decode($monster['voices'], true); $summons = json_decode($monster['summons'], true); @@ -89,3 +93,39 @@ if ($monsterModel && isset($monsterModel->name)) { // back button $twig->display('monsters.back_button.html.twig'); + +function getMonsterImage($monster): string +{ + $outfit = json_decode($monster['look'], true); + + if (!empty($outfit['typeEx'])) { + return setting('core.item_images_url') . $outfit['typeEx'] . setting('core.item_images_extension'); + } + + if (isset($outfit['type'])) { + $getValue = function ($val) use ($outfit) { + return (!empty($outfit[$val]) + ? '&' . $val . '=' . $outfit[$val] : ''); + }; + + return setting('core.outfit_images_url') . '?id=' . $outfit['type'] . $getValue('addons') . $getValue('head') . $getValue('body') . $getValue('legs') . $getValue('feet'); + } + + return getMonsterImgPath($monster['name']); +} + +function getMonsterImgPath($name): string +{ + $monster_path = setting('core.monsters_images_url'); + $monster_gfx_name = trim(strtolower($name)) . setting('core.monsters_images_extension'); + if (!file_exists($monster_path . $monster_gfx_name)) { + $monster_gfx_name = str_replace(" ", "", $monster_gfx_name); + if (file_exists($monster_path . $monster_gfx_name)) { + return $monster_path . $monster_gfx_name; + } else { + return $monster_path . 'nophoto.png'; + } + } else { + return $monster_path . $monster_gfx_name; + } +}