potential fix for this?
All checks were successful
Deploy Backend / deploy (push) Successful in 2m55s
Deploy Website / deploy (push) Successful in 5m13s

This commit is contained in:
Lee 2024-10-12 05:26:30 +01:00
parent e67fcf328e
commit f26b997fbb
3 changed files with 46 additions and 23 deletions

@ -168,39 +168,62 @@ export async function getScoreSaberPlayerFromToken(
*/ */
const getChange = (statType: "rank" | "countryRank" | "pp", daysAgo: number = 1): number | undefined => { const getChange = (statType: "rank" | "countryRank" | "pp", daysAgo: number = 1): number | undefined => {
const todayStats = statisticHistory[todayDate]; const todayStats = statisticHistory[todayDate];
let otherDate: string | undefined; let otherDate: Date | undefined;
// Use the same logic as the first version to get the date exactly 'daysAgo' days earlier // Use the same logic as the first version to get the date exactly 'daysAgo' days earlier
if (daysAgo === 1) { if (daysAgo === 1) {
otherDate = formatDateMinimal(getMidnightAlignedDate(getDaysAgoDate(1))); // Yesterday otherDate = getMidnightAlignedDate(getDaysAgoDate(1)); // Yesterday
} else { } else {
const targetDate = getDaysAgoDate(daysAgo); const targetDate = getDaysAgoDate(daysAgo);
const date = Object.keys(statisticHistory)
// Filter available dates to find the closest one to the target
const availableDates = Object.keys(statisticHistory)
.map(dateKey => new Date(dateKey)) .map(dateKey => new Date(dateKey))
.reduce((closestDate, currentDate) => { .filter(date => {
const currentDiff = Math.abs(currentDate.getTime() - targetDate.getTime()); // Convert date back to the correct format for statisticHistory lookup
const closestDiff = Math.abs(closestDate.getTime() - targetDate.getTime()); const formattedDate = formatDateMinimal(date);
return currentDiff < closestDiff ? currentDate : closestDate; const statsForDate = statisticHistory[formattedDate];
}, targetDate); const hasStat = statsForDate && statType in statsForDate;
otherDate = formatDateMinimal(date);
// Only consider past dates with the required statType
const isPast = date.getTime() < new Date().getTime();
return hasStat && isPast;
});
// If no valid dates are found, return undefined
if (availableDates.length === 0) {
return undefined;
}
// Find the closest date from the filtered available dates
otherDate = availableDates.reduce((closestDate, currentDate) => {
const currentDiff = Math.abs(currentDate.getTime() - targetDate.getTime());
const closestDiff = Math.abs(closestDate.getTime() - targetDate.getTime());
return currentDiff < closestDiff ? currentDate : closestDate;
}, availableDates[0]); // Start with the first available date
} }
if (!otherDate) {
// Ensure todayStats exists and contains the statType
if (!todayStats || !(statType in todayStats)) {
return undefined; return undefined;
} }
const otherStats = statisticHistory[otherDate]; const otherStats = statisticHistory[formatDateMinimal(otherDate)]; // This is now validated
const hasChange = !!(todayStats && otherStats);
if (!hasChange) { // Ensure otherStats exists and contains the statType
if (!otherStats || !(statType in otherStats)) {
return undefined; return undefined;
} }
const statToday = todayStats[`${statType}`]; const statToday = todayStats[statType];
const statOther = otherStats[`${statType}`]; const statOther = otherStats[statType];
if (!statToday || !statOther) {
if (statToday === undefined || statOther === undefined) {
return undefined; return undefined;
} }
return (statToday - statOther) * (statType == "pp" ? 1 : -1); // Return the difference, accounting for negative changes in ranks
return (statToday - statOther) * (statType === "pp" ? 1 : -1);
}; };
return { return {
@ -214,7 +237,7 @@ export async function getScoreSaberPlayerFromToken(
bio: bio, bio: bio,
pp: token.pp, pp: token.pp,
statisticChange: { statisticChange: {
today: { daily: {
rank: getChange("rank", 1), rank: getChange("rank", 1),
countryRank: getChange("countryRank", 1), countryRank: getChange("countryRank", 1),
pp: getChange("pp", 1), pp: getChange("pp", 1),

@ -56,7 +56,7 @@ export default class Player {
} }
export type StatisticChange = { export type StatisticChange = {
today: PlayerHistory; daily: PlayerHistory;
weekly: PlayerHistory; weekly: PlayerHistory;
monthly: PlayerHistory; monthly: PlayerHistory;
}; };

@ -40,7 +40,7 @@ const renderDailyChange = (change: number, tooltip: ReactElement, format?: (valu
* @param type the type of stat to get the change for * @param type the type of stat to get the change for
*/ */
const renderChange = (player: ScoreSaberPlayer, type: "rank" | "countryRank" | "pp", children: ReactElement) => { const renderChange = (player: ScoreSaberPlayer, type: "rank" | "countryRank" | "pp", children: ReactElement) => {
const todayStats = player.statisticChange?.today; const todayStats = player.statisticChange?.daily;
const weeklyStats = player.statisticChange?.weekly; const weeklyStats = player.statisticChange?.weekly;
const monthlyStats = player.statisticChange?.monthly; const monthlyStats = player.statisticChange?.monthly;
const todayStat = todayStats?.[type]; const todayStat = todayStats?.[type];
@ -94,7 +94,7 @@ const playerData = [
}, },
render: (player: ScoreSaberPlayer) => { render: (player: ScoreSaberPlayer) => {
const statisticChange = player.statisticChange; const statisticChange = player.statisticChange;
const rankChange = statisticChange?.today?.rank ?? 0; const rankChange = statisticChange?.daily?.rank ?? 0;
return ( return (
<div className="text-gray-300 flex gap-1 items-center"> <div className="text-gray-300 flex gap-1 items-center">
@ -117,7 +117,7 @@ const playerData = [
}, },
render: (player: ScoreSaberPlayer) => { render: (player: ScoreSaberPlayer) => {
const statisticChange = player.statisticChange; const statisticChange = player.statisticChange;
const rankChange = statisticChange?.today?.countryRank ?? 0; const rankChange = statisticChange?.daily?.countryRank ?? 0;
return ( return (
<div className="text-gray-300 flex gap-1 items-center"> <div className="text-gray-300 flex gap-1 items-center">
@ -139,7 +139,7 @@ const playerData = [
showWhenInactiveOrBanned: true, showWhenInactiveOrBanned: true,
render: (player: ScoreSaberPlayer) => { render: (player: ScoreSaberPlayer) => {
const statisticChange = player.statisticChange; const statisticChange = player.statisticChange;
const ppChange = statisticChange?.today?.pp ?? 0; const ppChange = statisticChange?.daily?.pp ?? 0;
return ( return (
<div className="text-gray-300 flex gap-1 items-center"> <div className="text-gray-300 flex gap-1 items-center">