fill in missing data from the scoresaber api data
All checks were successful
Deploy Backend / deploy (push) Successful in 3m0s
Deploy Website / deploy (push) Successful in 4m8s

This commit is contained in:
Lee 2024-10-11 04:02:21 +01:00
parent 570f5e1eab
commit dad8afe282

@ -124,24 +124,35 @@ export async function getScoreSaberPlayerFromToken(
isBeingTracked = true; isBeingTracked = true;
} }
statisticHistory = history; statisticHistory = history;
} catch (error) { } catch (e) {
// Fallback to ScoreSaber History if the player has no history console.log("Player has no history, using fallback");
}
const playerRankHistory = token.histories.split(",").map(value => { const playerRankHistory = token.histories.split(",").map(value => {
return parseInt(value); return parseInt(value);
}); });
playerRankHistory.push(token.rank); playerRankHistory.push(token.rank);
let missingDays = 0;
let daysAgo = 0; // Start from current day let daysAgo = 0; // Start from current day
for (let i = playerRankHistory.length - 1; i >= 0; i--) { for (let i = playerRankHistory.length - 1; i >= 0; i--) {
const rank = playerRankHistory[i]; const rank = playerRankHistory[i];
const date = getMidnightAlignedDate(getDaysAgoDate(daysAgo)); const date = getMidnightAlignedDate(getDaysAgoDate(daysAgo));
daysAgo += 1; // Increment daysAgo for each earlier rank daysAgo += 1; // Increment daysAgo for each earlier rank
if (statisticHistory[formatDateMinimal(date)] == undefined) {
missingDays += 1;
statisticHistory[formatDateMinimal(date)] = { statisticHistory[formatDateMinimal(date)] = {
rank: rank, rank: rank,
}; };
} }
} }
if (missingDays > 0) {
console.log(
`Player has ${missingDays} missing day${missingDays > 1 ? "s" : ""}, filling in with fallback history...`
);
}
// Sort the fallback history // Sort the fallback history
statisticHistory = Object.entries(statisticHistory) statisticHistory = Object.entries(statisticHistory)
.sort((a, b) => Date.parse(b[0]) - Date.parse(a[0])) .sort((a, b) => Date.parse(b[0]) - Date.parse(a[0]))