index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* eslint-disable no-param-reassign */
  2. module.exports = function boxOpener(dispatch) {
  3. const command = dispatch.command || dispatch.require.command;
  4. let hooks = [],
  5. enabled = false,
  6. boxEvent = null,
  7. gacha_detected = false,
  8. isLooting = false,
  9. location = null,
  10. timer = null,
  11. delay = 5500,
  12. useDelay = false,
  13. statOpened = 0,
  14. statUsed = 0,
  15. statStarted = null,
  16. scanning = false;
  17. command.add("box", () => {
  18. if (!enabled && !scanning) {
  19. scanning = true;
  20. load();
  21. command.message("Please normally open a box now and the script will continue opening it");
  22. } else {
  23. stop();
  24. }
  25. });
  26. command.add("boxdelay", (arg) => {
  27. if (arg === "0") {
  28. useDelay = false;
  29. delay = 5500;
  30. command.message("Turning OFF minimum box opening delay, enjoy the speed");
  31. } else if (!isNaN(arg)) {
  32. useDelay = true;
  33. delay = parseInt(arg);
  34. command.message(`Minimum box opening delay is set to: ${ delay / 1000 } sec`);
  35. } else {
  36. command.message(`Minimum box opening delay is set to: ${ useDelay ? `${delay / 1000 } sec` : "no delay"}`);
  37. }
  38. });
  39. dispatch.hook("C_PLAYER_LOCATION", 5, event => {location = event;});
  40. dispatch.game.initialize("inventory");
  41. dispatch.game.inventory.on("update", () => {
  42. if (!enabled) return;
  43. isLooting = false;
  44. });
  45. function load() {
  46. hook("C_USE_ITEM", 3, event => {
  47. if (!scanning) return;
  48. if (scanning) {
  49. boxEvent = event;
  50. boxEvent.dbid = 0n; // to open all inv slots
  51. command.message(`Box set to: ${event.id}, proceeding to auto-open it with ${ useDelay ? `a minimum ${ delay / 1000 } sec delay` : "no delay"}`);
  52. scanning = false;
  53. const d = new Date();
  54. statStarted = d.getTime();
  55. enabled = true;
  56. timer = setTimeout(openBox, delay);
  57. return true; // for consistency of sent data
  58. }
  59. });
  60. hook("S_SYSTEM_MESSAGE_LOOT_ITEM", 1, event => {
  61. if (!gacha_detected && !isLooting && boxEvent) {
  62. isLooting = true;
  63. statOpened++;
  64. if (!useDelay) {
  65. dispatch.clearTimeout(timer);
  66. openBox();
  67. }
  68. }
  69. });
  70. hook("S_GACHA_END", 1, () => {
  71. if (boxEvent) {
  72. statOpened++;
  73. if (!useDelay) {
  74. clearTimeout(timer);
  75. openBox();
  76. }
  77. }
  78. });
  79. hook("S_SYSTEM_MESSAGE", 1, event => {
  80. const msg = dispatch.parseSystemMessage(event.message);
  81. if (msg.id === "SMT_ITEM_MIX_NEED_METERIAL" || msg.id === "SMT_CANT_CONVERT_NOW") {
  82. command.message("Box can not be opened anymore, stopping");
  83. stop();
  84. }
  85. });
  86. hook("S_GACHA_START", 1, event => {
  87. gacha_detected = true;
  88. dispatch.send("C_GACHA_TRY", 1, {
  89. "id": event.id
  90. });
  91. });
  92. }
  93. function openBox() {
  94. if (dispatch.game.inventory.getTotalAmount(boxEvent.id) > 0) {
  95. boxEvent.loc = location.loc;
  96. boxEvent.w = location.w;
  97. dispatch.send("C_USE_ITEM", 3, boxEvent);
  98. if (useDelay) {
  99. statUsed++; // counter for used items other than boxes
  100. }
  101. timer = setTimeout(openBox, delay);
  102. } else {
  103. command.message("You ran out of boxes, stopping");
  104. stop();
  105. }
  106. }
  107. function addZero(i) {
  108. if (i < 10) {
  109. i = `0${ i}`;
  110. }
  111. return i;
  112. }
  113. function stop() {
  114. unload();
  115. if (scanning) {
  116. scanning = false;
  117. command.message("Scanning for a box is aborted");
  118. } else {
  119. clearTimeout(timer);
  120. enabled = false;
  121. gacha_detected = false;
  122. boxEvent = null;
  123. if (useDelay && statOpened == 0) {
  124. statOpened = statUsed;
  125. }
  126. let d = new Date();
  127. const t = d.getTime();
  128. const timeElapsedMSec = t - statStarted;
  129. d = new Date(1970, 0, 1); // Epoch
  130. d.setMilliseconds(timeElapsedMSec);
  131. const h = addZero(d.getHours());
  132. const m = addZero(d.getMinutes());
  133. const s = addZero(d.getSeconds());
  134. command.message(`Box opener stopped. Opened: ${ statOpened } boxes. Time elapsed: ${ h }:${ m }:${ s }. Per box: ${ ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) } sec`);
  135. statOpened = 0;
  136. statUsed = 0;
  137. }
  138. }
  139. function unload() {
  140. if (hooks.length) {
  141. for (const h of hooks) dispatch.unhook(h);
  142. hooks = [];
  143. }
  144. }
  145. function hook() {
  146. hooks.push(dispatch.hook(...arguments));
  147. }
  148. };