192 lines
4.0 KiB
JavaScript
192 lines
4.0 KiB
JavaScript
import path, { dirname } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
/**
|
|
* Main backend application class
|
|
*/
|
|
class App{
|
|
constructor(){
|
|
this.root = path.resolve(dirname(fileURLToPath(import.meta.url)) + '/../../');
|
|
console.log('App root is set to', this.root);
|
|
}
|
|
|
|
/**
|
|
* The plugins used by the application
|
|
* @type {AppPlugin[]}
|
|
*/
|
|
plugins = [];
|
|
|
|
/**
|
|
* Declaration of plugin usage
|
|
* @param {AppPlugin} plugin The plugin to be used
|
|
*/
|
|
async use(plugin){
|
|
this[plugin.name] = plugin;
|
|
plugin.app = this;
|
|
this.plugins.push(plugin);
|
|
}
|
|
|
|
/**
|
|
* Initializes the application. All plugins are initialized at this stage
|
|
*/
|
|
async init(){
|
|
for (let p of this.plugins){
|
|
console.debug('Initializing', p.name)
|
|
if(p.init) await p.init(this);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Imports modules as plugins in the app by a given list
|
|
* @param {Array} modules Modules to be imported
|
|
*/
|
|
async importModules(modules){
|
|
const mods = {};
|
|
for (let m of modules){
|
|
mods[m.name] = (await import(`../${m.path}`))[m.name];
|
|
}
|
|
|
|
for (let m of modules){
|
|
this.use(new mods[m.name]())
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Replaces a plugin
|
|
* @param {AppPlugin} p Plugin to be replaced
|
|
*/
|
|
async replace(p){
|
|
let old = this[p.name];
|
|
this.plugins[this.plugins.indexOf(old)] = p;
|
|
this[p.name] = p;
|
|
p.app = this;
|
|
if(p.init) await p.init(this);
|
|
}
|
|
|
|
/**
|
|
* Starts the application. All Plugins are started at this point
|
|
*/
|
|
async start(){
|
|
for (let p of this.plugins){
|
|
console.debug('Starting', p.name)
|
|
if(p.start) await p.start(this);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/* The following are pseudo in order to keep documentation aligned to the code */
|
|
|
|
/**
|
|
* The core application plugins
|
|
*/
|
|
class AppCore{
|
|
/**
|
|
* The configuration plugin instance
|
|
* @type Config
|
|
*/
|
|
config = undefined;
|
|
|
|
/**
|
|
* The database plugin instance
|
|
* @type Db
|
|
*/
|
|
db = undefined;
|
|
|
|
/**
|
|
* The web server plugin instance
|
|
* @type WebServer
|
|
*/
|
|
webServer = undefined;
|
|
}
|
|
|
|
/**
|
|
* The main logical part application plugins
|
|
*/
|
|
class AppManagement {
|
|
|
|
/**
|
|
* Game objects manager plugin instance
|
|
* @type GameObjectsManager
|
|
*/
|
|
gameObject = undefined;
|
|
|
|
/**
|
|
* Games manager plugin instance
|
|
* @type GamesManager
|
|
*/
|
|
games = undefined;
|
|
|
|
/**
|
|
* Rules manager plugin instance
|
|
* @type RulesManager
|
|
*/
|
|
rules = undefined;
|
|
|
|
/**
|
|
* Scenarios manager plugin instance
|
|
* @type ScenariosManager
|
|
*/
|
|
scenarios = undefined;
|
|
}
|
|
|
|
/**
|
|
* The main application controller plugin instances
|
|
*/
|
|
class AppControllerApi {
|
|
|
|
/**
|
|
* Game objects controller plugin instance
|
|
* @type GameObjectsController
|
|
*/
|
|
gameObjectsApi = undefined;
|
|
|
|
/**
|
|
* Games controller plugin instance
|
|
* @type GamesController
|
|
*/
|
|
gamesApi = undefined;
|
|
|
|
/**
|
|
* Rules controller plugin instance
|
|
* @type RulesController
|
|
*/
|
|
rulesApi = undefined;
|
|
|
|
/**
|
|
* Scenarios controller plugin instance
|
|
* @type ScenariosController
|
|
*/
|
|
scenariosApi = undefined;
|
|
|
|
/**
|
|
* Asset controller plugin instance
|
|
* @type AssetController
|
|
*/
|
|
assetApi = undefined;
|
|
}
|
|
|
|
class AppConcrete {
|
|
/**
|
|
* Application core plugins
|
|
* @type AppCore
|
|
* @memberof App
|
|
*/
|
|
core = undefined;
|
|
|
|
/**
|
|
* Application management plugins
|
|
* @type AppManagement
|
|
* @memberof App
|
|
*/
|
|
management = undefined;
|
|
|
|
/**
|
|
* Application controller API plugins
|
|
* @type AppControllerApi
|
|
* @memberof App
|
|
*/
|
|
controller = undefined
|
|
}
|
|
|
|
export default App; |