swdb.js 8.5 KB

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