Don't duplicate code for each osu! ruleset/gamemode

This commit is contained in:
Taevas 2025-02-17 16:10:05 +01:00
parent 41d33ab964
commit 3424924762
10 changed files with 24 additions and 274 deletions

View file

@ -4,7 +4,7 @@ import {type OsuInfo} from "../../src/components/Info/RhythmGames/Osu.js";
import {MongoClient} from "mongodb";
import {type Token} from "./osu_token.js";
const handler: Handler = async () => {
const handler: Handler = async (req) => {
const client = new MongoClient(process.env.URL_MONGODB!);
await client.connect();
@ -13,8 +13,9 @@ const handler: Handler = async () => {
const token = await collection.findOne();
void client.close();
const ruleset = Number(req.queryStringParameters?.ruleset);
const api = new osu.API({access_token: token?.access_token});
const profile = await api.getUser(7276846, osu.Ruleset.osu);
const profile = await api.getUser(7276846, !isNaN(ruleset) ? ruleset : undefined);
const info: OsuInfo = {
country: profile.country.name,

View file

@ -1,33 +0,0 @@
import {type Handler} from "@netlify/functions";
import * as osu from "osu-api-v2-js";
import {type FruitsInfo} from "../../src/components/Info/RhythmGames/OsuFruits.js";
import {MongoClient} from "mongodb";
import {type Token} from "./osu_token.js";
const handler: Handler = async () => {
const client = new MongoClient(process.env.URL_MONGODB!);
await client.connect();
const db = client.db("tokens");
const collection = db.collection<Token>("osu");
const token = await collection.findOne();
void client.close();
const api = new osu.API({access_token: token?.access_token});
const profile = await api.getUser(7276846, osu.Ruleset.fruits);
const info: FruitsInfo = {
country: profile.country.name,
ranks: {
global: profile.statistics.global_rank ?? 0,
country: profile.statistics.country_rank ?? 0,
},
};
return {
statusCode: 200,
body: JSON.stringify(info),
};
};
export {handler};

View file

@ -1,33 +0,0 @@
import {type Handler} from "@netlify/functions";
import * as osu from "osu-api-v2-js";
import {type ManiaInfo} from "../../src/components/Info/RhythmGames/OsuMania.js";
import {MongoClient} from "mongodb";
import {type Token} from "./osu_token.js";
const handler: Handler = async () => {
const client = new MongoClient(process.env.URL_MONGODB!);
await client.connect();
const db = client.db("tokens");
const collection = db.collection<Token>("osu");
const token = await collection.findOne();
void client.close();
const api = new osu.API({access_token: token?.access_token});
const profile = await api.getUser(7276846, osu.Ruleset.mania);
const info: ManiaInfo = {
country: profile.country.name,
ranks: {
global: profile.statistics.global_rank ?? 0,
country: profile.statistics.country_rank ?? 0,
},
};
return {
statusCode: 200,
body: JSON.stringify(info),
};
};
export {handler};

View file

@ -1,33 +0,0 @@
import {type Handler} from "@netlify/functions";
import * as osu from "osu-api-v2-js";
import {type TaikoInfo} from "../../src/components/Info/RhythmGames/OsuTaiko.js";
import {MongoClient} from "mongodb";
import {type Token} from "./osu_token.js";
const handler: Handler = async () => {
const client = new MongoClient(process.env.URL_MONGODB!);
await client.connect();
const db = client.db("tokens");
const collection = db.collection<Token>("osu");
const token = await collection.findOne();
void client.close();
const api = new osu.API({access_token: token?.access_token});
const profile = await api.getUser(7276846, osu.Ruleset.taiko);
const info: TaikoInfo = {
country: profile.country.name,
ranks: {
global: profile.statistics.global_rank ?? 0,
country: profile.statistics.country_rank ?? 0,
},
};
return {
statusCode: 200,
body: JSON.stringify(info),
};
};
export {handler};