-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
376 lines (335 loc) · 9.39 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
module.exports = function Banker(mod) {
const BANK_CONTRACT = 26;
const BANK_TYPE = 1;
const BANK_PAGE_SLOTS = 72;
const PAGE_CHANGE_TIMEOUT = 1000;
const ERROR = '#ff0000';
const PROTOCOLS = [
'C_GET_WARE_ITEM',
'C_PUT_WARE_ITEM',
'C_VIEW_WARE',
'S_VIEW_WARE_EX'
];
const BlacklistModes = Object.freeze({
NONE: 0,
REMOVE: 1,
ADD: 2,
ANY: 3})
let disabled;
let bankInventory;
let currentContract;
let onNextOffset;
let blacklist = new Set();
let blacklistMode = BlacklistModes.NONE;
loadConfig();
//we are already in a game, mod was likely reloaded
if (mod.game.me.gameId) {
validateProtocolMap();
}
mod.game.on('enter_game', () => {
validateProtocolMap();
});
if (disabled)
return;
//started or closed contract (window)
mod.hook('S_REQUEST_CONTRACT', 1, event => {
if (mod.game.me.is(event.senderId)) {
currentContract = event.type;
}
});
mod.hook('S_CANCEL_CONTRACT', 1, event => {
if (mod.game.me.is(event.senderId)) {
currentContract = null;
}
});
mod.hook('S_VIEW_WARE_EX', 1, event => {
if (!mod.game.me.is(event.gameId))
return;
if (event.type == BANK_TYPE) {
currentContract = BANK_CONTRACT;
bankInventory = event;
if (onNextOffset) onNextOffset(event);
}
});
mod.hook('C_GET_WARE_ITEM', 3, event => {
tryBlacklistNext(event, false);
});
mod.hook('C_PUT_WARE_ITEM', 3, event => {
tryBlacklistNext(event, true);
});
mod.command.add('banker', {
$default() {
argsError();
},
human() {
mod.settings.human = !mod.settings.human;
saveConfig();
msg('Human mode ' + (mod.settings.human ? 'enabled' : 'disabled'));
},
tab() {
if (checkDisabled()) return;
//force deposit tab
if (checkBankOpen()) {
msg('Depositing items in this tab');
autoDeposit(false);
}
},
all() {
if (checkDisabled()) return;
//force deposit all
if (checkBankOpen()) {
msg('Depositing items in all tabs');
depositAllTabs();
}
},
mode() {
//update tab mode
mod.settings.tab = !mod.settings.tab;
saveConfig();
msg('Single tab mode ' + (mod.settings.tab ? 'enabled' : 'disabled'));
},
blacklist(...args) {
processBlacklistCommand(args);
},
bl(...args){
processBlacklistCommand(args);
},
$none() {
if (checkDisabled()) return;
//deposit based on settings
if (checkBankOpen()) {
blacklistMode = BlacklistModes.NONE;
msg('Depositing items in ' + (mod.settings.tab ? 'this tab' : 'all tabs'));
if (mod.settings.tab) {
autoDeposit(false);
} else {
depositAllTabs();
}
}
}
});
function argsError() {
msg('Invalid arguments.', ERROR);
}
function checkDisabled() {
if (disabled)
msg('Banker is disabled. Add the required protocol maps to the tera-data folder.', ERROR);
return disabled
}
function depositAllTabs() {
if (bankInventory.offset != 0) {
changeBankOffset(0, () => {
autoDeposit(true);
});
return;
}
autoDeposit(true);
}
function processBlacklistCommand(args) {
if (args.length >= 1) {
switch (args[0]) {
case 'a':
case 'add':
if (args.length == 1) {
if (checkDisabled()) return;
blacklistMode = blacklistMode ? BlacklistModes.NONE : BlacklistModes.ADD;
msg(`Next item deposited or retrieved will be added to blacklist`);
} else if (args.length == 2 && isNormalInteger(args[1])) {
msg(`Item ${args[1]} added to blacklist`);
blacklist.add(args[1]);
saveConfig();
} else {
argsError();
}
break;
case 'r':
case 'remove':
if (args.length == 1) {
if (checkDisabled()) return;
blacklistMode = blacklistMode ? BlacklistModes.NONE : BlacklistModes.REMOVE;
msg(`Next item deposited or retrieved will be removed from blacklist`);
} else if (args.length == 2 && isNormalInteger(args[1])) {
msg(`Item ${args[1]} removed from blacklist`);
blacklist.add(args[1]);
saveConfig();
} else {
argsError();
}
break;
case 'mode':
if (checkDisabled()) return;
blacklistMode = blacklistMode ? BlacklistModes.NONE : BlacklistModes.ANY;
if (blacklistMode) {
msg('Next banked or retrieved items will be blacklisted');
msg('Use "bank blacklist mode" to disable');
} else {
msg('Blacklist mode disabled');
}
break;
case 'clear':
msg('Blacklist cleared');
blacklist.clear();
saveConfig();
break;
case 'list':
if (blacklist.size == 0) {
msg('Blacklist is empty.');
} else {
msg('Blacklist items:');
for (let item of blacklist)
msg(item);
}
break;
}
} else {
argsError();
}
}
function tryBlacklistNext(item, store) {
if (blacklistMode == BlacklistModes.ADD || (blacklistMode == BlacklistModes.ANY && !store)) {
blacklist.add(item.id);
msg(`Item ${item.id} added to blacklist`);
saveConfig();
} else if (blacklistMode == BlacklistModes.REMOVE || (blacklistMode == BlacklistModes.ANY && store)) {
blacklist.delete(item.id);
msg(`Item ${item.id} removed from blacklist`);
saveConfig();
}
if (blacklistMode != BlacklistModes.ANY)
blacklistMode = BlacklistModes.NONE;
}
function checkBankOpen() {
if (currentContract != BANK_CONTRACT) {
msg('Bank must be open to use banker module', ERROR);
return false;
}
return true;
}
function autoDeposit(allTabs) {
let bagItems = mod.game.inventory.bagItems.slice(0);
let bankItems = bankInventory.items.slice(0);
bagItems.sort((a, b) => a.id - b.id);
bankItems.sort((a, b) => a.id - b.id);
let aIdx = 0;
let bIdx = 0;
let depositNext = function () {
//iterate both lists and find matching items to deposit
while (aIdx < bagItems.length && bIdx < bankItems.length) {
if (bagItems[aIdx].id === bankItems[bIdx].id) {
if (currentContract != BANK_CONTRACT)
return;
if (!blacklist.has(bagItems[aIdx].id))
depositItem(bagItems[aIdx], bankInventory.offset);
aIdx++;
bIdx++;
setTimeout(() => {
depositNext();
}, getRandomDelay());
return;
} else if (bagItems[aIdx].id < bankItems[bIdx].id) {
aIdx++;
} else {
bIdx++;
}
}
if (allTabs) {
let next = getNextOffset(bankInventory);
if (next != undefined) {
changeBankOffset(next, () => autoDeposit(allTabs));
}
}
}
depositNext();
}
function depositItem(bagItem, offset) {
mod.send('C_PUT_WARE_ITEM', 3, {
gameId: mod.game.me.gameId,
type: BANK_TYPE,
page: offset,
money: 0,
pocket: 0,
pocket: bagItem.pocket,
invenPos: bagItem.slot,
id: bagItem.id,
dbid: bagItem.dbid,
amount: bagItem.amount,
bankPos: offset
});
}
function getNextOffset(bank) {
if (bank.offset + BANK_PAGE_SLOTS < bank.slots)
return bank.offset + BANK_PAGE_SLOTS;
}
function changeBankOffset(offset, callback) {
let bankLoaded;
onNextOffset = event => {
bankLoaded = true;
onNextOffset = false;
callback(event);
};
setTimeout(() => {
if (!bankLoaded)
msg('Failed to load next bank page.', ERROR);
}, PAGE_CHANGE_TIMEOUT);
setTimeout(() => {
mod.send('C_VIEW_WARE', 2, {
gameId: mod.game.me.gameId,
type: BANK_TYPE,
offset: offset
});
}, getRandomDelay());
}
function loadConfig() {
blacklist = new Set(mod.settings.blacklist);
}
function saveConfig() {
mod.settings.blacklist = Array.from(blacklist);
mod.saveSettings();
}
function validateProtocolMap() {
if (disabled)
return;
try {
let missing = [];
disabled = false;
for (var name of PROTOCOLS) {
var valid = mod.dispatch.protocolMap.name.get(name);
if (valid === undefined || valid == null) {
missing.push(name);
}
}
if (missing.length) {
let errorText = missing.join(', ') + ' are missing in the protocol map. Install missing protocols before using banker.';
msg(errorText, ERROR);
mod.error(errorText);
disabled = true;
}
} catch (e) {
mod.error(e);
}
}
function getRandomDelay() {
if (mod.settings.human) {
return 300 + Math.floor(gaussianRand() * 200);
} else {
return 50 + Math.floor(Math.random() * 100);
}
}
function gaussianRand() {
let rand = 0;
for (var i = 0; i < 6; i += 1) {
rand += Math.random();
}
return rand / 6;
}
function isNormalInteger(str) {
let n = Math.floor(Number(str));
return n !== Infinity && String(n) === str && n >= 0;
}
function msg(text, color) {
if (color !== undefined)
mod.command.message(`<font color="${color}"> ${text}</font>`);
else
mod.command.message(` ${text}`);
}
};