index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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', 18, 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. boxEvent.dbid = 0n; // to open all inv slots
  83. boxId = event.id;
  84. command.message("Box set to: "+boxId+", proceeding to auto-open it with " + (useDelay ? "a minimum " + (delay / 1000) + " sec delay" : "no delay" ));
  85. scanning = false;
  86. let d = new Date();
  87. statStarted = d.getTime();
  88. enabled = true;
  89. timer = setTimeout(openBox,delay);
  90. }
  91. });
  92. hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
  93. if(!gacha_detected && !isLooting && boxEvent)
  94. {
  95. isLooting = true;
  96. statOpened++;
  97. if(!useDelay)
  98. {
  99. clearTimeout(timer);
  100. openBox();
  101. }
  102. }
  103. });
  104. hook('S_GACHA_END', 1, event => {
  105. if(boxEvent)
  106. {
  107. statOpened++;
  108. if(!useDelay)
  109. {
  110. clearTimeout(timer);
  111. openBox();
  112. }
  113. }
  114. });
  115. hook('S_SYSTEM_MESSAGE', 1, event => {
  116. const msg = dispatch.parseSystemMessage(event.message);
  117. if(msg.id === 'SMT_ITEM_MIX_NEED_METERIAL' || msg.id === 'SMT_CANT_CONVERT_NOW')
  118. {
  119. command.message("Box can not be opened anymore, stopping");
  120. stop();
  121. }
  122. });
  123. hook('S_GACHA_START', 1, event => {
  124. gacha_detected = true;
  125. dispatch.toServer('C_GACHA_TRY', 1,{
  126. id:event.id
  127. })
  128. });
  129. }
  130. function openBox()
  131. {
  132. boxEvent.loc = location.loc;
  133. boxEvent.w = location.w;
  134. dispatch.toServer('C_USE_ITEM', 3, boxEvent);
  135. if(useDelay)
  136. {
  137. statUsed++; // counter for used items other than boxes
  138. }
  139. timer = setTimeout(openBox,delay);
  140. }
  141. function addZero(i)
  142. {
  143. if (i < 10) {
  144. i = "0" + i;
  145. }
  146. return i;
  147. }
  148. function stop()
  149. {
  150. unload();
  151. if(scanning)
  152. {
  153. scanning = false;
  154. command.message('Scanning for a box is aborted');
  155. }
  156. else
  157. {
  158. clearTimeout(timer);
  159. enabled = false;
  160. gacha_detected = false;
  161. boxEvent = null;
  162. if(useDelay && statOpened == 0)
  163. {
  164. statOpened = statUsed;
  165. }
  166. let d = new Date();
  167. let t = d.getTime();
  168. let timeElapsedMSec = t-statStarted;
  169. d = new Date(1970, 0, 1); // Epoch
  170. d.setMilliseconds(timeElapsedMSec);
  171. let h = addZero(d.getHours());
  172. let m = addZero(d.getMinutes());
  173. let s = addZero(d.getSeconds());
  174. command.message('Box opener stopped. Opened: ' + statOpened + ' boxes. Time elapsed: ' + (h + ":" + m + ":" + s) + ". Per box: " + ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) + " sec");
  175. statOpened = 0;
  176. statUsed = 0;
  177. }
  178. }
  179. function unload() {
  180. if(hooks.length) {
  181. for(let h of hooks) dispatch.unhook(h)
  182. hooks = []
  183. }
  184. }
  185. function hook() {
  186. hooks.push(dispatch.hook(...arguments))
  187. }
  188. }