diff --git a/src/utils/fetchWithQueue.ts b/src/utils/fetchWithQueue.ts index d7cac66..6beb9a2 100644 --- a/src/utils/fetchWithQueue.ts +++ b/src/utils/fetchWithQueue.ts @@ -1,63 +1,64 @@ export class FetchQueue { - private _queue: string[]; - private _rateLimitReset: number; + private queue: string[]; + private rateLimitReset: number; constructor() { - this._queue = []; - this._rateLimitReset = Date.now(); + this.queue = []; + this.rateLimitReset = Date.now(); } /** - * Fetches the given url, and handles rate limiting - * re-requesting if the rate limit is exceeded. + * Fetches the given URL and handles rate limiting, re-requesting if the rate limit is exceeded. * - * @param url the url to fetch - * @returns the response + * @param url - The URL to fetch. + * @returns The response. */ - public async fetch(url: string): Promise { + public async fetch(url: string, options?: any): Promise { const now = Date.now(); - if (now < this._rateLimitReset) { - this._queue.push(url); + + if (now < this.rateLimitReset) { + this.queue.push(url); await new Promise((resolve) => - setTimeout(resolve, this._rateLimitReset - now), + setTimeout(resolve, this.rateLimitReset - now), ); } - const response = await fetch(url, { - next: { - revalidate: 60 * 5, // Keep the data for 5 minutes - }, - }); + const response = await fetch(url, options); + if (response.status === 429) { - const hasRetryAfter = response.headers.has("retry-after"); - let retryAfter = - Number( - hasRetryAfter - ? response.headers.get("retry-after") - : new Date( - response.headers.get("X-Rate-Limit-Reset") as string, - ).getTime() / 1000, - ) * 1000; + const hasRetryAfterHeader = response.headers.has("retry-after"); + let retryAfter = hasRetryAfterHeader + ? Number(response.headers.get("retry-after")) + : Number( + new Date( + response.headers.get("X-Rate-Limit-Reset") as string, + ).getTime() / 1000, + ) * 1000; + + // Check if we couldn't get the reset time from the headers. + // Default to 3 seconds 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((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")) { const remaining = Number(response.headers.get("x-ratelimit-remaining")); + if (remaining === 0) { const reset = Number(response.headers.get("x-ratelimit-reset")) * 1000; - this._queue.push(url); + this.queue.push(url); await new Promise((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) { - const nextUrl = this._queue.shift(); + if (this.queue.length > 0) { + const nextUrl = this.queue.shift(); return this.fetch(nextUrl as string); } diff --git a/src/utils/scoresaber/scores.ts b/src/utils/scoresaber/scores.ts index 52d6cb7..db40436 100644 --- a/src/utils/scoresaber/scores.ts +++ b/src/utils/scoresaber/scores.ts @@ -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! import { useScoresaberScoresStore } from "@/store/scoresaberScoresStore"; @@ -7,38 +7,43 @@ export const WEIGHT_COEFFICIENT = 0.965; const starMultiplier = 42.11; const ppCurve = [ - [1, 7], - [0.999, 6.24], - [0.9975, 5.31], - [0.995, 4.14], - [0.9925, 3.31], - [0.99, 2.73], - [0.9875, 2.31], - [0.985, 2.0], - [0.9825, 1.775], - [0.98, 1.625], - [0.9775, 1.515], - [0.975, 1.43], - [0.9725, 1.36], - [0.97, 1.3], - [0.965, 1.195], - [0.96, 1.115], - [0.955, 1.05], + [1, 5.367394282890631], + [0.9995, 5.019543595874787], + [0.999, 4.715470646416203], + [0.99825, 4.325027383589547], + [0.9975, 3.996793606763322], + [0.99625, 3.5526145337555373], + [0.995, 3.2022017597337955], + [0.99375, 2.9190155639254955], + [0.9925, 2.685667856592722], + [0.99125, 2.4902905794106913], + [0.99, 2.324506282149922], + [0.9875, 2.058947159052738], + [0.985, 1.8563887693647105], + [0.9825, 1.697536248647543], + [0.98, 1.5702410055532239], + [0.9775, 1.4664726399289512], + [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.94, 0.94], - [0.93, 0.885], - [0.92, 0.835], - [0.91, 0.79], - [0.9, 0.75], - [0.875, 0.655], - [0.85, 0.57], - [0.825, 0.51], - [0.8, 0.47], - [0.75, 0.4], - [0.7, 0.34], - [0.65, 0.29], - [0.6, 0.25], - [0.0, 0.0], + [0.94, 0.9417362980580238], + [0.93, 0.9039994071865736], + [0.92, 0.8728710341448851], + [0.91, 0.8488375988124467], + [0.9, 0.825756123560842], + [0.875, 0.7816934560296046], + [0.85, 0.7462290664143185], + [0.825, 0.7150465663454271], + [0.8, 0.6872268862950283], + [0.75, 0.6451808210101443], + [0.7, 0.6125565959114954], + [0.65, 0.5866010012767576], + [0.6, 0.18223233667439062], + [0, 0], ]; function clamp(value: number, min: number, max: number) {