From 06a13bedc83cee1cdbd6090dea1897d402098019 Mon Sep 17 00:00:00 2001 From: Liam Date: Sun, 20 Oct 2024 19:49:10 +0100 Subject: [PATCH] use new stat for ranked and total play count change --- .../src/player/impl/scoresaber-player.ts | 38 ++++++++++--------- projects/common/src/player/player.ts | 1 + projects/website/src/common/player-utils.ts | 6 +-- .../src/components/player/player-stats.tsx | 12 ++---- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/projects/common/src/player/impl/scoresaber-player.ts b/projects/common/src/player/impl/scoresaber-player.ts index 712c146..936b79d 100644 --- a/projects/common/src/player/impl/scoresaber-player.ts +++ b/projects/common/src/player/impl/scoresaber-player.ts @@ -5,6 +5,7 @@ import ScoreSaberPlayerToken from "../../types/token/scoresaber/score-saber-play import { formatDateMinimal, getDaysAgoDate, getMidnightAlignedDate } from "../../utils/time-utils"; import { getPageFromRank } from "../../utils/utils"; import { Config } from "../../config"; +import { getValueFromHistory } from "website/src/common/player-utils"; /** * A ScoreSaber player. @@ -180,7 +181,7 @@ export async function getScoreSaberPlayerFromToken( * @param daysAgo the amount of days ago to get the stat for * @return the change */ - const getChange = (statType: "rank" | "countryRank" | "pp", daysAgo: number = 1): number | undefined => { + const getStatisticChange = (statType: string, daysAgo: number = 1): number | undefined => { const todayStats = statisticHistory[todayDate]; let otherDate: Date | undefined; @@ -229,8 +230,8 @@ export async function getScoreSaberPlayerFromToken( return undefined; } - const statToday = todayStats[statType]; - const statOther = otherStats[statType]; + const statToday = getValueFromHistory(todayStats, statType); + const statOther = getValueFromHistory(otherStats, statType); if (statToday === undefined || statOther === undefined) { return undefined; @@ -240,6 +241,18 @@ export async function getScoreSaberPlayerFromToken( return (statToday - statOther) * (statType === "pp" ? 1 : -1); }; + const getStatisticChanges = (daysAgo: number): PlayerHistory => { + return { + rank: getStatisticChange("rank", daysAgo), + countryRank: getStatisticChange("countryRank", daysAgo), + pp: getStatisticChange("pp", daysAgo), + scores: { + totalScores: getStatisticChange("scores.totalScores", daysAgo), + totalRankedScores: getStatisticChange("scores.totalRankedScores", daysAgo), + }, + }; + }; + return { id: token.id, name: token.name, @@ -251,21 +264,10 @@ export async function getScoreSaberPlayerFromToken( bio: bio, pp: token.pp, statisticChange: { - daily: { - rank: getChange("rank", 1), - countryRank: getChange("countryRank", 1), - pp: getChange("pp", 1), - }, - weekly: { - rank: getChange("rank", 7), - countryRank: getChange("countryRank", 7), - pp: getChange("pp", 7), - }, - monthly: { - rank: getChange("rank", 30), - countryRank: getChange("countryRank", 30), - pp: getChange("pp", 30), - }, + daily: getStatisticChanges(1), + weekly: getStatisticChanges(7), + monthly: getStatisticChanges(30), + yearly: getStatisticChanges(365), }, role: role, badges: badges, diff --git a/projects/common/src/player/player.ts b/projects/common/src/player/player.ts index ad0a59b..e928872 100644 --- a/projects/common/src/player/player.ts +++ b/projects/common/src/player/player.ts @@ -59,4 +59,5 @@ export type StatisticChange = { daily: PlayerHistory; weekly: PlayerHistory; monthly: PlayerHistory; + yearly: PlayerHistory; }; diff --git a/projects/website/src/common/player-utils.ts b/projects/website/src/common/player-utils.ts index 264786e..a1bbac1 100644 --- a/projects/website/src/common/player-utils.ts +++ b/projects/website/src/common/player-utils.ts @@ -7,7 +7,7 @@ import { PlayerHistory } from "@ssr/common/player/player-history"; * @param history the history to get the value from * @param field the field to get */ -export function getValueFromHistory(history: PlayerHistory, field: string): number | null { +export function getValueFromHistory(history: PlayerHistory, field: string): number | undefined { const keys = field.split("."); /* eslint-disable @typescript-eslint/no-explicit-any */ let value: any = history; @@ -17,10 +17,10 @@ export function getValueFromHistory(history: PlayerHistory, field: string): numb if (value && key in value) { value = value[key]; } else { - return null; // Return null if the key doesn't exist + return undefined; // Return null if the key doesn't exist } } // Ensure we return a number or null - return typeof value === "number" ? value : null; + return typeof value === "number" ? value : undefined; } diff --git a/projects/website/src/components/player/player-stats.tsx b/projects/website/src/components/player/player-stats.tsx index 9618306..8124e74 100644 --- a/projects/website/src/components/player/player-stats.tsx +++ b/projects/website/src/components/player/player-stats.tsx @@ -22,14 +22,13 @@ const playerStats: Stat[] = [ name: "Ranked Play Count", color: "bg-pp", create: (player: ScoreSaberPlayer) => { - const history = getPlayerHistoryToday(player); - const rankedScores = history.scores?.rankedScores; + const rankedScoresChange = player.statisticChange?.daily.scores?.totalRankedScores; return { value: ( <> {formatNumberWithCommas(player.statistics.rankedPlayCount)}{" "} - + ), }; @@ -56,16 +55,13 @@ const playerStats: Stat[] = [ { name: "Total Play Count", create: (player: ScoreSaberPlayer) => { - const history = getPlayerHistoryToday(player); - const rankedScores = history.scores?.rankedScores; - const unrankedScores = history.scores?.unrankedScores; - const totalChange = (rankedScores ?? 0) + (unrankedScores ?? 0); + const scoresChange = player.statisticChange?.daily.scores; return { value: ( <> {formatNumberWithCommas(player.statistics.totalPlayCount)}{" "} - + ), };