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

Smart listing cap handling #10

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @tf2autobot/bptf-listings

This module is a copy from original [`node-bptf-listing`](https://github.com/Nicklason/node-bptf-listings) by [Nicklason](https://github.com/Nicklason), with improved Backpack.tf inventory refresh and support buy orders for targeted items (Crates, Unusualifier, Strangifier, Fabricator, Killstreak Kit and Chemistry Set).
This module is a copy from original [`node-bptf-listings`](https://github.com/Nicklason/node-bptf-listings) by [Nicklason](https://github.com/Nicklason), with improved Backpack.tf inventory refresh and support buy orders for targeted items (Crates, Unusualifier, Strangifier, Fabricator, Killstreak Kit and Chemistry Set).

## Getting started

Expand All @@ -12,7 +12,7 @@ This module is a copy from original [`node-bptf-listing`](https://github.com/Nic

## Installation

Install it from [npm](https://www.npmjs.com/package/@tf2autobot/tf2-schema):
Install it from [npm](https://www.npmjs.com/package/@tf2autobot/bptf-listings):

npm install @tf2autobot/bptf-listings

Expand Down
43 changes: 40 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class ListingManager {
// Amount of listings to create at once
this.batchSize = options.batchSize || 100;

this.cap = null;
this._listingsCap = null;
this._listingsLimitsLastFetched = 0;
this.promotes = null;

this.listings = [];
Expand Down Expand Up @@ -317,7 +318,8 @@ class ListingManager {
.then(response => {
const body = response.data;

this.cap = body.cap;
this._listingsCap = body.cap;
this._listingsLimitsLastFetched = Date.now();
this.promotes = body.promotes_remaining;
this.listings = body.listings.filter(raw => raw.appid == 440).map(raw => new Listing(raw, this, false));

Expand Down Expand Up @@ -533,6 +535,40 @@ class ListingManager {
this._action('remove', listing.id);
}

_fetchListingsLimits(callback) {
console.debug('request!')
this._listingsLimitsLastFetched = Date.now(); // To make no additional requests, even if an error happens

const options = this.setRequestOptions('GET', '/classifieds/limits');
axios(options)
.then(response => {
const body = response.data;
this._listingsCap = body.listings.total;
})
.catch(err => {
if (err) {
return callback(err);
}
});
}

get cap() {
// Unsure about the exact API limits, so the time period is subject to change
if (Date.now() - this._listingsLimitsLastFetched > 15 * 1000) {
this._fetchListingsLimits(async (err) => {
if (err) {
console.log('Error while fetching listings limits:', err);
}
});
}
return this._listingsCap;
}

set cap(value) {
// For some simple backward compatibility
this._listingsCap = value;
}

/**
* Function used to enqueue jobs
* @param {String} type
Expand Down Expand Up @@ -665,7 +701,8 @@ class ListingManager {
// Reset values
this.ready = false;
this.listings = [];
this.cap = null;
this._listingsCap = null;
this._listingsLimitsLastFetched = Infinity; // I hope that's correct in JS
this.promotes = null;
this.actions = { create: [], remove: [], update: [] };
this._actions = { create: {}, remove: {}, update: {} };
Expand Down