index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. const path = require('path'); const fs = require('fs');
  2. module.exports = function AutoPOT(mod) {
  3. const cmd = mod.command || mod.require.command;
  4. let config = getConfig(), hpPot = getHP(), mpPot = getMP();
  5. let gameId = null, VehicleEx = null, getInv = false;
  6. let isAlive = false, isCombat = false, isBG = false, isSlaying = false;
  7. let zoneBG = 0, nowHP = 0, nowMP = 0, isCon = false, isMount = false;
  8. cmd.add(['autopot', 'pot'], (arg1, arg2) => {
  9. if(arg1 && arg1.length > 0) arg1 = arg1.toLowerCase();
  10. if(arg2 && arg2.length > 0) arg2 = arg2.toLowerCase();
  11. switch (arg1) {
  12. case 'id':
  13. case 'getid':
  14. case 'itemid':
  15. let getId = arg2.match(/#(\d*)@/);
  16. getId = getId ? Number(getId[1]) : 0;
  17. msg(`itemId: ${getId}.`);
  18. break;
  19. case 'load':
  20. case 'reload':
  21. switch(arg2) {
  22. case 'hp': hpPot = getHP(); msg(`HP.json has been reloaded.`); break;
  23. case 'mp': mpPot = getMP(); msg(`MP.json has been reloaded.`); break;
  24. case 'config': config = getConfig(); msg(`Config.json has been reloaded.`); break;
  25. }
  26. break;
  27. case 'notice':
  28. config.notice = !config.notice;
  29. msg(`Notice has ${config.notice ? 'Enable' : 'Disable'}.`);
  30. break;
  31. case 'slay':
  32. case 'slaying':
  33. isSlaying = !isSlaying;
  34. if (!config.hp) config.hp = true;
  35. msg(`Slaying mode has ${isSlaying ? 'Enable' : 'Disable'}.`);
  36. break;
  37. case 'hp':
  38. config.hp = !config.hp;
  39. if (isSlaying) isSlaying = false;
  40. msg(`HP pot has ${config.hp ? 'Enable' : 'Disable'}.`);
  41. break;
  42. case 'mp':
  43. case 'mana':
  44. config.mp = !config.mp;
  45. msg(`MP pot has ${config.mp ? 'Enable' : 'Disable'}.`);
  46. break;
  47. default:
  48. msg(`Wrong command :v`);
  49. break;
  50. }
  51. });
  52. mod.hook('S_MOUNT_VEHICLE', 2, e => {if (isMe(e.gameId)) isMount = true;});
  53. mod.hook('S_UNMOUNT_VEHICLE', 2, e => {if (isMe(e.gameId)) {isMount = false; isCon = false;}});
  54. mod.hook('S_REQUEST_CONTRACT', 'raw', () => {isCon = true;});
  55. mod.hook('S_ACCEPT_CONTRACT', 'raw', () => {isCon = false;});
  56. mod.hook('S_REJECT_CONTRACT', 'raw', () => {isCon = false;});
  57. mod.hook('S_CANCEL_CONTRACT', 'raw', () => {isCon = false;});
  58. mod.hook('S_GACHA_END', 'raw', () => {isCon = false;});
  59. mod.hook('C_BIND_ITEM_EXECUTE', 'raw', () => {isCon = false;});
  60. mod.hook('S_LOGIN', 10, e => {({gameId} = e);});
  61. mod.hook('S_SPAWN_ME', 3, e => {isAlive = e.alive;});
  62. mod.hook('S_CREATURE_LIFE', 2, e => {if (isMe(e.gameId)) {isAlive = e.alive;}});
  63. mod.hook('S_MOUNT_VEHICLE_EX', 1, e => {if (e.target.equals(gameId)) VehicleEx = e.vehicle;});
  64. mod.hook('S_UNMOUNT_VEHICLE_EX', 1, e => {if (e.target.equals(gameId)) VehicleEx = null;});
  65. mod.hook('S_USER_STATUS', 1, e => {if (e.target.equals(gameId)) isCombat = (e.status === 1);});
  66. mod.hook('S_BATTLE_FIELD_ENTRANCE_INFO', 1, e => {zoneBG = e.zone;});
  67. mod.hook('S_LOAD_TOPO', 3, e => {isBG = (e.zone === zoneBG); VehicleEx = null; isCon = false; isMount = false;});
  68. mod.hook('S_INVEN', 16, e => {
  69. if (config.enabled) {
  70. let gHP = null, gMP = null;
  71. for(i = 0; i < hpPot.length; i++) {
  72. gHP = e.items.find(item => item.id === Number(hpPot[i][0]));
  73. if (gHP) hpPot[i][1].amount = gHP.amount;
  74. }
  75. for (i = 0; i < mpPot.length; i++) {
  76. gMP = e.items.find(item => item.id === Number(mpPot[i][0]));
  77. if (gMP) mpPot[i][1].amount = gMP.amount;
  78. }
  79. }
  80. });
  81. mod.hook('S_PLAYER_STAT_UPDATE', 10, e => {
  82. if (config.enabled && config.hp) {
  83. nowHP = Math.round(parseInt(e.hp) / parseInt(e.maxHp) * 100);
  84. for (let hp = 0; hp < hpPot.length; hp++) {
  85. if (!hpPot[hp][1].inCd && ((!isSlaying && nowHP <= hpPot[hp][1].use_at) || (isSlaying && nowHP <= hpPot[hp][1].slaying)) && hpPot[hp][1].amount > 0 && isCombat && isAlive && !isBG) {
  86. useItem(hpPot[hp]); hpPot[hp][1].inCd = true; hpPot[hp][1].amount--; setTimeout(function () {hpPot[hp][1].inCd = false;}, hpPot[hp][1].cd * 1000);
  87. if (config.notice) msg(`Used ${hpPot[hp][1].name}, still have ${(hpPot[hp][1].amount)} left.`);
  88. }
  89. }
  90. }
  91. if (config.enabled && config.mp) {
  92. nowMP = Math.round(parseInt(e.mp) / parseInt(e.maxMp) * 100);
  93. for (let mp = 0; mp < mpPot.length; mp++) {
  94. if (!mpPot[mp][1].inCd && nowMP <= mpPot[mp][1].use_at && mpPot[mp][1].amount > 0 && isAlive && !isBG && !isCon && !isMount) {
  95. useItem(mpPot[mp]); mpPot[mp][1].inCd = true; mpPot[mp][1].amount--; setTimeout(function () {mpPot[mp][1].inCd = false;}, mpPot[mp][1].cd * 1000);
  96. if (config.notice) msg(`Used ${mpPot[mp][1].name}, still have ${(mpPot[mp][1].amount)} left.`);
  97. }
  98. }
  99. }
  100. });
  101. function useItem(itemId) {
  102. mod.send('C_USE_ITEM', 3, {
  103. gameId: gameId,
  104. id: Number(itemId[0]),
  105. amount: 1,
  106. unk4: true
  107. });
  108. }
  109. function getConfig() {
  110. let data = {};
  111. try {
  112. data = require('./config.json');
  113. } catch (e) {
  114. data = {
  115. enabled: true,
  116. hp: false,
  117. mp: true,
  118. notice: false
  119. }
  120. jsonSave('config.json', data);
  121. }
  122. return data;
  123. }
  124. function getHP() {
  125. let data = {};
  126. try {
  127. data = require('./hp.json');
  128. } catch (e) {
  129. data[6552] = {
  130. name: 'Prime Recovery Potable',
  131. use_at: 80,
  132. slay_at: 30,
  133. cd: 10
  134. }
  135. jsonSave('hp.json', data);
  136. }
  137. return jsonSort(data, 'use_at');
  138. }
  139. function getMP() {
  140. let data = {};
  141. try {
  142. data = require('./mp.json');
  143. } catch (e) {
  144. data[6562] = {
  145. name: 'Prime Replenishment Potable',
  146. use_at: 50,
  147. cd: 10
  148. }
  149. jsonSave('mp.json', data);
  150. }
  151. return jsonSort(data, 'use_at');
  152. }
  153. function jsonSort(data, sortby){
  154. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  155. let s2a = []; for(i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  156. return s2a;
  157. }
  158. function msg(msg) {cmd.message(msg);}
  159. function isMe(id) {return gameId.equals(id) || VehicleEx && VehicleEx.equals(id);}
  160. function jsonSave(name,data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  161. }