index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. cid = null,
  7. enabled = false,
  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 open the box you wish to set and it will auto-open all of it. Then use the same command to stop.');
  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
  39. {
  40. useDelay = true;
  41. delay = parseInt(arg);
  42. command.message("turning ON minimum box opening delay and setting it to " + (delay / 1000) + " sec");
  43. }
  44. });
  45. dispatch.hook('S_LOGIN', 1, event =>{cid = event.cid});
  46. dispatch.hook('C_PLAYER_LOCATION', 1, event =>{location = event});
  47. function load()
  48. {
  49. hook('S_INVEN', 5, event =>{
  50. if(!enabled) return
  51. isLooting = false; // S_INVEN comes only after all S_SYSTEM_MESSAGE_LOOT_ITEM were received
  52. if(event.first) inventory = []
  53. else if(!inventory) return
  54. for(let item of event.items) inventory.push(item)
  55. if(!event.more)
  56. {
  57. let box = false
  58. for(let item of inventory)
  59. {
  60. if(item.slot < 40) continue
  61. if(item.item == boxId)
  62. {
  63. box = true
  64. }
  65. }
  66. if(!box)
  67. {
  68. stop();
  69. }
  70. inventory.splice(0,inventory.length)
  71. inventory = [];
  72. inventory = null
  73. }
  74. });
  75. hook('C_USE_ITEM', 1, event =>{
  76. if(!scanning) return
  77. if(scanning){
  78. boxId = event.item;
  79. command.message('Box set to: '+boxId+', proceeding to mass-open it with a minimum ' + (delay / 1000) + ' sec delay');
  80. scanning = false;
  81. let d = new Date();
  82. statStarted = d.getTime();
  83. enabled = true;
  84. load();
  85. openBox();
  86. }
  87. });
  88. hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
  89. if(!useDelay && !gacha_detected && !isLooting)
  90. {
  91. isLooting = true;
  92. statOpened++;
  93. clearTimeout(timer);
  94. openBox();
  95. }
  96. });
  97. hook('S_GACHA_END', 1, event => {
  98. if(!useDelay)
  99. {
  100. statOpened++;
  101. clearTimeout(timer);
  102. openBox();
  103. }
  104. });
  105. hook('S_SYSTEM_MESSAGE', 1, event => {
  106. 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
  107. {
  108. stop();
  109. }
  110. });
  111. hook('S_GACHA_START', 1, event => {
  112. gacha_detected = true;
  113. dispatch.toServer('C_GACHA_TRY', 1,{
  114. id:event.id
  115. })
  116. });
  117. }
  118. function openBox()
  119. {
  120. dispatch.toServer('C_USE_ITEM', 1, {
  121. ownerId: cid,
  122. item: boxId,
  123. id: 0,
  124. unk1: 0,
  125. unk2: 0,
  126. unk3: 0,
  127. unk4: 1,
  128. unk5: 0,
  129. unk6: 0,
  130. unk7: 0,
  131. x: location.x1,
  132. y: location.y1,
  133. z: location.z1,
  134. w: location.w,
  135. unk8: 0,
  136. unk9: 0,
  137. unk10: 0,
  138. unk11: 1
  139. });
  140. timer = setTimeout(openBox,delay);
  141. if(useDelay)
  142. {
  143. statOpened++;
  144. }
  145. }
  146. function addZero(i)
  147. {
  148. if (i < 10) {
  149. i = "0" + i;
  150. }
  151. return i;
  152. }
  153. function stop()
  154. {
  155. unload();
  156. if(scanning)
  157. {
  158. scanning = false;
  159. command.message('Scanning aborted');
  160. }
  161. else
  162. {
  163. clearTimeout(timer);
  164. enabled = false;
  165. gacha_detected = false;
  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. }
  177. }
  178. function unload() {
  179. if(hooks.length) {
  180. for(let h of hooks) dispatch.unhook(h)
  181. hooks = []
  182. }
  183. }
  184. function hook() {
  185. hooks.push(dispatch.hook(...arguments))
  186. }
  187. }