From c2678aa91fe5a9d7335c3c26fff7b309e382d230 Mon Sep 17 00:00:00 2001 From: slawkens Date: Fri, 20 Oct 2017 15:04:11 +0200 Subject: [PATCH] * PLEASE RELOAD YOUR ITEMS AND MONSTERS AFTER THIS UPDATE * save monster loot in database in json format instead loading it every time from xml file * store monster voices and immunities in json format * removed useless monsters.gfx_name field from database * convert item name to item id in loot in monsters.xml loader * after changing template you will be redirected to latest viewed page * display gallery add image form only on main gallery page * fixed displaying monster loot when item.name in loot is used instead of item.id * (intern) added new function getItemNameById($id) * (intern) renamed database field monsters.hide_creature to hidden --- common.php | 2 +- index.php | 2 +- install/includes/schema.sql | 9 +- system/functions.php | 19 +++-- system/libs/creatures.php | 69 ++++++++------- system/libs/pot/OTS_Monster.php | 25 ++++-- system/migrations/14.php | 18 ++++ system/pages/creatures.php | 117 ++++++++++++-------------- system/pages/gallery.php | 18 ++-- system/template.php | 1 + system/templates/characters.html.twig | 6 +- 11 files changed, 157 insertions(+), 129 deletions(-) create mode 100644 system/migrations/14.php diff --git a/common.php b/common.php index 96748eee..88a5046b 100644 --- a/common.php +++ b/common.php @@ -28,7 +28,7 @@ session_start(); define('MYAAC', true); define('MYAAC_VERSION', '0.6.1'); -define('DATABASE_VERSION', 13); +define('DATABASE_VERSION', 14); define('TABLE_PREFIX', 'myaac_'); define('START_TIME', microtime(true)); define('MYAAC_OS', (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'WINDOWS' : (strtoupper(PHP_OS) == 'DARWIN' ? 'MAC' : 'LINUX')); diff --git a/index.php b/index.php index 1dc1b5a4..ed394ca5 100644 --- a/index.php +++ b/index.php @@ -155,9 +155,9 @@ define('PAGE', $page); $template_place_holders = array(); require_once(SYSTEM . 'init.php'); +require_once(SYSTEM . 'template.php'); require_once(SYSTEM . 'login.php'); require_once(SYSTEM . 'status.php'); -require_once(SYSTEM . 'template.php'); $twig->addGlobal('config', $config); $twig->addGlobal('status', $status); diff --git a/install/includes/schema.sql b/install/includes/schema.sql index 46d56dd9..34ef3d88 100644 --- a/install/includes/schema.sql +++ b/install/includes/schema.sql @@ -140,20 +140,19 @@ CREATE TABLE `myaac_items` CREATE TABLE `myaac_monsters` ( `id` int(11) NOT NULL AUTO_INCREMENT, - `hide_creature` tinyint(1) NOT NULL default '0', + `hidden` tinyint(1) NOT NULL default 0, `name` varchar(255) NOT NULL, - `mana` int(11) NOT NULL, + `mana` int(11) NOT NULL DEFAULT 0, `exp` int(11) NOT NULL, `health` int(11) NOT NULL, - `speed_lvl` int(11) NOT NULL default '1', + `speed_lvl` int(11) NOT NULL default 1, `use_haste` tinyint(1) NOT NULL, `voices` text NOT NULL, `immunities` varchar(255) NOT NULL, `summonable` tinyint(1) NOT NULL, `convinceable` tinyint(1) NOT NULL, `race` varchar(255) NOT NULL, - `gfx_name` varchar(255) NOT NULL, - `file_path` varchar(255) NOT NULL, + `loot` varchar(500) NOT NULL, PRIMARY KEY (`id`) ) ENGINE = MyISAM; diff --git a/system/functions.php b/system/functions.php index 5660289e..6186c498 100644 --- a/system/functions.php +++ b/system/functions.php @@ -117,17 +117,26 @@ function getGuildLink($name, $generate = true) return generateLink($url, $name); } -function getItemImage($id, $count = 1) -{ +function getItemNameById($id) { global $db; - - $tooltip = ''; $query = $db->query('SELECT `name` FROM `' . TABLE_PREFIX . 'items` WHERE `id` = ' . $db->quote($id) . ' LIMIT 1;'); if($query->rowCount() == 1) { $item = $query->fetch(); - $tooltip = ' class="tooltip" title="' . $item['name'] . '"'; + return $item['name']; } + return ''; +} + +function getItemImage($id, $count = 1) +{ + $tooltip = ''; + + $name = getItemNameById($id); + if(!empty($name)) { + $tooltip = ' class="tooltip" title="' . $name . '"'; + } + $file_name = $id; if($count > 1) $file_name .= '-' . $count; diff --git a/system/libs/creatures.php b/system/libs/creatures.php index dd3c0605..c25befe5 100644 --- a/system/libs/creatures.php +++ b/system/libs/creatures.php @@ -33,6 +33,12 @@ class Creatures { return false; } + $items = array(); + $items_db = $db->query('SELECT `id`, `name` FROM `' . TABLE_PREFIX . 'items`;'); + foreach($items_db->fetchAll() as $item) { + $items[$item['name']] = $item['id']; + } + //$names_added must be an array $names_added[] = ''; //add monsters @@ -44,10 +50,10 @@ class Creatures { } continue; } + //load monster mana needed to summon/convince $mana = $monster->getManaCost(); - //load monster experience - $exp = $monster->getExperience(); + //load monster name $name = $monster->getName(); //load monster health @@ -67,51 +73,44 @@ class Creatures { $use_haste = 1; } } - //load monster flags - $flags = $monster->getFlags(); - //create string with immunities - $immunities = $monster->getImmunities(); - $imu_nr = 0; - $imu_count = count($immunities); - $immunities_string = ''; - foreach($immunities as $immunitie) { - $immunities_string .= $immunitie; - $imu_nr++; - if($imu_count != $imu_nr) { - $immunities_string .= ", "; - } - } - //create string with voices - $voices = $monster->getVoices(); - $voice_nr = 0; - $voice_count = count($voices); - $voices_string = ''; - foreach($voices as $voice) { - $voices_string .= '"'.$voice.'"'; - $voice_nr++; - if($voice_count != $voice_nr) { - $voices_string .= ", "; - } - } //load race $race = $monster->getRace(); - //create monster gfx name - //$gfx_name = str_replace(" ", "", trim(mb_strtolower($name))).".gif"; - $gfx_name = trim(mb_strtolower($name)).".gif"; - //don't add 2 monsters with same name, like Butterfly - + + //load monster flags + $flags = $monster->getFlags(); if(!isset($flags['summonable'])) $flags['summonable'] = '0'; if(!isset($flags['convinceable'])) $flags['convinceable'] = '0'; + $loot = $monster->getLoot(); + foreach($loot as &$item) { + if(!Validator::number($item['id'])) { + if(isset($items[$item['id']])) { + $item['id'] = $items[$item['id']]; + } + } + } if(!in_array($name, $names_added)) { try { - $db->query("INSERT INTO `myaac_monsters` (`hide_creature`, `name`, `mana`, `exp`, `health`, `speed_lvl`, `use_haste`, `voices`, `immunities`, `summonable`, `convinceable`, `race`, `gfx_name`, `file_path`) VALUES (0, " . $db->quote($name) . ", " . $db->quote(empty($mana) ? 0 : $mana) . ", " . $db->quote($exp) . ", " . $db->quote($health) . ", " . $db->quote($speed_lvl) . ", " . $db->quote($use_haste) . ", " . $db->quote($voices_string) . ", " . $db->quote($immunities_string) . ", " . $db->quote($flags['summonable'] > 0 ? 1 : 0) . ", " . $db->quote($flags['convinceable'] > 0 ? 1 : 0) . ", ".$db->quote($race).", ".$db->quote($gfx_name).", " . $db->quote(self::$monstersList->currentFile()) . ")"); + $db->insert(TABLE_PREFIX . 'monsters', array( + 'name' => $name, + 'mana' => empty($mana) ? 0 : $mana, + 'exp' => $monster->getExperience(), + 'health' => $health, + 'speed_lvl' => $speed_lvl, + 'use_haste' => $use_haste, + 'voices' => json_encode($monster->getVoices()), + 'immunities' => json_encode($monster->getImmunities()), + 'summonable' => $flags['summonable'] > 0 ? 1 : 0, + 'convinceable' => $flags['convinceable'] > 0 ? 1 : 0, + 'race' => $race, + 'loot' => json_encode($loot) + )); if($show) { - success("Added: ".$name."
"); + success('Added: ' . $name . '
'); } } catch(PDOException $error) { diff --git a/system/libs/pot/OTS_Monster.php b/system/libs/pot/OTS_Monster.php index a8079890..d498714f 100644 --- a/system/libs/pot/OTS_Monster.php +++ b/system/libs/pot/OTS_Monster.php @@ -194,7 +194,6 @@ class OTS_Monster extends DOMDocument /** * @return array List of item IDs. - * @deprecated 0.1.0 Use getItems(). */ public function getLoot() { @@ -208,13 +207,25 @@ class OTS_Monster extends DOMDocument // adds all items foreach( $element->getElementsByTagName('item') as $item) { - $id = $item->getAttribute('id'); - - // avoid redundancy - if( !in_array($id, $loot) ) - { - $loot[] = $id; + $chance = $item->getAttribute('chance'); + if(empty($chance)) { + $chance = $item->getAttribute('chance1'); + if(empty($chance)) { + $chance = 100000; + } } + + $count = $item->getAttribute('countmax'); + if(empty($count)) { + $count = 1; + } + + $id = $item->getAttribute('id'); + if(empty($id)) { + $id = $item->getAttribute('name'); + } + + $loot[] = array('id' => $id, 'count' => $count, 'chance' => $chance); } } diff --git a/system/migrations/14.php b/system/migrations/14.php new file mode 100644 index 00000000..cbbf5505 --- /dev/null +++ b/system/migrations/14.php @@ -0,0 +1,18 @@ +query("ALTER TABLE `" . TABLE_PREFIX . "monsters` CHANGE `file_path` `loot` VARCHAR(5000);"); +} + +// update loot to empty string +$db->query("UPDATE `" . TABLE_PREFIX . "monsters` SET `loot` = '';"); + +// drop monsters.gfx_name field +$db->query("ALTER TABLE `" . TABLE_PREFIX . "monsters` DROP COLUMN `gfx_name`;"); + +// rename hide_creature to hidden +if(fieldExist('hide_creature', TABLE_PREFIX . 'monsters')) { + $db->query("ALTER TABLE `" . TABLE_PREFIX . "monsters` CHANGE `hide_creature` `hidden` TINYINT(1) NOT NULL DEFAULT 0;"); +} +?> \ No newline at end of file diff --git a/system/pages/creatures.php b/system/pages/creatures.php index 61131a92..fd053e14 100644 --- a/system/pages/creatures.php +++ b/system/pages/creatures.php @@ -12,62 +12,24 @@ defined('MYAAC') or die('Direct access not allowed!'); $title = "Creatures"; -$rarity = array( - 'Not Rare' => 7, - 'Semi Rare' => 2, - 'Rare' => 0.5, - 'Very Rare' => 0 -); - -function addLoot($loot, $level=1) -{ - foreach($loot as $test) { - $chance = $test['chance']; - if(!$chance) - $chance = $test['chance1']; - - printLoot($level, $test['id'], $test['countmax'], $chance); - foreach($test as $k => $v) - addLoot($v->item, $level + 1); - } -} - - $i = 0; -function printLoot($level, $itemid, $count, $chance) -{ - global $itemList, $rarity, $i; - - $chance /= 1000; - if(isset($_GET['lootrate'])) { - global $lootRate; - $chance *= $lootRate; - } - - foreach($rarity as $lootRarity => $percent) { - if($chance >= $percent) { - if(isset($itemid)) - echo str_repeat("... ", $level) . '' . ($count ? $count : 1) . ' ' . $itemList[(int)$itemid] . ' ' . $itemid . ' ' . $lootRarity . ' (' . $chance . '%)
'; - - if($i % 6 == 0) - { - if($i != 0) - echo ''; - echo ''; - } - echo getItemImage($itemid); - $i++; - break; - } - } -} +?> + + + +hasErrors()) + if(Creatures::loadFromXML(true)) { + if (Creatures::getMonstersList()->hasErrors()) error('There were some problems loading your monsters.xml file. Please check system/logs/error.log for more info.'); + } else { error(Creatures::getLastError()); } @@ -114,7 +76,7 @@ if(empty($_REQUEST['creature'])) $whereandorder = ' ORDER BY name'; } //send query to database - $monsters = $db->query('SELECT * FROM '.$db->tableName(TABLE_PREFIX . 'monsters').' WHERE hide_creature != 1'.$whereandorder); + $monsters = $db->query('SELECT * FROM `' . TABLE_PREFIX . 'monsters` WHERE `hidden` != 1'.$whereandorder); echo ''; if($order == 'name' && !isset($_REQUEST['desc'])) { echo ''; @@ -173,7 +135,7 @@ if(empty($_REQUEST['creature'])) $monster_name = stripslashes(trim(ucwords($_REQUEST['creature']))); -$monster = $db->query('SELECT * FROM '.$db->tableName(TABLE_PREFIX . 'monsters').' WHERE '.$db->fieldName('hide_creature').' != 1 AND '.$db->fieldName('name').' = '.$db->quote($monster_name).';')->fetch(); +$monster = $db->query('SELECT * FROM `' . TABLE_PREFIX . 'monsters` WHERE `hidden` != 1 AND `name` = '.$db->quote($monster_name).';')->fetch(); if(isset($monster['name'])) { $title = $monster['name'] . " - Creatures"; @@ -209,6 +171,7 @@ if(isset($monster['name'])) echo '
Name DESC
'; + $monster['gfx_name'] = trim(mb_strtolower($monster['name'])).".gif"; if(!file_exists('images/monsters/'.$monster['gfx_name'])) { $gfx_name = str_replace(" ", "", $monster['gfx_name']); if(file_exists('images/monsters/' . $gfx_name)) @@ -222,33 +185,59 @@ if(isset($monster['name'])) echo '
'; - if(!empty($monster['immunities'])) + $immunities = json_decode($monster['immunities'], true); + if(count($immunities) > 0) { $number_of_rows++; - echo ''; + echo ''; } - if(!empty($monster['voices'])) + + $voices = json_decode($monster['voices'], true); + if(count($voices) > 0) { + foreach($voices as &$voice) { + $voice = '"' . $voice . '"'; + } + $number_of_rows++; - echo ''; + echo ''; } echo '
Immunities: '.$monster['immunities'].'
Immunities: '.implode(', ', $immunities).'
Voices: '.$monster['voices'].'
Voices: '.implode(', ', $voices).'
'; - - echo ''; - $loot = simplexml_load_file($config['data_path'] . 'monster/' . $monster['file_path']); + + $loot = json_decode($monster['loot'], true); if($loot) - { - if($item = $loot->loot->item) - addLoot($item); + { + echo '
'; + function sort_by_chance($a, $b) + { + if($a['chance'] == $b['chance']) { + return 0; + } + return ($a['chance'] > $b['chance']) ? -1 : 1; + } + + usort($loot, 'sort_by_chance'); + + $i = 0; + foreach($loot as $item) { + $name = getItemNameById($item['id']); + $tooltip = $name . '
Chance: ' . round($item['chance'] / 1000, 2) . '%
Max count: ' . $item['count']; + + echo ' ' .$name . ''; + $i++; + } + + echo '
'; } - echo ''; + echo ''; echo ''; } else { - echo 'Monster with name '.$monster_name.' doesn\'t exist.'; + echo "Monster with name " . $monster_name . " doesn't exist."; } + //back button echo $twig->render('creatures.back_button.html.twig'); ?> diff --git a/system/pages/gallery.php b/system/pages/gallery.php index 6f4acb6f..b94ddc19 100644 --- a/system/pages/gallery.php +++ b/system/pages/gallery.php @@ -56,14 +56,16 @@ if($canEdit) { echo $twig->render('error_box.html.twig', array('errors' => $errors)); } - echo $twig->render('gallery.form.html.twig', array( - 'link' => getLink('gallery/' . ($action == 'edit' ? 'edit' : 'add')), - 'action' => $action, - 'id' => isset($id) ? $id : null, - 'comment' => isset($comment) ? $comment : null, - 'image' => isset($image) ? $image : null, - 'author' => isset($author) ? $author : null - )); + if(!isset($_GET['image'])) { + echo $twig->render('gallery.form.html.twig', array( + 'link' => getLink('gallery/' . ($action == 'edit' ? 'edit' : 'add')), + 'action' => $action, + 'id' => isset($id) ? $id : null, + 'comment' => isset($comment) ? $comment : null, + 'image' => isset($image) ? $image : null, + 'author' => isset($author) ? $author : null + )); + } } else echo 'You cannot edit/add gallery items as it seems your PHP installation doesnt have GD support enabled. Visit PHP Manual for more info.'; diff --git a/system/template.php b/system/template.php index 547e6d67..3b46a99e 100644 --- a/system/template.php +++ b/system/template.php @@ -20,6 +20,7 @@ if($config['template_allow_change']) if(!preg_match("/[^A-z0-9_\-]/", $template_name)) { // validate template //setcookie('template', $template_name, 0, BASE_DIR . '/', $_SERVER["SERVER_NAME"]); setSession('template', $template_name); + header('Location:' . getSession('last_uri')); } else $template_name = $config['template']; diff --git a/system/templates/characters.html.twig b/system/templates/characters.html.twig index 4db64d73..bf2c06a5 100644 --- a/system/templates/characters.html.twig +++ b/system/templates/characters.html.twig @@ -1,9 +1,9 @@ {% set rows = 0 %}