swdb.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. *
  3. * List of processed commands:
  4. *
  5. * - HubUserLogin
  6. * - BattleDungeonResult
  7. * - BattleScenarioResult
  8. * - BattleDimensionHoleDungeonResult
  9. * - BattleTrialTowerResult_v2
  10. *
  11. */
  12. const dateFormat = require('/usr/local/lib/node_modules/dateformat');
  13. var querystring = require('querystring');
  14. var http = require('http');
  15. module.exports = {
  16. defaultConfig: {
  17. enabled: true,
  18. debug: true,
  19. runs: true,
  20. apiKey: '',
  21. //host: 'http://localhost',
  22. host: 'localhost',
  23. port: '80',
  24. },
  25. defaultConfigDetails: {
  26. debug: { label: 'Print debug messages' },
  27. runs: { label: 'Also log runs' },
  28. apiKey: { label: 'SWDB instance API key', type: 'textarea' },
  29. host: { label: 'Host running the SWDB instance', type: 'textarea' },
  30. port: { label: 'Port', type: 'textarea' }
  31. },
  32. pluginName: 'SWDB',
  33. pluginDescription: 'Uploads data to SWDB.',
  34. temp: {},
  35. proxy: null,
  36. config: null,
  37. request: null,
  38. eventStartReq: null,
  39. eventStartRes: null,
  40. /**
  41. * Initializes he plugin.
  42. *
  43. * @param proxy The proxy.
  44. * @param config Global configurations.
  45. */
  46. init(proxy, config) {
  47. this.proxy = proxy;
  48. this.config = config;
  49. proxy.on('apiCommand', (req, resp) => {
  50. try {
  51. wizardID = '';
  52. command = '';
  53. apiKey = config.Config.Plugins[this.pluginName].apiKey;
  54. if (config.Config.Plugins[this.pluginName].enabled) {
  55. command = req["command"];
  56. // Check the API key.
  57. if (!apiKey) {
  58. this.log('error', `Profile upload is enabled, but missing API key. Check ${this.pluginName} settings.`);
  59. return;
  60. }
  61. this.processCommand(command, req, resp);
  62. }
  63. }
  64. catch (e) {
  65. this.log('error', `An unexpected error occured: ${e.message}`);
  66. }
  67. });
  68. },
  69. /**
  70. * Switches the command and calls the appropiate function.
  71. *
  72. * @param command The command nade.
  73. * @param req The full request data.
  74. * @param res The full response.
  75. */
  76. processCommand(command, req, res){
  77. // Process the command
  78. var apiCommand = "";
  79. var success = null;
  80. var start = false;
  81. switch (command){
  82. // Start events. Save in case they are needed.
  83. case 'BattleDungeonStart':
  84. case 'BattleScenarioStart':
  85. case 'BattleDimensionHoleDungeonStart':
  86. case 'BattleTrialTowerStart_v2':
  87. case 'BattleRiftDungeonStart':
  88. this.eventStartReq = req;
  89. this.eventStartRes = res;
  90. break;
  91. // Profile login
  92. case 'HubUserLogin':
  93. apiCommand = "upload_profile";
  94. success = "Profile uploaded sucesfully!"
  95. break;
  96. // Battle runs
  97. case 'BattleDungeonResult':
  98. apiCommand = "upload_run_dungeon";
  99. success = "Cairos run logged sucesfully!"
  100. break;
  101. case 'BattleScenarioResult':
  102. apiCommand = "upload_run_scenario";
  103. success = "Scenario run logged sucesfully!"
  104. break;
  105. case 'BattleDimensionHoleDungeonResult':
  106. apiCommand = "upload_run_dimension";
  107. success = "Dimension Hole run logged sucesfully!"
  108. break;
  109. case 'BattleTrialTowerResult_v2':
  110. apiCommand = "upload_run_toa";
  111. success = "TOA run logged sucesfully!"
  112. break;
  113. case 'BattleRiftDungeonResult':
  114. apiCommand = "upload_run_rift";
  115. success = "Rift Dungeon run logged sucesfully!"
  116. start = true;
  117. break;
  118. case 'battleGuildMazeResult':
  119. apiCommand = "upload_run_lab";
  120. success = "Tartarus Labyrinth run logged succesfully!"
  121. break;
  122. }
  123. if (apiCommand != ""){
  124. this.command(apiCommand, req, res, start, success);
  125. }
  126. },
  127. /**
  128. * Calls an arbitrary command on the SWDB APIv2.
  129. *
  130. * @param command Command nade.
  131. * @param req The full request data.
  132. * @param res The full response data.
  133. * @param start Indicates if event start request and response are needed.
  134. * @param success Optional. Message to show on success.
  135. */
  136. command(command, req, res, start = false, success = null){
  137. // Override success message if empty
  138. if (success == null || success == ""){
  139. sucess = `Command ${command} succesfully executed!`
  140. }
  141. this.log('debug', `Starting ${command}...`);
  142. // Generate POST data
  143. var post_data;
  144. if (start == false){
  145. post_data = querystring.stringify({
  146. 'request' : JSON.stringify(req),
  147. 'response' : JSON.stringify(res),
  148. 'key' : config.Config.Plugins[this.pluginName].apiKey
  149. });
  150. }
  151. else{
  152. post_data = querystring.stringify({
  153. 'request' : JSON.stringify(req),
  154. 'response' : JSON.stringify(res),
  155. 'start_request' : JSON.stringify(this.eventStartReq),
  156. 'start_response' : JSON.stringify(this.eventStartRes),
  157. 'key' : config.Config.Plugins[this.pluginName].apiKey
  158. });
  159. }
  160. // Configure POST requets
  161. var post_options = {
  162. host: config.Config.Plugins[this.pluginName].host,
  163. port: config.Config.Plugins[this.pluginName].port,
  164. path: '/API/v2/' + command,
  165. method: 'POST',
  166. headers: {
  167. 'Content-Type': 'application/x-www-form-urlencoded',
  168. 'Content-Length': Buffer.byteLength(post_data)
  169. }
  170. };
  171. // Set up the request
  172. var post_req = http.request(post_options, (function(post_res) {
  173. post_res.setEncoding('utf8');
  174. post_res.on('data', (function (body) {
  175. switch (post_res.statusCode){
  176. case 200:
  177. case 201:
  178. this.log('success', `${success} [${post_res.statusCode}]`);
  179. break;
  180. case 400:
  181. this.log('error', `[HTTP/1.1 400] There were errors importing the data: ${JSON.stringify(body)}`);
  182. break;
  183. case 404:
  184. this.log('error', '[HTTP/1.1 404] URL not found.');
  185. break;
  186. case 500:
  187. this.log('error', '[HTTP/1.1 500] The server returned an error.');
  188. break;
  189. default:
  190. this.log('error', `[HTTP/1.1 ${post_res.statusCode}] Unexpected error.`);
  191. }
  192. }).bind(this));
  193. }).bind(this));
  194. // Perform the request
  195. post_req.write(post_data);
  196. post_req.end();
  197. },
  198. /**
  199. * Prints a message to the SWEX output and to the console.
  200. *
  201. * @param type Log type. Debug type will be printed depending on config.
  202. * @param msg The message to log.
  203. */
  204. log(type, msg){
  205. if (type != "debug" || this.config.Config.Plugins[this.pluginName].debug == true){
  206. this.proxy.log({ type: type, source: 'plugin', name: this.pluginName, message: msg });
  207. pad_type = type.substring(0, 5).padEnd(5, ' ');
  208. pad_plugin = this.pluginName.substring(0, 4).padEnd(4, ' ');
  209. console.log(`[${pad_plugin}][${pad_type}] ${msg}`);
  210. }
  211. }
  212. };