Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add corrected current streak for streak command #34

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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