swdb.js 9.0 KB

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