forked from Owyn/generic-box-opener-item-user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
227 lines (200 loc) · 4.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
module.exports = function boxOpener(dispatch){
const command = dispatch.command || dispatch.require.command;
let hooks = [],
enabled = false,
boxEvent = null,
gacha_detected = false,
isLooting = false,
location = null,
timer = null,
delay = 5500,
useDelay = false,
statOpened = 0,
statUsed = 0,
statStarted = null,
scanning = false,
inventory = null;
command.add('box', () => {
if(!enabled && !scanning)
{
scanning = true;
load();
command.message('Please normally open a box now and the script will continue opening it');
}
else
{
stop();
}
});
command.add('boxdelay', (arg) => {
if(arg === "0")
{
useDelay = false;
delay = 5500;
command.message("Turning OFF minimum box opening delay, enjoy the speed");
}
else if(!isNaN(arg))
{
useDelay = true;
delay = parseInt(arg);
command.message("Minimum box opening delay is set to: " + (delay / 1000) + " sec");
}
else
{
command.message("Minimum box opening delay is set to: " + (useDelay ? (delay / 1000) + " sec" : "no delay" ));
}
});
dispatch.hook('C_PLAYER_LOCATION', 5, event =>{location = event});
dispatch.game.initialize("inventory");
dispatch.game.inventory.on('update', () => {
if(!enabled) return;
isLooting = false; // event comes only after all S_SYSTEM_MESSAGE_LOOT_ITEM (probably)
});
function load()
{
/*hook('S_INVEN', 18, event =>{ // pre patch 85
if(!enabled) return
isLooting = false; // S_INVEN comes only after all S_SYSTEM_MESSAGE_LOOT_ITEM
if(event.first) inventory = []
else if(!inventory) return
for(let item of event.items) inventory.push(item)
if(!event.more)
{
let box = false
for(let item of inventory)
{
if(item.slot < 40) continue
if(item.id == boxId)
{
box = true
}
}
if(!box)
{
command.message("You ran out of boxes, stopping");
stop();
}
inventory.splice(0,inventory.length)
inventory = [];
inventory = null
}
});*/
hook('C_USE_ITEM', 3, event =>{
if(!scanning) return
if(scanning){
boxEvent = event;
boxEvent.dbid = 0n; // to open all inv slots
command.message("Box set to: "+event.id+", proceeding to auto-open it with " + (useDelay ? "a minimum " + (delay / 1000) + " sec delay" : "no delay" ));
scanning = false;
let d = new Date();
statStarted = d.getTime();
enabled = true;
timer = setTimeout(openBox,delay);
return true; // for consistency of sent data
}
});
hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
if(!gacha_detected && !isLooting && boxEvent)
{
isLooting = true;
statOpened++;
if(!useDelay)
{
clearTimeout(timer);
openBox();
}
}
});
hook('S_GACHA_END', 1, event => {
if(boxEvent)
{
statOpened++;
if(!useDelay)
{
clearTimeout(timer);
openBox();
}
}
});
hook('S_SYSTEM_MESSAGE', 1, event => {
const msg = dispatch.parseSystemMessage(event.message);
if(msg.id === 'SMT_ITEM_MIX_NEED_METERIAL' || msg.id === 'SMT_CANT_CONVERT_NOW')
{
command.message("Box can not be opened anymore, stopping");
stop();
}
});
hook('S_GACHA_START', 1, event => {
gacha_detected = true;
dispatch.toServer('C_GACHA_TRY', 1,{
id:event.id
})
});
}
function openBox()
{
if(dispatch.game.inventory.getTotalAmount(boxEvent.id) > 0)
{
boxEvent.loc = location.loc;
boxEvent.w = location.w;
dispatch.toServer('C_USE_ITEM', 3, boxEvent);
if(useDelay)
{
statUsed++; // counter for used items other than boxes
}
timer = setTimeout(openBox,delay);
}
else
{
command.message("You ran out of boxes, stopping");
stop();
}
}
function addZero(i)
{
if (i < 10) {
i = "0" + i;
}
return i;
}
function stop()
{
unload();
if(scanning)
{
scanning = false;
command.message('Scanning for a box is aborted');
}
else
{
clearTimeout(timer);
enabled = false;
gacha_detected = false;
boxEvent = null;
if(useDelay && statOpened == 0)
{
statOpened = statUsed;
}
let d = new Date();
let t = d.getTime();
let timeElapsedMSec = t-statStarted;
d = new Date(1970, 0, 1); // Epoch
d.setMilliseconds(timeElapsedMSec);
let h = addZero(d.getHours());
let m = addZero(d.getMinutes());
let s = addZero(d.getSeconds());
command.message('Box opener stopped. Opened: ' + statOpened + ' boxes. Time elapsed: ' + (h + ":" + m + ":" + s) + ". Per box: " + ((timeElapsedMSec / statOpened) / 1000).toPrecision(2) + " sec");
statOpened = 0;
statUsed = 0;
}
}
function unload() {
if(hooks.length) {
for(let h of hooks) dispatch.unhook(h)
hooks = []
}
}
function hook() {
hooks.push(dispatch.hook(...arguments))
}
}