diff --git a/install/tools/7-finish.php b/install/tools/7-finish.php index 4f95e903..dc72b469 100644 --- a/install/tools/7-finish.php +++ b/install/tools/7-finish.php @@ -76,6 +76,13 @@ else { error(Spells::getLastError()); } +if (Towns::save()) { + success($locale['step_database_loaded_towns']); +} +else { + warning($locale['step_database_error_towns']); +} + // update config.highscores_ids_hidden require_once SYSTEM . 'migrations/20.php'; $database_migration_20 = true; diff --git a/system/init.php b/system/init.php index 139fa582..2a778862 100644 --- a/system/init.php +++ b/system/init.php @@ -159,60 +159,5 @@ else { } unset($tmp, $id, $vocation); -////////////////////////////////////// -// load towns from database or OTBM // -////////////////////////////////////// - -$tmp = ''; -$towns = []; -if($cache->enabled() && $cache->fetch('towns', $tmp)) { - $towns = unserialize($tmp); -} -else { - if($db->hasTable('towns')) { - $query = $db->query('SELECT `id`, `name` FROM `towns`;')->fetchAll(PDO::FETCH_ASSOC); - - foreach($query as $town) { - $towns[$town['id']] = $town['name']; - } - - unset($query); - } - else { - $mapName = configLua('mapName'); - if (!isset($mapName)) { - $mapName = configLua('map'); - $mapFile = $config['server_path'] . $mapName; - } - - if (strpos($mapName, '.otbm') === false) { - $mapName .= '.otbm'; - } - - if (!isset($mapFile)) { - $mapFile = config('data_path') . 'world/' . $mapName; - } - - if (file_exists($mapFile)) { - ini_set('memory_limit', '-1'); - - require LIBS . 'TownsReader.php'; - $townsReader = new TownsReader($mapFile); - $townsReader->load(); - - $towns = $townsReader->get(); - } - else { - $towns = config('towns'); - } - } - - if($cache->enabled()) { - $cache->set('towns', serialize($towns), 600); - } -} - -config(['towns', $towns]); -//////////////////////////////////////////// -// END - load towns from database or OTBM // -//////////////////////////////////////////// +require LIBS . 'Towns.php'; +Towns::load(); \ No newline at end of file diff --git a/system/libs/Towns.php b/system/libs/Towns.php new file mode 100644 index 00000000..3a0c5974 --- /dev/null +++ b/system/libs/Towns.php @@ -0,0 +1,134 @@ + + * @copyright 2020 MyAAC + * @link https://my-aac.org + */ + +/** + * Class Towns + */ +class Towns +{ + /** + * @var string + */ + private static $filename = CACHE . 'towns.php'; + + /** + * Determine towns + * + * @return array + */ + public static function determine() + { + global $db; + + if($db->hasTable('towns')) { + return self::getFromDatabase(); + } + + return self::getFromOTBM(); + } + + /** + * Load cached towns file + */ + public static function load() + { + $towns = config('towns'); + if (file_exists(self::$filename)) { + $towns = require self::$filename; + } + + config(['towns', $towns]); + } + + /** + * Save into cache file + * + * @return bool + */ + public static function save() + { + $towns = self::determine(); + if (count($towns) > 0) { + file_put_contents(self::$filename, 'load(); + + $towns = $townsReader->get(); + } + + return $towns; + } + + /** + * Load from database + * + * @return array + */ + public static function getFromDatabase() + { + global $db; + + $query = $db->query('SELECT `id`, `name` FROM `towns`;')->fetchAll(PDO::FETCH_ASSOC); + + $towns = []; + foreach($query as $town) { + $towns[$town['id']] = $town['name']; + } + + return $towns; + } +} \ No newline at end of file diff --git a/system/locale/en/install.php b/system/locale/en/install.php index bd36d018..3c45f288 100644 --- a/system/locale/en/install.php +++ b/system/locale/en/install.php @@ -86,6 +86,8 @@ $locale['step_database_loaded_weapons'] = 'Weapons has been loaded...'; $locale['step_database_loaded_monsters'] = 'Monsters has been loaded...'; $locale['step_database_error_monsters'] = 'There were some problems loading your monsters.xml file. Please check $LOG$ for more info.'; $locale['step_database_loaded_spells'] = 'Spells has been loaded...'; +$locale['step_database_loaded_towns'] = 'Towns has been loaded...'; +$locale['step_database_error_towns'] = 'There were some problems loading your towns. You will need to configure them manually in config.'; $locale['step_database_created_account'] = 'Created admin account...'; $locale['step_database_created_news'] = 'Newses has been created...'; diff --git a/system/locale/pl/install.php b/system/locale/pl/install.php index 4bcdcb92..c54bb30b 100644 --- a/system/locale/pl/install.php +++ b/system/locale/pl/install.php @@ -85,6 +85,8 @@ $locale['step_database_loaded_weapons'] = 'Załadowano bronie (weapons)...'; $locale['step_database_loaded_monsters'] = 'Załadowano potworki (monsters)...'; $locale['step_database_error_monsters'] = 'Wystąpiły problemy podczas ładowania pliku monsters.xml. Zobacz $LOG$ po więcej informacji.'; $locale['step_database_loaded_spells'] = 'Załadowano czary (spells)...'; +$locale['step_database_loaded_towns'] = 'Załadowano miasta (towns)...'; +$locale['step_database_error_towns'] = 'Wystąpił problem podczas ładowania miast. Trzeba będzie je skonfigurować manualnie.'; $locale['step_database_created_account'] = 'Utworzono konto admina...'; $locale['step_database_created_news'] = 'Utworzono newsy...'; diff --git a/system/pages/admin/items.php b/system/pages/admin/items.php index 3d6e712e..3cc77638 100644 --- a/system/pages/admin/items.php +++ b/system/pages/admin/items.php @@ -32,4 +32,12 @@ if ($reload) { else { error(Weapons::getError()); } + + $towns_start_time = microtime(true); + if (Towns::save()) { + success('Successfully loaded towns (in ' . round(microtime(true) - $towns_start_time, 4) . ' seconds).'); + } + else { + error('Error: No towns found.'); + } }