index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 = 6000,
  12. useDelay = false,
  13. statOpened = 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 = 6000;
  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', 4, event =>{location = event});
  49. function load()
  50. {
  51. hook('S_INVEN', 12, 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(timer)
  97. {
  98. clearTimeout(timer);
  99. }
  100. if(!useDelay)
  101. {
  102. timer = null;
  103. openBox();
  104. }
  105. }
  106. });
  107. hook('S_GACHA_END', 1, event => {
  108. statOpened++;
  109. if(timer)
  110. {
  111. clearTimeout(timer);
  112. }
  113. if(!useDelay)
  114. {
  115. timer = null;
  116. openBox();
  117. }
  118. });
  119. hook('S_SYSTEM_MESSAGE', 1, event => {
  120. const msg = dispatch.base.parseSystemMessage(event.message);
  121. if(msg.id === 'SMT_ITEM_MIX_NEED_METERIAL' || msg.id === 'SMT_CANT_CONVERT_NOW')
  122. {
  123. command.message("Box can not be opened anymore, stopping");
  124. stop();
  125. }
  126. });
  127. hook('S_GACHA_START', 1, event => {
  128. gacha_detected = true;
  129. dispatch.toServer('C_GACHA_TRY', 1,{
  130. id:event.id
  131. })
  132. });
  133. }
  134. function openBox()
  135. {
  136. boxEvent.loc = location.loc;
  137. boxEvent.w = location.w;
  138. dispatch.toServer('C_USE_ITEM', 3, boxEvent);
  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. 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. }
  172. }
  173. function unload() {
  174. if(hooks.length) {
  175. for(let h of hooks) dispatch.unhook(h)
  176. hooks = []
  177. }
  178. }
  179. function hook() {
  180. hooks.push(dispatch.hook(...arguments))
  181. }
  182. }