index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 gPot = null, isReady = false, isSlaying = false, isBattleground = 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. for(let i = 0; i < hpPot.length; i++) {
  46. gPot = e.items.find(item => item.id === Number(hpPot[i][0]));
  47. if (gPot) hpPot[i][1].amount = gPot.amount;
  48. }
  49. for(let i = 0; i < mpPot.length; i++) {
  50. gPot = e.items.find(item => item.id === Number(mpPot[i][0]));
  51. if (gPot) mpPot[i][1].amount = gPot.amount;
  52. }
  53. }
  54. });
  55. mod.hook('S_PLAYER_STAT_UPDATE', 10, e => {
  56. if (config.enabled) {
  57. isReady = mod.game.isIngame && !mod.game.isInLoadingScreen && mod.game.me.alive && !mod.game.me.mounted && !mod.game.contract.active;
  58. isBattleground = mod.game.me.inBattleground || mod.game.me.zone === 152;
  59. if (config.hp && isReady) {
  60. nowHP = Math.round(parseInt(e.hp) / parseInt(e.maxHp) * 100);
  61. for (let hp = 0; hp < hpPot.length; hp++) {
  62. if (!hpPot[hp][1].inCd && hpPot[hp][1].amount > 0 && ((!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].inBattleground ? isBattleground : true)) {
  63. useItem(hpPot[hp]); hpPot[hp][1].inCd = true; hpPot[hp][1].amount--; setTimeout(function () {hpPot[hp][1].inCd = false;}, hpPot[hp][1].cd * 1000);
  64. if (config.notice) msg(`Used ${hpPot[hp][1].name}, ${(hpPot[hp][1].amount)} left.`);
  65. }
  66. }
  67. }
  68. if (config.mp && isReady) {
  69. nowMP = Math.round(parseInt(e.mp) / parseInt(e.maxMp) * 100);
  70. for (let mp = 0; mp < mpPot.length; mp++) {
  71. 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[hp][1].inBattleground ? isBattleground : true)) {
  72. useItem(mpPot[mp]); mpPot[mp][1].inCd = true; mpPot[mp][1].amount--; setTimeout(function () {mpPot[mp][1].inCd = false;}, mpPot[mp][1].cd * 1000);
  73. if (config.notice) msg(`Used ${mpPot[mp][1].name}, ${(mpPot[mp][1].amount)} left.`);
  74. }
  75. }
  76. }
  77. }
  78. });
  79. function useItem(itemId) {
  80. mod.send('C_USE_ITEM', 3, {
  81. gameId: mod.game.me.gameId,
  82. id: Number(itemId[0]),
  83. amount: 1,
  84. unk4: true
  85. });
  86. }
  87. function getConfig() {
  88. let data = {};
  89. try {
  90. data = require('./config.json');
  91. } catch (e) {
  92. data = {
  93. enabled: true,
  94. hp: false,
  95. mp: true,
  96. notice: false
  97. }
  98. jsonSave('config.json', data);
  99. }
  100. return data;
  101. }
  102. function getHP() {
  103. let data = {};
  104. try {
  105. data = require('./hp.json');
  106. } catch (e) {
  107. data[6552] = {
  108. name: 'Prime Recovery Potable',
  109. inBattleground: false,
  110. inCombat: true,
  111. use_at: 80,
  112. slay_at: 30,
  113. cd: 10
  114. }
  115. jsonSave('hp.json', data);
  116. }
  117. return jsonSort(data, 'use_at');
  118. }
  119. function getMP() {
  120. let data = {};
  121. try {
  122. data = require('./mp.json');
  123. } catch (e) {
  124. data[6562] = {
  125. name: 'Prime Replenishment Potable',
  126. inBattleground: false,
  127. inCombat: false,
  128. use_at: 50,
  129. cd: 10
  130. }
  131. jsonSave('mp.json', data);
  132. }
  133. return jsonSort(data, 'use_at');
  134. }
  135. function msg(msg) {
  136. cmd.message(msg);
  137. }
  138. function jsonSort(data, sortby){
  139. let key = Object.keys(data).sort(function(a,b) {return parseFloat(data[b][sortby]) - parseFloat(data[a][sortby])});
  140. let s2a = []; for(let i = 0; i < key.length; i++) s2a.push([key[i], data[key[i]]]);
  141. return s2a;
  142. }
  143. function jsonSave(name,data) {fs.writeFile(path.join(__dirname, name), JSON.stringify(data, null, 4), err => {});}
  144. }