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 { 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,

@ -59,4 +59,5 @@ export type StatisticChange = {
daily: PlayerHistory;
weekly: 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 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;
}

@ -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)}{" "}
<DailyChange type={PlayerStat.RankedPlayCount} change={rankedScores} />
<DailyChange type={PlayerStat.RankedPlayCount} change={rankedScoresChange} />
</>
),
};
@ -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)}{" "}
<DailyChange type={PlayerStat.TotalPlayCount} change={totalChange} />
<DailyChange type={PlayerStat.TotalPlayCount} change={scoresChange?.totalScores} />
</>
),
};