-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.js
85 lines (68 loc) · 2.41 KB
/
timer.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const fs = require("fs");
class Timer {
constructor(bar) {
this.bar = bar
this.sessionID = Math.floor(new Date().getTime() / 1000);
this.refreshBar();
this.hour = 0
this.minute = 0
this.second = 0
};
updateSession() {
var sessions = require("./sessions.json");
var sameSession = false;
const closeDate = new Date();
const parsedDate = `${closeDate.getDate()}/${closeDate.getMonth() + 1}/${closeDate.getFullYear()}`
for (var date in sessions["sessions"]) {
if (sessions["sessions"][date].sessionID == this.sessionID) {
sameSession = true;
sessions["sessions"][date] = {
sessionID: this.sessionID,
date: parsedDate,
duration: `${this.hour}h ${this.minute}min ${this.second}sec`
}
fs.writeFile(__dirname + '/sessions.json', JSON.stringify(sessions, null, 2), () => {});
break;
}
}
if (!(sameSession)) {
sessions["sessions"].push({
sessionID: this.sessionID,
date: parsedDate,
duration: `${this.hour}h ${this.minute}min ${this.second}sec`
});
fs.writeFile(__dirname + '/sessions.json', JSON.stringify(sessions, null, 2), err => {
if (err) {
console.log(err)
}
});
}
};
handleTimer() {
this.hour = isNaN(this.hour) ? 0 : this.hour;
this.minute = isNaN(this.minute) ? 0 : this.minute;
this.second = isNaN(this.second) ? 0 : this.second;
if (this.second > 59) {
this.second = 0
this.minute++;
this.updateSession();
}
if (this.minute > 59) {
this.minute = 0
this.hour++;
}
var parsedSecond = (this.second < 10 ? '0' : '') + this.second;
var parsedMinute = (this.minute < 10 ? '0' : '') + this.minute;
var parsedHour = (this.hour < 10 ? '0' : '') + this.hour;
this.second++;
return `${parsedHour}:${parsedMinute}:${parsedSecond}`
};
refreshBar() {
this.bar.text = `${this.handleTimer()} Elapsed`;
this.bar.show();
setTimeout(() => {
this.refreshBar();
}, 1000);
};
};
module.exports = Timer