index.js 12 KB

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