diff --git a/system/init.php b/system/init.php index 9bc14514..dbc9db08 100644 --- a/system/init.php +++ b/system/init.php @@ -12,6 +12,7 @@ use DebugBar\StandardDebugBar; use MyAAC\Cache\Cache; use MyAAC\CsrfToken; use MyAAC\Hooks; +use MyAAC\Models\Town; use MyAAC\Settings; use MyAAC\Towns; @@ -173,4 +174,17 @@ define('USE_ACCOUNT_NAME', $db->hasColumn('accounts', 'name')); define('USE_ACCOUNT_NUMBER', $db->hasColumn('accounts', 'number')); define('USE_ACCOUNT_SALT', $db->hasColumn('accounts', 'salt')); -Towns::load(); +$towns = Cache::remember('towns', 10 * 60, function () use ($db) { + if ($db->hasTable('towns') && Town::count() > 0) { + return Town::orderBy('id', 'ASC')->pluck('name', 'id')->toArray(); + } + + return []; +}); + +if (count($towns) <= 0) { + $towns = setting('core.towns'); +} + +config(['towns', $towns]); +unset($towns); diff --git a/system/locale/en/install.php b/system/locale/en/install.php index e25537c9..0cbd29ad 100644 --- a/system/locale/en/install.php +++ b/system/locale/en/install.php @@ -94,7 +94,7 @@ $locale['step_database_loaded_npcs'] = 'NPCs has been loaded...'; $locale['step_database_error_npcs'] = 'There were some problems loading your NPCs'; $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_error_towns'] = 'There were some problems loading your towns. You will need to configure them manually in Settings.'; $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 22204adf..5b6725cf 100644 --- a/system/locale/pl/install.php +++ b/system/locale/pl/install.php @@ -93,7 +93,7 @@ $locale['step_database_loaded_npcs'] = 'Załadowano NPCs...'; $locale['step_database_error_npcs'] = 'Wystąpił problem podczas ładowania NPCs'; $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_error_towns'] = 'Wystąpił problem podczas ładowania miast. Trzeba będzie je skonfigurować manualnie w ustawieniach.'; $locale['step_database_created_account'] = 'Utworzono konto admina...'; $locale['step_database_created_news'] = 'Utworzono newsy...'; diff --git a/system/settings.php b/system/settings.php index 1086f4d2..6770b0c5 100644 --- a/system/settings.php +++ b/system/settings.php @@ -238,9 +238,8 @@ return [ 'towns' => [ 'name' => 'Towns', 'type' => 'textarea', - 'desc' => "if you use TFS 1.3 with support for 'towns' table in database, then you can ignore this - it will be configured automatically (from MySQL database - Table - towns)
" . - "otherwise it will try to load from your .OTBM map file
" . - "if you don't see towns on website, then you need to fill this out", + 'desc' => "If you use TFS 1.3+ with support for 'towns' table in database, then you can ignore this - it will be automatically configured from there.
" . + "If you don't see towns on website, then you need to fill this out", 'default' => "0=No Town\n1=Sample Town", 'callbacks' => [ 'get' => function ($value) { diff --git a/system/src/DataLoader.php b/system/src/DataLoader.php index df2ba6e9..6dca0c24 100644 --- a/system/src/DataLoader.php +++ b/system/src/DataLoader.php @@ -25,6 +25,9 @@ namespace MyAAC; +use MyAAC\Cache\Cache; +use MyAAC\Models\Town; + class DataLoader { private static $locale; @@ -78,7 +81,11 @@ class DataLoader self::$startTime = microtime(true); - if (Towns::save()) { + $cache = Cache::getInstance(); + $cache->delete('towns'); // will be reloaded after next page load + + global $db; + if ($db->hasTable('towns') && Town::count() > 0) { success(self::$locale['step_database_loaded_towns'] . self::getLoadedTime()); } else { diff --git a/system/src/Towns.php b/system/src/Towns.php deleted file mode 100644 index 6b02d41b..00000000 --- a/system/src/Towns.php +++ /dev/null @@ -1,129 +0,0 @@ - - * @copyright 2020 MyAAC - * @link https://my-aac.org - */ - -namespace MyAAC; - -use MyAAC\Models\Town; - -class Towns -{ - /** - * @var string - */ - private static $filename = CACHE . 'persistent/' . '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() - { - return Town::orderBy('id', 'ASC')->pluck('name', 'id')->toArray(); - } -} diff --git a/system/src/TownsReader.php b/system/src/TownsReader.php deleted file mode 100644 index b1323c31..00000000 --- a/system/src/TownsReader.php +++ /dev/null @@ -1,84 +0,0 @@ -file = fopen($file, 'rb'); - } - - public function load() - { - // checks if file is opened correctly - if ($this->file) { - // skips version - fseek($this->file, 4); - - // reads nodes chain - while (!feof($this->file)) { - // reads byte - switch (ord(fgetc($this->file))) { - // maybe a town node - case self::NODE_START: - // reads node type - if (ord(fgetc($this->file)) == self::OTBM_TOWN) { - $id = unpack('L', fread($this->file, 4)); - $length = unpack('S', fread($this->file, 2)); - - // reads town name - $this->towns[$id[1]] = fread($this->file, $length[1]); - } - break; - - // escape next character - it might be NODE_START character which is in fact not - case self::ESCAPE_CHAR: - fgetc($this->file); - break; - } - } - } - } - - public function get() { - return $this->towns; - } -}