fix timeframes
All checks were successful
Deploy Backend / deploy (push) Successful in 3m3s
Deploy Website / deploy (push) Successful in 4m15s

This commit is contained in:
Lee 2024-10-12 02:41:24 +01:00
parent 2ab3d6b023
commit afdbe0a3dc

@ -169,23 +169,19 @@ export async function getScoreSaberPlayerFromToken(
const todayStats = statisticHistory[todayDate]; const todayStats = statisticHistory[todayDate];
let otherDate: string | undefined; let otherDate: string | undefined;
// Yesterday // Use the same logic as the first version to get the date exactly 'daysAgo' days earlier
if (daysAgo == 1) { if (daysAgo === 1) {
otherDate = todayDate; otherDate = formatDateMinimal(getMidnightAlignedDate(getDaysAgoDate(1))); // Yesterday
} else { } else {
// get the closest next date const targetDate = getDaysAgoDate(daysAgo);
let closestDateDiff = Infinity; const date = Object.keys(statisticHistory)
.map(dateKey => new Date(dateKey))
for (const dateKey in statisticHistory) { .reduce((closestDate, currentDate) => {
const date = new Date(dateKey); const currentDiff = Math.abs(currentDate.getTime() - targetDate.getTime());
const daysDiff = Math.abs(date.getTime() - getDaysAgoDate(daysAgo).getTime()); const closestDiff = Math.abs(closestDate.getTime() - targetDate.getTime());
return currentDiff < closestDiff ? currentDate : closestDate;
// If this date is closer, update the closest date }, targetDate);
if (daysDiff < closestDateDiff) { otherDate = formatDateMinimal(date);
closestDateDiff = daysDiff;
otherDate = formatDateMinimal(date);
}
}
} }
if (!otherDate) { if (!otherDate) {
@ -194,10 +190,10 @@ export async function getScoreSaberPlayerFromToken(
const otherStats = statisticHistory[otherDate]; const otherStats = statisticHistory[otherDate];
const hasChange = !!(todayStats && otherStats); const hasChange = !!(todayStats && otherStats);
if (!hasChange) { if (!hasChange) {
return 0; return 0;
} }
const statToday = todayStats[`${statType}`]; const statToday = todayStats[`${statType}`];
const statOther = otherStats[`${statType}`]; const statOther = otherStats[`${statType}`];
return (!!(statToday && statOther) ? statToday - statOther : 0) * (statType == "pp" ? 1 : -1); return (!!(statToday && statOther) ? statToday - statOther : 0) * (statType == "pp" ? 1 : -1);