This commit is contained in:
tobi132 2019-07-17 19:01:44 +02:00
parent f5374e8ce7
commit 49fe7a4b38
2 changed files with 48 additions and 48 deletions

View File

@ -11,33 +11,33 @@
defined('MYAAC') or die('Direct access not allowed!'); defined('MYAAC') or die('Direct access not allowed!');
class Creatures { class Creatures {
private static $monstersList = null; private static $monstersList;
private static $lastError = ''; private static $lastError = '';
public static function loadFromXML($show = false) { public static function loadFromXML($show = false) {
global $config, $db; global $db;
try { $db->query("DELETE FROM `myaac_monsters`;"); } catch(PDOException $error) {} try { $db->exec('DELETE FROM `' . TABLE_PREFIX . 'monsters`;'); } catch(PDOException $error) {}
if($show) { if($show) {
echo '<h2>Reload monsters.</h2>'; echo '<h2>Reload monsters.</h2>';
echo "<h2>All records deleted from table 'myaac_monsters' in database.</h2>"; echo "<h2>All records deleted from table '" . TABLE_PREFIX . "monsters' in database.</h2>";
} }
try { try {
self::$monstersList = new OTS_MonstersList($config['data_path'].'monster/'); self::$monstersList = new OTS_MonstersList(config('data_path') . 'monster/');
} }
catch(Exception $e) { catch(Exception $e) {
self::$lastError = $e->getMessage(); self::$lastError = $e->getMessage();
return false; return false;
} }
$items = array(); $items = array();
$items_db = $db->query('SELECT `id`, `name` FROM `' . TABLE_PREFIX . 'items`;'); Items::load();
foreach($items_db->fetchAll() as $item) { foreach((array)Items::$items as $id => $item) {
$items[$item['name']] = $item['id']; $items[$item['name']] = $id;
} }
//$names_added must be an array //$names_added must be an array
$names_added[] = ''; $names_added[] = '';
//add monsters //add monsters
@ -49,7 +49,7 @@ class Creatures {
} }
continue; continue;
} }
//load monster mana needed to summon/convince //load monster mana needed to summon/convince
$mana = $monster->getManaCost(); $mana = $monster->getManaCost();
@ -72,7 +72,7 @@ class Creatures {
$use_haste = 1; $use_haste = 1;
} }
} }
//load race //load race
$race = $monster->getRace(); $race = $monster->getRace();
@ -82,7 +82,7 @@ class Creatures {
$flags['summonable'] = '0'; $flags['summonable'] = '0';
if(!isset($flags['convinceable'])) if(!isset($flags['convinceable']))
$flags['convinceable'] = '0'; $flags['convinceable'] = '0';
$loot = $monster->getLoot(); $loot = $monster->getLoot();
foreach($loot as &$item) { foreach($loot as &$item) {
if(!Validator::number($item['id'])) { if(!Validator::number($item['id'])) {
@ -107,7 +107,7 @@ class Creatures {
'race' => $race, 'race' => $race,
'loot' => json_encode($loot) 'loot' => json_encode($loot)
)); ));
if($show) { if($show) {
success('Added: ' . $name . '<br/>'); success('Added: ' . $name . '<br/>');
} }
@ -117,18 +117,18 @@ class Creatures {
warning('Error while adding monster (' . $name . '): ' . $error->getMessage()); warning('Error while adding monster (' . $name . '): ' . $error->getMessage());
} }
} }
$names_added[] = $name; $names_added[] = $name;
} }
} }
return true; return true;
} }
public static function getMonstersList() { public static function getMonstersList() {
return self::$monstersList; return self::$monstersList;
} }
public static function getLastError() { public static function getLastError() {
return self::$lastError; return self::$lastError;
} }

View File

@ -13,7 +13,7 @@ defined('MYAAC') or die('Direct access not allowed!');
class Items class Items
{ {
private static $error = ''; private static $error = '';
private static $items; public static $items;
public static function loadFromXML($show = false) public static function loadFromXML($show = false)
{ {
@ -22,10 +22,10 @@ class Items
self::$error = 'Cannot load file ' . $file_path; self::$error = 'Cannot load file ' . $file_path;
return false; return false;
} }
$xml = new DOMDocument; $xml = new DOMDocument;
$xml->load($file_path); $xml->load($file_path);
$items = array(); $items = array();
foreach ($xml->getElementsByTagName('item') as $item) { foreach ($xml->getElementsByTagName('item') as $item) {
if ($item->getAttribute('fromid')) { if ($item->getAttribute('fromid')) {
@ -36,52 +36,52 @@ class Items
} else { } else {
$tmp = self::parseNode($item->getAttribute('id'), $item, $show); $tmp = self::parseNode($item->getAttribute('id'), $item, $show);
$items[$tmp['id']] = $tmp['content']; $items[$tmp['id']] = $tmp['content'];
} }
} }
require_once LIBS . 'cache_php.php'; require_once LIBS . 'cache_php.php';
$cache_php = new Cache_PHP(config('cache_prefix'), CACHE); $cache_php = new Cache_PHP(config('cache_prefix'), CACHE);
$cache_php->set('items', $items); $cache_php->set('items', $items);
return true; return true;
} }
public static function parseNode($id, $node, $show = false) { public static function parseNode($id, $node, $show = false) {
$name = $node->getAttribute('name'); $name = $node->getAttribute('name');
$article = $node->getAttribute('article'); $article = $node->getAttribute('article');
$plural = $node->getAttribute('plural'); $plural = $node->getAttribute('plural');
$attributes = array(); $attributes = array();
foreach($node->getElementsByTagName('attribute') as $attr) { foreach($node->getElementsByTagName('attribute') as $attr) {
$attributes[strtolower($attr->getAttribute('key'))] = $attr->getAttribute('value'); $attributes[strtolower($attr->getAttribute('key'))] = $attr->getAttribute('value');
} }
return array('id' => $id, 'content' => array('article' => $article, 'name' => $name, 'plural' => $plural, 'attributes' => $attributes)); return array('id' => $id, 'content' => array('article' => $article, 'name' => $name, 'plural' => $plural, 'attributes' => $attributes));
} }
public static function getError() { public static function getError() {
return self::$error; return self::$error;
} }
public static function loadItems() { public static function load() {
if(self::$items) { if(self::$items) {
return; return;
} }
require_once LIBS . 'cache_php.php'; require_once LIBS . 'cache_php.php';
$cache_php = new Cache_PHP(config('cache_prefix'), CACHE); $cache_php = new Cache_PHP(config('cache_prefix'), CACHE);
self::$items = $cache_php->get('items'); self::$items = $cache_php->get('items');
} }
public static function getItem($id) { public static function get($id) {
self::loadItems(); self::load();
return self::$items[$id]; return self::$items[$id];
} }
public static function getDescription($id, $count = 1) { public static function getDescription($id, $count = 1) {
global $db; global $db;
$item = self::getItem($id); $item = self::get($id);
$attr = $item['attributes']; $attr = $item['attributes'];
$s = ''; $s = '';
if(!empty($item['name'])) { if(!empty($item['name'])) {
@ -89,7 +89,7 @@ class Items
if($attr['showcount']) { if($attr['showcount']) {
$s .= $count . ' '; $s .= $count . ' ';
} }
if(!empty($item['plural'])) { if(!empty($item['plural'])) {
$s .= $item['plural']; $s .= $item['plural'];
} }
@ -104,22 +104,22 @@ class Items
if(!empty($item['aticle'])) { if(!empty($item['aticle'])) {
$s .= $item['article'] . ' '; $s .= $item['article'] . ' ';
} }
$s .= $item['name']; $s .= $item['name'];
} }
} }
else else
$s .= 'an item of type ' . $item['id']; $s .= 'an item of type ' . $item['id'];
if(isset($attr['type']) && strtolower($attr['type']) == 'rune') { if(isset($attr['type']) && strtolower($attr['type']) == 'rune') {
$query = $db->query('SELECT `level`, `maglevel`, `vocations` FROM `' . TABLE_PREFIX . 'spells` WHERE `item_id` = ' . $id); $query = $db->query('SELECT `level`, `maglevel`, `vocations` FROM `' . TABLE_PREFIX . 'spells` WHERE `item_id` = ' . $id);
if($query->rowCount() == 1) { if($query->rowCount() == 1) {
$query = $query->fetch(); $query = $query->fetch();
if($query['level'] > 0 && $query['maglevel'] > 0) { if($query['level'] > 0 && $query['maglevel'] > 0) {
$s .= '. ' . ($count > 1 ? "They" : "It") . ' can only be used by '; $s .= '. ' . ($count > 1 ? "They" : "It") . ' can only be used by ';
} }
$configVocations = config('vocations'); $configVocations = config('vocations');
if(!empty(trim($query['vocations']))) { if(!empty(trim($query['vocations']))) {
$vocations = json_decode($query['vocations']); $vocations = json_decode($query['vocations']);
@ -132,9 +132,9 @@ class Items
else { else {
$s .= 'players'; $s .= 'players';
} }
$s .= ' with'; $s .= ' with';
} }
} }
return $s; return $s;