index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 i = 0; i < hpPot.length; i++) {
  47. hp = e.items.find(item => item.id === Number(hpPot[i][0]));
  48. if (hp) hpPot[i][1].amount = hp.amount;
  49. }
  50. for(let i = 0; i < mpPot.length; i++) {
  51. mp = e.items.find(item => item.id === Number(mpPot[i][0]));
  52. if (mp) mpPot[i][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 => Number(item[0]) === e.item);
  60. if (hp >= 0 && !hpPot[hp][1].inCd) {
  61. hpPot[hp][1].inCd = true;
  62. setTimeout(function () {
  63. hpPot[hp][1].inCd = false;
  64. }, e.cooldown);
  65. }
  66. mp = mpPot.findIndex(item => Number(item[0]) === e.item);
  67. if (mp >= 0 && !mpPot[mp][1].inCd) {
  68. mpPot[mp][1].inCd = true;
  69. setTimeout(function () {
  70. mpPot[mp][1].inCd = false;
  71. }, e.cooldown);
  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(parseInt(e.hp) / parseInt(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(parseInt(e.mp) / parseInt(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 useItem(itemId) {
  103. mod.send('C_USE_ITEM', 3, {
  104. gameId: mod.game.me.gameId,
  105. id: Number(itemId[0]),
  106. amount: 1,
  107. unk4: true
  108. });
  109. }
  110. function getConfig() {
  111. let data = {};
  112. try {
  113. data = require('./config.json');
  114. } catch (e) {
  115. data = {
  116. enabled: true,
  117. hp: false,
  118. mp: true,
  119. notice: false
  120. }
  121. jsonSave('config.json', data);
  122. }
  123. return data;
  124. }
  125. function getHP() {
  126. let data = {};
  127. try {
  128. data = require('./hp.json');
  129. } catch (e) {
  130. data[6552] = {
  131. name: 'Prime Recovery Potable',
  132. inCombat: true,
  133. use_at: 80,
  134. slay_at: 30
  135. }
  136. jsonSave('hp.json', data);
  137. }
  138. return jsonSort(data, 'use_at');
  139. }
  140. function getMP() {
  141. let data = {};
  142. try {
  143. data = require('./mp.json');
  144. } catch (e) {
  145. data[6562] = {
  146. name: 'Prime Replenishment Potable',
  147. inCombat: false,
  148. use_at: 50
  149. }
  150. jsonSave('mp.json', data);
  151. }
  152. return jsonSort(data, 'use_at');
  153. }
  154. function msg(msg) {
  155. cmd.message(msg);
  156. }
  157. function jsonSort(data, sortby){
  158. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  159. let s2a = []; for(let i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  160. return s2a;
  161. }
  162. function jsonSave(name,data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  163. }