use new stat for ranked and total play count change
Some checks failed
Deploy Backend / docker (ubuntu-latest) (push) Failing after 31s
Deploy Website / docker (ubuntu-latest) (push) Failing after 33s

This commit is contained in:
Lee 2024-10-20 19:49:10 +01:00
parent bded9969fe
commit 06a13bedc8
4 changed files with 28 additions and 29 deletions

@ -5,6 +5,7 @@ import ScoreSaberPlayerToken from "../../types/token/scoresaber/score-saber-play
import { formatDateMinimal, getDaysAgoDate, getMidnightAlignedDate } from "../../utils/time-utils"; import { formatDateMinimal, getDaysAgoDate, getMidnightAlignedDate } from "../../utils/time-utils";
import { getPageFromRank } from "../../utils/utils"; import { getPageFromRank } from "../../utils/utils";
import { Config } from "../../config"; import { Config } from "../../config";
import { getValueFromHistory } from "website/src/common/player-utils";
/** /**
* A ScoreSaber player. * A ScoreSaber player.
@ -180,7 +181,7 @@ export async function getScoreSaberPlayerFromToken(
* @param daysAgo the amount of days ago to get the stat for * @param daysAgo the amount of days ago to get the stat for
* @return the change * @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]; const todayStats = statisticHistory[todayDate];
let otherDate: Date | undefined; let otherDate: Date | undefined;
@ -229,8 +230,8 @@ export async function getScoreSaberPlayerFromToken(
return undefined; return undefined;
} }
const statToday = todayStats[statType]; const statToday = getValueFromHistory(todayStats, statType);
const statOther = otherStats[statType]; const statOther = getValueFromHistory(otherStats, statType);
if (statToday === undefined || statOther === undefined) { if (statToday === undefined || statOther === undefined) {
return undefined; return undefined;
@ -240,6 +241,18 @@ export async function getScoreSaberPlayerFromToken(
return (statToday - statOther) * (statType === "pp" ? 1 : -1); 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 { return {
id: token.id, id: token.id,
name: token.name, name: token.name,
@ -251,21 +264,10 @@ export async function getScoreSaberPlayerFromToken(
bio: bio, bio: bio,
pp: token.pp, pp: token.pp,
statisticChange: { statisticChange: {
daily: { daily: getStatisticChanges(1),
rank: getChange("rank", 1), weekly: getStatisticChanges(7),
countryRank: getChange("countryRank", 1), monthly: getStatisticChanges(30),
pp: getChange("pp", 1), yearly: getStatisticChanges(365),
},
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),
},
}, },
role: role, role: role,
badges: badges, badges: badges,

@ -59,4 +59,5 @@ export type StatisticChange = {
daily: PlayerHistory; daily: PlayerHistory;
weekly: PlayerHistory; weekly: PlayerHistory;
monthly: PlayerHistory; monthly: PlayerHistory;
yearly: PlayerHistory;
}; };

@ -7,7 +7,7 @@ import { PlayerHistory } from "@ssr/common/player/player-history";
* @param history the history to get the value from * @param history the history to get the value from
* @param field the field to get * @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("."); const keys = field.split(".");
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
let value: any = history; let value: any = history;
@ -17,10 +17,10 @@ export function getValueFromHistory(history: PlayerHistory, field: string): numb
if (value && key in value) { if (value && key in value) {
value = value[key]; value = value[key];
} else { } 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 // Ensure we return a number or null
return typeof value === "number" ? value : null; return typeof value === "number" ? value : undefined;
} }

@ -22,14 +22,13 @@ const playerStats: Stat[] = [
name: "Ranked Play Count", name: "Ranked Play Count",
color: "bg-pp", color: "bg-pp",
create: (player: ScoreSaberPlayer) => { create: (player: ScoreSaberPlayer) => {
const history = getPlayerHistoryToday(player); const rankedScoresChange = player.statisticChange?.daily.scores?.totalRankedScores;
const rankedScores = history.scores?.rankedScores;
return { return {
value: ( value: (
<> <>
{formatNumberWithCommas(player.statistics.rankedPlayCount)}{" "} {formatNumberWithCommas(player.statistics.rankedPlayCount)}{" "}
<DailyChange type={PlayerStat.RankedPlayCount} change={rankedScores} /> <DailyChange type={PlayerStat.RankedPlayCount} change={rankedScoresChange} />
</> </>
), ),
}; };
@ -56,16 +55,13 @@ const playerStats: Stat[] = [
{ {
name: "Total Play Count", name: "Total Play Count",
create: (player: ScoreSaberPlayer) => { create: (player: ScoreSaberPlayer) => {
const history = getPlayerHistoryToday(player); const scoresChange = player.statisticChange?.daily.scores;
const rankedScores = history.scores?.rankedScores;
const unrankedScores = history.scores?.unrankedScores;
const totalChange = (rankedScores ?? 0) + (unrankedScores ?? 0);
return { return {
value: ( value: (
<> <>
{formatNumberWithCommas(player.statistics.totalPlayCount)}{" "} {formatNumberWithCommas(player.statistics.totalPlayCount)}{" "}
<DailyChange type={PlayerStat.TotalPlayCount} change={totalChange} /> <DailyChange type={PlayerStat.TotalPlayCount} change={scoresChange?.totalScores} />
</> </>
), ),
}; };