-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_poller.ts
636 lines (585 loc) · 20.5 KB
/
data_poller.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
import { GetTradeOffers } from "./steam_api/requests/IEconService.ts";
import { EOfferFilter } from "./enums/EOfferFilter.ts";
import type { TradeManager } from "./trade_manager.ts";
import { SteamApi } from "./steam_api/mod.ts";
import { Deferred } from "./deferred.ts";
import { isNonTerminalState, TradeOffer } from "./trade_offer.ts";
import { ETradeOfferState } from "./enums/ETradeOfferState.ts";
import { EConfirmationMethod } from "./enums/EConfirmationMethod.ts";
import { fastConcat, hasNoName } from "./utils.ts";
const miminumPollInterval = 1000;
export type DataPollerOptions = {
/**
* Time in milliseconds. steam api will be polled every x milliseconds set by this interval. defaults to 30 seconds.
*
* If you set this to a negative number, you have to call `doPoll()` yourself.
*/
interval?: number;
manager: TradeManager;
/** will be used to save and load poll data. **both** save and load functions must be defined **together**. */
savePollData?: (
pollData: PollData,
username?: string,
) => Promise<void> | void;
/** will be used to save and load poll data. **both** save and load functions must be defined **together**. */
loadPollData?:
| ((username: string) => Promise<PollData | undefined>)
| ((username: string) => PollData | undefined);
};
export type PollData = {
/** STANDARD unix time. This is the latest updated offer time stamp of last poll. */
offersSince: number;
/** status of sent offers in format of Record<offerid, ETradeOfferState> */
sent: Record<string, number>;
/** status of received offers in format of Record<offerid, ETradeOfferState> */
received: Record<string, number>;
/** timestamps of offer.created in format of Record<offerid, timestamp> */
timestamps: Record<string, number>;
/** per offer custom cancelTimes in format of Record<offerid, number> (milliseconds) */
cancelTimes: Record<string, number>;
/** per offer custom pendingCancelTimes in format of Record<offerid, number> (milliseconds) */
pendingCancelTimes: Record<string, number>;
};
export class DataPoller {
private manager: TradeManager;
private steamApi: SteamApi;
private pollTimer: number | undefined;
private polling = false;
/** timestamp in milliseconds. same type as Date.now() */
private lastPoll: number;
private stopped = false;
// deno-lint-ignore no-explicit-any
private defferedPoll: Deferred<any> | undefined;
private savePollData;
private loadPollData;
private loadedPollData = false;
pollData: PollData;
interval: number;
constructor(options: DataPollerOptions) {
if (!options) throw new Error("DataPollerOptions cannot be empty");
const { interval, manager, loadPollData, savePollData } = options;
this.manager = manager;
this.steamApi = manager.steamApi;
this.interval = interval || 30 * 1000;
this.lastPoll = 0;
if (loadPollData && savePollData) {
this.loadPollData = loadPollData;
this.savePollData = savePollData;
}
this.pollData = {
sent: {},
received: {},
timestamps: {},
offersSince: 0,
cancelTimes: {},
pendingCancelTimes: {},
};
}
start() {
this.stopped = false;
if (this.polling || this.defferedPoll?.isPending() || this.interval < 0) {
return;
}
clearTimeout(this.pollTimer);
this.doPoll();
}
async stop() {
clearTimeout(this.pollTimer);
this.stopped = true;
if (this.defferedPoll) {
try {
await this.defferedPoll;
} catch {
// no need to handle
}
}
}
private deleteTimeProps(offerid: string) {
delete this.pollData.cancelTimes[offerid];
delete this.pollData.pendingCancelTimes[offerid];
}
private deleteOldProps(offerid: string) {
this.deleteTimeProps(offerid);
delete this.pollData.sent[offerid];
delete this.pollData.received[offerid];
delete this.pollData.timestamps[offerid];
}
private pruneOfferEntry(
entry: [string, number],
) {
const [offerid, offerState] = entry;
if (
isNonTerminalState(offerState) ||
(this.pollData.timestamps[offerid] &&
this.pollData.offersSince - 1800 < this.pollData.timestamps[offerid])
) {
return;
}
this.manager.evt.post([
"debug",
"cleaning offerid " + offerid + " from pollData",
]);
this.deleteOldProps(offerid);
}
private tryDeleteOldProps() {
Object.entries(this.pollData.received).forEach((entry) =>
this.pruneOfferEntry(entry)
);
Object.entries(this.pollData.sent).forEach((entry) =>
this.pruneOfferEntry(entry)
);
}
async doPoll(doFullUpdate?: boolean) {
if (this.polling) return;
this.polling = true;
this.defferedPoll = new Deferred();
const timeSinceLastPoll = Date.now() - this.lastPoll;
const doingPollTooFast = timeSinceLastPoll < miminumPollInterval;
try {
// don't poll when key is not set
if (!this.steamApi.hasApiKey()) {
return;
}
// don't poll if we don't have SteamID or username of this account.
if (
!this.manager.steamCommunity.steamID ||
!this.manager.steamCommunity.username
) {
return;
}
await this.tryLoadPollData();
// never allow faster than 1 second
if (doingPollTooFast) {
// handled by finally block
return;
}
let offersSince = 0;
let fullUpdate = false || doFullUpdate;
if (this.pollData.offersSince && !doFullUpdate) {
// It looks like sometimes Steam can be dumb and backdate a modified offer. We need to handle this.
// Let's add a 30-minute buffer.
offersSince = this.pollData.offersSince - 1800;
} else {
fullUpdate = true;
// Get offers up to 6 months ago
offersSince = Math.floor(
new Date(Date.now() - 15552000000).getTime() / 1000,
);
}
const getOffersOptions = {
filter: fullUpdate ? EOfferFilter.All : EOfferFilter.ActiveOnly,
historicalCutoff: offersSince,
};
this.manager.evt.post(
[
"debug",
`Doing trade offer poll since ${offersSince}${
fullUpdate ? " (full update)" : ""
}`,
],
);
const apiresp = await this.getOffers(getOffersOptions);
let hasGlitchedOffer = false;
apiresp.sentOffers.forEach((offer) => {
if (!offer.id) {
this.manager.evt.post([
"debug",
[
"Warning: an offer id in sent offers of response is not set. skipping.",
offer,
],
]);
return;
}
if (!this.pollData.sent[offer.id]) {
// We sent this offer, but we have no record of it!
// maybe someone made an offer outside the bot
// or maybe the newly sent offer has not finished yet
// either the case, only emit the `unknownOfferSent` event if currently there's no pending send offer requests
if (!this.manager.pendingSendOffersCount) {
if (offer.fromRealTimeTrade) {
// This is a real-time trade offer.
if (
offer.state === ETradeOfferState.CreatedNeedsConfirmation ||
(offer.state === ETradeOfferState.Active &&
offer.confirmationMethod != EConfirmationMethod.None)
) {
// we need to confirm this
this.manager.evt.post([
"realTimeTradeConfirmationRequired",
offer,
]);
} else if (offer.state === ETradeOfferState.Accepted) {
// both parties confirmed, trade complete
this.manager.evt.post(["realTimeTradeCompleted", offer]);
}
}
this.manager.evt.post(["unknownOfferSent", offer]);
this.pollData.sent[offer.id] = offer.state;
this.pollData.timestamps[offer.id] = offer.updated!.getTime() /
1000;
}
} else if (offer.state !== this.pollData.sent[offer.id]) {
if (!offer.isGlitched()) {
// We sent this offer, and it has now changed state
if (
offer.fromRealTimeTrade &&
offer.state == ETradeOfferState.Accepted
) {
this.manager.evt.post(["realTimeTradeCompleted", offer]);
}
this.manager.evt.post([
"sentOfferChanged",
[offer, this.pollData.sent[offer.id] as ETradeOfferState],
]);
this.pollData.sent[offer.id] = offer.state;
this.pollData.timestamps[offer.id] = offer.updated!.getTime() /
1000;
} else {
hasGlitchedOffer = true;
let countWithoutName = 0;
if (this.manager.getDescriptions) {
countWithoutName = offer.itemsToGive.filter(hasNoName).length +
offer.itemsToReceive.filter(hasNoName).length;
}
this.manager.evt.post([
"debug",
"Not emitting sentOfferChanged for " + offer.id +
" right now because it's glitched (" +
offer.itemsToGive.length + " to give, " +
offer.itemsToReceive.length + " to receive," +
countWithoutName + " without name",
]);
}
}
if (offer.state === ETradeOfferState.Active) {
// The offer is still Active, and we sent it. See if it's time to cancel it automatically.
const cancelTime = this.pollData.cancelTimes[offer.id] === undefined
? this.manager.cancelTime
: this.pollData.cancelTimes[offer.id];
if (
cancelTime &&
(Date.now() - offer.updated!.getTime() >= cancelTime)
) {
const offerid = offer.id;
offer.cancel().then(() => {
this.deleteTimeProps(offerid);
this.manager.evt.post(["sentOfferCanceled", [
offer,
"cancelTime",
]]);
}).catch((err) =>
this.manager.evt.post(
[
"debug",
"Can't auto-cancel offer #" + offerid + ": " + err.message,
],
)
);
}
}
if (
offer.state == ETradeOfferState.CreatedNeedsConfirmation
) {
// The offer needs to be confirmed to be sent. Let's see if the maximum time has elapsed before we cancel it.
const pendingCancelTime =
this.pollData.pendingCancelTimes[offer.id] === undefined
? this.manager.pendingCancelTime
: this.pollData.pendingCancelTimes[offer.id];
if (
pendingCancelTime &&
(Date.now() - offer.created!.getTime() >= pendingCancelTime)
) {
const offerid = offer.id;
offer.cancel().then(() => {
this.deleteTimeProps(offerid);
this.manager.evt.post([
"sentPendingOfferCanceled",
offer,
]);
}).catch((err) =>
this.manager.evt.post(
[
"debug",
"Can't auto-cancel pending-confirmation offer #" + offer.id +
": " + err.message,
],
)
);
}
}
});
if (this.manager.cancelOfferCount) {
// TODO make sure works fine
let allActive;
{
const sentActive: Array<[string, number]> = apiresp.sentOffers
.filter(
(offer) => offer.state === ETradeOfferState.Active,
).map((offer) => [offer.id!, offer.state]);
const polledSentActive = Object.entries(this.pollData.sent).filter((
[_, state],
) => state === ETradeOfferState.Active);
allActive = fastConcat(sentActive, polledSentActive);
}
if (allActive.length >= this.manager.cancelOfferCount) {
const cancelThisMany = allActive.length -
this.manager.cancelOfferCount;
allActive.sort((a, b) => {
// if a is older (meaning number is smaller)
// it will move to start of array
// because smaller minus bigger is always smaller than 0
return this.pollData.timestamps[a[0]] -
this.pollData.timestamps[b[0]];
});
for (let i = 0; i < cancelThisMany; i++) {
if (allActive[i] && allActive[i][0]) {
const offerid = allActive[i][0];
if (
this.manager.cancelOfferCountMinAge &&
Date.now() - this.pollData.timestamps[offerid] <
this.manager.cancelOfferCountMinAge
) {
continue;
}
TradeOffer.fromOfferId(this.manager, offerid).then(
(tradeOffer) => {
tradeOffer.cancel().then(() => {
this.deleteTimeProps(offerid);
this.manager.evt.post([
"sentOfferCanceled",
[tradeOffer, "cancelOfferCount"],
]);
}).catch((err) => {
this.manager.evt.post([
"debug",
"Can't auto-cancel offer #" + tradeOffer.id + ": " +
err.message,
]);
});
},
);
}
}
}
}
apiresp.receivedOffers.forEach((offer) => {
if (!offer.id) {
this.manager.evt.post([
"debug",
[
"Warning: an offer id in received offers of response is not set. skipping.",
offer,
],
]);
return;
}
if (offer.isGlitched()) {
hasGlitchedOffer = true;
return;
}
if (offer.fromRealTimeTrade) {
// This is a real-time trade offer
if (
!this.pollData.received[offer.id] &&
(offer.state === ETradeOfferState.CreatedNeedsConfirmation ||
(offer.state === ETradeOfferState.Active &&
offer.confirmationMethod !== EConfirmationMethod.None))
) {
this.manager.evt.post(["realTimeTradeConfirmationRequired", offer]);
} else if (
offer.state == ETradeOfferState.Accepted &&
(!this.pollData.received[offer.id] ||
(this.pollData.received[offer.id] !== offer.state))
) {
this.manager.evt.post(["realTimeTradeCompleted", offer]);
}
}
if (
!this.pollData.received[offer.id] &&
offer.state === ETradeOfferState.Active
) {
this.manager.evt.post(["newOffer", offer]);
} else if (
this.pollData.received[offer.id] &&
offer.state !== this.pollData.received[offer.id]
) {
this.manager.evt.post([
"receivedOfferChanged",
[offer, this.pollData.received[offer.id]],
]);
}
this.pollData.received[offer.id] = offer.state;
this.pollData.timestamps[offer.id] = offer.updated!.getTime() /
1000;
});
// TODO make sure works fine
// move historical cutoff based on oldest non-terminal offer
// this should work because in the poll we are checking for any changes after offerSince,
// after successfully handling all terminal states (meaning theres no changes after the state),
// there should be no changes up to our request time or the oldest updated timestamp of offers inside.
if (!hasGlitchedOffer) {
if (
apiresp.oldestNonTerminal &&
apiresp.oldestNonTerminal < apiresp.requestedAt
) {
this.pollData.offersSince = apiresp.oldestNonTerminal;
} else {
this.pollData.offersSince = apiresp.requestedAt;
}
}
// at the end
this.tryDeleteOldProps();
this.manager.evt.post(["pollSuccess", undefined]);
await this.trySavePollData();
} catch (err) {
this.manager.evt.post(
[
"debug",
"poll failed at: " + new Date().toUTCString() +
"\n Reason: " + err.message,
],
);
this.manager.evt.post(["pollFailure", err]);
} finally {
this.lastPoll = Date.now();
this.polling = false;
this.defferedPoll.resolve();
// repeat
if (!this.stopped) {
if (this.interval >= 0) {
let delay = this.interval;
if (doingPollTooFast) {
delay = miminumPollInterval - timeSinceLastPoll;
}
setTimeout(this.doPoll.bind(this), delay);
}
}
}
}
private async tryLoadPollData() {
if (!this.loadedPollData && this.loadPollData) {
this.manager.evt.post(["debug", "loading poll data"]);
let loadedData;
try {
loadedData = await this.loadPollData(
this.manager.steamCommunity.username!,
);
} catch (err) {
this.manager.evt.post([
"debug",
"loading poll data failed. error: " + err.message,
]);
} finally {
this.loadedPollData = true;
}
if (loadedData) {
// merge current data before replacing
Object.assign(loadedData.received, this.pollData.received);
Object.assign(loadedData.sent, this.pollData.sent);
Object.assign(loadedData.timestamps, this.pollData.timestamps);
this.pollData = loadedData;
}
}
}
async trySavePollData() {
if (this.savePollData) {
this.manager.evt.post(["debug", "saving poll data"]);
try {
if (!this.manager.steamCommunity.username) {
throw new Error("steam community username is not set");
}
await this.savePollData(
this.pollData,
this.manager.steamCommunity.username,
);
} catch (err) {
this.manager.evt.post([
"debug",
"saving poll data failed. error: " + err.message,
]);
}
}
}
async getOffers(
options: {
filter?: EOfferFilter;
/** Standard Unix timestamp */
historicalCutoff?: number;
},
) {
const {
filter = EOfferFilter.All,
historicalCutoff = Math.floor(
new Date(Date.now() - 31536000000).getTime() / 1000,
),
} = options || {};
if (!EOfferFilter[filter]) {
throw new Error(
'Unexpected value "' + filter +
'" for "filter" parameter. Expected a value from the EOfferFilter enum.',
);
}
if (typeof historicalCutoff !== "number") {
throw new Error(
'Unexpected value "' + historicalCutoff +
'" for "historicalCutoff" parameter. Expected a number.',
);
}
const getTraderOffersOptions = {
"get_sent_offers": true,
"get_received_offers": true,
"get_descriptions": false,
"language": this.manager.languageName,
"active_only": filter === EOfferFilter.ActiveOnly,
"historical_only": filter === EOfferFilter.HistoricalOnly,
"time_historical_cutoff": historicalCutoff,
};
// set request time to 30 minutes prior to now.
// UNIX timestamp
const requestedAt = Math.floor((Date.now() / 1000) - 1800);
const apiresp = await this.steamApi.fetch(
new GetTradeOffers(getTraderOffersOptions),
);
let oldestNonTerminalTimestamp = Infinity;
const sentPromises = apiresp?.response?.trade_offers_sent?.map((offer) => {
if (
offer.time_updated < oldestNonTerminalTimestamp &&
isNonTerminalState(offer)
) {
oldestNonTerminalTimestamp = offer.time_updated;
}
return TradeOffer.from(this.manager, offer);
});
let sentOffers: Array<TradeOffer> = [];
const receivedPromises = apiresp?.response?.trade_offers_received?.map(
(offer) => {
if (
offer.time_updated < oldestNonTerminalTimestamp &&
isNonTerminalState(offer)
) {
oldestNonTerminalTimestamp = offer.time_updated;
}
return TradeOffer.from(this.manager, offer);
},
);
let receivedOffers: Array<TradeOffer> = [];
if (sentPromises?.length) {
sentOffers = await Promise.all(sentPromises);
}
if (receivedPromises?.length) {
receivedOffers = await Promise.all(receivedPromises);
}
const result = {
sentOffers,
receivedOffers,
/** UNIX Timestamp */
oldestNonTerminal: Number.isFinite(oldestNonTerminalTimestamp)
? oldestNonTerminalTimestamp
: undefined,
/** UNIX Timestamp */
requestedAt,
};
return result;
}
}