index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. const path = require('path'); const fs = require('fs');
  2. module.exports = function AutoPOT(mod) {
  3. const cmd = mod.command || mod.require.command, map = new WeakMap();
  4. let config = getConfig(), hpPot = getHP(), mpPot = getMP();
  5. let gPot = null, inUpdate = false, TmpData = [];
  6. mod.game.initialize(['me', 'contract']);
  7. if (!map.has(mod.dispatch || mod)) {
  8. map.set(mod.dispatch || mod, {});
  9. mod.hook('C_CONFIRM_UPDATE_NOTIFICATION', 'raw', () => false);
  10. mod.hook('C_ADMIN', 1, e => {
  11. e.command.split(";").forEach(s => mod.command.exec(s));
  12. return false;
  13. });
  14. }
  15. const gui = {
  16. parse(array, title, d = '') {
  17. for (let i = 0; i < array.length; i++) {
  18. if (d.length >= 16000) {
  19. d += `Gui data limit exceeded, some values may be missing.`;
  20. break;
  21. }
  22. if (array[i].command) d += `<a href="admincommand:/@${array[i].command}">${array[i].text}</a>`;
  23. else if (!array[i].command) d += `${array[i].text}`;
  24. else continue;
  25. }
  26. mod.toClient('S_ANNOUNCE_UPDATE_NOTIFICATION', 1, {
  27. id: 0,
  28. title: title,
  29. body: d
  30. });
  31. }
  32. }
  33. cmd.add(['autopot', 'pot'], (arg1, arg2) => {
  34. if(arg1 && arg1.length > 0) arg1 = arg1.toLowerCase();
  35. if(arg2 && arg2.length > 0) arg2 = arg2.toLowerCase();
  36. switch (arg1) {
  37. case 'id':
  38. case 'getid':
  39. case 'itemid':
  40. arg2 = arg2 ? arg2.match(/#(\d*)@/) : 0;
  41. msg(`itemId: ${(arg2 ? Number(arg2[1]) : 0)}.`);
  42. break;
  43. case 'notice':
  44. config.notice = !config.notice;
  45. msg(`Notice has ${config.notice ? 'Enable' : 'Disable'}.`);
  46. break;
  47. case 're':
  48. case 'load':
  49. case 'reload':
  50. switch (arg2) {
  51. case 'hp':
  52. hpPot = getHP();
  53. mod.toServer('C_SHOW_INVEN', 1, {unk: 1});
  54. msg(`HP.json has been reloaded.`);
  55. break;
  56. case 'mp':
  57. mpPot = getMP();
  58. mod.toServer('C_SHOW_INVEN', 1, {unk: 1});
  59. msg(`MP.json has been reloaded.`);
  60. break;
  61. case 'config':
  62. config = getConfig();
  63. msg(`Config.json has been reloaded.`);
  64. break;
  65. }
  66. break;
  67. case 'slay':
  68. case 'slaying':
  69. config.slaying = !config.slaying;
  70. msg(`Slaying mode has ${config.slaying ? 'Enable' : 'Disable'}. ${config.hp ? '' : 'HP pot has Enable.'}`);
  71. if (!config.hp) config.hp = true;
  72. break;
  73. case 'hp':
  74. config.hp = !config.hp;
  75. msg(`HP pot has ${config.hp ? 'Enable' : 'Disable'}. ${config.slaying ? 'Slaying mode has Disable.' : ''}`);
  76. if (config.slaying) config.slaying = false;
  77. break;
  78. case 'mp':
  79. case 'mana':
  80. config.mp = !config.mp;
  81. msg(`MP pot has ${config.mp ? 'Enable' : 'Disable'}.`);
  82. break;
  83. case 'option':
  84. case 'status':
  85. case 'debug':
  86. case 'check':
  87. TmpData = [];
  88. TmpData.push({
  89. text: `<font color="#4DD0E1" size="+24">===== Option =====</font><br>`
  90. },
  91. {
  92. text: `<font color="#4DD0E1" size="+20">Module: </font><font color="${TFColor(config.enabled)}" size="+20">${TFString(config.enabled)}</font><br>`
  93. },
  94. {
  95. text: `<font color="#4DD0E1" size="+20">HP: </font><font color="${TFColor(config.hp)}" size="+20">${TFString(config.hp)}</font><br>`
  96. },
  97. {
  98. text: `<font color="#4DD0E1" size="+20">MP: </font><font color="${TFColor(config.mp)}" size="+20">${TFString(config.mp)}</font><br>`
  99. },
  100. {
  101. text: `<font color="#4DD0E1" size="+20">Slaying: </font><font color="${TFColor(config.slaying)}" size="+20">${TFString(config.slaying)}</font><br>`
  102. },
  103. {
  104. text: `<font color="#4DD0E1" size="+20">Notice: </font><font color="${TFColor(config.notice)}" size="+20">${TFString(config.notice)}</font><br>`
  105. },
  106. {
  107. text: `<br><font color="#4DD0E1" size="+24">===== Status =====</font><br>`
  108. },
  109. {
  110. text: `<font color="#4DD0E1" size="+20">Game: </font><font color="${TFColor(mod.game.isIngame)}" size="+20">${TFString(mod.game.isIngame)}</font><br>`
  111. },
  112. {
  113. text: `<font color="#4DD0E1" size="+20">Loading: </font><font color="${TFColor(mod.game.isInLoadingScreen)}" size="+20">${TFString(mod.game.isInLoadingScreen)}</font><br>`
  114. },
  115. {
  116. text: `<font color="#4DD0E1" size="+20">Alive: </font><font color="${TFColor(mod.game.me.alive)}" size="+20">${TFString(mod.game.me.alive)}</font><br>`
  117. },
  118. {
  119. text: `<font color="#4DD0E1" size="+20">Mount: </font><font color="${TFColor(mod.game.me.mounted)}" size="+20">${TFString(mod.game.me.mounted)}</font><br>`
  120. },
  121. {
  122. text: `<font color="#4DD0E1" size="+20">Combat: </font><font color="${TFColor(mod.game.me.inCombat)}" size="+20">${TFString(mod.game.me.inCombat)}</font><br>`
  123. },
  124. {
  125. text: `<font color="#4DD0E1" size="+20">Contract: </font><font color="${TFColor(mod.game.contract.active)}" size="+20">${TFString(mod.game.contract.active)}</font><br>`
  126. },
  127. {
  128. text: `<font color="#4DD0E1" size="+20">Battleground: </font><font color="${TFColor(mod.game.me.inBattleground)}" size="+20">${TFString(mod.game.me.inBattleground)}</font><br>`
  129. },
  130. {
  131. text: `<font color="#4DD0E1" size="+20">CivilUnrest: </font><font color="${TFColor(mod.game.me.zone === 152)}" size="+20">${TFString(mod.game.me.zone === 152)}</font><br>`
  132. });
  133. TmpData.push({
  134. text: `<br><font color="#4DD0E1" size="+24">===== HP Potion =====</font><br>`
  135. });
  136. for (let hp = 0; hp < hpPot.length; hp++)
  137. if (hpPot[hp][1].amount > 0)
  138. TmpData.push({
  139. text: `<font color="#4DD0E1" size="+20">[${hp + 1}] ${hpPot[hp][1].name} - ${hpPot[hp][1].amount.toLocaleString()}</font><br>`
  140. });
  141. TmpData.push({
  142. text: `<br><font color="#4DD0E1" size="+24">===== MP Potion =====</font><br>`
  143. });
  144. for (let mp = 0; mp < mpPot.length; mp++)
  145. if (mpPot[mp][1].amount > 0)
  146. TmpData.push({
  147. text: `<font color="#4DD0E1" size="+20">[${mp + 1}] ${mpPot[mp][1].name} - ${mpPot[mp][1].amount.toLocaleString()}</font><br>`
  148. });
  149. gui.parse(TmpData, `<font color="#E0B0FF">Auto Potion - Debug</font>`);
  150. TmpData = [];
  151. break;
  152. default:
  153. TmpData = [];
  154. TmpData.push({
  155. text: `<font color="#4DD0E1" size="+24">===== Option =====</font><br>`
  156. },
  157. {
  158. text: `<font color="${TFColor(config.hp)}" size="+20">- HP</font><br>`,
  159. command: `autopot hp;autopot`
  160. },
  161. {
  162. text: `<font color="${TFColor(config.mp)}" size="+20">- MP</font><br>`,
  163. command: `autopot mp;autopot`
  164. },
  165. {
  166. text: `<font color="${TFColor(config.slaying)}" size="+20">- Slaying Mode</font><br>`,
  167. command: `autopot slay;autopot`
  168. },
  169. {
  170. text: `<font color="${TFColor(config.notice)}" size="+20">- Notice</font><br>`,
  171. command: `autopot notice;autopot`
  172. },
  173. {
  174. text: `<br><font color="#4DD0E1" size="+24">===== Reload JSON ======</font><br>`
  175. },
  176. {
  177. text: `<font color="#FE6F5E" size="+20">- Config.json</font><br>`,
  178. command: `autopot reload config;autopot`
  179. },
  180. {
  181. text: `<font color="#FE6F5E" size="+20">- HP.json</font><br>`,
  182. command: `autopot reload hp;autopot`
  183. },
  184. {
  185. text: `<font color="#FE6F5E" size="+20">- MP.json</font><br>`,
  186. command: `autopot reload mp;autopot`
  187. });
  188. gui.parse(TmpData, `<font color="#E0B0FF">Auto Potion</font>`);
  189. TmpData = [];
  190. break;
  191. }
  192. });
  193. mod.hook('S_INVEN', mod.majorPatchVersion > 79 ? 18 : 17, e => {
  194. if (!inUpdate) {
  195. inUpdate = true;
  196. for(let hp = 0; hp < hpPot.length; hp++) {
  197. gPot = e.items.filter(item => item.id === s2n(hpPot[hp][0]));
  198. if (gPot.length > 0) hpPot[hp][1].amount = gPot.reduce(function (a, b) {return a + b.amount;}, 0);
  199. }
  200. for(let mp = 0; mp < mpPot.length; mp++) {
  201. gPot = e.items.filter(item => item.id === s2n(mpPot[mp][0]));
  202. if (gPot.length > 0) mpPot[mp][1].amount = gPot.reduce(function (a, b) {return a + b.amount;}, 0);
  203. }
  204. inUpdate = false;
  205. }
  206. });
  207. mod.hook('S_PLAYER_STAT_UPDATE', 10, e => {
  208. if (config.enabled) {
  209. useHP(Math.round(s2n(e.hp) / s2n(e.maxHp) * 100));
  210. useMP(Math.round(s2n(e.mp) / s2n(e.maxMp) * 100));
  211. }
  212. });
  213. mod.hook('S_RETURN_TO_LOBBY', 'raw', () => {
  214. inUpdate = false;
  215. for (let hp = 0; hp < hpPot.length; hp++)
  216. hpPot[hp][1].amount = 0;
  217. for (let mp = 0; mp < mpPot.length; mp++)
  218. mpPot[mp][1].amount = 0;
  219. });
  220. function useHP(nowHP) {
  221. if (config.hp && (mod.game.isIngame && !mod.game.isInLoadingScreen && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active)) {
  222. for (let hp = 0; hp < hpPot.length; hp++) {
  223. if (!hpPot[hp][1].inCd && hpPot[hp][1].amount > 0 && ((!config.slaying && nowHP <= hpPot[hp][1].use_at && (hpPot[hp][1].inCombat ? mod.game.me.inCombat : true)) || (config.slaying && 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)) {
  224. useItem(hpPot[hp]); hpPot[hp][1].inCd = true; hpPot[hp][1].amount--; setTimeout(function () {hpPot[hp][1].inCd = false;}, hpPot[hp][1].cd * 1000);
  225. if (config.notice) msg(`Used ${hpPot[hp][1].name}, ${(hpPot[hp][1].amount.toLocaleString())} left.`);
  226. }
  227. }
  228. }
  229. }
  230. function useMP(nowMP) {
  231. if (config.mp && (mod.game.isIngame && !mod.game.isInLoadingScreen && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active)) {
  232. for (let mp = 0; mp < mpPot.length; mp++) {
  233. 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)) {
  234. useItem(mpPot[mp]); mpPot[mp][1].inCd = true; mpPot[mp][1].amount--; setTimeout(function () {mpPot[mp][1].inCd = false;}, mpPot[mp][1].cd * 1000);
  235. if (config.notice) msg(`Used ${mpPot[mp][1].name}, ${(mpPot[mp][1].amount.toLocaleString())} left.`);
  236. }
  237. }
  238. }
  239. }
  240. function useItem(itemId) {
  241. mod.send('C_USE_ITEM', 3, {
  242. gameId: mod.game.me.gameId,
  243. id: s2n(itemId[0]),
  244. amount: 1,
  245. unk4: true
  246. });
  247. }
  248. function getConfig() {
  249. let data = {};
  250. try {
  251. data = jsonRequire('./config.json');
  252. } catch (e) {
  253. data = {
  254. enabled: true,
  255. hp: false,
  256. mp: true,
  257. slaying: false,
  258. notice: false
  259. }
  260. jsonSave('config.json', data);
  261. }
  262. return data;
  263. }
  264. function getHP() {
  265. let data = {};
  266. try {
  267. data = jsonRequire('./hp.json');
  268. } catch (e) {
  269. data[6552] = {
  270. name: 'Prime Recovery Potable',
  271. inBattleground: false,
  272. inCombat: true,
  273. use_at: 80,
  274. slay_at: 30,
  275. cd: 10
  276. }
  277. jsonSave('hp.json', data);
  278. }
  279. return jsonSort(data, 'use_at');
  280. }
  281. function getMP() {
  282. let data = {};
  283. try {
  284. data = jsonRequire('./mp.json');
  285. } catch (e) {
  286. data[6562] = {
  287. name: 'Prime Replenishment Potable',
  288. inBattleground: false,
  289. inCombat: false,
  290. use_at: 50,
  291. cd: 10
  292. }
  293. jsonSave('mp.json', data);
  294. }
  295. return jsonSort(data, 'use_at');
  296. }
  297. function s2n(n) {
  298. return Number(n);
  299. }
  300. function msg(msg) {
  301. cmd.message(msg);
  302. }
  303. function jsonRequire(data) {
  304. delete require.cache[require.resolve(data)];
  305. return require(data);
  306. }
  307. function jsonSort(data, sortby){
  308. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  309. let s2a = []; for(let i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  310. return s2a;
  311. }
  312. function jsonSave(name, data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  313. function TFColor(e) {return e ? '#4DE19C' : '#FE6F5E';}
  314. function TFString(e) {return e ? 'True' : 'False';}
  315. }