index.js 11 KB

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