swdb.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. switch (command){
  79. case 'HubUserLogin':
  80. this.uploadProfile(res);
  81. break;
  82. case 'BattleDungeonStart':
  83. this.eventStartReq = req;
  84. this.eventStartRes = res;
  85. break;
  86. case 'BattleDungeonResult':
  87. this.uploadRunDungeon(req, res);
  88. break;
  89. case 'BattleScenarioStart':
  90. this.eventStart = req;
  91. this.eventStartRes = res;
  92. break;
  93. case 'BattleScenarioResult':
  94. this.uploadRunScenario(req, res);
  95. break;
  96. case 'BattleDimensionHoleDungeonStart':
  97. this.eventStartReq = req;
  98. this.eventStartRes = res;
  99. break;
  100. case 'BattleDimensionHoleDungeonResult':
  101. this.uploadRunDimensionDungeon(req, res);
  102. break;
  103. case 'BattleTrialTowerStart_v2':
  104. this.eventStart = req;
  105. this.eventStartRes = res;
  106. break;
  107. case 'BattleTrialTowerResult_v2':
  108. this.uploadTOARun(res, req);
  109. break;
  110. }
  111. },
  112. /**
  113. * Makes a call to the upload_profile API to sync user data.
  114. *
  115. * @param req The full request data.
  116. */
  117. uploadProfile(res){
  118. this.log('info', 'Uploading profile to SWDB...');
  119. // Generate POST data
  120. var post_data = querystring.stringify({
  121. 'data' : JSON.stringify(res),
  122. 'key' : config.Config.Plugins[this.pluginName].apiKey
  123. });
  124. // Configure POST requets
  125. var post_options = {
  126. host: config.Config.Plugins[this.pluginName].host,
  127. port: config.Config.Plugins[this.pluginName].port,
  128. path: '/API/v2/upload_profile',
  129. method: 'POST',
  130. headers: {
  131. 'Content-Type': 'application/x-www-form-urlencoded',
  132. 'Content-Length': Buffer.byteLength(post_data)
  133. }
  134. };
  135. // Set up the request
  136. var post_req = http.request(post_options, (function(post_res) {
  137. post_res.setEncoding('utf8');
  138. post_res.on('data', (function (body) {
  139. switch (post_res.statusCode){
  140. case 200:
  141. this.log('success', 'Profile successfully uploaded.');
  142. break;
  143. case 201:
  144. this.log('success', 'Profile successfully uploaded.');
  145. break;
  146. case 400:
  147. this.log('error', `There were errors importing the data: ${JSON.stringify(body)}`);
  148. break;
  149. case 404:
  150. this.log('error', 'URL not found.');
  151. break;
  152. case 500:
  153. this.log('error', 'The server returned an error.');
  154. break;
  155. default:
  156. this.log('error', `Unexpected error importing profile: ${post_res.statusCode}`);
  157. }
  158. }).bind(this));
  159. }).bind(this));
  160. // Perform the request
  161. post_req.write(post_data);
  162. post_req.end();
  163. },
  164. /**
  165. * Makes a call to the upload_run_dungeon API to register a dungeon run.
  166. *
  167. * @param req The full request data.
  168. * @param req The full response data.
  169. */
  170. uploadRunDungeon(req, res){
  171. this.log('info', 'Uploading dungeon run to SWDB...')
  172. // Generate POST data
  173. var post_data = querystring.stringify({
  174. 'request': JSON.stringify(req),
  175. 'response': JSON.stringify(res),
  176. 'key' : config.Config.Plugins[this.pluginName].apiKey
  177. });
  178. // Configure POST requets
  179. var post_options = {
  180. host: config.Config.Plugins[this.pluginName].host,
  181. port: config.Config.Plugins[this.pluginName].port,
  182. path: '/API/v2/upload_run_dungeon',
  183. method: 'POST',
  184. headers: {
  185. 'Content-Type': 'application/x-www-form-urlencoded',
  186. 'Content-Length': Buffer.byteLength(post_data)
  187. }
  188. };
  189. // Set up the request
  190. var post_req = http.request(post_options, (function(post_res) {
  191. post_res.setEncoding('utf8');
  192. post_res.on('data', (function (body) {
  193. switch (post_res.statusCode){
  194. case 200:
  195. this.log('success', 'Dungeon run successfully uploaded.');
  196. break;
  197. case 201:
  198. this.log('success', 'Dungeon run successfully uploaded.');
  199. break;
  200. case 400:
  201. this.log('error', `There were errors importing the run: ${JSON.stringify(body)}`);
  202. break;
  203. case 404:
  204. this.log('error', 'URL not found.');
  205. break;
  206. case 500:
  207. this.log('error', 'The server returned an error.');
  208. break;
  209. default:
  210. this.log('error', `Unexpected error importing run: ${post_res.statusCode}`);
  211. }
  212. }).bind(this));
  213. }).bind(this));
  214. // Perform the request
  215. post_req.write(post_data);
  216. post_req.end();
  217. },
  218. /**
  219. * Makes a call to the upload_run_dimension API to register a Dimension Hole
  220. * Dungeon run.
  221. *
  222. * @param req The full request data.
  223. * @param req The full response data.
  224. */
  225. uploadRunDimensionDungeon(req, res){
  226. this.log('info', 'Uploading Dimension Hole Dungeon run to SWDB...')
  227. // Generate POST data
  228. var post_data = querystring.stringify({
  229. 'request': JSON.stringify(req),
  230. 'response': JSON.stringify(res),
  231. 'key' : config.Config.Plugins[this.pluginName].apiKey
  232. });
  233. // Configure POST requets
  234. var post_options = {
  235. host: config.Config.Plugins[this.pluginName].host,
  236. port: config.Config.Plugins[this.pluginName].port,
  237. path: '/API/v2/upload_run_dimension',
  238. method: 'POST',
  239. headers: {
  240. 'Content-Type': 'application/x-www-form-urlencoded',
  241. 'Content-Length': Buffer.byteLength(post_data)
  242. }
  243. };
  244. // Set up the request
  245. var post_req = http.request(post_options, (function(post_res) {
  246. post_res.setEncoding('utf8');
  247. post_res.on('data', (function (body) {
  248. switch (post_res.statusCode){
  249. case 200:
  250. this.log('success', 'Dimension Hole Dungeon run successfully uploaded.');
  251. break;
  252. case 201:
  253. this.log('success', 'Dimension Hole Dungeon run successfully uploaded.');
  254. break;
  255. case 400:
  256. this.log('error', `There were errors importing the run: ${JSON.stringify(body)}`);
  257. break;
  258. case 404:
  259. this.log('error', 'URL not found.');
  260. break;
  261. case 500:
  262. this.log('error', 'The server returned an error.');
  263. break;
  264. default:
  265. this.log('error', `Unexpected error importing run: ${post_res.statusCode}`);
  266. }
  267. }).bind(this));
  268. }).bind(this));
  269. // Perform the request
  270. post_req.write(post_data);
  271. post_req.end();
  272. },
  273. /**
  274. * Makes a call to the upload_run_dungeon API to register a TOA run.
  275. *
  276. * @param req The full request data.
  277. * @param req The full response data.
  278. */
  279. uploadRunTOA(req, res){
  280. this.log('info', 'Uploading TOA run to SWDB...')
  281. // Generate POST data
  282. var post_data = querystring.stringify({
  283. 'request': JSON.stringify(req),
  284. 'response': JSON.stringify(res),
  285. 'key' : config.Config.Plugins[this.pluginName].apiKey
  286. });
  287. // Configure POST requets
  288. var post_options = {
  289. host: config.Config.Plugins[this.pluginName].host,
  290. port: config.Config.Plugins[this.pluginName].port,
  291. path: '/API/v2/upload_run_toa',
  292. method: 'POST',
  293. headers: {
  294. 'Content-Type': 'application/x-www-form-urlencoded',
  295. 'Content-Length': Buffer.byteLength(post_data)
  296. }
  297. };
  298. // Set up the request
  299. var post_req = http.request(post_options, (function(post_res) {
  300. post_res.setEncoding('utf8');
  301. post_res.on('data', (function (body) {
  302. switch (post_res.statusCode){
  303. case 200:
  304. this.log('success', 'TOA run successfully uploaded.');
  305. break;
  306. case 201:
  307. this.log('success', 'TOA run successfully uploaded.');
  308. break;
  309. case 400:
  310. this.log('error', `There were errors importing the run: ${JSON.stringify(body)}`);
  311. break;
  312. case 404:
  313. this.log('error', 'URL not found.');
  314. break;
  315. case 500:
  316. this.log('error', 'The server returned an error.');
  317. break;
  318. default:
  319. this.log('error', `Unexpected error importing run: ${post_res.statusCode}`);
  320. }
  321. }).bind(this));
  322. }).bind(this));
  323. // Perform the request
  324. post_req.write(post_data);
  325. post_req.end();
  326. },
  327. /**
  328. * Makes a call to the upload_run_scenario API to register a scenario run.
  329. *
  330. * @param req The full request data.
  331. * @param req The full response data.
  332. */
  333. uploadRunScenario(req, res){
  334. this.log('info', 'Uploading scenario run to SWDB...')
  335. this.request.post(
  336. 'upload_run_scenario',
  337. {json: {
  338. key: config.Config.Plugins[this.pluginName].apiKey,
  339. request: req,
  340. response: res
  341. } },
  342. //req_options,
  343. (error, response, body) => {
  344. if (error) {
  345. this.log('error', `Error uplading scenario run: ${error.message}`);
  346. return;
  347. }
  348. switch (response.statusCode){
  349. case 200:
  350. this.log('success', 'Scenario run successfully uploaded.');
  351. break;
  352. case 201:
  353. this.log('success', 'Scenario run successfully uploaded.');
  354. break;
  355. case 400:
  356. this.log('error', `There were errors importing the data: ${JSON.stringify(body)}`);
  357. break;
  358. case 404:
  359. this.log('error', 'URL not found.');
  360. break;
  361. case 500:
  362. this.log('error', 'The server returned an error.');
  363. break;
  364. default:
  365. this.log('error', `Unexpected error importing profile: ${response.statusCode}`);
  366. }
  367. }
  368. );
  369. },
  370. /**
  371. * Prints a message to the SWEX output and to the console.
  372. *
  373. * @param type Log type. Debug type will be printed depending on config.
  374. * @param msg The message to log.
  375. */
  376. log(type, msg){
  377. //if (type != "debug" || this.config.Config.Plugins[this.pluginName].debug == true){
  378. this.proxy.log({ type: type, source: 'plugin', name: this.pluginName, message: msg });
  379. pad_type = type.substring(0, 5).padEnd(5, ' ');
  380. pad_plugin = this.pluginName.substring(0, 4).padEnd(4, ' ');
  381. console.log(`[${pad_plugin}][${pad_type}] ${msg}`);
  382. //}
  383. }
  384. };