From d00e550972b8d6dc236d0cf5bf4ff850dcfd882e Mon Sep 17 00:00:00 2001 From: Exodus4D Date: Sun, 30 Jul 2017 15:34:32 +0200 Subject: [PATCH] - new import for static "region"/"constellation" universe data #515 - refactored DB connection handling - removed build.js (RequireJs is now handled by Gulp task) --- app/main/controller/api/user.php | 1 - app/main/controller/ccp/universe.php | 93 ++++++++ app/main/controller/mapcontroller.php | 2 +- app/main/controller/setup.php | 40 ++-- app/main/db/database.php | 17 +- app/main/lib/config.php | 17 +- .../model/universe/basicuniversemodel.php | 17 ++ .../model/universe/constellationmodel.php | 57 +++++ app/main/model/universe/regionmodel.php | 36 +++ build.js | 214 ------------------ 10 files changed, 245 insertions(+), 249 deletions(-) create mode 100644 app/main/controller/ccp/universe.php create mode 100644 app/main/model/universe/basicuniversemodel.php create mode 100644 app/main/model/universe/constellationmodel.php create mode 100644 app/main/model/universe/regionmodel.php delete mode 100644 build.js diff --git a/app/main/controller/api/user.php b/app/main/controller/api/user.php index 41bfbdc7..d268af9a 100644 --- a/app/main/controller/api/user.php +++ b/app/main/controller/api/user.php @@ -11,7 +11,6 @@ use Controller; use controller\MailController; use Model; use Exception; -use DB; class User extends Controller\Controller{ diff --git a/app/main/controller/ccp/universe.php b/app/main/controller/ccp/universe.php new file mode 100644 index 00000000..e199eec2 --- /dev/null +++ b/app/main/controller/ccp/universe.php @@ -0,0 +1,93 @@ +setupRegions($f3); + $this->setupConstellations($f3); + } + + /** + * get all regions from CCP and store region data + * @param \Base $f3 + */ + private function setupRegions(\Base $f3){ + $this->getDB('UNIVERSE'); + $regionIds = $f3->ccpClient->getRegions(); + $regionModel = BasicModel::getNew('Universe\RegionModel'); + + foreach($regionIds as $regionId){ + $regionModel->getById($regionId); + + if($regionModel->dry()){ + $regionData = $f3->ccpClient->getRegionData($regionId); + if( !empty($regionData) ){ + $regionModel->copyfrom($regionData, ['id', 'name', 'description']); + $regionModel->save(); + } + } + + $regionModel->reset(); + } + } + + /** + * get all constellations from CCP and store constellation data + * @param \Base $f3 + */ + private function setupConstellations(\Base $f3){ + $this->getDB('UNIVERSE'); + $constellationIds = $f3->ccpClient->getConstellations(); + $constellationModel = BasicModel::getNew('Universe\ConstellationModel'); + + foreach($constellationIds as $constellationId){ + $constellationModel->getById($constellationId); + + if($constellationModel->dry()){ + $constellationData = $f3->ccpClient->getConstellationData($constellationId); + + if( !empty($constellationData) ){ + // $constellationModel->copyfrom($constellationData, ['id', 'name', 'regionId']); + $constellationModel->copyfrom($constellationData, function($fields){ + // add position coordinates as separate columns + if(is_array($fields['position'])){ + $position = $fields['position']; + if( + isset($position['x']) && + isset($position['y']) && + isset($position['z']) + ){ + $fields['x'] = $position['x']; + $fields['y'] = $position['y']; + $fields['z'] = $position['z']; + } + } + + // filter relevant data for insert + return array_intersect_key($fields, array_flip(['id', 'name', 'regionId', 'x', 'y', 'z'])); + }); + + $constellationModel->save(); + } + } + + $constellationModel->reset(); + } + } +} \ No newline at end of file diff --git a/app/main/controller/mapcontroller.php b/app/main/controller/mapcontroller.php index 947a866e..300c7bcc 100644 --- a/app/main/controller/mapcontroller.php +++ b/app/main/controller/mapcontroller.php @@ -16,7 +16,7 @@ class MapController extends AccessController { /** * @param \Base $f3 */ - public function init($f3) { + public function init(\Base $f3) { $character = $this->getCharacter(); // page title diff --git a/app/main/controller/setup.php b/app/main/controller/setup.php index 67cefba0..080c39aa 100644 --- a/app/main/controller/setup.php +++ b/app/main/controller/setup.php @@ -95,6 +95,15 @@ class Setup extends Controller { ], 'tables' => [] ], + /* WIP ... + 'UNIVERSE' => [ + 'info' => [], + 'models' => [ + 'Model\Universe\RegionModel', + 'Model\Universe\ConstellationModel' + ], + 'tables' => [] + ], */ 'CCP' => [ 'info' => [], 'models' => [], @@ -596,8 +605,6 @@ class Setup extends Controller { foreach($this->databases as $dbKey => $dbData){ $dbLabel = ''; - $dbName = ''; - $dbUser = ''; $dbConfig = []; // DB connection status @@ -612,26 +619,25 @@ class Setup extends Controller { $dbColumnQueries = []; // tables that should exist in this DB $requiredTables = []; + // get DB config + $dbConfigValues = Config::getDatabaseConfig($dbKey); // check DB for valid connection $db = DB\Database::instance()->getDB($dbKey); // check config that does NOT require a valid DB connection switch($dbKey){ - case 'PF': - $dbLabel = 'Pathfinder'; - $dbName = Controller::getEnvironmentData('DB_NAME'); - $dbUser = Controller::getEnvironmentData('DB_USER'); - break; - case 'CCP': - $dbLabel = 'EVE-Online [SDE]'; - $dbName = Controller::getEnvironmentData('DB_CCP_NAME'); - $dbUser = Controller::getEnvironmentData('DB_CCP_USER'); - break; + case 'PF': $dbLabel = 'Pathfinder'; break; + case 'UNIVERSE': $dbLabel = 'EVE-Online universe'; break; + case 'CCP': $dbLabel = 'EVE-Online [SDE]'; break; } + $dbName = $dbConfigValues['NAME']; + $dbUser = $dbConfigValues['USER']; + if($db){ switch($dbKey){ case 'PF': + case 'UNIVERSE': // enable (table) setup for this DB $dbSetupEnable = true; @@ -854,10 +860,6 @@ class Setup extends Controller { $dbStatusCheckCount++; } - if($exec){ - $f3->reroute('@setup'); - } - if($dbStatusCheckCount !== 0){ $this->databaseCheck = false; } @@ -880,6 +882,10 @@ class Setup extends Controller { ]; } + if($exec){ + $f3->reroute('@setup'); + } + return $this->databases; } @@ -956,7 +962,7 @@ class Setup extends Controller { // setup tables foreach($this->databases[$dbKey]['models'] as $modelClass){ - $checkTables[] = call_user_func($modelClass . '::setup'); + $checkTables[] = call_user_func($modelClass . '::setup', $db); } } return $checkTables; diff --git a/app/main/db/database.php b/app/main/db/database.php index bfda794f..f6049ff9 100644 --- a/app/main/db/database.php +++ b/app/main/db/database.php @@ -9,6 +9,7 @@ namespace DB; use Controller; use controller\LogController; +use lib\Config; class Database extends \Prefab { @@ -31,21 +32,9 @@ class Database extends \Prefab { // check if DB connection already exists if( !$f3->exists($dbHiveKey, $db) ){ - if($database === 'CCP'){ - // CCP DB - $dns = Controller\Controller::getEnvironmentData('DB_CCP_DNS'); - $name = Controller\Controller::getEnvironmentData('DB_CCP_NAME'); - $user = Controller\Controller::getEnvironmentData('DB_CCP_USER'); - $password = Controller\Controller::getEnvironmentData('DB_CCP_PASS'); - }else{ - // Pathfinder(PF) DB - $dns = Controller\Controller::getEnvironmentData('DB_DNS'); - $name = Controller\Controller::getEnvironmentData('DB_NAME'); - $user = Controller\Controller::getEnvironmentData('DB_USER'); - $password = Controller\Controller::getEnvironmentData('DB_PASS'); - } + $dbConfig = Config::getDatabaseConfig($database); - $db = $this->connect($dns, $name, $user, $password); + $db = call_user_func_array([$this, 'connect'], $dbConfig); if( !is_null($db) ){ // set DB timezone to UTC +00:00 (eve server time) diff --git a/app/main/lib/config.php b/app/main/lib/config.php index c788d9d1..9aa28bb9 100644 --- a/app/main/lib/config.php +++ b/app/main/lib/config.php @@ -132,8 +132,6 @@ class Config extends \Prefab { * Nginx (server config): * -> FastCGI syntax * fastcgi_param PF-ENV-DEBUG 3; - * - * @return array */ protected function setServerData(){ $data = []; @@ -170,6 +168,21 @@ class Config extends \Prefab { return $data; } + /** + * get database config values + * @param string $dbKey + * @return array + */ + static function getDatabaseConfig($dbKey = 'PF'){ + $dbConfKey = ($dbKey === 'PF') ? '' : $dbKey . '_'; + return [ + 'DNS' => self::getEnvironmentData('DB_' . $dbConfKey . 'DNS'), + 'NAME' => self::getEnvironmentData('DB_' . $dbConfKey . 'NAME'), + 'USER' => self::getEnvironmentData('DB_' . $dbConfKey . 'USER'), + 'PASS' => self::getEnvironmentData('DB_' . $dbConfKey . 'PASS') + ]; + } + /** * get email for notifications by hive key * @param $key diff --git a/app/main/model/universe/basicuniversemodel.php b/app/main/model/universe/basicuniversemodel.php new file mode 100644 index 00000000..eb2ac4d1 --- /dev/null +++ b/app/main/model/universe/basicuniversemodel.php @@ -0,0 +1,17 @@ + [ + 'type' => Schema::DT_VARCHAR128, + 'nullable' => false, + 'default' => '' + ], + 'regionId' => [ + 'type' => Schema::DT_INT, + 'index' => true, + 'belongs-to-one' => 'Model\Universe\RegionModel', + 'constraint' => [ + [ + 'table' => 'region', + 'on-delete' => 'CASCADE' + ] + ] + ], + 'x' => [ + 'type' => Schema::DT_INT8, + 'nullable' => false, + 'default' => 0 + ], + 'y' => [ + 'type' => Schema::DT_INT8, + 'nullable' => false, + 'default' => 0 + ], + 'z' => [ + 'type' => Schema::DT_INT8, + 'nullable' => false, + 'default' => 0 + ] + ]; + +} \ No newline at end of file diff --git a/app/main/model/universe/regionmodel.php b/app/main/model/universe/regionmodel.php new file mode 100644 index 00000000..44db09bd --- /dev/null +++ b/app/main/model/universe/regionmodel.php @@ -0,0 +1,36 @@ + [ + 'type' => Schema::DT_VARCHAR128, + 'nullable' => false, + 'default' => '' + ], + 'description' => [ + 'type' => Schema::DT_TEXT + ], + 'constellations' => [ + 'has-many' => ['Model\Universe\ConstellationModel', 'regionId'] + ], + ]; +} \ No newline at end of file diff --git a/build.js b/build.js deleted file mode 100644 index 5229680b..00000000 --- a/build.js +++ /dev/null @@ -1,214 +0,0 @@ -({ - //The top level directory that contains your app. If this option is used - //then it assumed your scripts are in a subdirectory under this path. - //This option is not required. If it is not specified, then baseUrl - //below is the anchor point for finding things. If this option is specified, - //then all the files from the app directory will be copied to the dir: - //output area, and baseUrl will assume to be a relative path under - //this directory. - appDir: './js', - - //By default, all modules are located relative to this path. If baseUrl - //is not explicitly set, then all modules are loaded relative to - //the directory that holds the build file. If appDir is set, then - //baseUrl should be specified as relative to the appDir. - baseUrl: './', - - //By default all the configuration for optimization happens from the command - //line or by properties in the config file, and configuration that was - //passed to requirejs as part of the app's runtime "main" JS file is *not* - //considered. However, if you prefer the "main" JS file configuration - //to be read for the build so that you do not have to duplicate the values - //in a separate configuration, set this property to the location of that - //main JS file. The first requirejs({}), require({}), requirejs.config({}), - //or require.config({}) call found in that file will be used. - //As of 2.1.10, mainConfigFile can be an array of values, with the last - //value's config take precedence over previous values in the array. - mainConfigFile: './js/app.js', - - //Specify modules to stub out in the optimized file. The optimizer will - //use the source version of these modules for dependency tracing and for - //plugin use, but when writing the text into an optimized bundle, these - //modules will get the following text instead: - //If the module is used as a plugin: - // define({load: function(id){throw new Error("Dynamic load not allowed: " + id);}}); - //If just a plain module: - // define({}); - //This is useful particularly for plugins that inline all their resources - //and use the default module resolution behavior (do *not* implement the - //normalize() method). In those cases, an AMD loader just needs to know - //that the module has a definition. These small stubs can be used instead of - //including the full source for a plugin. - //stubModules: ['text'], - - //As of RequireJS 2.0.2, the dir above will be deleted before the - //build starts again. If you have a big build and are not doing - //source transforms with onBuildRead/onBuildWrite, then you can - //set keepBuildDir to true to keep the previous dir. This allows for - //faster rebuilds, but it could lead to unexpected errors if the - //built code is transformed in some way. - keepBuildDir: false, - - //Finds require() dependencies inside a require() or define call. By default - //this value is false, because those resources should be considered dynamic/runtime - //calls. However, for some optimization scenarios, it is desirable to - //include them in the build. - //Introduced in 1.0.3. Previous versions incorrectly found the nested calls - //by default. - findNestedDependencies: false, - - - //Inlines the text for any text! dependencies, to avoid the separate - //async XMLHttpRequest calls to load those dependencies. - inlineText: false, - - //If set to true, any files that were combined into a build bundle will be - //removed from the output folder. - removeCombined: true, - - //List the modules that will be optimized. All their immediate and deep - //dependencies will be included in the module's file when the build is - //done. If that module or any of its dependencies includes i18n bundles, - //only the root bundles will be included unless the locale: section is set above. - modules: [ - //Just specifying a module name means that module will be converted into - //a built file that contains all of its dependencies. If that module or any - //of its dependencies includes i18n bundles, they may not be included in the - //built file unless the locale: section is set above. - { - name: 'login', - include: ['text'], - excludeShallow: [ - 'app' - ] - },{ - name: 'mappage', - include: ['text'], - excludeShallow: [ - 'app' - ] - },{ - name: 'setup', - excludeShallow: [ - 'app' - ] - },{ - name: 'admin', - excludeShallow: [ - 'app' - ] - },{ - name: 'app/notification', - excludeShallow: [ - 'app', - 'jquery' - ] - } - ], - - //By default, comments that have a license in them are preserved in the - //output when a minifier is used in the "optimize" option. - //However, for a larger built files there could be a lot of - //comment files that may be better served by having a smaller comment - //at the top of the file that points to the list of all the licenses. - //This option will turn off the auto-preservation, but you will need - //work out how best to surface the license information. - //NOTE: As of 2.1.7, if using xpcshell to run the optimizer, it cannot - //parse out comments since its native Reflect parser is used, and does - //not have the same comments option support as esprima. - preserveLicenseComments: false, // not working with "generate source maps" :( - - //Introduced in 2.1.2 and considered experimental. - //If the minifier specified in the "optimize" option supports generating - //source maps for the minified code, then generate them. The source maps - //generated only translate minified JS to non-minified JS, it does not do - //anything magical for translating minified JS to transpiled source code. - //Currently only optimize: "uglify2" is supported when running in node or - //rhino, and if running in rhino, "closure" with a closure compiler jar - //build after r1592 (20111114 release). - //The source files will show up in a browser developer tool that supports - //source maps as ".js.src" files. - generateSourceMaps: false, - - //Sets the logging level. It is a number. If you want "silent" running, - //set logLevel to 4. From the logger.js file: - //TRACE: 0, - //INFO: 1, - //WARN: 2, - //ERROR: 3, - //SILENT: 4 - //Default is 0. - logLevel: 0, - - //How to optimize all the JS files in the build output directory. - //Right now only the following values - //are supported: - //- "uglify": (default) uses UglifyJS to minify the code. - //- "uglify2": in version 2.1.2+. Uses UglifyJS2. - //- "closure": uses Google's Closure Compiler in simple optimization - //mode to minify the code. Only available if running the optimizer using - //Java. - //- "closure.keepLines": Same as closure option, but keeps line returns - //in the minified files. - //- "none": no minification will be done. - optimize: 'uglify2', - - //Introduced in 2.1.2: If using "dir" for an output directory, normally the - //optimize setting is used to optimize the build bundles (the "modules" - //section of the config) and any other JS file in the directory. However, if - //the non-build bundle JS files will not be loaded after a build, you can - //skip the optimization of those files, to speed up builds. Set this value - //to true if you want to skip optimizing those other non-build bundle JS - //files. - //skipDirOptimize: true, - - //If using UglifyJS2 for script optimization, these config options can be - //used to pass configuration values to UglifyJS2. - //For possible `output` values see: - //https://github.com/mishoo/UglifyJS2#beautifier-options - //For possible `compress` values see: - //https://github.com/mishoo/UglifyJS2#compressor-options - uglify2: { - //Example of a specialized config. If you are fine - //with the default options, no need to specify - //any of these properties. - output: { - beautify: false, - comments: false - }, - compress: { - sequences: false, - drop_console: true, - global_defs: { - DEBUG: false - } - }, - warnings: false, - mangle: true - }, - - //A function that will be called for every write to an optimized bundle - //of modules. This allows transforms of the content before serialization. - onBuildWrite: function (moduleName, path, contents) { - - // show module names for each file - if(moduleName === 'mappage'){ - // perform transformations on the original source - // contents = contents.replace( /#version/i, new Date().toString() ); - } - - return contents; - }, - - paths: { - app: './../js/app' // the main config file will not be build - }, - - //The directory path to save the output. If not specified, then - //the path will default to be a directory called "build" as a sibling - //to the build file. All relative paths are relative to the build file. - dir: './build_js' - - - -}) \ No newline at end of file