swdb.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. // Profile logbook
  97. case 'GetLobbyWizardLog':
  98. apiCommand = "update_logbook";
  99. success = "Logbook updated succesfully!";
  100. break;
  101. // Battle runs
  102. case 'BattleDungeonResult':
  103. apiCommand = "upload_run_dungeon";
  104. success = "Cairos run logged sucesfully!"
  105. break;
  106. case 'BattleScenarioResult':
  107. apiCommand = "upload_run_scenario";
  108. success = "Scenario run logged sucesfully!"
  109. break;
  110. case 'BattleDimensionHoleDungeonResult':
  111. apiCommand = "upload_run_dimension";
  112. success = "Dimension Hole run logged sucesfully!"
  113. break;
  114. case 'BattleTrialTowerResult_v2':
  115. apiCommand = "upload_run_toa";
  116. success = "TOA run logged sucesfully!"
  117. break;
  118. case 'BattleRiftDungeonResult':
  119. apiCommand = "upload_run_rift";
  120. success = "Rift Dungeon run logged sucesfully!"
  121. start = true;
  122. break;
  123. }
  124. if (apiCommand != ""){
  125. this.command(apiCommand, req, res, start, success);
  126. }
  127. },
  128. /**
  129. * Calls an arbitrary command on the SWDB APIv2.
  130. *
  131. * @param command Command nade.
  132. * @param req The full request data.
  133. * @param res The full response data.
  134. * @param start Indicates if event start request and response are needed.
  135. * @param success Optional. Message to show on success.
  136. */
  137. command(command, req, res, start = false, success = null){
  138. // Override success message if empty
  139. if (success == null || success == ""){
  140. sucess = `Command ${command} succesfully executed!`
  141. }
  142. this.log('debug', `Starting ${command}...`);
  143. // Generate POST data
  144. var post_data;
  145. if (start == false){
  146. post_data = querystring.stringify({
  147. 'request' : JSON.stringify(req),
  148. 'response' : JSON.stringify(res),
  149. 'key' : config.Config.Plugins[this.pluginName].apiKey
  150. });
  151. }
  152. else{
  153. post_data = querystring.stringify({
  154. 'request' : JSON.stringify(req),
  155. 'response' : JSON.stringify(res),
  156. 'start_request' : JSON.stringify(this.eventStartReq),
  157. 'start_response' : JSON.stringify(this.eventStartRes),
  158. 'key' : config.Config.Plugins[this.pluginName].apiKey
  159. });
  160. }
  161. // Configure POST requets
  162. var post_options = {
  163. host: config.Config.Plugins[this.pluginName].host,
  164. port: config.Config.Plugins[this.pluginName].port,
  165. path: '/API/v2/' + command,
  166. method: 'POST',
  167. headers: {
  168. 'Content-Type': 'application/x-www-form-urlencoded',
  169. 'Content-Length': Buffer.byteLength(post_data)
  170. }
  171. };
  172. // Set up the request
  173. var post_req = http.request(post_options, (function(post_res) {
  174. post_res.setEncoding('utf8');
  175. post_res.on('data', (function (body) {
  176. switch (post_res.statusCode){
  177. case 200:
  178. case 201:
  179. this.log('success', `${success} [${post_res.statusCode}]`);
  180. break;
  181. case 400:
  182. this.log('error', `[HTTP/1.1 400] There were errors importing the data: ${JSON.stringify(body)}`);
  183. break;
  184. case 404:
  185. this.log('error', '[HTTP/1.1 404] URL not found.');
  186. break;
  187. case 500:
  188. this.log('error', '[HTTP/1.1 500] The server returned an error.');
  189. break;
  190. default:
  191. this.log('error', `[HTTP/1.1 ${post_res.statusCode}] Unexpected error.`);
  192. }
  193. }).bind(this));
  194. }).bind(this));
  195. // Perform the request
  196. post_req.write(post_data);
  197. post_req.end();
  198. },
  199. /**
  200. * Prints a message to the SWEX output and to the console.
  201. *
  202. * @param type Log type. Debug type will be printed depending on config.
  203. * @param msg The message to log.
  204. */
  205. log(type, msg){
  206. if (type != "debug" || this.config.Config.Plugins[this.pluginName].debug == true){
  207. this.proxy.log({ type: type, source: 'plugin', name: this.pluginName, message: msg });
  208. pad_type = type.substring(0, 5).padEnd(5, ' ');
  209. pad_plugin = this.pluginName.substring(0, 4).padEnd(4, ' ');
  210. console.log(`[${pad_plugin}][${pad_type}] ${msg}`);
  211. }
  212. }
  213. };