diff --git a/commands/100-games/leaderboard.js b/commands/100-games/leaderboard.js index 3bd9e7b..0c1fe66 100644 --- a/commands/100-games/leaderboard.js +++ b/commands/100-games/leaderboard.js @@ -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; diff --git a/commands/100-games/wrapped.js b/commands/100-games/wrapped.js index 15ea8e6..3fead11 100644 --- a/commands/100-games/wrapped.js +++ b/commands/100-games/wrapped.js @@ -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); } diff --git a/databaseHelperFunctions.js b/databaseHelperFunctions.js index b8398f7..5f54ce6 100644 --- a/databaseHelperFunctions.js +++ b/databaseHelperFunctions.js @@ -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; }