index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const Command = require('command');
  2. module.exports = function boxOpener(dispatch){
  3. const command = Command(dispatch)
  4. let hooks = [],
  5. cid = null,
  6. enabled = false,
  7. location = null,
  8. timer = null,
  9. delay = 200,
  10. statOpened = 0,
  11. statStarted = null,
  12. scanning = false;
  13. boxId = 166901, // MWA box as default.
  14. inventory = null;
  15. command.add('getbox', () => {
  16. scanning = true;
  17. load();
  18. command.message('Please open the box you wish to set.');
  19. });
  20. command.add('openbox', () => {
  21. if(!enabled)
  22. {
  23. var d = new Date();
  24. statStarted = d.getTime();
  25. enabled = true;
  26. load();
  27. command.message('Opening selected box, use the same command to stop.');
  28. openBox();
  29. }
  30. else
  31. {
  32. stop();
  33. }
  34. });
  35. command.add('stopbox', () => {
  36. stop();
  37. });
  38. command.add('boxdelay', (arg) => {
  39. delay = parseInt(arg);
  40. command.message("setting box opening delay to " + delay);
  41. });
  42. dispatch.hook('S_LOGIN', 1, event =>{cid = event.cid});
  43. function load()
  44. {
  45. hook('C_PLAYER_LOCATION', 1, event =>{location = event});
  46. hook('S_INVEN', 5, event =>{
  47. if(!enabled) return
  48. if(event.first) inventory = []
  49. else if(!inventory) return
  50. for(let item of event.items) inventory.push(item)
  51. if(!event.more)
  52. {
  53. let box = false
  54. for(let item of inventory)
  55. {
  56. if(item.slot < 40) continue
  57. if(item.item == boxId)
  58. {
  59. box = true
  60. }
  61. }
  62. if(!box)
  63. {
  64. stop();
  65. }
  66. inventory.splice(0,inventory.length)
  67. inventory = [];
  68. inventory = null
  69. }
  70. });
  71. hook('C_USE_ITEM', 1, event =>{
  72. if(!scanning) return
  73. if(scanning){
  74. boxId = event.item;
  75. command.message('Box set to: '+boxId+'');
  76. scanning = false;
  77. unload();
  78. }
  79. });
  80. hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
  81. clearTimeout(timer);
  82. openBox();
  83. });
  84. hook('S_SYSTEM_MESSAGE', 1, event => {
  85. if(event.message == "@1242") // SMT_ITEM_MIX_NEED_METERIAL
  86. {
  87. stop();
  88. }
  89. });
  90. }
  91. function openBox()
  92. {
  93. statOpened++;
  94. dispatch.toServer('C_USE_ITEM', 1, {
  95. ownerId: cid,
  96. item: boxId,
  97. id: 0,
  98. unk1: 0,
  99. unk2: 0,
  100. unk3: 0,
  101. unk4: 1,
  102. unk5: 0,
  103. unk6: 0,
  104. unk7: 0,
  105. x: location.x1,
  106. y: location.y1,
  107. z: location.z1,
  108. w: location.w,
  109. unk8: 0,
  110. unk9: 0,
  111. unk10: 0,
  112. unk11: 1
  113. });
  114. timer = setTimeout(openBox,delay);
  115. }
  116. function addZero(i)
  117. {
  118. if (i < 10) {
  119. i = "0" + i;
  120. }
  121. return i;
  122. }
  123. function stop()
  124. {
  125. clearTimeout(timer);
  126. unload();
  127. enabled = false;
  128. let d = new Date();
  129. let t = d.getTime();
  130. let timeElapsedMSec = t-statStarted;
  131. d = new Date(1970, 0, 1); // Epoch
  132. d.setMilliseconds(timeElapsedMSec);
  133. let h = addZero(d.getHours());
  134. let m = addZero(d.getMinutes());
  135. let s = addZero(d.getSeconds());
  136. x.innerHTML = h + ":" + m + ":" + s;
  137. command.message('Box opener stopped. Opened: ' + statOpened + ' boxes. Time elapsed: ' + (h + ":" + m + ":" + s) + " per box: " ((timeElapsedMSec / statOpened) / 1000) + " sec");
  138. statOpened = 0;
  139. }
  140. function unload() {
  141. if(hooks.length) {
  142. for(let h of hooks) dispatch.unhook(h)
  143. hooks = []
  144. }
  145. }
  146. function hook() {
  147. hooks.push(hook(...arguments))
  148. }
  149. }