-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
383 lines (312 loc) · 11.5 KB
/
script.js
File metadata and controls
383 lines (312 loc) · 11.5 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
let currentUser = null;
let users = JSON.parse(localStorage.getItem("users")) || {};
document.getElementById("showPassword").addEventListener("change", (e) => {
const passwordField = document.getElementById("password");
passwordField.type = e.target.checked ? "text" : "password";
});
function handleAuth() {
if (currentUser) {
// Logout
logout();
return;
}
// Login / Signup
const username = document.getElementById("username").value.trim();
const password = document.getElementById("password").value.trim();
if (!username || !password) {
alert("Please enter username and password");
return;
}
if (!users[username]) {
// new signup
users[username] = { password, points: 0, tasks: [], rewards: [] };
alert("New account created!");
} else if (users[username].password !== password) {
alert("Incorrect password!");
return;
}
currentUser = username;
saveUsers();
document.getElementById("authButton").textContent = "Logout";
document.getElementById("app").style.display = "block";
// Hide the welcome description after login
document.getElementById("description").style.display = "none";
loadUserData();
}
function logout() {
currentUser = null;
document.getElementById("authBox").style.display = "block";
document.getElementById("app").style.display = "none";
document.getElementById("username").value = "";
document.getElementById("password").value = "";
document.getElementById("authButton").textContent = "Login / Signup";
document.getElementById("description").style.display = "block";
}
function saveUsers() {
localStorage.setItem("users", JSON.stringify(users));
}
function loadUserData() {
const user = users[currentUser];
points = user.points || 0;
tasks = user.tasks || [];
rewards = user.rewards || [];
document.getElementById("points").textContent = points.toFixed(1);
renderTasks();
renderRewards();
}
let tasks = [];
let points = 0;
let rewards = [];
// Save current user’s data
function saveData() {
if (!currentUser) return;
users[currentUser].points = points;
users[currentUser].tasks = tasks;
users[currentUser].rewards = rewards;
saveUsers();
}
// ---- TASKS ----
function renderTasks() {
const taskList = document.getElementById("taskList");
taskList.innerHTML = "";
tasks.forEach(task => {
const taskDiv = document.createElement("div");
taskDiv.className = "task" + (task.completed ? " completed" : "");
taskDiv.innerHTML = `
<div class="task-title">
<strong>${task.title}</strong> - ${Math.floor(task.hours)} hr ${Math.round((task.hours % 1) * 60)} min = ${task.points.toFixed(1)} pts
<em>(${task.category})</em>
</div>
<div class="task-actions">
<button onclick="toggleTask(${task.id})">${task.completed ? "Undo" : "Complete"}</button>
<button class="delete-btn" onclick="deleteTask(${task.id})">Delete</button>
</div>
`;
taskList.appendChild(taskDiv);
});
}
function addTask() {
const title = document.getElementById("taskTitle").value.trim();
const hours = Number(document.getElementById("taskHours").value);
const minutes = Number(document.getElementById("taskMinutes").value);
const category = document.getElementById("taskCategory").value;
if (!title || (hours === 0 && minutes === 0)) return;
// Convert total time to fractional hours
const totalHours = hours + minutes / 60;
const points = totalHours * 10; // 1 hour = 10 points
const newTask = {
id: Date.now(),
title,
hours: totalHours,
points,
completed: false,
category
};
tasks.push(newTask);
document.getElementById("taskTitle").value = "";
document.getElementById("taskHours").value = 0;
document.getElementById("taskMinutes").value = 30;
renderTasks();
saveData();
}
function toggleTask(id) {
tasks = tasks.map(task => {
if (task.id === id) {
if (!task.completed) {
points += task.points;
} else {
points -= task.points;
}
task.completed = !task.completed;
}
return task;
});
document.getElementById("points").textContent = points.toFixed(1);
renderTasks();
saveData();
}
function deleteTask(id) {
const task = tasks.find(t => t.id === id);
if (task?.completed) {
points -= task.points;
}
tasks = tasks.filter(task => task.id !== id);
document.getElementById("points").textContent = points.toFixed(1);
renderTasks();
saveData();
}
let taskTimer = null;
let taskStartTime = null;
function startTaskTimer() {
const title = document.getElementById("taskTitle").value.trim();
if (!title) return alert("Enter a task title before starting the timer!");
taskStartTime = Date.now();
document.getElementById("startTaskTimerBtn").disabled = true;
document.getElementById("stopTaskTimerBtn").disabled = false;
taskTimer = setInterval(() => {
const elapsed = Date.now() - taskStartTime;
const hrs = Math.floor(elapsed / (1000 * 60 * 60));
const mins = Math.floor((elapsed % (1000 * 60 * 60)) / (1000 * 60));
const secs = Math.floor((elapsed % (1000 * 60)) / 1000);
document.getElementById("taskTimerDisplay").textContent =
`${hrs.toString().padStart(2,'0')}:${mins.toString().padStart(2,'0')}:${secs.toString().padStart(2,'0')}`;
}, 1000);
}
function stopTaskTimer() {
if (!taskStartTime) return;
const elapsedMs = Date.now() - taskStartTime;
clearInterval(taskTimer);
taskTimer = null;
document.getElementById("startTaskTimerBtn").disabled = false;
document.getElementById("stopTaskTimerBtn").disabled = true;
// Convert ms to total minutes, round up
const totalMinutes = Math.floor(elapsedMs / (1000 * 60));
const hrs = Math.floor(totalMinutes / 60);
const mins = totalMinutes % 60;
const totalHours = totalMinutes / 60; // fractional hours for points
const pointsEarned = totalHours * 10; // 1 hour = 10 pts
// Auto-add task using the title and category from inputs
const title = document.getElementById("taskTitle").value.trim();
const category = document.getElementById("taskCategory").value;
if (!title) return alert("Task title is required!");
const newTask = {
id: Date.now(),
title,
hours: totalHours,
points: pointsEarned,
completed: true, // mark completed automatically
category
};
tasks.push(newTask);
points += pointsEarned;
// Reset inputs and timer display
document.getElementById("taskTitle").value = "";
document.getElementById("taskHours").value = 0;
document.getElementById("taskMinutes").value = 30;
document.getElementById("taskTimerDisplay").textContent = "00:00:00";
document.getElementById("points").textContent = points.toFixed(1);
renderTasks();
saveData();
}
// ---- REWARDS ----
function redeemReward() {
const name = document.getElementById("rewardName").value.trim();
const hours = Number(document.getElementById("rewardHours").value);
const minutes = Number(document.getElementById("rewardMinutes").value);
const category = document.getElementById("rewardCategory").value;
if (!name || (hours === 0 && minutes === 0)) {
return alert("Enter a valid reward and time!");
}
const totalHours = hours + minutes / 60;
const cost = totalHours * 40; // 1 hour reward = 40 points
if (points < cost) return alert("Not enough points!");
points -= cost;
rewards.push({
id: Date.now(),
name,
hours: totalHours,
cost,
category
});
document.getElementById("points").textContent = points.toFixed(1);
document.getElementById("rewardName").value = "";
document.getElementById("rewardHours").value = 0;
document.getElementById("rewardMinutes").value = 30;
renderRewards();
saveData();
}
function renderRewards() {
const rewardHistory = document.getElementById("rewardHistory");
rewardHistory.innerHTML = "";
rewards.forEach(r => {
const rewardDiv = document.createElement("div");
rewardDiv.className = "reward-item";
const hrs = Math.floor(r.hours);
const mins = Math.round((r.hours % 1) * 60);
rewardDiv.innerHTML = `
<div class="reward-title">
<strong>${r.name}</strong> - ${hrs} hr ${mins} min = ${r.cost.toFixed(1)} pts
<em>(${r.category})</em>
</div>
<div class="reward-actions">
<button onclick="undoReward(${r.id})">Undo</button>
<button class="delete-btn" onclick="deleteReward(${r.id})">Delete</button>
</div>
`;
rewardHistory.appendChild(rewardDiv);
});
}
// Undo a reward: refund points and remove reward
function undoReward(id) {
const rewardIndex = rewards.findIndex(r => r.id === id);
if (rewardIndex === -1) return;
const reward = rewards[rewardIndex];
points += reward.cost; // refund points
rewards.splice(rewardIndex, 1);
document.getElementById("points").textContent = points.toFixed(1);
renderRewards();
saveData();
}
// Delete a reward and refund points
function deleteReward(id) {
const rewardIndex = rewards.findIndex(r => r.id === id);
if (rewardIndex === -1) return;
const reward = rewards[rewardIndex];
// Refund points
points += reward.cost;
// Remove reward
rewards.splice(rewardIndex, 1);
document.getElementById("points").textContent = points.toFixed(1);
renderRewards();
saveData();
}
let rewardTimer = null;
let rewardStartTime = null;
function startRewardTimer() {
const name = document.getElementById("rewardName").value.trim();
if (!name) return alert("Enter a reward name before starting the timer!");
rewardStartTime = Date.now();
document.getElementById("startTimerBtn").disabled = true;
document.getElementById("stopTimerBtn").disabled = false;
rewardTimer = setInterval(() => {
const elapsed = Date.now() - rewardStartTime;
const hrs = Math.floor(elapsed / (1000 * 60 * 60));
const mins = Math.floor((elapsed % (1000 * 60 * 60)) / (1000 * 60));
const secs = Math.floor((elapsed % (1000 * 60)) / 1000);
document.getElementById("timerDisplay").textContent =
`${hrs.toString().padStart(2,'0')}:${mins.toString().padStart(2,'0')}:${secs.toString().padStart(2,'0')}`;
}, 1000);
}
function stopRewardTimer() {
if (!rewardStartTime) return;
const elapsedMs = Date.now() - rewardStartTime;
clearInterval(rewardTimer);
rewardTimer = null;
document.getElementById("startTimerBtn").disabled = false;
document.getElementById("stopTimerBtn").disabled = true;
// Convert ms to total minutes, round up
const totalMinutes = Math.ceil(elapsedMs / (1000 * 60));
const hrs = Math.floor(totalMinutes / 60);
const mins = totalMinutes % 60;
const totalHours = totalMinutes / 60; // fractional hours for points
const cost = totalHours * 40;
if (points < cost) return alert("Not enough points for this reward!");
points -= cost;
const name = document.getElementById("rewardName").value.trim();
const category = document.getElementById("rewardCategory").value;
rewards.push({
id: Date.now(),
name,
hours: totalHours,
cost,
category
});
// Reset display and inputs
document.getElementById("points").textContent = points.toFixed(1);
document.getElementById("rewardName").value = "";
document.getElementById("rewardHours").value = 0;
document.getElementById("rewardMinutes").value = 30;
document.getElementById("timerDisplay").textContent = "00:00:00";
renderRewards();
saveData();
}