diff --git a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/router.ts b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/router.ts index 103275bc6..552f1c7c3 100644 --- a/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/router.ts +++ b/server/src/server/router/api/v1/users/_userID/games/_game/_playtype/router.ts @@ -16,6 +16,7 @@ import { integer, PBScoreDocument, UserGameStatsSnapshot, + UserGameStats, } from "tachi-common"; import { CheckStrProfileAlg } from "utils/string-checks"; import { IsString } from "utils/misc"; @@ -41,6 +42,8 @@ router.get("/", async (req, res) => { const game = req[SYMBOL_TachiData]!.game!; const playtype = req[SYMBOL_TachiData]!.playtype!; + const gptConfig = GetGamePTConfig(game, playtype); + const [totalScores, firstScore, mostRecentScore, rankingData] = await Promise.all([ db.scores.count({ userID: user.id, @@ -73,7 +76,7 @@ router.get("/", async (req, res) => { }, } ), - GetUsersRankingAndOutOf(stats), + GetAllRankings(stats), ]); return res.status(200).json({ @@ -89,6 +92,18 @@ router.get("/", async (req, res) => { }); }); +async function GetAllRankings(stats: UserGameStats) { + const gptConfig = GetGamePTConfig(stats.game, stats.playtype); + + const entries = await Promise.all( + gptConfig.profileRatingAlgs.map((k) => + GetUsersRankingAndOutOf(stats, k).then((r) => [k, r]) + ) + ); + + return Object.fromEntries(entries); +} + /** * Returns a users game-stats for the past 90 days. * @name GET /api/v1/users/:userID/games/:game/:playtype/history @@ -355,7 +370,7 @@ router.get("/leaderboard-adjacent", async (req, res) => { ...below.map((e) => e.userID), ]); - const thisUsersRanking = await GetUsersRankingAndOutOf(thisUsersStats); + const thisUsersRanking = await GetUsersRankingAndOutOf(thisUsersStats, alg); return res.status(200).json({ success: true, diff --git a/server/src/utils/misc.ts b/server/src/utils/misc.ts index 9156b3f8f..75f5ad566 100644 --- a/server/src/utils/misc.ts +++ b/server/src/utils/misc.ts @@ -1,6 +1,6 @@ import crypto from "crypto"; import { ServerTypeInfo } from "lib/setup/config"; -import { Game, Playtypes, GetGameConfig } from "tachi-common"; +import { Game, Playtypes, GetGameConfig, GamePTConfig } from "tachi-common"; // https://github.com/sindresorhus/escape-string-regexp/blob/main/index.js // the developer of this has migrated everything to Force ES6 style modules, @@ -69,6 +69,13 @@ export function IsValidPlaytype(game: Game, str: string): str is Playtypes[Game] return GetGameConfig(game).validPlaytypes.includes(str as Playtypes[Game]); } +export function IsValidScoreAlg( + gptConfig: GamePTConfig, + str: unknown +): str is GamePTConfig["scoreRatingAlgs"][0] { + return gptConfig.scoreRatingAlgs.includes(str as GamePTConfig["scoreRatingAlgs"][0]); +} + export function IsString(val: unknown): val is string { return typeof val === "string"; }