-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Autokeys.ts
745 lines (664 loc) · 30.7 KB
/
Autokeys.ts
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
import Currencies from '@tf2autobot/tf2-currencies';
import { genUserPure, genScrapAdjustment } from './userSettings';
import Bot from '../Bot';
import { EntryData, KeyPrices, PricelistChangedSource } from '../Pricelist';
import log from '../../lib/logger';
import { currPure } from '../../lib/tools/pure';
import sendAlert from '../DiscordWebhook/sendAlert';
export interface OverallStatus {
isBuyingKeys: boolean;
isBankingKeys: boolean;
checkAlertOnLowPure: boolean;
alreadyUpdatedToBank: boolean;
alreadyUpdatedToBuy: boolean;
alreadyUpdatedToSell: boolean;
}
type SetOverallStatus = [boolean, boolean, boolean, boolean, boolean, boolean];
interface OldAmount {
keysCanBuy: number;
keysCanSell: number;
keysCanBankMin: number;
keysCanBankMax: number;
ofKeys: number;
}
type SetOldAmount = [number, number, number, number, number];
export default class Autokeys {
get isEnabled(): boolean {
return this.bot.options.autokeys.enable;
}
get isKeyBankingEnabled(): boolean {
return this.bot.options.autokeys.banking.enable;
}
get isEnableScrapAdjustment(): boolean {
return genScrapAdjustment(
this.bot.options.autokeys.scrapAdjustment.value,
this.bot.options.autokeys.scrapAdjustment.enable
).enabled;
}
get scrapAdjustmentValue(): number {
return genScrapAdjustment(
this.bot.options.autokeys.scrapAdjustment.value,
this.bot.options.autokeys.scrapAdjustment.enable
).value;
}
get userPure(): {
minKeys: number;
maxKeys: number;
minRefs: number;
maxRefs: number;
} {
return genUserPure(
this.bot.options.autokeys.minKeys,
this.bot.options.autokeys.maxKeys,
this.bot.options.autokeys.minRefined,
this.bot.options.autokeys.maxRefined
);
}
private isActive = false;
private set setActiveStatus(set: boolean) {
this.isActive = set;
}
get getActiveStatus(): boolean {
return this.isActive;
}
private status: OverallStatus = {
isBuyingKeys: false,
isBankingKeys: false,
checkAlertOnLowPure: false,
alreadyUpdatedToBank: false,
alreadyUpdatedToBuy: false,
alreadyUpdatedToSell: false
};
private set setOverallStatus(set: SetOverallStatus) {
this.status = {
isBuyingKeys: set[0],
isBankingKeys: set[1],
checkAlertOnLowPure: set[2],
alreadyUpdatedToBank: set[3],
alreadyUpdatedToBuy: set[4],
alreadyUpdatedToSell: set[5]
};
}
get getOverallStatus(): OverallStatus {
return this.status;
}
private oldAmount: OldAmount = {
keysCanBuy: 0,
keysCanSell: 0,
keysCanBankMin: 0,
keysCanBankMax: 0,
ofKeys: 0
};
private set setOldAmount(set: SetOldAmount) {
this.oldAmount = {
keysCanBuy: set[0],
keysCanSell: set[1],
keysCanBankMin: set[2],
keysCanBankMax: set[3],
ofKeys: set[4]
};
}
private OldKeyPrices: { buy: Currencies; sell: Currencies };
constructor(private readonly bot: Bot) {
this.bot = bot;
}
check(): void {
log.debug(`checking autokeys (Enabled: ${String(this.isEnabled)})`);
if (this.isEnabled === false) {
return;
}
const userPure = this.userPure;
const userMinKeys = userPure.minKeys;
const userMaxKeys = userPure.maxKeys;
const userMinRef = userPure.minRefs;
const userMaxRef = userPure.maxRefs;
if (isNaN(userMinKeys) || isNaN(userMaxKeys) || isNaN(userMinRef) || isNaN(userMaxRef)) {
log.warn(
"You've entered a non-number on either your MINIMUM_KEYS/MINIMUM_REFINED/MAXIMUM_REFINED variables," +
' please correct it. Autokeys is disabled until you correct it.'
);
return;
}
const pure = currPure(this.bot);
const currKeys = pure.key;
const currRef = pure.refTotalInScrap;
const currKeyPrice = this.bot.pricelist.getKeyPrices;
if (currKeyPrice !== this.OldKeyPrices && this.isEnableScrapAdjustment) {
// When scrap adjustment activated, if key rate changes, then it will force update key prices after a trade.
this.setOverallStatus = [false, false, false, false, false, false];
this.OldKeyPrices = { buy: currKeyPrice.buy, sell: currKeyPrice.sell };
}
/** enable Autokeys - Buying - true if currRef \> maxRef AND currKeys \< maxKeys */
const isBuyingKeys = currRef > userMaxRef && currKeys < userMaxKeys;
/*
* <————————————○ \
* Keys ----|--------|----> ⟩ AND
* ○————> /
* Refs ----|--------|---->
* min max
*/
/** enable Autokeys - Selling - true if currRef \< minRef AND currKeys \> minKeys */
const isSellingKeys = currRef < userMinRef && currKeys > userMinKeys;
/*
* ○—————————————> \
* Keys ----|--------|----> ⟩ AND
* <———○ /
* Refs ----|--------|---->
* min max
*/
/**
* disable Autokeys - true if currRef \>= maxRef AND currKeys \>= maxKeys OR
* (minRef \<= currRef \<= maxRef AND currKeys \<= maxKeys)
*/
const isRemoveAutoKeys =
(currRef >= userMaxRef && currKeys >= userMaxKeys) ||
(currRef >= userMinRef && currRef <= userMaxRef && currKeys <= userMaxKeys);
/*
* <————————————●····> \
* Keys ----|--------|----> ⟩ AND
* ●————————●····> /
* Refs ----|--------|---->
* min max
* ^^|^^
* OR
*/
/** enable Autokeys - Banking - true if user set autokeys.banking.enable to true */
const isEnableKeyBanking = this.isKeyBankingEnabled;
/** enable Autokeys - Banking - true if minRef \< currRef \< maxRef AND currKeys \> minKeys */
const isBankingKeys = currRef > userMinRef && currRef < userMaxRef && currKeys > userMinKeys;
/*
* ○—————————————> \
* Keys ----|--------|----> ⟩ AND
* ○————————○ /
* Refs ----|-------|---->
* min max
*/
/** enable Autokeys - Banking - true if currRef \> minRef AND keys \< minKeys will buy keys. */
const isBankingBuyKeysWithEnoughRefs = currRef > userMinRef && currKeys <= userMinKeys;
/*
* <———● \
* Keys ----|--------|----> ⟩ AND
* ○—————————————> /
* Refs ----|--------|---->
* min max
*/
/** disable Autokeys - Banking - true if currRef \< minRef AND currKeys \< minKeys */
const isRemoveBankingKeys = currRef <= userMaxRef && currKeys <= userMinKeys;
/*
* <———● \
* Keys ----|--------|----> ⟩ AND
* <————————————● /
* Refs ----|--------|---->
* min max
*/
/** send alert to admins when both keys and refs below minimum */
const isAlertAdmins = currRef <= userMinRef && currKeys <= userMinKeys;
/*
* <———● \
* Keys ----|--------|----> ⟩ AND
* <———● /
* Refs ----|--------|---->
* min max
*/
let setMinKeys: number;
let setMaxKeys: number;
const rKeysCanBuy = Math.round((currRef - userMaxRef) / currKeyPrice.buy.toValue());
const rKeysCanSell = Math.round((userMinRef - currRef) / currKeyPrice.sell.toValue());
const rKeysCanBankMin = Math.round((userMaxRef - currRef) / currKeyPrice.sell.toValue());
const rKeysCanBankMax = Math.round((currRef - userMinRef) / currKeyPrice.buy.toValue());
// Check and set new min and max
if (isBuyingKeys) {
const inventoryManager = this.bot.inventoryManager;
if (!inventoryManager.isCanAffordToBuy(currKeyPrice.buy, inventoryManager.getInventory)) {
// if the bot can't afford to buy, just abort
// this is very unlikely, but possible I guess.
return;
}
// If buying - we need to set min = currKeys and max = currKeys + CanBuy
const min = currKeys;
setMinKeys = min <= userMinKeys ? userMinKeys : min;
const max = currKeys + rKeysCanBuy;
setMaxKeys = max >= userMaxKeys ? userMaxKeys : max < 1 ? 1 : max;
if (setMinKeys === setMaxKeys) {
setMaxKeys += 1;
} else if (setMinKeys > setMaxKeys) {
setMaxKeys = setMinKeys + 1;
}
//
} else if (isBankingBuyKeysWithEnoughRefs && isEnableKeyBanking) {
const inventoryManager = this.bot.inventoryManager;
if (!inventoryManager.isCanAffordToBuy(currKeyPrice.buy, inventoryManager.getInventory)) {
// if the bot can't afford to buy, just abort
// example, set min ref to 60 ref, but the bot have 50 ref and the current buying price is 68 ref
return;
}
// If buying (while banking) - we need to set min = currKeys and max = currKeys + CanBankMax
const min = currKeys;
setMinKeys = min <= userMinKeys ? userMinKeys : min;
const max = currKeys + rKeysCanBankMax;
setMaxKeys = max >= userMaxKeys ? userMaxKeys : max < 1 ? 1 : max;
if (setMinKeys === setMaxKeys) {
setMaxKeys += 1;
} else if (setMinKeys > setMaxKeys) {
setMaxKeys = setMinKeys + 2;
}
//
} else if (isSellingKeys) {
// If selling - we need to set min = currKeys - CanSell and max = currKeys
const min = currKeys - rKeysCanSell;
setMinKeys = min <= userMinKeys ? userMinKeys : min;
const max = currKeys;
setMaxKeys = max >= userMaxKeys ? userMaxKeys : max < 1 ? 1 : max;
if (setMinKeys === setMaxKeys) {
setMinKeys -= 1;
} else if (setMinKeys > setMaxKeys) {
setMaxKeys = setMinKeys + 1;
}
//
} else if (isBankingKeys && isEnableKeyBanking) {
// If banking - we need to set min = currKeys - CanBankMin and max = currKeys + CanBankMax
const min = currKeys - rKeysCanBankMin;
setMinKeys = min <= userMinKeys ? userMinKeys : min;
const max = currKeys + rKeysCanBankMax;
setMaxKeys = max >= userMaxKeys ? userMaxKeys : max < 1 ? 1 : max;
if (setMinKeys === setMaxKeys) {
setMinKeys -= 1;
} else if (setMaxKeys - setMinKeys === 1) {
setMaxKeys += 1;
} else if (setMinKeys > setMaxKeys) {
// When banking, the bot should be able to both buy and sell keys.
setMaxKeys = setMinKeys + 2;
}
// When banking, the bot should be able to both buy and sell keys.
if (setMaxKeys === currKeys) {
setMaxKeys += 1;
}
if (currKeys >= setMaxKeys) {
setMaxKeys = currKeys + 1;
}
}
const opt = this.bot.options;
const isBanking =
isBankingKeys &&
isEnableKeyBanking &&
(!this.status.alreadyUpdatedToBank ||
rKeysCanBankMin !== this.oldAmount.keysCanBankMin ||
rKeysCanBankMax !== this.oldAmount.keysCanBankMax ||
currKeys !== this.oldAmount.ofKeys);
const isTooManyRefWhileBanking =
isBankingBuyKeysWithEnoughRefs &&
isEnableKeyBanking &&
(!this.status.alreadyUpdatedToBuy ||
rKeysCanBankMax !== this.oldAmount.keysCanBuy ||
currKeys !== this.oldAmount.ofKeys);
const buyKeys =
isBuyingKeys &&
(!this.status.alreadyUpdatedToBuy ||
rKeysCanBuy !== this.oldAmount.keysCanBuy ||
currKeys !== this.oldAmount.ofKeys);
const sellKeys =
isSellingKeys &&
(!this.status.alreadyUpdatedToSell ||
rKeysCanSell !== this.oldAmount.keysCanSell ||
currKeys !== this.oldAmount.ofKeys);
if (this.isActive) {
// if Autokeys already running
if (isBanking) {
// enable keys banking - if banking conditions to enable banking matched and banking is enabled
this.setOverallStatus = [false, true, false, true, false, false];
this.setOldAmount = [0, 0, rKeysCanBankMin, rKeysCanBankMax, currKeys];
this.setActiveStatus = true;
this.updateToBank(setMinKeys, setMaxKeys, currKeyPrice);
//
} else if (isTooManyRefWhileBanking) {
// enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys.
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBankMax, 0, 0, currKeys];
this.setActiveStatus = true;
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (buyKeys) {
// enable Autokeys - Buying - if buying keys conditions matched
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBuy, 0, 0, currKeys];
this.setActiveStatus = true;
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (sellKeys) {
// enable Autokeys - Selling - if selling keys conditions matched
this.setOverallStatus = [false, false, false, false, false, true];
this.setOldAmount = [rKeysCanSell, 0, 0, 0, currKeys];
this.setActiveStatus = true;
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'sell');
//
} else if (isRemoveBankingKeys && isEnableKeyBanking) {
// disable keys banking - if to conditions to disable banking matched and banking is enabled
this.setOverallStatus = [false, false, false, false, false, false];
this.setActiveStatus = false;
void this.disable(currKeyPrice);
//
} else if (isRemoveAutoKeys && !isEnableKeyBanking) {
// disable Autokeys when conditions to disable Autokeys matched
this.setOverallStatus = [false, false, false, false, false, false];
this.setActiveStatus = false;
void this.disable(currKeyPrice);
//
} else if (isAlertAdmins && !this.status.checkAlertOnLowPure) {
// alert admins when low pure
this.setOverallStatus = [false, false, true, false, false, false];
this.setActiveStatus = false;
const msg = 'I am now low on both keys and refs.';
if (opt.sendAlert.enable && opt.sendAlert.autokeys.lowPure) {
if (opt.discordWebhook.sendAlert.enable && opt.discordWebhook.sendAlert.url.main !== '') {
sendAlert('lowPure', this.bot, msg);
} else {
this.bot.messageAdmins(msg, []);
}
}
}
} else {
// if Autokeys is not running/disabled
if (this.bot.pricelist.getPrice({ priceKey: '5021;6', onlyEnabled: false }) === null) {
// if Mann Co. Supply Crate Key entry does not exist in the pricelist.json
if (isBankingKeys && isEnableKeyBanking) {
//create new Key entry and enable keys banking - if banking conditions to enable banking matched and banking is enabled
this.setOverallStatus = [false, true, false, true, false, false];
this.setOldAmount = [0, 0, rKeysCanBankMin, rKeysCanBankMax, currKeys];
this.setActiveStatus = true;
this.createToBank(setMinKeys, setMaxKeys, currKeyPrice);
//
} else if (isBankingBuyKeysWithEnoughRefs && isEnableKeyBanking) {
// enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys.
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBankMax, 0, 0, currKeys];
this.setActiveStatus = true;
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (isBuyingKeys) {
// create new Key entry and enable Autokeys - Buying - if buying keys conditions matched
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBuy, 0, 0, currKeys];
this.setActiveStatus = true;
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (isSellingKeys) {
// create new Key entry and enable Autokeys - Selling - if selling keys conditions matched
this.setOverallStatus = [false, false, false, false, false, true];
this.setOldAmount = [rKeysCanSell, 0, 0, 0, currKeys];
this.setActiveStatus = true;
this.create(setMinKeys, setMaxKeys, currKeyPrice, 'sell');
//
} else if (isAlertAdmins && !this.status.checkAlertOnLowPure) {
// alert admins when low pure
this.setOverallStatus = [false, false, true, false, false, false];
this.setActiveStatus = false;
const msg = 'I am now low on both keys and refs.';
if (opt.sendAlert.enable && opt.sendAlert.autokeys.lowPure) {
if (opt.discordWebhook.sendAlert.enable && opt.discordWebhook.sendAlert.url.main !== '') {
sendAlert('lowPure', this.bot, msg);
} else {
this.bot.messageAdmins(msg, []);
}
}
}
} else {
// if Mann Co. Supply Crate Key entry already in the pricelist.json
if (isBanking) {
// enable keys banking - if banking conditions to enable banking matched and banking is enabled
this.setOverallStatus = [false, true, false, true, false, false];
this.setOldAmount = [0, 0, rKeysCanBankMin, rKeysCanBankMax, currKeys];
this.setActiveStatus = true;
this.updateToBank(setMinKeys, setMaxKeys, currKeyPrice);
//
} else if (isTooManyRefWhileBanking) {
// enable keys banking - if refs > minRefs but Keys < minKeys, will buy keys.
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBankMax, 0, 0, currKeys];
this.setActiveStatus = true;
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (buyKeys) {
// enable Autokeys - Buying - if buying keys conditions matched
this.setOverallStatus = [true, false, false, false, true, false];
this.setOldAmount = [0, rKeysCanBuy, 0, 0, currKeys];
this.setActiveStatus = true;
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'buy');
//
} else if (sellKeys) {
// enable Autokeys - Selling - if selling keys conditions matched
this.setOverallStatus = [false, false, false, false, false, true];
this.setOldAmount = [rKeysCanSell, 0, 0, 0, currKeys];
this.setActiveStatus = true;
this.update(setMinKeys, setMaxKeys, currKeyPrice, 'sell');
//
} else if (isAlertAdmins && !this.status.checkAlertOnLowPure) {
// alert admins when low pure
this.setOverallStatus = [false, false, true, false, false, false];
this.setActiveStatus = false;
const msg = 'I am now low on both keys and refs.';
if (opt.sendAlert.enable && opt.sendAlert.autokeys.lowPure) {
if (opt.discordWebhook.sendAlert.enable && opt.discordWebhook.sendAlert.url.main !== '') {
sendAlert('lowPure', this.bot, msg);
} else {
this.bot.messageAdmins(msg, []);
}
}
}
}
}
log.debug(
`Autokeys status:-\n Ref: minRef(${Currencies.toRefined(userMinRef)})` +
` < currRef(${Currencies.toRefined(currRef)})` +
` < maxRef(${Currencies.toRefined(userMaxRef)})` +
`\n Key: minKeys(${userMinKeys}) ≤ currKeys(${currKeys}) ≤ maxKeys(${userMaxKeys})` +
`\nStatus: ${
isBankingKeys && isEnableKeyBanking && this.isActive
? `Banking (Min: ${setMinKeys}, Max: ${setMaxKeys})`
: isBuyingKeys && this.isActive
? `Buying (Min: ${setMinKeys}, Max: ${setMaxKeys})`
: isSellingKeys && this.isActive
? `Selling (Min: ${setMinKeys}, Max: ${setMaxKeys})`
: 'Not active'
}`
);
}
private static generateEntry(enabled: boolean, min: number, max: number, intent: 0 | 1 | 2): EntryData {
return {
sku: '5021;6',
enabled: enabled,
autoprice: true,
min: min,
max: max,
intent: intent
};
}
private static setManual(entry: EntryData, keyPrices: KeyPrices): EntryData {
entry.autoprice = false;
entry.buy = {
keys: 0,
metal: keyPrices.buy.metal
};
entry.sell = {
keys: 0,
metal: keyPrices.sell.metal
};
return entry;
}
private setWithScrapAdjustment(entry: EntryData, keyPrices: KeyPrices, type: 'buy' | 'sell'): EntryData {
const optSA = this.bot.options.autokeys.scrapAdjustment;
const scrapAdjustment = genScrapAdjustment(optSA.value, optSA.enable);
entry.autoprice = false;
entry.buy = {
keys: 0,
metal: Currencies.toRefined(
keyPrices.buy.toValue() + (type === 'buy' ? scrapAdjustment.value : -scrapAdjustment.value)
)
};
entry.sell = {
keys: 0,
metal: Currencies.toRefined(
keyPrices.sell.toValue() + (type === 'buy' ? scrapAdjustment.value : -scrapAdjustment.value)
)
};
return entry;
}
private onError(
setActive: boolean,
msg: string,
isEnableSend: boolean,
sendToDiscord: boolean,
type: string
): void {
this.setActiveStatus = setActive;
log.warn(msg);
if (isEnableSend) {
if (sendToDiscord) {
sendAlert(type as AlertType, this.bot, msg);
} else {
this.bot.messageAdmins(msg, []);
}
}
}
private createToBank(minKeys: number, maxKeys: number, keyPrices: KeyPrices): void {
let entry = Autokeys.generateEntry(true, minKeys, maxKeys, 2);
if (keyPrices.src === 'manual') {
entry = Autokeys.setManual(entry, keyPrices);
}
this.bot.pricelist
.addPrice({ entryData: entry, emitChange: true, src: PricelistChangedSource.Autokeys })
.then(() => log.debug(`✅ Automatically added Mann Co. Supply Crate Key to bank.`))
.catch(err => {
const opt2 = this.bot.options;
this.onError(
false,
`❌ Failed to add Mann Co. Supply Crate Key to bank automatically: ${(err as Error).message}`,
opt2.sendAlert.enable && opt2.sendAlert.autokeys.failedToAdd,
opt2.discordWebhook.sendAlert.enable && opt2.discordWebhook.sendAlert.url.main !== '',
'autokeys-failedToAdd-bank'
);
});
}
private create(minKeys: number, maxKeys: number, keyPrices: KeyPrices, intent: 'buy' | 'sell'): void {
let entry = Autokeys.generateEntry(true, minKeys, maxKeys, intent === 'buy' ? 0 : 1);
if (keyPrices.src === 'manual' && !this.isEnableScrapAdjustment) {
entry = Autokeys.setManual(entry, keyPrices);
} else if (this.isEnableScrapAdjustment) {
entry = this.setWithScrapAdjustment(entry, keyPrices, intent);
}
this.bot.pricelist
.addPrice({ entryData: entry, emitChange: true, src: PricelistChangedSource.Autokeys })
.then(() => log.debug(`✅ Automatically added Mann Co. Supply Crate Key to ${intent}.`))
.catch(err => {
const opt2 = this.bot.options;
this.onError(
false,
`❌ Failed to add Mann Co. Supply Crate Key to ${intent} automatically: ${(err as Error).message}`,
opt2.sendAlert.enable && opt2.sendAlert.autokeys.failedToAdd,
opt2.discordWebhook.sendAlert.enable && opt2.discordWebhook.sendAlert.url.main !== '',
`autokeys-failedToAdd-${intent}`
);
});
}
private updateToBank(minKeys: number, maxKeys: number, keyPrices: KeyPrices): void {
let entry = Autokeys.generateEntry(true, minKeys, maxKeys, 2);
if (keyPrices.src === 'manual') {
entry = Autokeys.setManual(entry, keyPrices);
}
this.bot.pricelist
.updatePrice({
priceKey: entry.sku,
entryData: entry,
emitChange: true,
src: PricelistChangedSource.Autokeys
})
.then(() => log.debug(`✅ Automatically updated Mann Co. Supply Crate Key to bank.`))
.catch(err => {
const opt2 = this.bot.options;
this.onError(
false,
`❌ Failed to update Mann Co. Supply Crate Key to bank automatically: ${(err as Error).message}`,
opt2.sendAlert.enable && opt2.sendAlert.autokeys.failedToUpdate,
opt2.discordWebhook.sendAlert.enable && opt2.discordWebhook.sendAlert.url.main !== '',
'autokeys-failedToUpdate-bank'
);
});
}
private update(minKeys: number, maxKeys: number, keyPrices: KeyPrices, intent: 'buy' | 'sell'): void {
let entry = Autokeys.generateEntry(true, minKeys, maxKeys, intent === 'buy' ? 0 : 1);
if (keyPrices.src === 'manual' && !this.isEnableScrapAdjustment) {
entry = Autokeys.setManual(entry, keyPrices);
} else if (this.isEnableScrapAdjustment) {
entry = this.setWithScrapAdjustment(entry, keyPrices, intent);
}
this.bot.pricelist
.updatePrice({
priceKey: entry.sku,
entryData: entry,
emitChange: true,
src: PricelistChangedSource.Autokeys
})
.then(() => log.debug(`✅ Automatically update Mann Co. Supply Crate Key to ${intent}.`))
.catch(err => {
const opt2 = this.bot.options;
this.onError(
false,
`❌ Failed to update Mann Co. Supply Crate Key to ${intent} automatically: ${
(err as Error).message
}`,
opt2.sendAlert.enable && opt2.sendAlert.autokeys.failedToUpdate,
opt2.discordWebhook.sendAlert.enable && opt2.discordWebhook.sendAlert.url.main !== '',
`autokeys-failedToUpdate-${intent}`
);
});
}
disable(keyPrices: KeyPrices): Promise<void> {
return new Promise((resolve, reject) => {
const match = this.bot.pricelist.getPrice({ priceKey: '5021;6', onlyEnabled: false });
if (match === null) {
return resolve();
}
if (!match.enabled) {
return resolve();
}
let entry = Autokeys.generateEntry(false, 0, 1, 2);
if (keyPrices.src === 'manual') {
entry = Autokeys.setManual(entry, keyPrices);
}
this.bot.pricelist
.updatePrice({
priceKey: entry.sku,
entryData: entry,
emitChange: true,
src: PricelistChangedSource.Autokeys
})
.then(() => {
log.debug('✅ Automatically disabled Autokeys.');
resolve();
})
.catch(err => {
const opt2 = this.bot.options;
this.onError(
true,
`❌ Failed to disable Autokeys: ${(err as Error).message}`,
opt2.sendAlert.enable && opt2.sendAlert.autokeys.failedToDisable,
opt2.discordWebhook.sendAlert.enable && opt2.discordWebhook.sendAlert.url.main !== '',
'autokeys-failedToDisable'
);
reject();
});
});
}
refresh(): void {
this.setOverallStatus = [false, false, false, false, false, false];
this.setActiveStatus = false;
this.check();
}
}
type AlertType =
| 'autokeys-failedToDisable'
| 'autokeys-failedToAdd-bank'
| 'autokeys-failedToAdd-sell'
| 'autokeys-failedToAdd-buy'
| 'autokeys-failedToUpdate-bank'
| 'autokeys-failedToUpdate-sell'
| 'autokeys-failedToUpdate-buy';