index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. const Command = require('command');
  2. const sysmsg = require('tera-data-parser').sysmsg;
  3. module.exports = function boxOpener(dispatch){
  4. const command = Command(dispatch)
  5. let hooks = [],
  6. enabled = false,
  7. boxEvent = null,
  8. gacha_detected = false,
  9. isLooting = false,
  10. location = null,
  11. timer = null,
  12. delay = 5000,
  13. useDelay = false,
  14. statOpened = 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', 4, event =>{location = event});
  50. function load()
  51. {
  52. hook('S_INVEN', 12, 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.item == boxId)
  65. {
  66. box = true
  67. }
  68. }
  69. if(!box)
  70. {
  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.item;
  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. load();
  89. timer = setTimeout(openBox,delay);
  90. }
  91. });
  92. hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
  93. if(!useDelay && !gacha_detected && !isLooting)
  94. {
  95. isLooting = true;
  96. statOpened++;
  97. clearTimeout(timer);
  98. openBox();
  99. }
  100. });
  101. hook('S_GACHA_END', 1, event => {
  102. if(!useDelay)
  103. {
  104. statOpened++;
  105. clearTimeout(timer);
  106. openBox();
  107. }
  108. });
  109. hook('S_SYSTEM_MESSAGE', 1, event => {
  110. if((event.message == "@" + sysmsg.maps.get(dispatch.base.protocolVersion).name.get('SMT_ITEM_MIX_NEED_METERIAL')) || (event.message == "@" + sysmsg.maps.get(dispatch.base.protocolVersion).name.get('SMT_CANT_CONVERT_NOW'))) // 1242 || 1776
  111. {
  112. stop();
  113. }
  114. });
  115. hook('S_GACHA_START', 1, event => {
  116. gacha_detected = true;
  117. dispatch.toServer('C_GACHA_TRY', 1,{
  118. id:event.id
  119. })
  120. });
  121. }
  122. function openBox()
  123. {
  124. boxEvent.loc = location.loc;
  125. boxEvent.w = location.w;
  126. dispatch.toServer('C_USE_ITEM', 3, boxEvent);
  127. timer = setTimeout(openBox,delay);
  128. if(useDelay)
  129. {
  130. statOpened++;
  131. }
  132. }
  133. function addZero(i)
  134. {
  135. if (i < 10) {
  136. i = "0" + i;
  137. }
  138. return i;
  139. }
  140. function stop()
  141. {
  142. unload();
  143. if(scanning)
  144. {
  145. scanning = false;
  146. command.message('Scanning for a box is aborted');
  147. }
  148. else
  149. {
  150. clearTimeout(timer);
  151. enabled = false;
  152. gacha_detected = false;
  153. let d = new Date();
  154. let t = d.getTime();
  155. let timeElapsedMSec = t-statStarted;
  156. d = new Date(1970, 0, 1); // Epoch
  157. d.setMilliseconds(timeElapsedMSec);
  158. let h = addZero(d.getHours());
  159. let m = addZero(d.getMinutes());
  160. let s = addZero(d.getSeconds());
  161. command.message('Box opener stopped. Opened: ' + statOpened + ' boxes. Time elapsed: ' + (h + ":" + m + ":" + s) + ". Per box: " + ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) + " sec");
  162. statOpened = 0;
  163. }
  164. }
  165. function unload() {
  166. if(hooks.length) {
  167. for(let h of hooks) dispatch.unhook(h)
  168. hooks = []
  169. }
  170. }
  171. function hook() {
  172. hooks.push(dispatch.hook(...arguments))
  173. }
  174. }