-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastermind.html
executable file
·223 lines (196 loc) · 6.89 KB
/
mastermind.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="description" content="Mastermind countdown">
<title>Mastermind</title>
<style>
body {
font: 0.9em Verdana, sans-serif;
}
fieldset {
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
display: inline-block;
}
button {
margin: 2px;
}
input {
margin: 2px;
}
td {
vertical-align: top;
text-align: center;
}
th {
padding: 2em;
}
#start {
width: 100px;
color: #14396A !important;
padding: 10px 25px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
border: 2px solid #3866A3;
background: #63B8EE;
}
#start:hover {
color: #14396A !important;
background: #468CCF;
}
</style>
<script>
const INTERVAL_MS = 100;
// convert duration into a string with format m:ss, return 0:00 for empty/negative values
function formatMs(durationMs) {
s = '0:00';
if (durationMs > 0) {
let seconds = Math.ceil(durationMs / 1000);
let minutes = Math.floor(seconds / 60);
let secondsPart = seconds % 60;
s = minutes + ':' + (secondsPart < 10 ? '0' + secondsPart : secondsPart);
}
return s;
}
// convert string with comma-separated numbers (e.g. 3,4) into sorted integers
function string2sortedInts(s) {
let people = [];
if (typeof s === 'string' && /^[0-9, ]+$/.test(s)) {
unique = new Set(s.split(',').map(x => parseInt(x)));
people = Array.from(unique).sort((x,y) => x-y);
}
return people;
}
// create table content, append a row for each integer in the people array
function createRooms(people, startTimeMs, minutes, transitionSeconds) {
let s = '<tr><th>Room size</th><th>Speaker</th><th>Time left: <span id="countdown">0:00</span></th></tr>';
people.forEach(n => {
let url = makeUrl(n, startTimeMs, minutes, transitionSeconds);
s += `<tr><td><a href="${url}">${n} people</a></td><td id="speaker${n}"></td><td id="countdown${n}"></td></tr>`;
});
return s;
}
// update table with overall remaining time, current speaker and their remaining time
function updateTime(people, minutes, startTimeMs, transitionSeconds) {
let now = Date.now();
let durationMs = minutes * 60 * 1000;
let transitionMs = transitionSeconds * 1000;
let elapsedMs = now - startTimeMs;
document.querySelector('#countdown').innerHTML = formatMs(durationMs - elapsedMs);
people.forEach(n => {
let msPerSpeaker = Math.floor(durationMs / n);
let speaker = Math.min(n, Math.floor(elapsedMs / msPerSpeaker) + 1); // starting with speaker 1
let currentSpeaker = document.querySelector('#speaker' + n).innerText;
let s = '0:00';
let elapsedMsForSpeaker = elapsedMs - ((speaker - 1) * msPerSpeaker);
if (elapsedMsForSpeaker < transitionMs) {
// transition period, mark countdown as negative
s = formatMs(transitionMs - elapsedMsForSpeaker);
speaker = 'transition';
} else if (elapsedMsForSpeaker < msPerSpeaker) {
// speaking period
s = formatMs(Math.max(0, msPerSpeaker - elapsedMsForSpeaker));
}
document.querySelector('#countdown' + n).innerText = s;
document.querySelector('#speaker' + n).innerText = speaker;
});
}
function makeUrl(peopleString, minutes, startTimeMs, transitionSeconds) {
const url = new URL(window.location.href); // http://.../mastermind.html?duration=25&people=3&ts=123456789
url.searchParams.set('people', peopleString);
url.searchParams.set('duration', minutes);
url.searchParams.set('ts', startTimeMs);
url.searchParams.set('transition', transitionSeconds);
return url.href;
}
// start countdown
// peopleString: string with comma-separated number of people in rooms, e.g. 3,4
// minutes: duration in minutes
// startTimeMs: Unix timestamp of the start time (TODO: server side for consistency across clients)
// transitionSeconds: duration of the transition period between speakers
function startTimer(peopleString, minutes, startTimeMs, transitionSeconds) {
// console.log(peopleString, minutes, startTimeMs);
people = string2sortedInts(peopleString);
document.querySelector('#rooms').innerHTML = createRooms(people, minutes, startTimeMs, transitionSeconds);
let endTimeMs = startTimeMs + minutes * 60 * 1000;
let timerId = setInterval(function() {
let now = Date.now();
updateTime(people, minutes, startTimeMs, transitionSeconds);
if (now > endTimeMs) {
// done, we've reached the end of the mastermind
clearInterval(timerId);
return;
}
}, INTERVAL_MS);
return timerId;
}
document.addEventListener("DOMContentLoaded", function() {
let timerId;
// select our play button
document.querySelector('#start').addEventListener('click', function() {
clearInterval(timerId);
let minutes = parseInt(document.querySelector('#time').value);
let people = document.querySelector('#n_users').value;
let transitionSeconds = parseInt(document.querySelector('#transition').value);
if (isNaN(transitionSeconds)) {
// transition is optional
transitionSeconds = 0;
}
let startTimeMs = Date.now();
if (!isNaN(minutes)) {
timerId = startTimer(people, minutes, startTimeMs, transitionSeconds);
let url = makeUrl(people, minutes, startTimeMs, transitionSeconds);
history.pushState(null, document.querySelector('title').text, url);
}
}, false);
// start timer if the url contains the necessary parameters
let url = new URL(window.location.href);
let duration = url.searchParams.get('duration');
let people = url.searchParams.get('people');
let startTimeMs = url.searchParams.get('ts');
let transitionSeconds = url.searchParams.get('transition');
if (duration && people && startTimeMs && transitionSeconds) {
timerId = startTimer(people, duration, startTimeMs, transitionSeconds);
}
});
</script>
</head>
<body>
<h1>Mastermind</h1>
<form action="">
<fieldset>
<legend>Control</legend>
<table>
<tr>
<td><label for="time">Total time (min)</label></td>
<td><input type="text" name="time" id="time" placeholder="duration in minutes, e.g. 25" value=""></td>
</tr>
<tr>
<td><label for="n_users">Users</label></td>
<td><input type="text" name="n_users" id="n_users" placeholder="people in rooms, e.g. 3,4,5" value="3,4"></td>
</tr>
<tr>
<td><label for="transition">Transition (sec)</label></td>
<td><input type="text" name="transition" id="transition" placeholder="seconds" value="10"></td>
</tr>
<tr>
<td><button id="start" onclick="return false" type="submit">></button></td>
</tr>
</table>
</fieldset>
</form>
<table id="rooms">
</table>
<div>
<br>
Splits the given time among different numbers of users.
<br>
Privacy: No cookies. You can simply download the file and run everything locally (unless you need server time-sync).
<br>
Inspired by <a href="https://www.productivityday.com/">Productivity Day</a>.
</div>
</body>
</html>