Skip to content

Commit

Permalink
Merge pull request #14 from TTTaevas/cleanup41
Browse files Browse the repository at this point in the history
Cleanup for v0.4.1
  • Loading branch information
TTTaevas committed Feb 24, 2023
2 parents d69094d + bab5e5d commit 4aa773c
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 94 deletions.
46 changes: 20 additions & 26 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ export class API {
.catch((error: Error | AxiosError) => {
if (axios.isAxiosError(error)) {
if (error.response) {
if (this.verbose) console.log("osu!api v1 ->", error.response.statusText, error.response.status, {type, params})
console.log("osu!api v1 ->", error.response.statusText, error.response.status, {type, params})
if (error.response.status === 401) console.log("osu!api v1 -> Server responded with status code 401, are you sure you're using a valid API key?")
} else if (error.request) {
if (this.verbose) console.log("osu!api v1 ->", "Request made but server did not respond", `Try #${number_try}`, {type, params})
console.log("osu!api v1 ->", "Request made but server did not respond", `(Try #${number_try})`, {type, params})
to_retry = true
} else { // Something happened in setting up the request that triggered an error, I think
if (this.verbose) console.error(error)
console.error(error)
}
} else {
if (this.verbose) console.error(error)
console.error(error)
}
})

Expand All @@ -83,9 +84,9 @@ export class API {
*/
async getUser(search: {user_id?: number, username?: string} | User, mode: Gamemodes): Promise<User | APIError> {
if (!search.user_id && !search.username) {return new APIError("No proper `search` argument was given")}
let type = search.user_id ? "id" : "string"
let lookup = search.user_id !== undefined ? `u=${search.user_id}&type=id` : `u=${search.username}&type=string`

let response = await this.request("get_user", `u=${type == "id" ? search.user_id : search.username}&type=${type}&m=${mode}`)
let response = await this.request("get_user", `${lookup}&m=${mode}`)
if (!response[0]) {return new APIError(`No User could be found (user_id: ${search.user_id} | username: ${search.username})`)}
return correctType(response[0]) as User
}
Expand All @@ -99,14 +100,12 @@ export class API {
*/
async getUserScores(user: {user_id?: number, username?: string} | User, mode: Gamemodes, plays: "best" | "recent", limit?: number): Promise<Score[] | APIError> {
let scores: Score[] = []

if (!user.user_id && !user.username) {return new APIError("No proper `user` argument was given")}
let type = user.user_id ? "id" : "string"
let lookup = user.user_id !== undefined ? `u=${user.user_id}&type=id` : `u=${user.username}&type=string`

let response = await this.request(`get_user_${plays}`, `u=${type == "id" ? user.user_id : user.username}&type=${type}&m=${mode}&limit=${limit || 100}`)
let response = await this.request(`get_user_${plays}`, `${lookup}&m=${mode}&limit=${limit || 100}`)
if (response) response.forEach((s: Object) => scores.push(correctType(s) as Score))
if (!scores.length) {return new APIError(`No Score could be found (user_id: ${user.user_id} | username: ${user.username})`)}

return scores
}

Expand All @@ -119,8 +118,8 @@ export class API {
*/
async getBeatmap(beatmap: {beatmap_id: number} | Beatmap, mods?: Mods, mode?: Gamemodes): Promise<Beatmap | APIError> {
if (mods === undefined) {mods = Mods.None}
if (getMods(mods).includes(Mods[Mods.Nightcore]) && !getMods(mods).includes(Mods[Mods.DoubleTime])) {mods -= Mods.Nightcore - Mods.DoubleTime}
unsupported_mods.forEach((mod) => getMods(mods!).includes(Mods[mod]) ? mods! -= mod : mods! -= 0)
if (getMods(mods).includes(Mods[Mods.Nightcore])) {mods -= Mods.Nightcore - Mods.DoubleTime}
if (mode === undefined) {mode = Gamemodes.OSU}

let response = await this.request("get_beatmaps", `b=${beatmap.beatmap_id}&mods=${mods}&mode=${mode}&a=1`)
Expand All @@ -146,8 +145,8 @@ export class API {
since?: Date
): Promise<Beatmap[] | APIError> {
if (mods === undefined) {mods = Mods.None}
if (getMods(mods).includes(Mods[Mods.Nightcore]) && !getMods(mods).includes(Mods[Mods.DoubleTime])) {mods -= Mods.Nightcore - Mods.DoubleTime}
unsupported_mods.forEach((mod) => getMods(mods!).includes(Mods[mod]) ? mods! -= mod : mods! -= 0)
if (getMods(mods).includes(Mods[Mods.Nightcore])) {mods -= Mods.Nightcore - Mods.DoubleTime}
let lookup = `mods=${mods}`

if (beatmap) {
Expand Down Expand Up @@ -196,10 +195,9 @@ export class API {
let scores: Score[] = []

if (user && !user.user_id && !user.username) {return new APIError("The `user` argument lacks a user_id/username property")}
let type = user ? user.user_id ? "id" : user.username ? "string" : false : false
let r_user = type ? type == "id" ? "&u="+user!.user_id : "&u="+user!.username : ""
let user_lookup = user ? user.user_id !== undefined ? `u=${user.user_id}&type=id` : `u=${user.username}&type=string` : ""

let response = await this.request("get_scores", `b=${diff_id}&m=${mode}${r_user}${mods ? "&mods="+mods : ""}${type ? "&type="+type : ""}&limit=${limit || 100}`)
let response = await this.request("get_scores", `b=${diff_id}&m=${mode}${mods ? "&mods="+mods : ""}${user_lookup}&limit=${limit || 100}`)
if (response) response.forEach((s: Object) => scores.push(correctType(s) as Score))
if (!scores.length) {return new APIError(`No Score could be found (diff_id: ${diff_id})`)}

Expand All @@ -226,18 +224,14 @@ export class API {
async getReplay(mode: Gamemodes, score?: {score_id: number} | Score,
search?: {user: {user_id?: number, username?: string} | User, beatmap: {beatmap_id: number} | Beatmap, mods: Mods}): Promise<Replay | APIError> {
let lookup: string
let type: string | Boolean

if (score !== undefined) {
lookup = `s=${score.score_id}`
type = false
} else if (search !== undefined) {
if (search.user.user_id !== undefined) {
lookup = `u=${search.user.user_id}`
type = "id"
lookup = `u=${search.user.user_id}&type=id`
} else if (search.user.username !== undefined) {
lookup = `u=${search.user.username}`
type = "string"
lookup = `u=${search.user.username}&type=string`
} else {
return new APIError("No proper `score.search.user` argument was given (it lacks either an `user_id` or an `username`)")
}
Expand All @@ -247,7 +241,7 @@ export class API {
return new APIError("No proper `score` or `search` argument was given")
}

let response = await this.request("get_replay", `${lookup}&m=${mode}${type ? "&type="+type : ""}`)
let response = await this.request("get_replay", `${lookup}&m=${mode}`)
if (!response.content) {return new APIError(`No Replay could be found`)}
return correctType(response) as Replay
}
Expand All @@ -258,19 +252,19 @@ export class API {
*/
export enum Gamemodes {
/**
* https://osu.ppy.sh/wiki/en/Game_mode/osu!
* https://osu.ppy.sh/wiki/en/Game_mode/osu%21
*/
OSU = 0,
/**
* https://osu.ppy.sh/wiki/en/Game_mode/osu!taiko
* https://osu.ppy.sh/wiki/en/Game_mode/osu%21taiko
*/
TAIKO = 1,
/**
* https://osu.ppy.sh/wiki/en/Game_mode/osu!catch
* https://osu.ppy.sh/wiki/en/Game_mode/osu%21catch
*/
CTB = 2,
/**
* https://osu.ppy.sh/wiki/en/Game_mode/osu!mania
* https://osu.ppy.sh/wiki/en/Game_mode/osu%21mania
*/
MANIA = 3,
}
Expand Down
14 changes: 7 additions & 7 deletions lib/mods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export enum Mods { // Directly taken from https://github.com/ppy/osu-api/wiki
/**
* So you can do something like `Mods.Hidden + Mods["HardRock"]` instead of thinking about the bitwise number
* (https://github.com/ppy/osu-api/wiki#mods)
*/
export enum Mods {
None = 0,
NoFail = 1,
Easy = 2,
Expand Down Expand Up @@ -31,17 +35,13 @@ export enum Mods { // Directly taken from https://github.com/ppy/osu-api/wiki
Key2 = 268435456,
ScoreV2 = 536870912,
Mirror = 1073741824,
// KeyMod = Key1 | Key2 | Key3 | Key4 | Key5 | Key6 | Key7 | Key8 | Key9 | KeyCoop,
// FreeModAllowed = NoFail | Easy | Hidden | HardRock | SuddenDeath | Flashlight | FadeIn | Relax | Autopilot | SpunOut | KeyMod,
// ScoreIncreaseMods = Hidden | HardRock | DoubleTime | Flashlight | FadeIn
}

/**
* API returns the SR (and pp stuff) of a Beatmap as 0/null if any of those mods are included.
* Nightcore would also be featured in this Array, but getBeatmap() circumvents that issue by converting it to DoubleTime!
*/
export const unsupported_mods = [
Mods.NoFail, Mods.Hidden, Mods.SpunOut, Mods.FadeIn,
export const unsupported_mods = [ // exported only for test.ts, not to be used normally
Mods.NoFail, Mods.Hidden, Mods.SpunOut, Mods.FadeIn, Mods.Nightcore, // note that Score.enabled_mods, when Nightcore, also has DoubleTime
Mods.SuddenDeath, Mods.Perfect,
Mods.Relax, Mods.Autoplay, Mods.Autopilot, Mods.Cinema,
Mods.Random, Mods.Target, Mods.ScoreV2, Mods.Mirror,
Expand Down
Loading

0 comments on commit 4aa773c

Please sign in to comment.