index.js 11 KB

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