Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔨 Update getPricelist code for prices.tf #1686

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 43 additions & 21 deletions src/lib/pricer/pricestf/prices-tf-pricer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import timersPromises from 'timers/promises';
import Currencies from '@tf2autobot/tf2-currencies';
import PricesTfSocketManager from './prices-tf-socket-manager';
import IPricer, {
Expand All @@ -8,12 +7,14 @@ import IPricer, {
PricerOptions,
RequestCheckResponse
} from '../../../classes/IPricer';
import PricesTfApi, { PricesTfItem, PricesTfItemMessageEvent } from './prices-tf-api';
import PricesTfApi, { PricesTfGetPricesResponse, PricesTfItem, PricesTfItemMessageEvent } from './prices-tf-api';
import log from '../../logger';

export default class PricesTfPricer implements IPricer {
private socketManager: PricesTfSocketManager;

private attempts = 0;

public constructor(private api: PricesTfApi) {
this.socketManager = new PricesTfSocketManager(api);
}
Expand Down Expand Up @@ -42,35 +43,56 @@ export default class PricesTfPricer implements IPricer {
}

async getPricelist(): Promise<GetPricelistResponse> {
try {
const pricelist = await PricesTfApi.apiRequest(
'GET',
'/json/pricelist-array',
undefined,
undefined,
undefined,
'https://autobot.tf'
);

return pricelist;
} catch (err) {
log.error('Failed to get pricelist from autobot.tf: ', err);
if (JSON.parse(process.env.DEV) === true) {
try {
const pricelist = await PricesTfApi.apiRequest(
'GET',
'/json/pricelist-array',
undefined,
undefined,
undefined,
'https://autobot.tf'
);

return pricelist;
} catch (err) {
log.error('Failed to get pricelist from autobot.tf: ', err);
}
}

let prices: PricesTfItem[] = [];
let currentPage = 1;
let totalPages = 0;

let delay = 0;
const minDelay = 200;
const minDelay = 100;
let response: PricesTfGetPricesResponse;

log.debug('Requesting pricelist pages...');

do {
await timersPromises.setTimeout(delay);
await new Promise(resolve => setTimeout(resolve, delay));
const start = new Date().getTime();
log.debug('Requesting pricelist pages...');
const response = await this.api.getPricelistPage(currentPage);
totalPages = response.meta.totalPages;
currentPage++;

try {
log.debug(`Getting page ${currentPage}${totalPages === 0 ? '' : ` of ${totalPages}`}...`);
response = await this.api.getPricelistPage(currentPage);
currentPage++;
totalPages = response.meta.totalPages;
} catch (e) {
if (currentPage > 1) {
await new Promise(resolve => setTimeout(resolve, 60 * 1000));
continue;
} else {
if (this.attempts < 3) {
this.attempts++;
return this.getPricelist();
}

this.attempts = 0;
throw e;
}
}

prices = prices.concat(response.items);
const time = new Date().getTime() - start;
Expand Down