From 201b164aa0327bd484b115af1331b063c818eda9 Mon Sep 17 00:00:00 2001 From: Taevas <67872932+TTTaevas@users.noreply.github.com> Date: Wed, 22 Feb 2023 21:47:33 +0100 Subject: [PATCH 1/3] Make `Beatmap` more reliable --- lib/beatmap.ts | 12 +++++++++++- lib/index.ts | 10 ++++++++-- lib/test.ts | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/beatmap.ts b/lib/beatmap.ts index 095ff8d..38cea1e 100644 --- a/lib/beatmap.ts +++ b/lib/beatmap.ts @@ -122,7 +122,10 @@ export interface Beatmap { creator: string creator_id: number bpm: number - source: string + /** + * Is 0 if no source + */ + source: string | 0 tags: string genre_id: number language_id: number @@ -130,7 +133,13 @@ export interface Beatmap { rating: number storyboard: boolean video: boolean + /** + * If the map can **not** be downloaded from the website + */ download_unavailable: boolean + /** + * If the map can **not** be downloaded with its audio file + */ audio_unavailable: boolean playcount: number passcount: number @@ -149,6 +158,7 @@ export interface Beatmap { } export const adjustBeatmapStatsToMods: (beatmap: Beatmap, mods: Mods) => Beatmap = (beatmap: Beatmap, mods: Mods) => { + beatmap = Object.assign({}, beatmap) // Do not change the original Beatmap outside this function const arr = getMods(mods) const convertARtoMS = (ar: number) => { ar *= 10 diff --git a/lib/index.ts b/lib/index.ts index 075deae..a68ad40 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -261,8 +261,14 @@ export function getMods(value: Mods): string[] { * @param x Anything, but should be a string, an array that contains a string, or an object which has a string * @returns x, but with it (or what it contains) now having the correct type */ -const bools = ["perfect", "replay_available"] function correctType(x: any): any { + const bools = [ + "replay_available", // Score + "pass", // Match.games + "perfect", // Score, Match.games + "storyboard", "video", "download_unavailable", "audio_unavailable" // Beatmap + ] + if (!isNaN(x)) { return Number(x) } else if (/^[+-[0-9][0-9]+-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/.test(x)) { @@ -273,7 +279,7 @@ function correctType(x: any): any { const k = Object.keys(x) const v = Object.values(x) for (let i = 0; i < k.length; i++) { - x[k[i]] = bools.includes(k[i]) ? Boolean(v[i]) : correctType(v[i]) + x[k[i]] = bools.includes(k[i]) ? Boolean(Number(v[i])) : correctType(v[i]) } } return x diff --git a/lib/test.ts b/lib/test.ts index 7f5cb65..49421fb 100644 --- a/lib/test.ts +++ b/lib/test.ts @@ -151,7 +151,7 @@ const test: () => Promise = async () => { } const testBeatmapWithMods = (b: osu.Beatmap, mods: osu.Mods, expected: object) => { - let bm = osu.adjustBeatmapStatsToMods(Object.assign({}, b), mods) + let bm = osu.adjustBeatmapStatsToMods(b, mods) let stats = { bpm: roundTo(bm.bpm, 2), cs: roundTo(bm.diff_size, 2), From bd73607dc05714932db0b5029b9e8b5c1212bf25 Mon Sep 17 00:00:00 2001 From: Taevas <67872932+TTTaevas@users.noreply.github.com> Date: Thu, 23 Feb 2023 02:29:43 +0100 Subject: [PATCH 2/3] Move `getLength` away from Beatmap objects --- lib/beatmap.ts | 1 - lib/index.ts | 31 +++++++++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/beatmap.ts b/lib/beatmap.ts index 38cea1e..4f9f99c 100644 --- a/lib/beatmap.ts +++ b/lib/beatmap.ts @@ -154,7 +154,6 @@ export interface Beatmap { * Star Rating https://osu.ppy.sh/wiki/en/Beatmap/Star_rating */ difficultyrating: number - getLength: Function } export const adjustBeatmapStatsToMods: (beatmap: Beatmap, mods: Mods) => Beatmap = (beatmap: Beatmap, mods: Mods) => { diff --git a/lib/index.ts b/lib/index.ts index a68ad40..ba7f3bc 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -123,18 +123,6 @@ export class API { if (!response[0]) {return new APIError(`No Beatmap could be found (diff_id: ${diff_id})`)} let beatmap: Beatmap = adjustBeatmapStatsToMods(correctType(response[0]) as Beatmap, mods) - beatmap.getLength = (type: "hit" | "total") => { - let length = type === "hit" ? beatmap.hit_length : beatmap.total_length - let m: number = 0 - let s: string | number = 0 - - while (length >= 60) {m += 1; length -= 60} - while (length >= 1) {s += 1; length -= 1} - if (s < 10) {s = `0${s}`} - - return `${m}:${s}` - } - return beatmap } @@ -256,6 +244,25 @@ export function getMods(value: Mods): string[] { return arr } +/** + * This function exists in case you need help getting a Beatmap's length in a readable way + * @param seconds A number of seconds + * @returns A String that represents `seconds` in format m:ss (with support for hours if needed) + */ +export function getLength(seconds: number): string { + let h: string | number = 0 + let m: string | number = 0 + let s: string | number = 0 + + while (seconds >= 3600) {h += 1; seconds -= 3600} + while (seconds >= 60) {m += 1; seconds -= 60} + if (m < 10 && h > 0) {m = `0${m}`} + while (seconds >= 1) {s += 1; seconds -= 1} + if (s < 10) {s = `0${s}`} + + return `${h > 0 ? `${h}:` : ""}${m}:${s}` +} + /** * *Almost* **everything** in the JSONs the API returns is a string, this function fixes that * @param x Anything, but should be a string, an array that contains a string, or an object which has a string From 7e1f2a4750e5e6b8a13f308f6e0cf456626351f0 Mon Sep 17 00:00:00 2001 From: Taevas <67872932+TTTaevas@users.noreply.github.com> Date: Thu, 23 Feb 2023 02:49:26 +0100 Subject: [PATCH 3/3] Miscellaneous stuff --- lib/index.ts | 12 +++++++++++- package.json | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index ba7f3bc..7785249 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -45,7 +45,8 @@ export class API { headers: { "Accept": "application/json", "Accept-Encoding": "gzip", - "Content-Type": "application/json" + "Content-Type": "application/json", + "User-Agent": "osu-api-v1-js (https://github.com/TTTaevas/osu-api-v1-js)" } }) .catch((error: Error | AxiosError) => { @@ -158,6 +159,12 @@ export class API { return correctType(response) as Match } + /** + * @param score An Object with either the id of the score, or with the id of a `Beatmap` and an `User`'s id or username + * @param mode A number representing the `Gamemode` the `Score` was set in + * @param mods A number representing the `Mods` used in the `Score` + * @returns + */ async getReplay(score: {id?: number, search?: {user?: {user_id?: number, username?: string} | User, beatmap_id?: number}}, mode: Gamemodes, mods?: Mods): Promise { let lookup: string @@ -269,6 +276,9 @@ export function getLength(seconds: number): string { * @returns x, but with it (or what it contains) now having the correct type */ function correctType(x: any): any { + /** + * This package transforms some properties into Booleans when fitting + */ const bools = [ "replay_available", // Score "pass", // Match.games diff --git a/package.json b/package.json index e443798..37212d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "osu-api-v1-js", - "version": "0.3.3", + "version": "0.4.0", "description": "Package to easily access osu!api version 1.0", "main": "dist/index.js", "types": "dist/index.d.ts",