Skip to content

Commit

Permalink
adding current streak
Browse files Browse the repository at this point in the history
  • Loading branch information
knighthinata committed Oct 7, 2024
1 parent d34ebc5 commit f3d00ea
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,36 @@ const client = new Client({
]
});

function calculateStreak(submissionCalendar) {
submissionCalendar = JSON.parse(submissionCalendar);
const datesWithSubmissions = Object.entries(submissionCalendar)
.filter(([ts, count]) => count > 0)
.map(([ts]) => new Date(ts * 1000))
.sort((a, b) => a - b);

if (datesWithSubmissions.length === 0) return 0;

let currentStreak = 0;
let hasSolvedToday = false;
const currentDate = new Date();
const lastSubmissionDate = datesWithSubmissions[datesWithSubmissions.length - 1];
// Check if the last submission was today
if (lastSubmissionDate.setHours(0, 0, 0, 0) === currentDate.setHours(0, 0, 0, 0)) {
hasSolvedToday = true;
currentDate.setDate(currentDate.getDate() + 1);
}
// Calculate streak
for (let i = datesWithSubmissions.length - 1; i >= 0; i--) {
currentDate.setDate(currentDate.getDate() - 1);
if (datesWithSubmissions[i].setHours(0, 0, 0, 0) === currentDate.setHours(0, 0, 0, 0)) {
currentStreak += 1;
} else {
break;
}
}
return {currentStreak, hasSolvedToday};
}

async function fetchLeetCodeProblems() {
try {
const response = await axios.get('https://leetcode.com/api/problems/all/');
Expand Down Expand Up @@ -180,13 +210,24 @@ client.on('messageCreate', async (message) => {
} else if (command === ';streak' && args.length === 2) {
const username = args[1];
try {
const streakInfo = await lc.user(username);
const user = await lc.user(username);
let streakInfo = 0;
let hasSolvedToday = false;
if(user.matchedUser) {
({ currentStreak: streakInfo, hasSolvedToday } = calculateStreak(user.matchedUser.submissionCalendar));
}

let streakMessage;
if (streakInfo.consecutiveDays > 0) {
streakMessage = `**${username}** has solved a problem for ${streakInfo.consecutiveDays} consecutive days! Keep it up!`;
if (streakInfo > 0) {
if (hasSolvedToday) {
streakMessage = `🎉 **${username}** has solved a problem for ${streakInfo} consecutive days! Great work, keep it up! 💪`;
} else {
streakMessage = `⚠️ **${username}** has solved a problem for ${streakInfo} consecutive days! Solve today's problem to maintain your streak and prevent it from resetting! 🔄`;
}
} else {
streakMessage = `**${username}** does not have a streak yet. Start solving problems to build your streak!`;
}
streakMessage = `❌ **${username}** does not have a streak yet. Start solving problems today to build your streak! 🚀`;
}

message.channel.send(streakMessage);
} catch (error) {
console.error('Error fetching streak info:', error);
Expand Down

0 comments on commit f3d00ea

Please sign in to comment.