diff --git a/lib/beatmap.ts b/lib/beatmap.ts index 095ff8d..4f9f99c 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 @@ -145,10 +154,10 @@ 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) => { + 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..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) => { @@ -123,18 +124,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 } @@ -170,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 @@ -256,13 +251,41 @@ 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 * @returns x, but with it (or what it contains) now having the correct type */ -const bools = ["perfect", "replay_available"] function correctType(x: any): any { + /** + * This package transforms some properties into Booleans when fitting + */ + 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 +296,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), 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",