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
178 changes: 73 additions & 105 deletions code.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,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 +167,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 @@ -1250,7 +1252,11 @@
css('background', COLOR_ERROR);
}

next();
const delay = sellQueue.length() > 0
? parseInt(getSettingWithDefault(SETTING_DELAY_BETWEEN_MARKET_ACTIONS), 10) * 1000
: 0;

setTimeout(() => next(), delay);
Sadzurami marked this conversation as resolved.
Show resolved Hide resolved
}
);
},
Expand Down Expand Up @@ -1722,33 +1728,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 +2405,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 +2418,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 +2501,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 +2698,19 @@
item,
false,
(success) => {
let delay = parseInt(getSettingWithDefault(SETTING_DELAY_BETWEEN_MARKET_ACTIONS), 10) * 1000;

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

if (success) {
setTimeout(
() => {
increaseMarketProgress();
next();
},
getRandomInt(1000, 1500)
);
delay = marketOverpricedQueue.length() > 0 ? Math.max(delay, getRandomInt(1000, 1500)) : 0;
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 +2817,24 @@
listingid,
false,
(success) => {
let delay = parseInt(getSettingWithDefault(SETTING_DELAY_BETWEEN_MARKET_ACTIONS), 10) * 1000;

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

if (success) {
setTimeout(
() => {
increaseMarketProgress();
next();
},
getRandomInt(50, 100)
);
delay = marketRemoveQueue.length() > 0 ? Math.max(delay, getRandomInt(50, 100)) : 0;
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 +2874,18 @@

const marketListingsItemsQueue = async.queue(
(listing, next) => {
const callback = () => {
let 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 +2898,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 +3773,10 @@
Market items per page: 
<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 +3796,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