index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const path = require('path'); const fs = require('fs');
  2. module.exports = function AutoPOT(mod) {
  3. const cmd = mod.command || mod.require.command;
  4. let config = getConfig(), hpPot = getHP(), mpPot = getMP();
  5. let isReady = false, isSlaying = false, nowHP = 0, nowMP = 0;
  6. mod.game.initialize(['me', 'contract']);
  7. cmd.add(['autopot', 'pot'], (arg1, arg2) => {
  8. if(arg1 && arg1.length > 0) arg1 = arg1.toLowerCase();
  9. if(arg2 && arg2.length > 0) arg2 = arg2.toLowerCase();
  10. switch (arg1) {
  11. case 'id':
  12. case 'getid':
  13. case 'itemid':
  14. let getId = arg2.match(/#(\d*)@/);
  15. getId = getId ? Number(getId[1]) : 0;
  16. msg(`itemId: ${getId}.`);
  17. break;
  18. case 'notice':
  19. config.notice = !config.notice;
  20. msg(`Notice has ${config.notice ? 'Enable' : 'Disable'}.`);
  21. break;
  22. case 'slay':
  23. case 'slaying':
  24. isSlaying = !isSlaying;
  25. msg(`Slaying mode has ${isSlaying ? 'Enable' : 'Disable'}. ${config.hp ? '' : 'HP pot has Enable.'}`);
  26. if (!config.hp) config.hp = true;
  27. break;
  28. case 'hp':
  29. config.hp = !config.hp;
  30. msg(`HP pot has ${config.hp ? 'Enable' : 'Disable'}. ${isSlaying ? 'Slaying mode has Disable.' : ''}`);
  31. if (isSlaying) isSlaying = false;
  32. break;
  33. case 'mp':
  34. case 'mana':
  35. config.mp = !config.mp;
  36. msg(`MP pot has ${config.mp ? 'Enable' : 'Disable'}.`);
  37. break;
  38. default:
  39. msg(`Wrong command :v`);
  40. break;
  41. }
  42. });
  43. mod.hook('S_INVEN', 16, e => {
  44. if (config.enabled) {
  45. let hp = null, mp = null;
  46. for(let h = 0; h < hpPot.length; h++) {
  47. hp = e.items.find(item => item.id === s2n(hpPot[h][0]));
  48. if (hp) hpPot[h][1].amount = hp.amount;
  49. }
  50. for(let m = 0; m < mpPot.length; m++) {
  51. mp = e.items.find(item => item.id === s2n(mpPot[m][0]));
  52. if (mp) mpPot[m][1].amount = mp.amount;
  53. }
  54. }
  55. });
  56. mod.hook('S_START_COOLTIME_ITEM', 1, e => {
  57. if (config.enabled) {
  58. let hp = null, mp = null;
  59. hp = hpPot.findIndex(item => s2n(item[0]) === e.item);
  60. if (hp >= 0) {
  61. hpPot[hp][1].inCd = true;
  62. setTimeout(function (h) {
  63. hpPot[h][1].inCd = false;
  64. }, e.cooldown, hp);
  65. }
  66. mp = mpPot.findIndex(item => s2n(item[0]) === e.item);
  67. if (mp >= 0) {
  68. mpPot[mp][1].inCd = true;
  69. setTimeout(function (m) {
  70. mpPot[m][1].inCd = false;
  71. }, e.cooldown, mp);
  72. }
  73. }
  74. });
  75. mod.hook('S_PLAYER_STAT_UPDATE', 10, e => {
  76. if (config.enabled) {
  77. isReady = mod.game.isIngame && !mod.game.isInLoadingScreen && mod.game.me.alive && !mod.game.me.inBattleground && !mod.game.me.mounted && !mod.game.contract.active;
  78. if (config.hp && isReady) {
  79. nowHP = Math.round(s2n(e.hp) / s2n(e.maxHp) * 100);
  80. for (let hp = 0; hp < hpPot.length; hp++) {
  81. if (!hpPot[hp][1].inCd && ((!isSlaying && nowHP <= hpPot[hp][1].use_at && (hpPot[hp][1].inCombat ? mod.game.me.inCombat : true)) || (isSlaying && nowHP <= hpPot[hp][1].slay_at && mod.game.me.inCombat)) && hpPot[hp][1].amount > 0) {
  82. useItem(hpPot[hp]);
  83. hpPot[hp][1].amount--;
  84. if (config.notice)
  85. msg(`Used ${hpPot[hp][1].name}, ${(hpPot[hp][1].amount)} left.`);
  86. }
  87. }
  88. }
  89. if (config.mp && isReady) {
  90. nowMP = Math.round(s2n(e.mp) / s2n(e.maxMp) * 100);
  91. for (let mp = 0; mp < mpPot.length; mp++) {
  92. if (!mpPot[mp][1].inCd && nowMP <= mpPot[mp][1].use_at && mpPot[mp][1].amount > 0 && (mpPot[mp][1].inCombat ? mod.game.me.inCombat : true)) {
  93. useItem(mpPot[mp]);
  94. mpPot[mp][1].amount--;
  95. if (config.notice)
  96. msg(`Used ${mpPot[mp][1].name}, ${(mpPot[mp][1].amount)} left.`);
  97. }
  98. }
  99. }
  100. }
  101. });
  102. function s2n(n) {
  103. return Number(n);
  104. }
  105. function useItem(itemId) {
  106. mod.send('C_USE_ITEM', 3, {
  107. gameId: mod.game.me.gameId,
  108. id: Number(itemId[0]),
  109. amount: 1,
  110. unk4: true
  111. });
  112. }
  113. function getConfig() {
  114. let data = {};
  115. try {
  116. data = require('./config.json');
  117. } catch (e) {
  118. data = {
  119. enabled: true,
  120. hp: false,
  121. mp: true,
  122. notice: false
  123. }
  124. jsonSave('config.json', data);
  125. }
  126. return data;
  127. }
  128. function getHP() {
  129. let data = {};
  130. try {
  131. data = require('./hp.json');
  132. } catch (e) {
  133. data[6552] = {
  134. name: 'Prime Recovery Potable',
  135. inCombat: true,
  136. use_at: 80,
  137. slay_at: 30
  138. }
  139. jsonSave('hp.json', data);
  140. }
  141. return jsonSort(data, 'use_at');
  142. }
  143. function getMP() {
  144. let data = {};
  145. try {
  146. data = require('./mp.json');
  147. } catch (e) {
  148. data[6562] = {
  149. name: 'Prime Replenishment Potable',
  150. inCombat: false,
  151. use_at: 50
  152. }
  153. jsonSave('mp.json', data);
  154. }
  155. return jsonSort(data, 'use_at');
  156. }
  157. function msg(msg) {
  158. cmd.message(msg);
  159. }
  160. function jsonSort(data, sortby){
  161. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  162. let s2a = []; for(let i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  163. return s2a;
  164. }
  165. function jsonSave(name,data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  166. }