Account for earliest date beaten when creating leaderboards

Closes #23
This commit is contained in:
baz 2024-12-31 00:11:42 +00:00
parent d9feef56c4
commit c1efae3b69
3 changed files with 26 additions and 9 deletions

View File

@ -12,7 +12,6 @@ module.exports = {
if (!leaderboard) return interaction.editReply({ content: 'There was a problem!', ephemeral: true });
await leaderboard.sort((a, b) => parseInt(b.count) - parseInt(a.count));
let desc = '';
let count = 0;

View File

@ -374,14 +374,12 @@ async function GetNumberOfDroppedGames(userDatabaseEntry) {
async function GetYearLeaderboardPosition(userDatabaseEntry, year) {
const leaderboard = await getLeaderboardEntriesBetweenDates(`${year}-01-01`, `${year}-12-31`);
await leaderboard.sort((a, b) => parseInt(b.count) - parseInt(a.count));
const index = leaderboard.findIndex(item => item.username === userDatabaseEntry.username) + 1;
return await appendOrdinalSuffix(index);
}
async function GetLeaderboardPosition(userDatabaseEntry) {
const leaderboard = await getLeaderboardEntries();
await leaderboard.sort((a, b) => parseInt(b.count) - parseInt(a.count));
const index = leaderboard.findIndex(item => item.username === userDatabaseEntry.username) + 1;
return await appendOrdinalSuffix(index);
}

View File

@ -291,7 +291,14 @@ async function getLeaderboardEntries() {
const results = [];
for (let i = 0; i < users.length; i++) {
const count = await LoggedGames.count({ where: { userId: users[i].id, status: 'beat' } });
const games = await LoggedGames.findAll({ where: { userId: users[i].id, status: 'beat' } });
const count = games.length;
let dateLastBeat = new Date();
if (count > 0) {
const lastGame = games.at(-1);
dateLastBeat = lastGame.statusLastChanged;
}
const res = await Users.findOne({ where: { id: users[i].id } })
.catch((err) => {
@ -299,10 +306,13 @@ async function getLeaderboardEntries() {
});
const username = res.username;
const fun = { username, count };
results.push(fun);
const result = { username, count, dateLastBeat };
results.push(result);
}
await results.sort((a, b) => parseInt(b.dateLastBeat) - parseInt(a.dateLastBeat));
await results.sort((a, b) => parseInt(b.count) - parseInt(a.count));
return results;
}
@ -318,7 +328,14 @@ async function getLeaderboardEntriesBetweenDates(start, end) {
const endDate = new Date(end);
for (let i = 0; i < users.length; i++) {
const count = await LoggedGames.count({ where: { userId: users[i].id, status: 'beat', statusLastChanged: { [ Op.between ]: [startDate, endDate] } } });
const games = await LoggedGames.findAll({ where: { userId: users[i].id, status: 'beat', statusLastChanged: { [ Op.between ]: [startDate, endDate] } } });
const count = games.length;
let dateLastBeat = new Date();
if (count > 0) {
const lastGame = games.at(-1);
dateLastBeat = lastGame.statusLastChanged;
}
const res = await Users.findOne({ where: { id: users[i].id } })
.catch((err) => {
@ -326,10 +343,13 @@ async function getLeaderboardEntriesBetweenDates(start, end) {
});
const username = res.username;
const fun = { username, count };
results.push(fun);
const result = { username, count, dateLastBeat };
results.push(result);
}
await results.sort((a, b) => parseInt(b.dateLastBeat) - parseInt(a.dateLastBeat));
await results.sort((a, b) => parseInt(b.count) - parseInt(a.count));
return results;
}