-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.html
304 lines (271 loc) · 9.42 KB
/
play.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chess Clock</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
font-family: Arial, sans-serif;
}
.container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.board {
border: 2px solid #000;
padding: 10px;
display: flex;
flex-direction: column;
align-items: center;
width: 500px;
}
.row {
display: flex;
justify-content: space-between;
width: 100%;
margin-bottom: 20px;
}
.player {
width: 48%;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.player button {
border: 2px solid #000;
padding: 10px;
background-color: #4CAF50;
color: white;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
font-weight: bold;
text-align: center;
font-family: Arial, sans-serif;
}
.player button:hover {
background-color: #45a049;
}
.player:nth-child(1) {
background-color: #4CAF50;
}
#player2 {
background-color: #008CBA;
}
#player3 {
background-color: #f44336;
}
#player4 {
background-color: #FFD700;
}
.timer h2 {
background-color: transparent;
}
#timer {
margin-top: 10px;
margin-bottom: 26px;
}
body.dark-mode {
background-color: #222;
color: #fff;
}
</style>
</head>
<body>
<div class="container">
<div class="board">
<div class="row">
<button class="player" id="player1">
<span style="font-weight: bold;">Player 1</span>
<div class="timer" style="background-color: transparent;">
<h2 id="timer1">00:00</h2>
</div>
</button>
<button class="player" id="player3">
<span style="font-weight: bold;">Player 3</span>
<div class="timer" style="background-color: transparent;">
<h2 id="timer3">00:00</h2>
</div>
</button>
</div>
<div class="timer_global">
<h2 id="timer">00:00</h2>
</div>
<div class="row">
<button class="player" id="player2">
<span style="font-weight: bold;">Player 2</span>
<div class="timer" style="background-color: transparent;">
<h2 id="timer2">00:00</h2>
</div>
</button>
<button class="player" id="player4">
<span style="font-weight: bold;">Player 4</span>
<div class="timer" style="background-color: transparent;">
<h2 id="timer4">00:00</h2>
</div>
</button>
</div>
<div style="display: flex;flex-direction: column;align-items: center;">
<a href="index.html" class="btn btn-primary mt-3">Back To Home</a>
<button class="btn btn-dark" id="darkModeBtn" style="margin-top: 15px;">Dark Mode</button>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
function toggleDarkMode() {
document.body.classList.toggle('dark-mode');
}
document.getElementById('darkModeBtn').addEventListener('click', function() {
toggleDarkMode();
});
document.addEventListener("DOMContentLoaded", function() {
startTimers();
});
// Function to start the timers for players
function startTimers() {
var userData = localStorage.getItem("userData");
if (!userData) {
console.error("User data not found.");
return;
}
var userDataJson = JSON.parse(userData);
var numPlayers = userDataJson.numPlayers.split(",");
var players = [];
for (var i = 0; i < numPlayers.length; i++) {
var player = document.getElementById("player" + (i + 1));
if (player) {
players.push(player);
}
}
var defaultPlayers = 4;
if (players.length < defaultPlayers) {
for (var i = players.length + 1; i <= defaultPlayers; i++) {
var player = document.getElementById("player" + i);
if (player) {
player.style.display = "none";
}
}
}
var activePlayerIndex = 0;
var intervalId;
var paused = false;
var numberUnoPlayer = parseInt(userDataJson.number_uno_player);
if (isNaN(numberUnoPlayer) || numberUnoPlayer < 1) {
console.error("Invalid number_uno_player.");
return;
}
var maxDuration = 300;
var duration = Math.min(numberUnoPlayer, maxDuration) * 60 * 1000;
if (numberUnoPlayer > maxDuration) {
console.warn("Number of Uno players exceeds maximum duration. Setting duration to maximum.");
duration = maxDuration * 1000;
}
var remainingTimes = localStorage.getItem("remainingTimes");
if (remainingTimes) {
remainingTimes = JSON.parse(remainingTimes);
} else {
remainingTimes = {};
players.forEach(function(player, index) {
remainingTimes["Player_" + (index + 1)] = duration;
});
localStorage.setItem("remainingTimes", JSON.stringify(remainingTimes));
}
function updateDisplay() {
var remaining = remainingTimes["Player_" + (activePlayerIndex + 1)];
var minutes = Math.floor(remaining / 60000);
var seconds = Math.floor((remaining / 1000) % 60);
var timerString = (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
players[activePlayerIndex].querySelector(".timer h2").textContent = timerString;
if (remaining <= 0) {
clearInterval(intervalId);
switchPlayer();
}
};
function startTimer() {
clearInterval(intervalId);
intervalId = setInterval(function() {
remainingTimes["Player_" + (activePlayerIndex + 1)] -= 1000;
updateDisplay();
}, 1000);
players[activePlayerIndex].classList.add("active");
players[activePlayerIndex].querySelector(".timer").style.backgroundColor = "#e3e3e3";
players[activePlayerIndex].querySelector(".timer").style.width = "100%";
};
function switchPlayer() {
clearInterval(intervalId);
players[activePlayerIndex].classList.remove("active");
players[activePlayerIndex].querySelector(".timer").style.backgroundColor = "transparent";
activePlayerIndex = (activePlayerIndex + 1) % players.length;
startTimer();
};
players.forEach(function(player, index) {
player.addEventListener("click", function() {
if (!paused && player === players[activePlayerIndex]) {
switchPlayer();
}
});
});
function pauseGame() {
clearInterval(intervalId);
paused = true;
}
function resumeGame() {
paused = false;
startTimer();
}
window.addEventListener("blur", pauseGame);
window.addEventListener("focus", resumeGame);
startTimer();
}
window.onload = function() {
fixTimerTheme();
updateTimer();
startTimers();
};
function fixTimerTheme() {
var timerH2 = document.querySelector('.timer_global');
if (timerH2) {
timerH2.style.backgroundColor = 'transparent';
console.log("Timer theme fixed.");
} else {
console.error("Timer element not found.");
}
}
function updateTimer() {
var timerElement = document.querySelector("#timer");
if (timerElement) {
console.log("Time is running");
var userData = localStorage.getItem("userData");
var numberUnoGlobal = 0;
if (userData) {
var userDataJson = JSON.parse(userData);
var numberUnoGlobal = parseInt(userDataJson.number_uno_global);
}
var duration = isNaN(numberUnoGlobal) ? 0 : numberUnoGlobal * 60 * 1000;
var startTime = Date.now();
var updateDisplay = function() {
var elapsed = Date.now() - startTime;
var remaining = Math.max(duration - elapsed, 0);
var minutes = Math.floor(remaining / 60000);
var seconds = Math.floor((remaining / 1000) % 60);
var timerString = (minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
timerElement.innerHTML = timerString;
if (remaining === 0) {
clearInterval(intervalId);
}
};
updateDisplay();
var intervalId = setInterval(updateDisplay, 100);
} else {
console.error("Timer element not found.");
}
}
</script>
</html>