index.js 4.3 KB

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