swdb.js 8.7 KB

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