index.js 5.2 KB

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