trivia: add streaks

This commit is contained in:
Ayden Jahola 2024-09-07 11:32:03 +01:00
parent b33a4c02cc
commit 9d3a56a640
No known key found for this signature in database
GPG key ID: 71DD90AE4AE92742
3 changed files with 9 additions and 2 deletions

View file

@ -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}`,

View file

@ -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);

View file

@ -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);