/** * * 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 switch (command){ case 'HubUserLogin': this.uploadProfile(res); break; case 'BattleDungeonStart': this.eventStartReq = req; this.eventStartRes = res; break; case 'BattleDungeonResult': this.uploadRunDungeon(req, res); break; case 'BattleScenarioStart': this.eventStart = req; this.eventStartRes = res; break; case 'BattleScenarioResult': this.uploadRunScenario(req, res); break; case 'BattleDimensionHoleDungeonStart': this.eventStartReq = req; this.eventStartRes = res; break; case 'BattleDimensionHoleDungeonResult': this.uploadRunDimensionDungeon(req, res); break; case 'BattleTrialTowerStart_v2': this.eventStart = req; this.eventStartRes = res; break; case 'BattleTrialTowerResult_v2': this.uploadTOARun(res, req); break; } }, /** * Makes a call to the upload_profile API to sync user data. * * @param req The full request data. */ uploadProfile(res){ this.log('info', 'Uploading profile to SWDB...'); // Generate POST data var post_data = querystring.stringify({ 'data' : JSON.stringify(res), '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/upload_profile', 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: this.log('success', 'Profile successfully uploaded.'); break; case 201: this.log('success', 'Profile successfully uploaded.'); break; case 400: this.log('error', `There were errors importing the data: ${JSON.stringify(body)}`); break; case 404: this.log('error', 'URL not found.'); break; case 500: this.log('error', 'The server returned an error.'); break; default: this.log('error', `Unexpected error importing profile: ${post_res.statusCode}`); } }).bind(this)); }).bind(this)); // Perform the request post_req.write(post_data); post_req.end(); }, /** * Makes a call to the upload_run_dungeon API to register a dungeon run. * * @param req The full request data. * @param req The full response data. */ uploadRunDungeon(req, res){ this.log('info', 'Uploading dungeon run to SWDB...') // Generate POST data var post_data = querystring.stringify({ 'request': JSON.stringify(req), 'response': JSON.stringify(res), '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/upload_run_dungeon', 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: this.log('success', 'Dungeon run successfully uploaded.'); break; case 201: this.log('success', 'Dungeon run successfully uploaded.'); break; case 400: this.log('error', `There were errors importing the run: ${JSON.stringify(body)}`); break; case 404: this.log('error', 'URL not found.'); break; case 500: this.log('error', 'The server returned an error.'); break; default: this.log('error', `Unexpected error importing run: ${post_res.statusCode}`); } }).bind(this)); }).bind(this)); // Perform the request post_req.write(post_data); post_req.end(); }, /** * Makes a call to the upload_run_dimension API to register a Dimension Hole * Dungeon run. * * @param req The full request data. * @param req The full response data. */ uploadRunDimensionDungeon(req, res){ this.log('info', 'Uploading Dimension Hole Dungeon run to SWDB...') // Generate POST data var post_data = querystring.stringify({ 'request': JSON.stringify(req), 'response': JSON.stringify(res), '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/upload_run_dimension', 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: this.log('success', 'Dimension Hole Dungeon run successfully uploaded.'); break; case 201: this.log('success', 'Dimension Hole Dungeon run successfully uploaded.'); break; case 400: this.log('error', `There were errors importing the run: ${JSON.stringify(body)}`); break; case 404: this.log('error', 'URL not found.'); break; case 500: this.log('error', 'The server returned an error.'); break; default: this.log('error', `Unexpected error importing run: ${post_res.statusCode}`); } }).bind(this)); }).bind(this)); // Perform the request post_req.write(post_data); post_req.end(); }, /** * Makes a call to the upload_run_dungeon API to register a TOA run. * * @param req The full request data. * @param req The full response data. */ uploadRunTOA(req, res){ this.log('info', 'Uploading TOA run to SWDB...') // Generate POST data var post_data = querystring.stringify({ 'request': JSON.stringify(req), 'response': JSON.stringify(res), '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/upload_run_toa', 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: this.log('success', 'TOA run successfully uploaded.'); break; case 201: this.log('success', 'TOA run successfully uploaded.'); break; case 400: this.log('error', `There were errors importing the run: ${JSON.stringify(body)}`); break; case 404: this.log('error', 'URL not found.'); break; case 500: this.log('error', 'The server returned an error.'); break; default: this.log('error', `Unexpected error importing run: ${post_res.statusCode}`); } }).bind(this)); }).bind(this)); // Perform the request post_req.write(post_data); post_req.end(); }, /** * Makes a call to the upload_run_scenario API to register a scenario run. * * @param req The full request data. * @param req The full response data. */ uploadRunScenario(req, res){ this.log('info', 'Uploading scenario run to SWDB...') this.request.post( 'upload_run_scenario', {json: { key: config.Config.Plugins[this.pluginName].apiKey, request: req, response: res } }, //req_options, (error, response, body) => { if (error) { this.log('error', `Error uplading scenario run: ${error.message}`); return; } switch (response.statusCode){ case 200: this.log('success', 'Scenario run successfully uploaded.'); break; case 201: this.log('success', 'Scenario run successfully uploaded.'); break; case 400: this.log('error', `There were errors importing the data: ${JSON.stringify(body)}`); break; case 404: this.log('error', 'URL not found.'); break; case 500: this.log('error', 'The server returned an error.'); break; default: this.log('error', `Unexpected error importing profile: ${response.statusCode}`); } } ); }, /** * 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}`); //} } };