-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeathTracker.js
67 lines (56 loc) · 1.55 KB
/
deathTracker.js
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
class DeathTracker {
constructor() {
this.totalDeaths = 0;
this.exitsCleared = 0;
this.startTime = 0;
this.elapsedTime = 0;
}
startTimer() {
if (!this.timerInterval) {
this.startTime = Date.now();
this.timerInterval = setInterval(() => {
this.updateElapsedTime();
}, 1000); // Update every second
}
}
stopTimer() {
if (this.timerInterval) {
clearInterval(this.timerInterval);
this.timerInterval = null;
this.elapsedTime += Date.now() - this.startTime;
this.startTime = 0;
}
}
updateElapsedTime() {
if (this.startTime !== 0) {
this.elapsedTime = Date.now() - this.startTime;
}
}
getElapsedTime() {
return this.formatTime(this.elapsedTime);
}
formatTime(milliseconds) {
let totalSeconds = Math.floor(milliseconds / 1000);
let seconds = totalSeconds % 60;
let minutes = Math.floor(totalSeconds / 60) % 60;
let hours = Math.floor(totalSeconds / 3600);
return `${this.pad(hours)}:${this.pad(minutes)}:${this.pad(seconds)}`;
}
pad(number) {
return String(number).padStart(2, '0');
}
addDeath() {
this.totalDeaths++;
}
setExits(exits) {
this.exitsCleared = exits;
}
getDeaths() {
return this.totalDeaths;
}
getExits() {
console.log(this.exitsCleared);
return this.exitsCleared;
}
}
module.exports = DeathTracker;