|
|
@@ -80,224 +80,94 @@ module.exports = {
|
|
|
*/
|
|
|
processCommand(command, req, res){
|
|
|
// Process the command
|
|
|
+ var apiCommand = "";
|
|
|
+ var success = null;
|
|
|
+ var start = false;
|
|
|
switch (command){
|
|
|
- case 'HubUserLogin':
|
|
|
- this.uploadProfile(res);
|
|
|
- break;
|
|
|
+
|
|
|
+ // 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;
|
|
|
- case 'BattleDungeonResult':
|
|
|
- this.uploadRunDungeon(req, res);
|
|
|
+
|
|
|
+ // Profile login
|
|
|
+ case 'HubUserLogin':
|
|
|
+ apiCommand = "upload_profile";
|
|
|
+ success = "Profile uploaded sucesfully!"
|
|
|
break;
|
|
|
- case 'BattleScenarioStart':
|
|
|
- this.eventStart = req;
|
|
|
- this.eventStartRes = res;
|
|
|
+
|
|
|
+ // Battle runs
|
|
|
+ case 'BattleDungeonResult':
|
|
|
+ apiCommand = "upload_run_dungeon";
|
|
|
+ success = "Cairos run logged sucesfully!"
|
|
|
break;
|
|
|
case 'BattleScenarioResult':
|
|
|
- this.uploadRunScenario(req, res);
|
|
|
- break;
|
|
|
- case 'BattleDimensionHoleDungeonStart':
|
|
|
- this.eventStartReq = req;
|
|
|
- this.eventStartRes = res;
|
|
|
+ apiCommand = "upload_run_scenario";
|
|
|
+ success = "Scenario run logged sucesfully!"
|
|
|
break;
|
|
|
case 'BattleDimensionHoleDungeonResult':
|
|
|
- this.uploadRunDimensionDungeon(req, res);
|
|
|
- break;
|
|
|
- case 'BattleTrialTowerStart_v2':
|
|
|
- this.eventStart = req;
|
|
|
- this.eventStartRes = res;
|
|
|
+ apiCommand = "upload_run_dimension";
|
|
|
+ success = "Dimension Hole run logged sucesfully!"
|
|
|
break;
|
|
|
case 'BattleTrialTowerResult_v2':
|
|
|
- this.uploadTOARun(res, req);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
- * 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.
|
|
|
+ * Calls an arbitrary command on the SWDB APIv2.
|
|
|
*
|
|
|
+ * @param command Command nade.
|
|
|
* @param req The full request data.
|
|
|
- * @param req The full response 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.
|
|
|
*/
|
|
|
- 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();
|
|
|
- },
|
|
|
+ command(command, req, res, start = false, success = null){
|
|
|
|
|
|
- /**
|
|
|
- * 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...')
|
|
|
+ // 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 = querystring.stringify({
|
|
|
- 'request': JSON.stringify(req),
|
|
|
- 'response': JSON.stringify(res),
|
|
|
- 'key' : config.Config.Plugins[this.pluginName].apiKey
|
|
|
- });
|
|
|
+ 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/upload_run_toa',
|
|
|
+ path: '/API/v2/' + command,
|
|
|
method: 'POST',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
@@ -310,22 +180,20 @@ module.exports = {
|
|
|
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.');
|
|
|
+ this.log('success', `${success} [${post_res.statusCode}]`);
|
|
|
break;
|
|
|
case 400:
|
|
|
- this.log('error', `There were errors importing the run: ${JSON.stringify(body)}`);
|
|
|
+ this.log('error', `[HTTP/1.1 400] There were errors importing the data: ${JSON.stringify(body)}`);
|
|
|
break;
|
|
|
case 404:
|
|
|
- this.log('error', 'URL not found.');
|
|
|
+ this.log('error', '[HTTP/1.1 404] URL not found.');
|
|
|
break;
|
|
|
case 500:
|
|
|
- this.log('error', 'The server returned an error.');
|
|
|
+ this.log('error', '[HTTP/1.1 500] The server returned an error.');
|
|
|
break;
|
|
|
default:
|
|
|
- this.log('error', `Unexpected error importing run: ${post_res.statusCode}`);
|
|
|
+ this.log('error', `[HTTP/1.1 ${post_res.statusCode}] Unexpected error.`);
|
|
|
}
|
|
|
}).bind(this));
|
|
|
}).bind(this));
|
|
|
@@ -334,50 +202,6 @@ module.exports = {
|
|
|
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.
|
|
|
*
|
|
|
@@ -385,11 +209,11 @@ module.exports = {
|
|
|
* @param msg The message to log.
|
|
|
*/
|
|
|
log(type, msg){
|
|
|
- //if (type != "debug" || this.config.Config.Plugins[this.pluginName].debug == true){
|
|
|
+ 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}`);
|
|
|
- //}
|
|
|
+ }
|
|
|
}
|
|
|
};
|