/** * * List of processed commands: * * - HubUserLogin * - BattleDungeonResult * - BattleScenarioResult * - BattleDimensionHoleDungeonResult * - BattleTrialTowerResult_v2 * */ const dateFormat = require('/usr/local/lib/node_modules/dateformat'); var querystring = require('querystring'); var http = require('http'); module.exports = { defaultConfig: { enabled: true, debug: true, runs: true, apiKey: '', //host: 'http://localhost', host: 'localhost', port: '80', }, defaultConfigDetails: { debug: { label: 'Print debug messages' }, runs: { label: 'Also log runs' }, apiKey: { label: 'SWDB instance API key', type: 'textarea' }, host: { label: 'Host running the SWDB instance', type: 'textarea' }, port: { label: 'Port', type: 'textarea' } }, pluginName: 'SWDB', pluginDescription: 'Uploads data to SWDB.', temp: {}, proxy: null, config: null, request: null, eventStartReq: null, eventStartRes: null, /** * Initializes he plugin. * * @param proxy The proxy. * @param config Global configurations. */ init(proxy, config) { this.proxy = proxy; this.config = config; proxy.on('apiCommand', (req, resp) => { try { wizardID = ''; command = ''; apiKey = config.Config.Plugins[this.pluginName].apiKey; if (config.Config.Plugins[this.pluginName].enabled) { command = req["command"]; // Check the API key. if (!apiKey) { this.log('error', `Profile upload is enabled, but missing API key. Check ${this.pluginName} settings.`); return; } this.processCommand(command, req, resp); } } catch (e) { this.log('error', `An unexpected error occured: ${e.message}`); } }); }, /** * Switches the command and calls the appropiate function. * * @param command The command nade. * @param req The full request data. * @param res The full response. */ processCommand(command, req, res){ // Process the command var apiCommand = ""; var success = null; var start = false; switch (command){ // Start events. Save in case they are needed. case 'BattleDungeonStart': case 'BattleScenarioStart': case 'BattleDimensionHoleDungeonStart': case 'BattleTrialTowerStart_v2': case 'BattleRiftDungeonStart': this.eventStartReq = req; this.eventStartRes = res; break; // Profile login case 'HubUserLogin': apiCommand = "upload_profile"; success = "Profile uploaded sucesfully!" break; // Battle runs case 'BattleDungeonResult': apiCommand = "upload_run_dungeon"; success = "Cairos run logged sucesfully!" break; case 'BattleScenarioResult': apiCommand = "upload_run_scenario"; success = "Scenario run logged sucesfully!" break; case 'BattleDimensionHoleDungeonResult': apiCommand = "upload_run_dimension"; success = "Dimension Hole run logged sucesfully!" break; case 'BattleTrialTowerResult_v2': apiCommand = "upload_run_toa"; success = "TOA run logged sucesfully!" break; case 'BattleRiftDungeonResult': apiCommand = "upload_run_rift"; success = "Rift Dungeon run logged sucesfully!" start = true; break; } if (apiCommand != ""){ this.command(apiCommand, req, res, start, success); } }, /** * Calls an arbitrary command on the SWDB APIv2. * * @param command Command nade. * @param req The full request data. * @param res The full response data. * @param start Indicates if event start request and response are needed. * @param success Optional. Message to show on success. */ command(command, req, res, start = false, success = null){ // Override success message if empty if (success == null || success == ""){ sucess = `Command ${command} succesfully executed!` } this.log('debug', `Starting ${command}...`); // Generate POST data var post_data; if (start == false){ post_data = querystring.stringify({ 'request' : JSON.stringify(req), 'response' : JSON.stringify(res), 'key' : config.Config.Plugins[this.pluginName].apiKey }); } else{ post_data = querystring.stringify({ 'request' : JSON.stringify(req), 'response' : JSON.stringify(res), 'start_request' : JSON.stringify(this.eventStartReq), 'start_response' : JSON.stringify(this.eventStartRes), 'key' : config.Config.Plugins[this.pluginName].apiKey }); } // Configure POST requets var post_options = { host: config.Config.Plugins[this.pluginName].host, port: config.Config.Plugins[this.pluginName].port, path: '/API/v2/' + command, method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(post_data) } }; // Set up the request var post_req = http.request(post_options, (function(post_res) { post_res.setEncoding('utf8'); post_res.on('data', (function (body) { switch (post_res.statusCode){ case 200: case 201: this.log('success', `${success} [${post_res.statusCode}]`); break; case 400: this.log('error', `[HTTP/1.1 400] There were errors importing the data: ${JSON.stringify(body)}`); break; case 404: this.log('error', '[HTTP/1.1 404] URL not found.'); break; case 500: this.log('error', '[HTTP/1.1 500] The server returned an error.'); break; default: this.log('error', `[HTTP/1.1 ${post_res.statusCode}] Unexpected error.`); } }).bind(this)); }).bind(this)); // Perform the request post_req.write(post_data); post_req.end(); }, /** * Prints a message to the SWEX output and to the console. * * @param type Log type. Debug type will be printed depending on config. * @param msg The message to log. */ log(type, msg){ if (type != "debug" || this.config.Config.Plugins[this.pluginName].debug == true){ this.proxy.log({ type: type, source: 'plugin', name: this.pluginName, message: msg }); pad_type = type.substring(0, 5).padEnd(5, ' '); pad_plugin = this.pluginName.substring(0, 4).padEnd(4, ' '); console.log(`[${pad_plugin}][${pad_type}] ${msg}`); } } };