Skip to content

Commit

Permalink
Merge pull request #11 from TTTaevas/cleanup4fourpointo
Browse files Browse the repository at this point in the history
v0.4.0
  • Loading branch information
TTTaevas committed Feb 23, 2023
2 parents d8ff9f3 + 7e1f2a4 commit 2857be7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
13 changes: 11 additions & 2 deletions lib/beatmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,24 @@ 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
favourite_count: number
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
Expand All @@ -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
Expand Down
53 changes: 38 additions & 15 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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<Replay | APIError> {
let lookup: string
Expand Down Expand Up @@ -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)) {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const test: () => Promise<void> = 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),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 2857be7

Please sign in to comment.