New class: DataLoader (loads data from server)

Also combined some code responsible for loading server data
This commit is contained in:
slawkens 2020-10-09 23:37:24 +02:00
parent 6c9e6af154
commit 94b145b215
7 changed files with 170 additions and 84 deletions

View File

@ -0,0 +1,45 @@
<?php
/**
* Project: MyAAC
* Automatic Account Creator for Open Tibia Servers
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @package MyAAC
* @author Slawkens <slawkens@gmail.com>
* @copyright 2020 MyAAC
* @link https://my-aac.org
*/
require '../../common.php';
require SYSTEM . 'functions.php';
require SYSTEM . 'init.php';
require SYSTEM . 'login.php';
if (!admin())
die('Access denied.');
ini_set('max_execution_time', 300);
ob_implicit_flush();
ob_end_flush();
header('X-Accel-Buffering: no');
require LIBS . 'DataLoader.php';
require LOCALE . 'en/main.php';
require LOCALE . 'en/install.php';
DataLoader::setLocale($locale);
DataLoader::load();

View File

@ -43,45 +43,9 @@ if($success) {
success($locale['step_database_imported_players']);
}
require LIBS . 'items.php';
if(Items::loadFromXML())
success($locale['step_database_loaded_items']);
else
error(Items::getError());
require LIBS . 'weapons.php';
if(Weapons::loadFromXML())
success($locale['step_database_loaded_weapons']);
else
error(Weapons::getError());
require LIBS . 'creatures.php';
if(Creatures::loadFromXML()) {
success($locale['step_database_loaded_monsters']);
if(Creatures::getMonstersList()->hasErrors()) {
$locale['step_database_error_monsters'] = str_replace('$LOG$', 'system/logs/error.log', $locale['step_database_error_monsters']);
warning($locale['step_database_error_monsters']);
}
}
else {
error(Creatures::getLastError());
}
require LIBS . 'spells.php';
if(Spells::loadFromXML()) {
success($locale['step_database_loaded_spells']);
}
else {
error(Spells::getLastError());
}
if (Towns::save()) {
success($locale['step_database_loaded_towns']);
}
else {
warning($locale['step_database_error_towns']);
}
require LIBS . 'DataLoader.php';
DataLoader::setLocale($locale);
DataLoader::load();
// update config.highscores_ids_hidden
require_once SYSTEM . 'migrations/20.php';

103
system/libs/DataLoader.php Normal file
View File

@ -0,0 +1,103 @@
<?php
/**
* Project: MyAAC
* Automatic Account Creator for Open Tibia Servers
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @package MyAAC
* @author Slawkens <slawkens@gmail.com>
* @copyright 2020 MyAAC
* @link https://my-aac.org
*/
/**
* Class DataLoader
*/
class DataLoader
{
private static $locale;
private static $startTime;
/**
* Load data from server
*/
public static function load()
{
self::$startTime = microtime(true);
require LIBS . 'items.php';
if(Items::loadFromXML()) {
success(self::$locale['step_database_loaded_items'] . self::getLoadedTime());
}
else {
error(Items::getError());
}
self::$startTime = microtime(true);
require LIBS . 'creatures.php';
if(Creatures::loadFromXML()) {
success(self::$locale['step_database_loaded_monsters'] . self::getLoadedTime());
if(Creatures::getMonstersList()->hasErrors()) {
self::$locale['step_database_error_monsters'] = str_replace('$LOG$', 'system/logs/error.log', self::$locale['step_database_error_monsters']);
warning(self::$locale['step_database_error_monsters']);
}
}
else {
error(Creatures::getLastError());
}
self::$startTime = microtime(true);
require LIBS . 'spells.php';
if(Spells::loadFromXML()) {
success(self::$locale['step_database_loaded_spells'] . self::getLoadedTime());
}
else {
error(Spells::getLastError());
}
self::$startTime = microtime(true);
if (Towns::save()) {
success(self::$locale['step_database_loaded_towns'] . self::getLoadedTime());
}
else {
warning(self::$locale['step_database_error_towns']);
}
self::$startTime = microtime(true);
require LIBS . 'weapons.php';
if(Weapons::loadFromXML()) {
success(self::$locale['step_database_loaded_weapons'] . self::getLoadedTime());
}
else {
error(Weapons::getError());
}
}
public static function setLocale($locale) {
self::$locale = $locale;
}
private static function getLoadedTime()
{
$endTime = round(microtime(true) - self::$startTime, 3);
return ' (' . str_replace('$TIME$', $endTime, self::$locale['loaded_in_ms']) . ')';
}
}

View File

@ -12,4 +12,4 @@ $locale['direction']= 'ltr';
$locale['error404'] = 'The requested page was not found.';
$locale['news'] = 'Latest news';
?>
$locale['loaded_in_ms'] = 'in $TIME$ ms';

View File

@ -12,4 +12,4 @@ $locale['direction']= 'ltr';
$locale['error404'] = 'Strona nie została odnaleziona.';
$locale['news'] = 'Ostatnie newsy';
?>
$locale['loaded_in_ms'] = 'w $TIME$ ms';

View File

@ -8,36 +8,6 @@
* @link https://my-aac.org
*/
defined('MYAAC') or die('Direct access not allowed!');
$title = 'Load items.xml';
require LIBS . 'items.php';
require LIBS . 'weapons.php';
$title = 'Server Data';
$twig->display('admin.items.html.twig');
$reload = isset($_REQUEST['reload']) && (int)$_REQUEST['reload'] === 1;
if ($reload) {
$items_start_time = microtime(true);
if (Items::loadFromXML(true)) {
success('Successfully loaded items (in ' . round(microtime(true) - $items_start_time, 4) . ' seconds).');
}
else {
error(Items::getError());
}
$weapons_start_time = microtime(true);
if (Weapons::loadFromXML(true)) {
success('Successfully loaded weapons (in ' . round(microtime(true) - $weapons_start_time, 4) . ' seconds).');
}
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.');
}
}

View File

@ -1,26 +1,30 @@
<form method="post" action="{{ constant('ADMIN_URL') }}?p=items" id="items_form">
<input type="hidden" name="reload" value="1"/>
<button type="submit" class="btn btn-info"><i class="fas fa-update"></i> Reload items and weapons</button>
</form>
<p>Clicking the button will reload server data (items, monsters, spells, towns, weapons).</p>
<p>It may take up to few minutes.</p>
<button id="reload_button" class="btn btn-info"><i class="fas fa-update"></i>Reload</button>
<div id="loader" class="spinner-border" role="status">
<div id="spinner" class="spinner-border" role="status">
<span class="sr-only">Loading...</span>
</div>
<small id="notice"> (it may take some time to finish)</small>
<div style="height: 20px"></div>
<div id="messages"></div>
<style>
#loader {
#spinner {
display: none;
}
</style>
<script>
$(function () {
$('#items_form').on('submit', function (e) {
$('#loader').show();
$('#reload_button').on('click', function (e) {
$('#spinner').show();
var $notice = $('#notice');
$notice.html('Reloading...' + $notice.text());
$('[id^=success]').remove();
$('#messages').append('<div id="success-1"></div>');
performInstall('{{ constant('ADMIN_URL') }}tools/reload_data.php');
});
})
</script>
</script>
<script type="text/javascript" src="{{ constant('BASE_URL') }}tools/installer.js"></script>