-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
259 lines (231 loc) · 6.71 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<link rel="manifest" href="manifest.json">
<meta name="theme-color" content="#000000">
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>♟️</text></svg>">
<meta charset="UTF-8">
<title>klaxon</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
user-select: none;
-webkit-user-select: none;
-webkit-tap-highlight-color: transparent;
}
.player-section {
height: 50%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
position: relative;
transition: all 0.3s ease;
padding: 20px;
cursor: pointer;
}
.timer {
font-size: min(25vw, 40vh); /* Responsive font size */
width: 80%; /* Take up 80% of the container width */
text-align: center;
}
.player-indicator {
position: absolute;
top: 10px;
right: 10px;
font-size: min(8vw, 6vh);
transform: rotate(180deg); /* Upside-down for the opponent */
color: inherit;
}
body {
height: 100vh;
width: 100vw;
overflow: hidden;
font-family: 'Segment7';
font-weight: normal;
font-style: normal;
}
#player1 {
background: #ff0000;
color: black;
transform: rotate(180deg);
}
#player2 {
background: #ff0000;
color: black;
}
.inactive {
background: black !important;
color: #ff0000 !important;
}
@font-face {
font-family: 'Segment7';
src: url('resources/7segment.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
</style>
</head>
<body>
<div id="player1" class="player-section">
<div class="timer">10:00</div>
<div class="player-indicator" id="player1-indicator">10:00</div>
</div>
<div id="player2" class="player-section inactive">
<div class="timer">10:00</div>
<div class="player-indicator" id="player2-indicator">10:00</div>
</div>
<script>
// Cache files for offline PWA
if ('serviceWorker' in navigator) {
const swContent = `
const CACHE_NAME = 'klaxon-cache-v1';
const urlsToCache = [
'/klaxon/',
'/klaxon/index.html',
'/klaxon/manifest.json',
'/klaxon/resources/7segment.ttf',
'/klaxon/resources/icon.svg'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => cache.addAll(urlsToCache))
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => {
if (response) {
return response;
}
return fetch(event.request);
})
);
});
`;
const blob = new Blob([swContent], {type: 'text/javascript'});
const swUrl = URL.createObjectURL(blob);
navigator.serviceWorker.register(swUrl, { scope: '/klaxon/' });
}
// Disable device sleep while countdown is in progress
let wakeLock = null;
async function requestWakeLock() {
if ('wakeLock' in navigator) {
try {
wakeLock = await navigator.wakeLock.request('screen');
} catch (err) {
console.error(`${err.name}, ${err.message}`);
}
}
}
function releaseWakeLock() {
if (wakeLock !== null) {
wakeLock.release();
wakeLock = null;
}
}
window.addEventListener('load', requestWakeLock);
document.addEventListener('visibilitychange', () => {
if (wakeLock !== null && document.visibilityState === 'visible') {
requestWakeLock();
}
});
const player1 = document.getElementById('player1');
const player2 = document.getElementById('player2');
const timers = document.querySelectorAll('.timer');
const indicators = {
player1: document.getElementById('player1-indicator'),
player2: document.getElementById('player2-indicator')
};
let timePlayer1 = 600; // 10 minutes in seconds
let timePlayer2 = 600;
let activePlayer = 1;
let interval;
let gameStarted = false;
function formatTime(seconds) {
const mins = Math.floor(seconds / 60);
const secs = seconds % 60;
return `${String(mins).padStart(2, '0')}:${String(secs).padStart(2, '0')}`;
}
function updateDisplay() {
timers[0].textContent = formatTime(timePlayer1);
timers[1].textContent = formatTime(timePlayer2);
indicators.player1.textContent = formatTime(timePlayer1);
indicators.player2.textContent = formatTime(timePlayer2);
}
function playTimeoutSound() {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.type = 'square';
oscillator.frequency.setValueAtTime(440, audioContext.currentTime);
gainNode.gain.setValueAtTime(1, audioContext.currentTime);
oscillator.start();
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 1);
setTimeout(() => {
oscillator.stop();
}, 1000);
}
function playBlipTone() {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.type = 'sine';
oscillator.frequency.setValueAtTime(880, audioContext.currentTime);
gainNode.gain.setValueAtTime(1, audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.1);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.1);
}
function switchPlayer() {
activePlayer = activePlayer === 1 ? 2 : 1;
player1.classList.toggle('inactive');
player2.classList.toggle('inactive');
playBlipTone();
}
function tick() {
if (activePlayer === 1) {
timePlayer1--;
if (timePlayer1 === 0) {
clearInterval(interval);
playTimeoutSound();
releaseWakeLock();
}
} else {
timePlayer2--;
if (timePlayer2 === 0) {
clearInterval(interval);
playTimeoutSound();
releaseWakeLock();
}
}
updateDisplay();
}
function startGame() {
if (!gameStarted) {
gameStarted = true;
interval = setInterval(tick, 1000);
player2.classList.add('inactive');
playBlipTone();
} else {
switchPlayer();
}
}
player1.addEventListener('click', startGame);
player2.addEventListener('click', startGame);
// Prevent scrolling/bouncing on iOS
document.addEventListener('touchmove', (e) => e.preventDefault(), { passive: false });
// Initialize display
updateDisplay();
</script>
</body>
</html>