| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- 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 method = "";
- var params = {'key': config.Config.Plugins[this.pluginName].apiKey};
- 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':
- case 'BattleRiftOfWorldsRaidStart':
- this.eventStartReq = req;
- this.eventStartRes = res;
- break;
- // Profile login
- case 'HubUserLogin':
- apiCommand = "profile/";
- method = "PUT";
- success = "Profile uploaded sucesfully!";
- params['response'] = JSON.stringify(res);
- break;
- // Catalog status
- case 'GetUnitCollection':
- apiCommand = "profile/";
- method = "PUT";
- success = "Collection succesfully updated!";
- params['response'] = JSON.stringify(res);
- break;
- // Profile logbook
- case 'GetLobbyWizardLog':
- apiCommand = "profile/";
- method = "PUT";
- success = "Records succesfully updated!";
- params['response'] = JSON.stringify(res);
- break;
- // Cairos Dungeon run
- case 'BattleDungeonResult_V2':
- apiCommand = "run/";
- method = "POST";
- success = "Cairos run logged sucesfully!"
- params['result_response'] = JSON.stringify(res);
- params['result_request'] = JSON.stringify(req);
- break;
- // Dimension Hole run
- case 'BattleDimensionHoleDungeonResult_v2':
- apiCommand = "run/";
- method = "POST";
- success = "Dimension Hole run logged sucesfully!"
- params['result_response'] = JSON.stringify(res);
- params['result_request'] = JSON.stringify(req);
- break;
- // Rift raid run
- case 'BattleRiftOfWorldsRaidResult':
- apiCommand = "run/";
- method = "POST";
- success = "Rift Rarid run logged sucesfully!"
- params['result_response'] = JSON.stringify(res);
- params['start_response'] = JSON.stringify(this.eventStartRes);
- break;
- // Scenario run
- case 'BattleScenarioResult':
- apiCommand = "run/";
- method = "POST";
- success = "Scenario run logged sucesfully!"
- params['result_response'] = JSON.stringify(res);
- params['start_response'] = JSON.stringify(this.eventStartRes);
- break;
- // Arena run
- case 'BattleArenaResult':
- apiCommand = "run/";
- method = "POST";
- success = "Arena run logged sucesfully!"
- params['result_response'] = JSON.stringify(res);
- break;
- // Dimensional Rift run
- case 'BattleDarkPortalResult':
- apiCommand = "run/";
- method = "POST";
- success = "Dimensional Rift run logged sucesfully!"
- params['result_response'] = JSON.stringify(res);
- break;
- case 'BattleTrialTowerResult_v2':
- //command = "log_run_toa";
- //success = "TOA run logged sucesfully!"
- break;
- }
- if (apiCommand != ""){
- this.command(apiCommand, method, params, success);
- }
- },
- /**
- * Calls an arbitrary command on the SWDB APIv3.
- *
- * @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, method, params, 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 data = querystring.stringify(params);
- // Configure POST requets
- var post_options = {
- host: config.Config.Plugins[this.pluginName].host,
- port: config.Config.Plugins[this.pluginName].port,
- path: '/API/v3/' + command,
- method: method,
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Content-Length': Buffer.byteLength(data)
- }
- };
- // Set up the request
- var req = http.request(post_options, (function(res) {
- res.setEncoding('utf8');
- res.on('data', (function (body) {
- switch (res.statusCode){
- case 200:
- case 201:
- this.log('success', `${success} [${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 ${res.statusCode}] Unexpected error.`);
- }
- }).bind(this));
- }).bind(this));
- // Perform the request
- req.write(data);
- 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}`);
- }
- }
- };
|