Skip to content

Commit

Permalink
Add option to allow users to specify values for adaptive request limi…
Browse files Browse the repository at this point in the history
…ts (close #1684)
  • Loading branch information
rafaelgomesxyz committed Dec 4, 2020
1 parent bfc0bbb commit a0a633f
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "esgst",
"version": "8.8.5",
"betaVersion": "8.8.6-beta.17",
"betaVersion": "8.8.6-beta.18",
"title": "Enhanced SteamGifts & SteamTrades (ESGST)",
"description": "A script that adds some cool features to SteamGifts and SteamTrades.",
"author": "rafaelgssa",
Expand Down
42 changes: 36 additions & 6 deletions src/class/Queue.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { LocalStorage } from './LocalStorage';
import { Settings } from './Settings';
import { Shared } from './Shared';

interface RequestQueueList {
threshold: number;
thresholds: Record<string, number> | null;
minThresholds: Record<string, number>;
minute_limit: number;
hour_limit: number;
day_limit: number;
Expand All @@ -16,6 +19,14 @@ class _RequestQueue {
constructor() {
this.queue.sg = {
threshold: 250,
thresholds: null,
minThresholds: {
default: 0.25,
minute50: 0.5,
minute75: 1,
hourly75: 1.5,
daily75: 2,
},
minute_limit: 120,
hour_limit: 2400,
day_limit: 14400,
Expand Down Expand Up @@ -66,6 +77,10 @@ class _RequestQueue {
}
this.queue.sg.wasRequesting = false;

if (!this.queue.sg.thresholds) {
this.queue.sg.thresholds = await this.loadRequestThresholds();
}

const currentDate = new Date();
const now = currentDate.getTime();
const currentDay = currentDate.getDate();
Expand Down Expand Up @@ -98,27 +113,42 @@ class _RequestQueue {
}

if (dayTotal > this.queue.sg.day_limit * 0.75) {
this.queue.sg.threshold = 2000;
this.queue.sg.threshold = this.queue.sg.thresholds.daily75 * 1000;
} else if (hourTotal > this.queue.sg.hour_limit * 0.75) {
this.queue.sg.threshold = 1500;
this.queue.sg.threshold = this.queue.sg.thresholds.hourly75 * 1000;
} else if (minuteTotal > this.queue.sg.minute_limit * 0.75) {
this.queue.sg.threshold = 1000;
this.queue.sg.threshold = this.queue.sg.thresholds.minute75 * 1000;
} else if (minuteTotal > this.queue.sg.minute_limit * 0.5) {
this.queue.sg.threshold = 500;
this.queue.sg.threshold = this.queue.sg.thresholds.minute50 * 1000;
} else {
this.queue.sg.threshold = 250;
this.queue.sg.threshold = this.queue.sg.thresholds.default * 1000;
}

window.setTimeout(this.checkLocalRequests, 15000);
};

loadRequestThresholds = async (): Promise<Record<string, number>> => {
if (Settings.get('useCustomAdaReqLim')) {
const thresholds: Record<string, number> = {};
for (const [key, minThreshold] of Object.entries(this.queue.sg.minThresholds)) {
thresholds[key] = parseFloat(Settings.get(`customAdaReqLim_${key}`) ?? 0.0);
if (thresholds[key] < minThreshold) {
thresholds[key] = minThreshold;
}
}
return thresholds;
} else {
return { ...this.queue.sg.minThresholds };
}
};

getRequestLog = async () => {
return Shared.esgst.requestLog;
};

enqueue = (key: string): Promise<void> => {
const promise = new Promise<void>((resolve) => {
this.queue[key] = this.queue[key] ?? { threshold: 100, requests: [], numRequests: 0 };
this.queue[key] = this.queue[key] ?? { threshold: 100, requests: [], wasRequesting: false };
this.queue[key].requests.push(resolve);
this.queue[key].wasRequesting = true;
});
Expand Down
5 changes: 5 additions & 0 deletions src/class/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class _Settings {
this.aliases = {};

this.defaultValues = {
customAdaReqLim_default: 0.25,
customAdaReqLim_minute50: 0.5,
customAdaReqLim_minute75: 1,
customAdaReqLim_hourly75: 1.5,
customAdaReqLim_daily75: 2,
cgb_levelColors: [],
ge_a_sg: true,
glwc_mm_sg: false,
Expand Down
17 changes: 17 additions & 0 deletions src/eventPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ RequestQueue.setLastRequest = (key, lastRequest) => {
lastRequests[key] = lastRequest;
};

RequestQueue.loadRequestThresholds = async (key) => {
const values = await browser.storage.local.get('settings');
const settings = values.settings ? JSON.parse(values.settings) : {};
if (settings['useCustomAdaReqLim_sg']) {
const thresholds = {};
for (const [key, minThreshold] of Object.entries(RequestQueue.queue.sg.minThresholds)) {
thresholds[key] = parseFloat(settings[`customAdaReqLim_${key}`] ?? 0.0);
if (thresholds[key] < minThreshold) {
thresholds[key] = minThreshold;
}
}
return thresholds;
} else {
return { ...RequestQueue.queue.sg.minThresholds };
}
};

RequestQueue.getRequestLog = async () => {
const values = await browser.storage.local.get('requestLog');
return JSON.parse(values.requestLog);
Expand Down
17 changes: 17 additions & 0 deletions src/eventPage_sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ RequestQueue.setLastRequest = (key, lastRequest) => {
lastRequests[key] = lastRequest;
};

RequestQueue.loadRequestThresholds = async (key) => {
const values = await handle_storage(TYPE_GET, 'settings');
const settings = values.settings ? JSON.parse(values.settings) : {};
if (settings['useCustomAdaReqLim_sg']) {
const thresholds = {};
for (const [key, minThreshold] of Object.entries(RequestQueue.queue.sg.minThresholds)) {
thresholds[key] = parseFloat(settings[`customAdaReqLim_${key}`] ?? 0.0);
if (thresholds[key] < minThreshold) {
thresholds[key] = minThreshold;
}
}
return thresholds;
} else {
return { ...RequestQueue.queue.sg.minThresholds };
}
};

RequestQueue.getRequestLog = async () => {
const values = await handle_storage(TYPE_GET, 'requestLog');
return JSON.parse(values.requestLog);
Expand Down
56 changes: 56 additions & 0 deletions src/modules/Common.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,62 @@ class Common extends Module {
},
others: {
features: {
useCustomAdaReqLim: {
name: 'Use custom adaptive request limits for SteamGifts.',
sg: true,
inputItems: [
{
id: 'customAdaReqLim_default',
prefix: '1 request every ',
suffix: ' seconds (default)',
attributes: {
type: 'number',
min: '0.25',
step: '0.01',
},
},
{
id: 'customAdaReqLim_minute50',
prefix: '1 request every ',
suffix: ' seconds (after using 50% of the minute limit)',
attributes: {
type: 'number',
min: '0.5',
step: '0.01',
},
},
{
id: 'customAdaReqLim_minute75',
prefix: '1 request every ',
suffix: ' seconds (after using 75% of the minute limit)',
attributes: {
type: 'number',
min: '1',
step: '0.01',
},
},
{
id: 'customAdaReqLim_hourly75',
prefix: '1 request every ',
suffix: ' seconds (after using 75% of the hourly limit)',
attributes: {
type: 'number',
min: '1.5',
step: '0.01',
},
},
{
id: 'customAdaReqLim_daily75',
prefix: '1 request every ',
suffix: ' seconds (after using 75% of the daily limit)',
attributes: {
type: 'number',
min: '2',
step: '0.01',
},
},
],
},
notifyLogs: {
name: 'Notify about console logs.',
sg: true,
Expand Down

0 comments on commit a0a633f

Please sign in to comment.