-
Notifications
You must be signed in to change notification settings - Fork 15
/
time.js
167 lines (140 loc) · 3.61 KB
/
time.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
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
const splash = document.getElementById("splash");
const startButton = document.getElementById("start");
const container = document.getElementById("container");
const timer = document.getElementById("timer");
const pauseButton = document.getElementById("pause");
let timerInterval;
let isPaused = false;
let seconds = 0;
let minutes = 0;
let hours = 0;
let display = "00:00";
let started = false;
function startGame() {
started = true;
splash.style.display = "none";
container.style.display = "block";
document.getElementById("splashTitle").textContent = "Paused";
document.getElementById("splashTheme").style.display = "none";
document.getElementById("splashCredit").style.display = "none";
document
.querySelectorAll(".hidden-start")
.forEach((el) => el.classList.remove("hidden-start"));
timerInterval = setInterval(updateTimer, 1000);
fathom.trackEvent("Started a game");
}
function updateTimer() {
if (!isPaused) {
seconds++;
if (seconds === 60) {
seconds = 0;
minutes++;
if (minutes === 60) {
minutes = 0;
hours++;
}
}
display =
(hours > 0 ? hours + ":" : "") +
("0" + minutes).slice(-2) +
":" +
("0" + seconds).slice(-2);
timer.textContent = display;
}
}
function pauseGame() {
clearInterval(timerInterval);
isPaused = true;
pauseButton.innerHTML = `<img src="./assets/play.svg" alt="play" />`;
splash.style.display = "flex";
container.style.display = "none";
}
function unpauseGame() {
isPaused = false;
pauseButton.innerHTML = `<img src="./assets/pause.svg" alt="pause" />`;
splash.style.display = "none";
container.style.display = "block";
timerInterval = setInterval(updateTimer, 1000);
}
function endGame() {
clearInterval(timerInterval);
return timer.textContent;
}
startButton.addEventListener("click", () => {
if (!started) {
startGame();
startButton.textContent = "Resume";
} else {
unpauseGame();
}
});
pauseButton.addEventListener("click", () => {
if (isPaused) {
unpauseGame();
} else {
pauseGame();
}
});
function convertTimeHMS(timeStr) {
const timeComponents = timeStr.split(":");
let hours = 0;
let minutes = 0;
let seconds = 0;
if (timeComponents.length === 3) {
hours = parseInt(timeComponents[0]);
minutes = parseInt(timeComponents[1]);
seconds = parseInt(timeComponents[2]);
} else if (timeComponents.length === 2) {
minutes = parseInt(timeComponents[0]);
seconds = parseInt(timeComponents[1]);
} else {
return "Invalid time format";
}
let result = "";
if (hours > 0) {
result += hours + "h ";
}
if (minutes > 0) {
result += minutes + "m ";
}
if (seconds > 0) {
result += seconds + "s ";
}
return result.trim();
}
// convert the time string (hh:mm:ss or mm:ss) to milliseconds
function convertTimeToMilliseconds(timeStr) {
const timeComponents = timeStr.split(":");
let hours = 0;
let minutes = 0;
let seconds = 0;
if (timeComponents.length === 3) {
hours = parseInt(timeComponents[0]);
minutes = parseInt(timeComponents[1]);
seconds = parseInt(timeComponents[2]);
} else if (timeComponents.length === 2) {
minutes = parseInt(timeComponents[0]);
seconds = parseInt(timeComponents[1]);
} else {
return "Invalid time format";
}
return hours * 3600000 + minutes * 60000 + seconds * 1000;
}
function convertMillisecondsToTime(milliseconds) {
let seconds = Math.floor(milliseconds / 1000);
let minutes = Math.floor(seconds / 60);
let hours = Math.floor(minutes / 60);
seconds = seconds % 60;
minutes = minutes % 60;
let result = "";
if (hours > 0) {
result += hours + "h ";
}
if (minutes > 0) {
result += minutes + "m ";
}
if (seconds > 0) {
result += seconds + "s ";
}
return result.trim();
}