index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 gPot = null, isSlaying = false, inUpdate = false;
  6. mod.game.initialize(['me', 'contract']);
  7. cmd.add(['autopot', 'pot'], (arg1, arg2) => {
  8. if(arg1 && arg1.length > 0) arg1 = arg1.toLowerCase();
  9. if(arg2 && arg2.length > 0) arg2 = arg2.toLowerCase();
  10. switch (arg1) {
  11. case 'id':
  12. case 'getid':
  13. case 'itemid':
  14. let getId = arg2.match(/#(\d*)@/);
  15. getId = getId ? Number(getId[1]) : 0;
  16. msg(`itemId: ${getId}.`);
  17. break;
  18. case 'notice':
  19. config.notice = !config.notice;
  20. msg(`Notice has ${config.notice ? 'Enable' : 'Disable'}.`);
  21. break;
  22. case 're':
  23. case 'load':
  24. case 'reload':
  25. switch (arg2) {
  26. case 'hp':
  27. hpPot = getHP();
  28. mod.toServer('C_SHOW_INVEN', 1, {unk: 1});
  29. msg(`HP.json has been reloaded.`);
  30. break;
  31. case 'mp':
  32. mpPot = getMP();
  33. mod.toServer('C_SHOW_INVEN', 1, {unk: 1});
  34. msg(`MP.json has been reloaded.`);
  35. break;
  36. case 'config':
  37. config = getConfig();
  38. msg(`Config.json has been reloaded.`);
  39. break;
  40. }
  41. break;
  42. case 'slay':
  43. case 'slaying':
  44. isSlaying = !isSlaying;
  45. msg(`Slaying mode has ${isSlaying ? 'Enable' : 'Disable'}. ${config.hp ? '' : 'HP pot has Enable.'}`);
  46. if (!config.hp) config.hp = true;
  47. break;
  48. case 'hp':
  49. config.hp = !config.hp;
  50. msg(`HP pot has ${config.hp ? 'Enable' : 'Disable'}. ${isSlaying ? 'Slaying mode has Disable.' : ''}`);
  51. if (isSlaying) isSlaying = false;
  52. break;
  53. case 'mp':
  54. case 'mana':
  55. config.mp = !config.mp;
  56. msg(`MP pot has ${config.mp ? 'Enable' : 'Disable'}.`);
  57. break;
  58. case 'option':
  59. case 'status':
  60. case 'debug':
  61. case 'check':
  62. let data = '\n===== Option =====\n';
  63. data += `Module: ${config.enabled}\n`;
  64. data += `HP: ${config.hp}\n`;
  65. data += `MP: ${config.mp}\n`;
  66. data += `Slaying: ${isSlaying}\n`;
  67. data += `Notice: ${config.notice}\n`;
  68. data += '===== Status =====\n';
  69. data += `inGame: ${mod.game.isIngame}\n`;
  70. data += `inLoading: ${mod.game.isInLoadingScreen}\n`;
  71. data += `isAlive: ${mod.game.me.alive}\n`;
  72. data += `onMount: ${mod.game.me.mounted}\n`;
  73. data += `inCombat: ${mod.game.me.inCombat}\n`;
  74. data += `inContract: ${mod.game.me.active}\n`;
  75. data += `inBattleground: ${mod.game.me.inBattleground}\n`;
  76. data += `inCivilUnrest: ${mod.game.me.zone === 152}\n`;
  77. data += '===== HP Potion =====\n';
  78. for (let hp = 0; hp < hpPot.length; hp++)
  79. if (hpPot[hp][1].amount > 0)
  80. data += `[${hp}] ${hpPot[hp][1].name} - ${hpPot[hp][1].amount.toLocaleString()}\n`;
  81. data += '===== MP Potion =====\n';
  82. for (let mp = 0; mp < mpPot.length; mp++)
  83. if (mpPot[mp][1].amount > 0)
  84. data += `[${mp}] ${mpPot[mp][1].name} - ${mpPot[mp][1].amount.toLocaleString()}\n`;
  85. msg(data);
  86. data = '';
  87. break;
  88. default:
  89. msg(`Wrong command :v`);
  90. break;
  91. }
  92. });
  93. mod.hook('S_INVEN', 17, e => {
  94. if (!inUpdate) {
  95. inUpdate = true;
  96. for(let hp = 0; hp < hpPot.length; hp++) {
  97. gPot = e.items.filter(item => item.id === s2n(hpPot[hp][0]));
  98. if (gPot.length > 0) hpPot[hp][1].amount = gPot.reduce(function (a, b) {return a + b.amount;}, 0);
  99. }
  100. for(let mp = 0; mp < mpPot.length; mp++) {
  101. gPot = e.items.filter(item => item.id === s2n(mpPot[mp][0]));
  102. if (gPot.length > 0) mpPot[mp][1].amount = gPot.reduce(function (a, b) {return a + b.amount;}, 0);
  103. }
  104. inUpdate = false;
  105. }
  106. });
  107. mod.hook('S_PLAYER_STAT_UPDATE', 10, e => {
  108. if (config.enabled) {
  109. useHP(Math.round(s2n(e.hp) / s2n(e.maxHp) * 100));
  110. useMP(Math.round(s2n(e.mp) / s2n(e.maxMp) * 100));
  111. }
  112. });
  113. mod.hook('S_RETURN_TO_LOBBY', 'raw', () => {
  114. inUpdate = false;
  115. for (let hp = 0; hp < hpPot.length; hp++)
  116. hpPot[hp][1].amount = 0;
  117. for (let mp = 0; mp < mpPot.length; mp++)
  118. mpPot[mp][1].amount = 0;
  119. });
  120. function useHP(nowHP) {
  121. if (config.hp && (mod.game.isIngame && !mod.game.isInLoadingScreen && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active)) {
  122. for (let hp = 0; hp < hpPot.length; hp++) {
  123. if (!hpPot[hp][1].inCd && hpPot[hp][1].amount > 0 && ((!isSlaying && nowHP <= hpPot[hp][1].use_at && (hpPot[hp][1].inCombat ? mod.game.me.inCombat : true)) || (isSlaying && nowHP <= hpPot[hp][1].slay_at && mod.game.me.inCombat)) && (hpPot[hp][1].inBattleground ? (mod.game.me.inBattleground || mod.game.me.zone === 152) : !mod.game.me.inBattleground)) {
  124. useItem(hpPot[hp]); hpPot[hp][1].inCd = true; hpPot[hp][1].amount--; setTimeout(function () {hpPot[hp][1].inCd = false;}, hpPot[hp][1].cd * 1000);
  125. if (config.notice) msg(`Used ${hpPot[hp][1].name}, ${(hpPot[hp][1].amount.toLocaleString())} left.`);
  126. }
  127. }
  128. }
  129. }
  130. function useMP(nowMP) {
  131. if (config.mp && (mod.game.isIngame && !mod.game.isInLoadingScreen && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active)) {
  132. for (let mp = 0; mp < mpPot.length; mp++) {
  133. if (!mpPot[mp][1].inCd && mpPot[mp][1].amount > 0 && nowMP <= mpPot[mp][1].use_at && (mpPot[mp][1].inCombat ? mod.game.me.inCombat : true) && (mpPot[mp][1].inBattleground ? (mod.game.me.inBattleground || mod.game.me.zone === 152) : !mod.game.me.inBattleground)) {
  134. useItem(mpPot[mp]); mpPot[mp][1].inCd = true; mpPot[mp][1].amount--; setTimeout(function () {mpPot[mp][1].inCd = false;}, mpPot[mp][1].cd * 1000);
  135. if (config.notice) msg(`Used ${mpPot[mp][1].name}, ${(mpPot[mp][1].amount.toLocaleString())} left.`);
  136. }
  137. }
  138. }
  139. }
  140. function useItem(itemId) {
  141. mod.send('C_USE_ITEM', 3, {
  142. gameId: mod.game.me.gameId,
  143. id: s2n(itemId[0]),
  144. amount: 1,
  145. unk4: true
  146. });
  147. }
  148. function getConfig() {
  149. let data = {};
  150. try {
  151. data = jsonRequire('./config.json');
  152. } catch (e) {
  153. data = {
  154. enabled: true,
  155. hp: false,
  156. mp: true,
  157. notice: false
  158. }
  159. jsonSave('config.json', data);
  160. }
  161. return data;
  162. }
  163. function getHP() {
  164. let data = {};
  165. try {
  166. data = jsonRequire('./hp.json');
  167. } catch (e) {
  168. data[6552] = {
  169. name: 'Prime Recovery Potable',
  170. inBattleground: false,
  171. inCombat: true,
  172. use_at: 80,
  173. slay_at: 30,
  174. cd: 10
  175. }
  176. jsonSave('hp.json', data);
  177. }
  178. return jsonSort(data, 'use_at');
  179. }
  180. function getMP() {
  181. let data = {};
  182. try {
  183. data = jsonRequire('./mp.json');
  184. } catch (e) {
  185. data[6562] = {
  186. name: 'Prime Replenishment Potable',
  187. inBattleground: false,
  188. inCombat: false,
  189. use_at: 50,
  190. cd: 10
  191. }
  192. jsonSave('mp.json', data);
  193. }
  194. return jsonSort(data, 'use_at');
  195. }
  196. function s2n(n) {
  197. return Number(n);
  198. }
  199. function msg(msg) {
  200. cmd.message(msg);
  201. }
  202. function jsonRequire(data) {
  203. delete require.cache[require.resolve(data)];
  204. return require(data);
  205. }
  206. function jsonSort(data, sortby){
  207. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  208. let s2a = []; for(let i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  209. return s2a;
  210. }
  211. function jsonSave(name, data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  212. }