index.js 4.9 KB

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