index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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(), aLoc = null, wLoc = 0;
  5. let gPot = null, invUpdate = false, TmpData = [], aRes = null;
  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 'info':
  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>${TFString(config.enabled)}`
  94. },
  95. {
  96. text: `<font color="#4DD0E1" size="+20">HP: </font>${TFString(config.hp)}`
  97. },
  98. {
  99. text: `<font color="#4DD0E1" size="+20">MP: </font>${TFString(config.mp)}`
  100. },
  101. {
  102. text: `<font color="#4DD0E1" size="+20">Slaying: </font>${TFString(config.slaying)}`
  103. },
  104. {
  105. text: `<font color="#4DD0E1" size="+20">Notice: </font>${TFString(config.notice)}`
  106. },
  107. {
  108. text: `<font color="#4DD0E1" size="+20">Delay After Resurrect: </font> <font color="#4DE19C" size="+20">${config.delayafterRes}</font>`
  109. },
  110. {
  111. text: `<br><font color="#4DD0E1" size="+24">===== Status =====</font><br>`
  112. },
  113. {
  114. text: `<font color="#4DD0E1" size="+20">Game: </font>${TFString(mod.game.isIngame)}`
  115. },
  116. {
  117. text: `<font color="#4DD0E1" size="+20">Loading: </font>${TFString(mod.game.isInLoadingScreen)}`
  118. },
  119. {
  120. text: `<font color="#4DD0E1" size="+20">Alive: </font>${TFString(mod.game.me.alive)}`
  121. },
  122. {
  123. text: `<font color="#4DD0E1" size="+20">Mount: </font>${TFString(mod.game.me.mounted)}`
  124. },
  125. {
  126. text: `<font color="#4DD0E1" size="+20">Combat: </font>${TFString(mod.game.me.inCombat)}`
  127. },
  128. {
  129. text: `<font color="#4DD0E1" size="+20">Contract: </font>${TFString(mod.game.contract.active)}`
  130. },
  131. {
  132. text: `<font color="#4DD0E1" size="+20">Battleground: </font>${TFString(mod.game.me.inBattleground)}`
  133. },
  134. {
  135. text: `<font color="#4DD0E1" size="+20">Civil Unrest: </font>${TFString(mod.game.me.zone === 152)}`
  136. });
  137. TmpData.push({
  138. text: `<br><font color="#4DD0E1" size="+24">===== HP Potion =====</font><br>`
  139. });
  140. for (let hp = 0; hp < hpPot.length; hp++)
  141. if (hpPot[hp][1].amount > 0)
  142. TmpData.push({
  143. text: `<font color="#4DD0E1" size="+20">[${hp + 1}] ${hpPot[hp][1].name} - ${hpPot[hp][1].amount.toLocaleString()}</font><br>`
  144. });
  145. TmpData.push({
  146. text: `<br><font color="#4DD0E1" size="+24">===== MP Potion =====</font><br>`
  147. });
  148. for (let mp = 0; mp < mpPot.length; mp++)
  149. if (mpPot[mp][1].amount > 0)
  150. TmpData.push({
  151. text: `<font color="#4DD0E1" size="+20">[${mp + 1}] ${mpPot[mp][1].name} - ${mpPot[mp][1].amount.toLocaleString()}</font><br>`
  152. });
  153. gui.parse(TmpData, `<font color="#E0B0FF">Auto Potion - Debug</font>`);
  154. TmpData = [];
  155. break;
  156. default:
  157. TmpData = [];
  158. TmpData.push({
  159. text: `<font color="#4DD0E1" size="+24">===== Option =====</font><br>`
  160. },
  161. {
  162. text: `<font color="${TFColor(config.hp)}" size="+20">- HP</font><br>`,
  163. command: `autopot hp;autopot`
  164. },
  165. {
  166. text: `<font color="${TFColor(config.mp)}" size="+20">- MP</font><br>`,
  167. command: `autopot mp;autopot`
  168. },
  169. {
  170. text: `<font color="${TFColor(config.slaying)}" size="+20">- Slaying Mode</font><br>`,
  171. command: `autopot slay;autopot`
  172. },
  173. {
  174. text: `<font color="${TFColor(config.notice)}" size="+20">- Notice</font><br>`,
  175. command: `autopot notice;autopot`
  176. },
  177. {
  178. text: `<br><font color="#4DD0E1" size="+24">===== Reload JSON ======</font><br>`
  179. },
  180. {
  181. text: `<font color="#FE6F5E" size="+20">- Config.json</font><br>`,
  182. command: `autopot reload config`
  183. },
  184. {
  185. text: `<font color="#FE6F5E" size="+20">- HP.json</font><br>`,
  186. command: `autopot reload hp`
  187. },
  188. {
  189. text: `<font color="#FE6F5E" size="+20">- MP.json</font><br>`,
  190. command: `autopot reload mp`
  191. });
  192. TmpData.push({
  193. text: `<br><font color="#4DD0E1" size="+24">===== HP Potion =====</font><br>`
  194. });
  195. for (let hp = 0; hp < hpPot.length; hp++)
  196. if (hpPot[hp][1].amount > 0)
  197. TmpData.push({
  198. text: `<font color="#4DD0E1" size="+20">[${hp + 1}] ${hpPot[hp][1].name} - ${hpPot[hp][1].amount.toLocaleString()}</font><br>`
  199. });
  200. TmpData.push({
  201. text: `<br><font color="#4DD0E1" size="+24">===== MP Potion =====</font><br>`
  202. });
  203. for (let mp = 0; mp < mpPot.length; mp++)
  204. if (mpPot[mp][1].amount > 0)
  205. TmpData.push({
  206. text: `<font color="#4DD0E1" size="+20">[${mp + 1}] ${mpPot[mp][1].name} - ${mpPot[mp][1].amount.toLocaleString()}</font><br>`
  207. });
  208. gui.parse(TmpData, `<font color="#E0B0FF">Auto Potion</font>`);
  209. TmpData = [];
  210. break;
  211. }
  212. });
  213. mod.hook('S_INVEN', 18, e => {
  214. if (!invUpdate) {
  215. invUpdate = true;
  216. for(let hp = 0; hp < hpPot.length; hp++) {
  217. gPot = e.items.filter(item => item.id === s2n(hpPot[hp][0]));
  218. if (gPot.length > 0) hpPot[hp][1].amount = gPot.reduce(function (a, b) {return a + b.amount;}, 0);
  219. }
  220. for(let mp = 0; mp < mpPot.length; mp++) {
  221. gPot = e.items.filter(item => item.id === s2n(mpPot[mp][0]));
  222. if (gPot.length > 0) mpPot[mp][1].amount = gPot.reduce(function (a, b) {return a + b.amount;}, 0);
  223. }
  224. invUpdate = false;
  225. }
  226. });
  227. mod.hook('S_PLAYER_STAT_UPDATE', 12, e => {
  228. if (config.enabled) {
  229. useHP(Math.round(s2n(e.hp) / s2n(e.maxHp) * 100));
  230. useMP(Math.round(s2n(e.mp) / s2n(e.maxMp) * 100));
  231. }
  232. });
  233. mod.hook('S_RETURN_TO_LOBBY', 'raw', () => {
  234. for (let hp = 0; hp < hpPot.length; hp++)
  235. hpPot[hp][1].amount = 0;
  236. for (let mp = 0; mp < mpPot.length; mp++)
  237. mpPot[mp][1].amount = 0;
  238. if (aRes) clearTimeout(aRes);
  239. invUpdate = false;
  240. aRes = null;
  241. });
  242. mod.hook('C_PLAYER_LOCATION', 5, e => {
  243. aLoc = e.loc;
  244. wLoc = e.w;
  245. });
  246. mod.hook('S_SPAWN_ME', 3, e => {
  247. aLoc = e.loc;
  248. wLoc = e.w;
  249. });
  250. mod.game.me.on('resurrect', () => {
  251. if (aRes) clearTimeout(aRes);
  252. aRes = setTimeout(() => {aRes = null;}, config.delayafterRes);
  253. });
  254. function useHP(nowHP) {
  255. if (config.hp && (mod.game.isIngame && !mod.game.isInLoadingScreen && !aRes && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active)) {
  256. for (let hp = 0; hp < hpPot.length; hp++) {
  257. 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)) {
  258. useItem(hpPot[hp]); hpPot[hp][1].inCd = true; hpPot[hp][1].amount--; setTimeout(function () {hpPot[hp][1].inCd = false;}, hpPot[hp][1].cd * 1000);
  259. if (config.notice) msg(`Used ${hpPot[hp][1].name}, ${(hpPot[hp][1].amount.toLocaleString())} left.`);
  260. }
  261. }
  262. }
  263. }
  264. function useMP(nowMP) {
  265. if (config.mp && (mod.game.isIngame && !mod.game.isInLoadingScreen && !aRes && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active)) {
  266. for (let mp = 0; mp < mpPot.length; mp++) {
  267. 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)) {
  268. useItem(mpPot[mp]); mpPot[mp][1].inCd = true; mpPot[mp][1].amount--; setTimeout(function () {mpPot[mp][1].inCd = false;}, mpPot[mp][1].cd * 1000);
  269. if (config.notice) msg(`Used ${mpPot[mp][1].name}, ${(mpPot[mp][1].amount.toLocaleString())} left.`);
  270. }
  271. }
  272. }
  273. }
  274. function useItem(itemId) {
  275. mod.send('C_USE_ITEM', 3, {
  276. gameId: mod.game.me.gameId,
  277. id: s2n(itemId[0]),
  278. dbid: 0,
  279. target: 0,
  280. amount: 1,
  281. dest: {x: 0, y: 0, z: 0},
  282. loc: aLoc,
  283. w: wLoc,
  284. unk1: 0,
  285. unk2: 0,
  286. unk3: 0,
  287. unk4: true
  288. });
  289. }
  290. function getConfig() {
  291. let data = {};
  292. try {
  293. data = jsonRequire('./config.json');
  294. //--- Update Config !!START!! ---//
  295. if (!data.delayafterRes || !data.slaying) {
  296. data = {
  297. enabled: data.enabled || true,
  298. hp: data.hp || false,
  299. mp: data.mp || true,
  300. slaying: data.slaying || false,
  301. notice: data.notice || false,
  302. delayafterRes: data.delayafterRes ||2000
  303. }
  304. jsonSave('config.json', data);
  305. }
  306. //--- Update Config !!END!! ---//
  307. } catch (e) {
  308. data = {
  309. enabled: true,
  310. hp: false,
  311. mp: true,
  312. slaying: false,
  313. notice: false,
  314. delayafterRes: 2000
  315. }
  316. jsonSave('config.json', data);
  317. }
  318. return data;
  319. }
  320. function getHP() {
  321. let data = {};
  322. try {
  323. data = jsonRequire('./hp.json');
  324. } catch (e) {
  325. data[6552] = {
  326. name: 'Prime Recovery Potable',
  327. inBattleground: false,
  328. inCombat: true,
  329. use_at: 80,
  330. slay_at: 30,
  331. cd: 10
  332. }
  333. jsonSave('hp.json', data);
  334. }
  335. return jsonSort(data, 'use_at');
  336. }
  337. function getMP() {
  338. let data = {};
  339. try {
  340. data = jsonRequire('./mp.json');
  341. } catch (e) {
  342. data[6562] = {
  343. name: 'Prime Replenishment Potable',
  344. inBattleground: false,
  345. inCombat: false,
  346. use_at: 50,
  347. cd: 10
  348. }
  349. jsonSave('mp.json', data);
  350. }
  351. return jsonSort(data, 'use_at');
  352. }
  353. function s2n(n) {
  354. return Number(n);
  355. }
  356. function msg(msg) {
  357. cmd.message(msg);
  358. }
  359. function jsonRequire(data) {
  360. delete require.cache[require.resolve(data)];
  361. return require(data);
  362. }
  363. function jsonSort(data, sortby){
  364. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  365. let s2a = []; for(let i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  366. return s2a;
  367. }
  368. function jsonSave(name, data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  369. function TFString(e) {return `<font color="${TFColor(e)}" size="+20">${(e ? 'True' : 'False')}</font><br>`;}
  370. function TFColor(e) {return e ? '#4DE19C' : '#FE6F5E';}
  371. }