From 9d3a56a64011f52a390350b81dd6e6c73dbaddd9 Mon Sep 17 00:00:00 2001 From: Ayden Jahola Date: Sat, 7 Sep 2024 11:32:03 +0100 Subject: [PATCH] trivia: add streaks --- commands/fun/leaderboard.js | 4 +++- commands/fun/trivia.js | 6 +++++- models/Leaderboard.js | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/commands/fun/leaderboard.js b/commands/fun/leaderboard.js index c09a329..0932a15 100644 --- a/commands/fun/leaderboard.js +++ b/commands/fun/leaderboard.js @@ -27,6 +27,7 @@ module.exports = { name: displayName, correctAnswers: entry.correctAnswers, gamesPlayed: entry.gamesPlayed, + streak: entry.streak, // Include streak }; } catch (error) { console.error( @@ -38,6 +39,7 @@ module.exports = { name: entry.username, correctAnswers: entry.correctAnswers, gamesPlayed: entry.gamesPlayed, + streak: entry.streak, // Include streak }; } }) @@ -50,7 +52,7 @@ module.exports = { // Add each leaderboard entry leaderboardEntries.forEach((entry) => { - const fieldValue = `${entry.correctAnswers} correct answers in ${entry.gamesPlayed} games`; + const fieldValue = `${entry.correctAnswers} correct answers in ${entry.gamesPlayed} games\nCurrent streak: ${entry.streak}`; leaderboardEmbed.addFields({ name: `${entry.position}. ${entry.name}`, diff --git a/commands/fun/trivia.js b/commands/fun/trivia.js index 90975e1..c946b33 100644 --- a/commands/fun/trivia.js +++ b/commands/fun/trivia.js @@ -163,17 +163,21 @@ const handleAnswerCollection = async ( username, gamesPlayed: 1, correctAnswers: userAnswer === correctAnswer ? 1 : 0, + streak: userAnswer === correctAnswer ? 1 : 0, // Start streak }); } else { userScore.gamesPlayed += 1; if (userAnswer === correctAnswer) { userScore.correctAnswers += 1; + userScore.streak += 1; // Increment streak + } else { + userScore.streak = 0; // Reset streak } } await userScore.save(); await interaction.followUp( - `${resultMessage} <@${userId}> You've answered ${userScore.correctAnswers} questions correctly out of ${userScore.gamesPlayed} games.` + `${resultMessage} <@${userId}> You've answered ${userScore.correctAnswers} questions correctly out of ${userScore.gamesPlayed} games. Your current streak is **${userScore.streak}**.` ); ONGOING_TRIVIA.delete(userId); diff --git a/models/Leaderboard.js b/models/Leaderboard.js index 29bc347..6f005c9 100644 --- a/models/Leaderboard.js +++ b/models/Leaderboard.js @@ -5,6 +5,7 @@ const leaderboardSchema = new mongoose.Schema({ username: { type: String, required: true }, gamesPlayed: { type: Number, default: 0 }, correctAnswers: { type: Number, default: 0 }, + streak: { type: Number, default: 0 }, }); const Leaderboard = mongoose.model("Leaderboard", leaderboardSchema);