Change /:ugpt to retrieve all ranking infos

This commit is contained in:
zkldi
2021-09-02 15:59:54 +01:00
parent dce6b6e20a
commit 3b03f1c506
2 changed files with 25 additions and 3 deletions
@@ -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,
+8 -1
View File
@@ -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";
}