index.js 4.3 KB

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