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

Add adjustable delay between market actions #220

Merged
merged 20 commits into from
Aug 12, 2024
204 changes: 99 additions & 105 deletions code.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
const spinnerBlock =
'<div class="spinner"><div class="rect1"></div>&nbsp;<div class="rect2"></div>&nbsp;<div class="rect3"></div>&nbsp;<div class="rect4"></div>&nbsp;<div class="rect5"></div>&nbsp;</div>';
let numberOfFailedRequests = 0;
let marketRateLimitReached = false;

const enableConsoleLog = false;

Expand Down Expand Up @@ -148,6 +149,7 @@
const SETTING_LAST_CACHE = 'SETTING_LAST_CACHE';
const SETTING_RELIST_AUTOMATICALLY = 'SETTING_RELIST_AUTOMATICALLY';
const SETTING_MARKET_PAGE_COUNT = 'SETTING_MARKET_PAGE_COUNT';
const SETTING_DELAY_BETWEEN_MARKET_ACTIONS = 'SETTING_DELAY_BETWEEN_MARKET_ACTIONS';

const settingDefaults = {
SETTING_MIN_NORMAL_PRICE: 0.05,
Expand All @@ -166,7 +168,8 @@
SETTING_QUICK_SELL_BUTTONS: 1,
SETTING_LAST_CACHE: 0,
SETTING_RELIST_AUTOMATICALLY: 0,
SETTING_MARKET_PAGE_COUNT: 100
SETTING_MARKET_PAGE_COUNT: 100,
SETTING_DELAY_BETWEEN_MARKET_ACTIONS: 0
};

function getSettingWithDefault(name) {
Expand Down Expand Up @@ -465,6 +468,13 @@
error: function(data) {
return callback(ERROR_FAILED, data);
},
statusCode: {
200: () => (marketRateLimitReached = false),
429: () => (marketRateLimitReached = true),
500: () => (marketRateLimitReached = false),
501: () => (marketRateLimitReached = false),
502: () => (marketRateLimitReached = false)
},
dataType: 'json'
});
};
Expand All @@ -488,6 +498,13 @@
error: function() {
return callback(ERROR_FAILED);
},
statusCode: {
Sadzurami marked this conversation as resolved.
Show resolved Hide resolved
200: () => (marketRateLimitReached = false),
429: () => (marketRateLimitReached = true),
500: () => (marketRateLimitReached = false),
501: () => (marketRateLimitReached = false),
502: () => (marketRateLimitReached = false)
},
dataType: 'json'
});
return;
Expand All @@ -505,6 +522,13 @@
error: function() {
return callback(ERROR_FAILED);
},
statusCode: {
200: () => (marketRateLimitReached = false),
429: () => (marketRateLimitReached = true),
500: () => (marketRateLimitReached = false),
501: () => (marketRateLimitReached = false),
502: () => (marketRateLimitReached = false)
},
dataType: 'json'
});
};
Expand Down Expand Up @@ -1250,7 +1274,11 @@
css('background', COLOR_ERROR);
}

next();
let delay = parseInt(getSettingWithDefault(SETTING_DELAY_BETWEEN_MARKET_ACTIONS), 10) * 1000 || 0;
delay = marketRateLimitReached ? Math.max(delay, getRandomInt(90000, 150000)) : delay;
delay = sellQueue.length() > 0 ? delay : 0;

setTimeout(() => next(), delay);
Sadzurami marked this conversation as resolved.
Show resolved Hide resolved
}
);
},
Expand Down Expand Up @@ -1722,33 +1750,22 @@
item,
item.ignoreErrors,
(success, cached) => {
let delay = 0;

if (success) {
setTimeout(
() => {
next();
},
cached ? 0 : getRandomInt(1000, 1500)
);
delay = Math.max(delay, getRandomInt(1000, 1500));
setTimeout(() => next(), cached ? 0 : delay);
} else {
if (!item.ignoreErrors) {
item.ignoreErrors = true;
itemQueue.push(item);
}

const delay = numberOfFailedRequests > 1
? getRandomInt(30000, 45000)
: getRandomInt(1000, 1500);
delay = Math.max(delay, numberOfFailedRequests > 1 ? getRandomInt(30000, 45000) : getRandomInt(1000, 1500));

if (numberOfFailedRequests > 3) {
numberOfFailedRequests = 0;
}
numberOfFailedRequests = numberOfFailedRequests > 3 ? 0 : numberOfFailedRequests;

setTimeout(
() => {
next();
},
cached ? 0 : delay
);
setTimeout(() => next(), cached ? 0 : delay);
}
}
);
Expand Down Expand Up @@ -2410,13 +2427,11 @@
item,
false,
(success, cached) => {
let delay = 0;

if (success) {
setTimeout(
() => {
next();
},
cached ? 0 : getRandomInt(1000, 1500)
);
delay = Math.max(delay, getRandomInt(1000, 1500));
setTimeout(() => next(), cached ? 0 : delay);
} else {
if (!item.ignoreErrors) {
item.ignoreErrors = true;
Expand All @@ -2425,17 +2440,11 @@

numberOfFailedRequests++;

const delay = numberOfFailedRequests > 1
? getRandomInt(30000, 45000)
: getRandomInt(1000, 1500);
delay = Math.max(delay, numberOfFailedRequests > 1 ? getRandomInt(30000, 45000) : getRandomInt(1000, 1500));

if (numberOfFailedRequests > 3) {
numberOfFailedRequests = 0;
}
numberOfFailedRequests = numberOfFailedRequests > 3 ? 0 : numberOfFailedRequests;

setTimeout(() => {
next();
}, cached ? 0 : delay);
setTimeout(() => next(), cached ? 0 : delay);
}
}
);
Expand Down Expand Up @@ -2514,28 +2523,19 @@
listing,
false,
(success, cached) => {
let delay = 0;

const callback = () => {
increaseMarketProgress();
next();
}

if (success) {
setTimeout(
() => {
increaseMarketProgress();
next();
},
cached ? 0 : getRandomInt(1000, 1500)
);
delay = marketListingsQueue.length() > 0 ? Math.max(delay, getRandomInt(1000, 1500)) : 0;
setTimeout(callback, cached ? 0 : delay);
} else {
setTimeout(
() => {
marketListingsQueueWorker(
listing,
true,
() => {
increaseMarketProgress();
next(); // Go to the next queue item, regardless of success.
}
);
},
cached ? 0 : getRandomInt(30000, 45000)
);
delay = marketListingsQueue.length() > 0 ? Math.max(delay, getRandomInt(30000, 45000)) : 0;
setTimeout(() => marketListingsQueueWorker(listing, true, callback), cached ? 0 : delay);
}
}
);
Expand Down Expand Up @@ -2720,28 +2720,21 @@
item,
false,
(success) => {
let delay = parseInt(getSettingWithDefault(SETTING_DELAY_BETWEEN_MARKET_ACTIONS), 10) * 1000 || 0;
delay = delay || getRandomInt(1000, 1500);
delay = marketRateLimitReached ? Math.max(delay, getRandomInt(90000, 150000)) : delay;
delay = marketOverpricedQueue.length() > 0 ? delay : 0

const callback = () => {
increaseMarketProgress();
next();
};

if (success) {
setTimeout(
() => {
increaseMarketProgress();
next();
},
getRandomInt(1000, 1500)
);
setTimeout(callback, delay);
} else {
setTimeout(
() => {
marketOverpricedQueueWorker(
item,
true,
() => {
increaseMarketProgress();
next(); // Go to the next queue item, regardless of success.
}
);
},
getRandomInt(30000, 45000)
);
delay = Math.max(delay, getRandomInt(30000, 45000));
setTimeout(marketOverpricedQueueWorker(item, true, callback), delay);
}
}
);
Expand Down Expand Up @@ -2848,33 +2841,26 @@
listingid,
false,
(success) => {
let delay = parseInt(getSettingWithDefault(SETTING_DELAY_BETWEEN_MARKET_ACTIONS), 10) * 1000 || 0;
delay = delay || getRandomInt(50, 100);
delay = marketRateLimitReached ? Math.max(delay, getRandomInt(90000, 150000)) : delay;
delay = marketRemoveQueue.length() > 0 ? delay : 0

const callback = () => {
increaseMarketProgress();
next();
};

if (success) {
setTimeout(
() => {
increaseMarketProgress();
next();
},
getRandomInt(50, 100)
);
setTimeout(callback, delay);
} else {
setTimeout(
() => {
marketRemoveQueueWorker(
listingid,
true,
() => {
increaseMarketProgress();
next(); // Go to the next queue item, regardless of success.
}
);
},
getRandomInt(30000, 45000)
);
delay = Math.max(delay, getRandomInt(30000, 45000));
setTimeout(() => marketRemoveQueueWorker(listingid, true, callback), delay);
}
}
);
},
10
1
);

function marketRemoveQueueWorker(listingid, ignoreErrors, callback) {
Expand Down Expand Up @@ -2914,12 +2900,18 @@

const marketListingsItemsQueue = async.queue(
(listing, next) => {
const callback = () => {
const delay = 0;

increaseMarketProgress();
setTimeout(() => next(), delay);
};

$.get(
`${window.location.origin}/market/mylistings?count=100&start=${listing}`,
(data) => {
if (!data || !data.success) {
increaseMarketProgress();
next();
callback();
return;
}

Expand All @@ -2932,16 +2924,13 @@
// g_rgAssets
unsafeWindow.MergeWithAssetArray(data.assets); // This is a method from Steam.

increaseMarketProgress();
next();
callback()
},
'json'
).
fail(() => {
increaseMarketProgress();
next();
).fail(() => {
callback();
return;
});
});
},
1
);
Expand Down Expand Up @@ -3810,6 +3799,10 @@
Market items per page:&nbsp;
<input type="number" min="1" step="5" id="${SETTING_MARKET_PAGE_COUNT}" value=${getSettingWithDefault(SETTING_MARKET_PAGE_COUNT)}>
</div>
<div style="margin-top:6px;">
Delay in seconds between market actions:&nbsp;
<input type="number" min="0" step="1" id="${SETTING_DELAY_BETWEEN_MARKET_ACTIONS}" value=${getSettingWithDefault(SETTING_DELAY_BETWEEN_MARKET_ACTIONS)}>
</div>
<div style="margin-top:6px;">
Automatically relist overpriced market listings (slow on large inventories):&nbsp;
<input id="${SETTING_RELIST_AUTOMATICALLY}" class="market_relist_auto" type="checkbox" ${getSettingWithDefault(SETTING_RELIST_AUTOMATICALLY) == 1 ? 'checked' : ''}>
Expand All @@ -3829,6 +3822,7 @@
setSetting(SETTING_PRICE_IGNORE_LOWEST_Q, $(`#${SETTING_PRICE_IGNORE_LOWEST_Q}`, price_options).prop('checked') ? 1 : 0);
setSetting(SETTING_PRICE_HISTORY_HOURS, $(`#${SETTING_PRICE_HISTORY_HOURS}`, price_options).val());
setSetting(SETTING_MARKET_PAGE_COUNT, $(`#${SETTING_MARKET_PAGE_COUNT}`, price_options).val());
setSetting(SETTING_DELAY_BETWEEN_MARKET_ACTIONS, $(`#${SETTING_DELAY_BETWEEN_MARKET_ACTIONS}`, price_options).val());
setSetting(SETTING_RELIST_AUTOMATICALLY, $(`#${SETTING_RELIST_AUTOMATICALLY}`, price_options).prop('checked') ? 1 : 0);
setSetting(SETTING_INVENTORY_PRICE_LABELS, $(`#${SETTING_INVENTORY_PRICE_LABELS}`, price_options).prop('checked') ? 1 : 0);
setSetting(SETTING_TRADEOFFER_PRICE_LABELS, $(`#${SETTING_TRADEOFFER_PRICE_LABELS}`, price_options).prop('checked') ? 1 : 0);
Expand Down