update the curve
All checks were successful
deploy / deploy (push) Successful in 47s

This commit is contained in:
Lee 2023-10-24 12:07:48 +01:00
parent 54c9d866b8
commit 49497a9fef
2 changed files with 71 additions and 65 deletions

@ -1,63 +1,64 @@
export class FetchQueue { export class FetchQueue {
private _queue: string[]; private queue: string[];
private _rateLimitReset: number; private rateLimitReset: number;
constructor() { constructor() {
this._queue = []; this.queue = [];
this._rateLimitReset = Date.now(); this.rateLimitReset = Date.now();
} }
/** /**
* Fetches the given url, and handles rate limiting * Fetches the given URL and handles rate limiting, re-requesting if the rate limit is exceeded.
* re-requesting if the rate limit is exceeded.
* *
* @param url the url to fetch * @param url - The URL to fetch.
* @returns the response * @returns The response.
*/ */
public async fetch(url: string): Promise<any> { public async fetch(url: string, options?: any): Promise<any> {
const now = Date.now(); const now = Date.now();
if (now < this._rateLimitReset) {
this._queue.push(url); if (now < this.rateLimitReset) {
this.queue.push(url);
await new Promise<void>((resolve) => await new Promise<void>((resolve) =>
setTimeout(resolve, this._rateLimitReset - now), setTimeout(resolve, this.rateLimitReset - now),
); );
} }
const response = await fetch(url, { const response = await fetch(url, options);
next: {
revalidate: 60 * 5, // Keep the data for 5 minutes
},
});
if (response.status === 429) { if (response.status === 429) {
const hasRetryAfter = response.headers.has("retry-after"); const hasRetryAfterHeader = response.headers.has("retry-after");
let retryAfter = let retryAfter = hasRetryAfterHeader
Number( ? Number(response.headers.get("retry-after"))
hasRetryAfter : Number(
? response.headers.get("retry-after") new Date(
: new Date( response.headers.get("X-Rate-Limit-Reset") as string,
response.headers.get("X-Rate-Limit-Reset") as string, ).getTime() / 1000,
).getTime() / 1000, ) * 1000;
) * 1000;
// Check if we couldn't get the reset time from the headers.
// Default to 3 seconds
if (!retryAfter) { if (!retryAfter) {
retryAfter = 3_000; // default to 3 seconds if we can't get the reset time retryAfter = 3000;
} }
this._queue.push(url);
this.queue.push(url);
await new Promise<void>((resolve) => setTimeout(resolve, retryAfter)); await new Promise<void>((resolve) => setTimeout(resolve, retryAfter));
return this.fetch(this._queue.shift() as string); return this.fetch(this.queue.shift() as string);
} }
if (response.headers.has("x-ratelimit-remaining")) { if (response.headers.has("x-ratelimit-remaining")) {
const remaining = Number(response.headers.get("x-ratelimit-remaining")); const remaining = Number(response.headers.get("x-ratelimit-remaining"));
if (remaining === 0) { if (remaining === 0) {
const reset = Number(response.headers.get("x-ratelimit-reset")) * 1000; const reset = Number(response.headers.get("x-ratelimit-reset")) * 1000;
this._queue.push(url); this.queue.push(url);
await new Promise<void>((resolve) => setTimeout(resolve, reset - now)); await new Promise<void>((resolve) => setTimeout(resolve, reset - now));
return this.fetch(this._queue.shift() as string); return this.fetch(this.queue.shift() as string);
} }
} }
if (this._queue.length > 0) { if (this.queue.length > 0) {
const nextUrl = this._queue.shift(); const nextUrl = this.queue.shift();
return this.fetch(nextUrl as string); return this.fetch(nextUrl as string);
} }

@ -1,4 +1,4 @@
// Yoinked from https://github.com/Shurdoof/pp-calculator/blob/c24b5ca452119339928831d74e6d603fb17fd5ef/src/lib/pp/calculator.ts // Yoinked from https://github.com/Shurdoof/pp-calculator
// Thank for for this I have no fucking idea what the maths is doing but it works! // Thank for for this I have no fucking idea what the maths is doing but it works!
import { useScoresaberScoresStore } from "@/store/scoresaberScoresStore"; import { useScoresaberScoresStore } from "@/store/scoresaberScoresStore";
@ -7,38 +7,43 @@ export const WEIGHT_COEFFICIENT = 0.965;
const starMultiplier = 42.11; const starMultiplier = 42.11;
const ppCurve = [ const ppCurve = [
[1, 7], [1, 5.367394282890631],
[0.999, 6.24], [0.9995, 5.019543595874787],
[0.9975, 5.31], [0.999, 4.715470646416203],
[0.995, 4.14], [0.99825, 4.325027383589547],
[0.9925, 3.31], [0.9975, 3.996793606763322],
[0.99, 2.73], [0.99625, 3.5526145337555373],
[0.9875, 2.31], [0.995, 3.2022017597337955],
[0.985, 2.0], [0.99375, 2.9190155639254955],
[0.9825, 1.775], [0.9925, 2.685667856592722],
[0.98, 1.625], [0.99125, 2.4902905794106913],
[0.9775, 1.515], [0.99, 2.324506282149922],
[0.975, 1.43], [0.9875, 2.058947159052738],
[0.9725, 1.36], [0.985, 1.8563887693647105],
[0.97, 1.3], [0.9825, 1.697536248647543],
[0.965, 1.195], [0.98, 1.5702410055532239],
[0.96, 1.115], [0.9775, 1.4664726399289512],
[0.955, 1.05], [0.975, 1.3807102743105126],
[0.9725, 1.3090333065057616],
[0.97, 1.2485807759957321],
[0.965, 1.1552120359501035],
[0.96, 1.0871883573850478],
[0.955, 1.0388633331418984],
[0.95, 1], [0.95, 1],
[0.94, 0.94], [0.94, 0.9417362980580238],
[0.93, 0.885], [0.93, 0.9039994071865736],
[0.92, 0.835], [0.92, 0.8728710341448851],
[0.91, 0.79], [0.91, 0.8488375988124467],
[0.9, 0.75], [0.9, 0.825756123560842],
[0.875, 0.655], [0.875, 0.7816934560296046],
[0.85, 0.57], [0.85, 0.7462290664143185],
[0.825, 0.51], [0.825, 0.7150465663454271],
[0.8, 0.47], [0.8, 0.6872268862950283],
[0.75, 0.4], [0.75, 0.6451808210101443],
[0.7, 0.34], [0.7, 0.6125565959114954],
[0.65, 0.29], [0.65, 0.5866010012767576],
[0.6, 0.25], [0.6, 0.18223233667439062],
[0.0, 0.0], [0, 0],
]; ];
function clamp(value: number, min: number, max: number) { function clamp(value: number, min: number, max: number) {