-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtask.cpp
More file actions
337 lines (324 loc) · 13.2 KB
/
Copy pathtask.cpp
File metadata and controls
337 lines (324 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "task.h"
#include <ctime>
#include <fstream>
#include <sstream>
#include <cmath>
#include <raylib.h>
using namespace std;
vector<Task> allTasks;
string activeUsername = "";
bool showPopup = false;
double popupStartTime = 0;
Texture2D texMotivation;
int activeUserStreak = 0;
int activeUserMaxStreak = 0;
int lastYear = 0, lastMonth = 0, lastDay = 0;
int totalXP = 0; // Cumulative XP earned by completing tasks (persisted per-user)
ProfileData profile; // GitHub-style profile fields, persisted in "<username>_profile.txt"
// ── XP / Leveling helpers ──
int xpForPriority(int priority) {
// Higher priority tasks award proportionally more XP (10 XP per priority point)
if (priority < 1) priority = 1;
if (priority > 5) priority = 5;
return priority * 10;
}
int xpNeededForNextLevel() { return 100; } // Flat 100 XP per level, kept as a function for easy tuning later
int levelForXP(int xp) { return (xp / xpNeededForNextLevel()) + 1; }
int xpIntoCurrentLevel(int xp) { return xp % xpNeededForNextLevel(); }
int nextAvailableTaskId() {
int maxId = 0;
for (const auto& t : allTasks) if (t.taskId > maxId) maxId = t.taskId;
return maxId + 1;
}
string todayDateString() {
time_t now = time(0);
tm *ltm = localtime(&now);
char buf[16];
strftime(buf, sizeof(buf), "%Y-%m-%d", ltm);
return string(buf);
}
// ── Profile persistence ──
// One pipe-delimited line, same convention as the streak/XP header line
// written by saveUserData(). Kept in its own file so it never interferes
// with existing "<username>.txt" task saves.
void loadProfileData() {
ifstream file(activeUsername + "_profile.txt");
if (!file.is_open()) {
// First time this user opens Profile — stamp a join date and
// default the display name to the username, then create the file.
profile = ProfileData();
profile.joinDate = todayDateString();
profile.fullName = activeUsername;
saveProfileData();
return;
}
string line;
if (getline(file, line)) {
stringstream ss(line);
getline(ss, profile.joinDate, '|');
getline(ss, profile.fullName, '|');
getline(ss, profile.pronouns, '|');
getline(ss, profile.bio, '|');
getline(ss, profile.website, '|');
getline(ss, profile.twitter, '|');
getline(ss, profile.linkedin, '|');
}
file.close();
}
void saveProfileData() {
ofstream file(activeUsername + "_profile.txt");
file << profile.joinDate << "|" << profile.fullName << "|" << profile.pronouns << "|"
<< profile.bio << "|" << profile.website << "|" << profile.twitter << "|"
<< profile.linkedin << "\n";
file.close();
}
void loadUserData() {
loadProfileData(); // profile fields are independent of task history — load first
allTasks.clear();
ifstream file(activeUsername + ".txt");
if (!file.is_open()) {
time_t now = time(0);
tm *ltm = localtime(&now);
lastYear = ltm->tm_year + 1900;
lastMonth = ltm->tm_mon + 1;
lastDay = ltm->tm_mday;
activeUserStreak = 1;
activeUserMaxStreak = 1;
saveUserData();
return;
}
string line;
if (getline(file, line)) {
stringstream ss(line);
string temp;
getline(ss, temp, '|'); activeUserStreak = stoi(temp);
getline(ss, temp, '|'); activeUserMaxStreak = stoi(temp);
getline(ss, temp, '|'); lastYear = stoi(temp);
getline(ss, temp, '|'); lastMonth = stoi(temp);
getline(ss, temp, '|'); lastDay = stoi(temp);
// totalXP is a newer field — older save files won't have it, so default to 0
if (getline(ss, temp, '|') && !temp.empty()) totalXP = stoi(temp);
else totalXP = 0;
}
time_t now = time(0);
tm *ltm = localtime(&now);
int currY = ltm->tm_year + 1900;
int currM = ltm->tm_mon + 1;
int currD = ltm->tm_mday;
tm last_tm = {0};
last_tm.tm_year = lastYear - 1900;
last_tm.tm_mon = lastMonth - 1;
last_tm.tm_mday = lastDay;
time_t last_time = mktime(&last_tm);
tm now_tm = {0};
now_tm.tm_year = currY - 1900;
now_tm.tm_mon = currM - 1;
now_tm.tm_mday = currD;
time_t now_time = mktime(&now_tm);
double diff = difftime(now_time, last_time) / (60 * 60 * 24);
int daysDiff = round(diff);
if (daysDiff == 1) {
activeUserStreak++;
if (activeUserStreak > activeUserMaxStreak) activeUserMaxStreak = activeUserStreak;
} else if (daysDiff > 1) {
activeUserStreak = 1;
}
lastYear = currY; lastMonth = currM; lastDay = currD;
while (getline(file, line)) {
if (line.empty()) continue;
stringstream ss(line);
string temp;
Task t;
getline(ss, temp, '|'); t.taskId = stoi(temp);
getline(ss, t.taskName, '|');
getline(ss, t.taskDescription, '|');
getline(ss, t.taskCategory, '|');
getline(ss, temp, '|'); t.priority = stoi(temp);
getline(ss, temp, '|'); t.isCompleted = (temp == "1");
getline(ss, temp, '|'); t.daysToComplete = stoi(temp);
getline(ss, temp, '|'); t.dueDay = stoi(temp);
getline(ss, temp, '|'); t.dueMonth = stoi(temp);
getline(ss, temp, '|'); t.dueYear = stoi(temp);
getline(ss, t.dateCreated, '|');
// dateCompleted is a newer trailing field — default to empty for older rows
if (!getline(ss, t.dateCompleted, '|')) t.dateCompleted = "";
allTasks.push_back(t);
}
file.close();
saveUserData();
}
void saveUserData() {
ofstream file(activeUsername + ".txt");
file << activeUserStreak << "|" << activeUserMaxStreak << "|" << lastYear << "|" << lastMonth << "|" << lastDay << "|" << totalXP << "\n";
for (const auto& t : allTasks) {
file << t.taskId << "|" << t.taskName << "|" << t.taskDescription << "|"
<< t.taskCategory << "|" << t.priority << "|" << (t.isCompleted ? "1" : "0") << "|"
<< t.daysToComplete << "|" << t.dueDay << "|" << t.dueMonth << "|" << t.dueYear << "|" << t.dateCreated
<< "|" << t.dateCompleted << "\n";
}
file.close();
}
void addTask() {
Task t;
std::cout << "\n========== ADD TASK ==========\n";
// Task ID is now auto-assigned — the user is never asked for one
t.taskId = nextAvailableTaskId();
t.dateCompleted = "";
cin.ignore();
std::cout << "Enter Task Name: "; getline(cin, t.taskName);
std::cout << "Enter Description: "; getline(cin, t.taskDescription);
std::cout << "Enter Category: "; getline(cin, t.taskCategory);
std::cout << "Enter Priority (1-5): "; cin >> t.priority;
std::cout << "Enter Number of Days To Complete: "; cin >> t.daysToComplete;
time_t now = time(0);
now += t.daysToComplete * 24 * 60 * 60;
tm *deadline = localtime(&now);
t.dueDay = deadline->tm_mday;
t.dueMonth = deadline->tm_mon + 1;
t.dueYear = deadline->tm_year + 1900;
t.isCompleted = false;
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M", timeinfo);
t.dateCreated = string(buffer);
allTasks.push_back(t);
saveUserData();
std::cout << "\nTask Added Successfully!\n";
}
void displayTasks() {
if (allTasks.empty()) { cout << "\nNo tasks available!\n"; return; }
cout << "\n========== ALL TASKS ==========\n";
for (const auto& t : allTasks) {
cout << "ID: " << t.taskId << " | Name: " << t.taskName
<< " | Priority: " << t.priority << " | Status: " << (t.isCompleted ? "Completed" : "Pending")
<< " | Due: " << t.dueDay << "/" << t.dueMonth << "/" << t.dueYear << "\n";
}
}
void editTask() {
int id; cout << "\nEnter Task ID: "; cin >> id; cin.ignore();
for (auto& t : allTasks) {
if (t.taskId == id) {
int choice;
cout << "1. Edit Name\n2. Edit Description\n3. Edit Category\n4. Edit Priority\n5. Edit Days\nEnter choice: ";
cin >> choice; cin.ignore();
switch(choice) {
case 1: getline(cin, t.taskName); break;
case 2: getline(cin, t.taskDescription); break;
case 3: getline(cin, t.taskCategory); break;
case 4: cin >> t.priority; break;
case 5: {
cin >> t.daysToComplete; time_t now = time(0); now += t.daysToComplete * 24 * 60 * 60;
tm *deadline = localtime(&now); t.dueDay = deadline->tm_mday; t.dueMonth = deadline->tm_mon + 1; t.dueYear = deadline->tm_year + 1900; break;
}
default: cout << "\nInvalid Choice\n"; return;
}
saveUserData(); // Save automatically!
std::cout << "\nTask Edited Successfully!\n";
return;
}
}
cout << "\nTask Not Found!\n";
}
void removeTask() {
int id; cout << "\nEnter Task ID: "; cin >> id;
for (size_t i = 0; i < allTasks.size(); i++) {
if (allTasks[i].taskId == id) {
allTasks.erase(allTasks.begin() + i);
saveUserData(); // Save automatically!
cout << "\nTask Removed!\n";
return;
}
}
cout << "\nTask Not Found!\n";
}
void markCompleted() {
int id; cout << "\nEnter Task ID: "; cin >> id;
for (auto& t : allTasks) {
if (t.taskId == id) {
t.isCompleted = true;
t.dateCompleted = todayDateString();
int gained = xpForPriority(t.priority);
totalXP += gained;
saveUserData(); // Save automatically!
cout << "\nTask Marked as Complete! (+" << gained << " XP)\n";
cout << "Total XP: " << totalXP << " | Level: " << levelForXP(totalXP) << "\n";
showPopup = true;
popupStartTime = GetTime();
return;
}
}
cout << "\nTask Not Found!\n";
}
void checkReminders() {
bool urgencyFound = false;
time_t now = time(0);
for (const auto &t : allTasks) {
if (!t.isCompleted) {
tm deadline_tm = {0};
deadline_tm.tm_year = t.dueYear - 1900;
deadline_tm.tm_mon = t.dueMonth - 1;
deadline_tm.tm_mday = t.dueDay;
time_t deadline_time = mktime(&deadline_tm);
int secondsLeft = difftime(deadline_time, now);
if (secondsLeft <= 0) { // Deadline already passed
cout << "\n========================================\n";
cout << "⚠️ DEADLINE REACHED: '" << t.taskName << "' is overdue!\n";
cout << "========================================\n";
}
int daysLeft =static_cast<int>( secondsLeft / (24 * 3600));
if( daysLeft <0) daysLeft =0;
int hoursLeft=static_cast<int>((secondsLeft- daysLeft*24*3600)/3600);
if (daysLeft <= 2.0) {
if (!urgencyFound) {
cout << "\n========================================\n";
cout << " PENDING DUE TASKS \n";
cout << "========================================\n";
urgencyFound = true;
}
cout << "[!] URGENT: '" << t.taskName << "' is due in " << daysLeft << " days!\n";
cout << hoursLeft <<"hours!\n";
cout << hoursLeft << endl;
}
}
}
if (urgencyFound) cout << "========================================\n";
}
void displayHistory() {
cout << "\n=====================================================\n";
cout << " TASK HISTORY \n";
cout << "=====================================================\n";
for (const auto &t : allTasks) {
cout << "ID: " << t.taskId << " | Name: " << t.taskName
<< "\nCreated: " << t.dateCreated
<< " | Status: " << (t.isCompleted ? "COMPLETED" : "PENDING") << "\n\n";
}
cout << "=====================================================\n";
}
void ShowCompletionPopup() {
if (texMotivation.id == 0) {
texMotivation = LoadTexture("assets/motivation.png");
}
int popupWidth = 500;
int popupHeight = 300;
int x = (GetScreenWidth() - popupWidth) / 2;
int y = (GetScreenHeight() - popupHeight) / 2;
// Background rectangle
DrawRectangle(x, y, popupWidth, popupHeight, Fade(DARKGRAY, 0.85f));
DrawRectangleLines(x, y, popupWidth, popupHeight, RAYWHITE);
// Draw motivational image centered
DrawTexture(texMotivation,
x + (popupWidth/2 - texMotivation.width/2),
y + (popupHeight/2 - texMotivation.height/2),
WHITE);
// Overlay text
DrawText("Task Completed!", x + 50, y + popupHeight - 50, 30, GREEN);
}
void CleanupTaskTextures() {
if (texMotivation.id != 0) {
UnloadTexture(texMotivation);
texMotivation.id = 0;
}
}