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 | /*
BSD Zero Clause License
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
window.addEventListener('load', () => {
'use strict';
// Mulberry32 PRNG,
// because we want a deterministic pseudo-random source.
// Adapted from:
// https://gist.github.com/tommyettinger/46a874533244883189143505d203312c
// And:
// https://github.com/bryc/code/blob/master/jshash/PRNGs.md#mulberry32
class Mulberry32 {
state;
constructor(seed) {
if (seed !== undefined) {
this.setSeed(seed);
}
}
setSeed(seed) {
// Force seed into int32 with a bitwise operator.
this.state = seed | 0;
}
_next() {
this.state = this.state + 0x6D2B79F5 | 0;
let z = this.state;
z = Math.imul(z ^ (z >>> 15), z | 1);
z ^= z + Math.imul(z ^ (z >>> 7), z | 61);
// The right shift by zero here interprets as unsigned.
return (z ^ (z >>> 14)) >>> 0;
}
// Int in half-open range [low, high).
randInt(low, high = undefined) {
if (high === undefined) {
high = low;
low = 0;
}
const range = high - low;
if (range <= 0) {
throw "randInt range <= 0";
}
// Set all bits below the highest set bit
let mask = range - 1;
mask |= mask >>> 1;
mask |= mask >>> 2;
mask |= mask >>> 4;
mask |= mask >>> 8;
mask |= mask >>> 16;
// Retry until we roll in range.
while (true) {
// Right shift by zero to interpret as unsigned int32.
const r = (this._next() & mask) >>> 0;
if (r < range) {
return r + low;
}
}
}
randChoice(anArray) {
const n = anArray.length;
if (n == 0) {
return null;
}
return anArray[this.randInt(n)];
}
};
const dayZero = 1681081200000;
const msPerDay = 24 * 60 * 60 * 1000;
const dayNum = Math.floor((Date.now() - dayZero) / msPerDay);
document.getElementById('day').innerHTML = `Day ${dayNum}`;
const scoreDisplay = document.getElementById('score');
const bestDisplay = document.getElementById('best');
const scoreIncrementDisplay = document.getElementById('scoreinc');
const popup = document.getElementById('popup');
const roundDisplay = document.getElementById('round');
const button = document.getElementById('collect');
const bars =
Array.from(document.getElementById('bars').children)
.filter(e => e.tagName == 'DIV')
.map(div => ({
div,
meter: div.getElementsByTagName('METER')[0],
label: div.getElementsByTagName('SPAN')[0],
speed: 0,
multiplier: 1,
}));
const rng = new Mulberry32(dayNum);
let best = 0;
let score = 0;
function setScore(newScore) {
score = newScore;
scoreDisplay.innerHTML = 'Score ' + score.toString().padStart(5, '0');
}
const maxRounds = 3;
let round = 0;
function setRound(newRound) {
round = newRound;
roundDisplay.innerHTML = `Round ${round + 1} / ${maxRounds}`;
}
let roundStart = 0;
async function doPopup(seconds, text) {
button.disabled = true;
popup.hidden = false;
if (text !== undefined) {
popup.innerHTML = `${text}`;
}
await new Promise(resolve => setTimeout(resolve, seconds * 1000));
popup.hidden = true;
button.disabled = false;
}
async function doCountdown(seconds) {
while (seconds > 0) {
await doPopup(0.7, seconds.toString());
seconds--;
}
}
async function startGame() {
setRound(0);
setScore(0);
rng.setSeed(dayNum);
button.disabled = true;
button.innerHTML = "Collect";
button.onclick = collectPoints;
await doCountdown(3);
startRound();
}
function updateBars(roundTime) {
let total = 0;
bars.forEach(b => {
const value = Math.floor(b.speed * roundTime);
// Drain at 3x speed once we're past full.
const clippedValue = Math.max(0,
Math.min(4 * b.meter.max - 3 * value, value));
total += clippedValue * b.multiplier;
b.meter.value = clippedValue;
});
return total;
}
let scoreAddTimeout = null;
function addScore(points) {
if (scoreAddTimeout) {
clearTimeout(scoreAddTimeout);
}
scoreIncrementDisplay.innerHTML = `+ ${points}`;
scoreAddTimeout = setTimeout(
() => scoreIncrementDisplay.innerHTML = '', 2000);
setScore(score + points);
}
let animFrame = null;
function roundFrame() {
const roundTime = (Date.now() - roundStart) / 1000;
const roundScore = updateBars(roundTime);
if (roundScore == 0 && roundTime > 1) {
// all barMeters ended, fail state
animFrame = null;
popup.innerHTML = 'Too slow!';
popup.hidden = false;
button.innerHTML = "Retry?";
button.onclick = startGame;
setTimeout(() => button.disabled = false, 800);
} else {
animFrame = window.requestAnimationFrame(roundFrame);
}
}
const multiplierColors = {
1: '#333',
3: '#172',
5: '#227',
7: '#627'
};
function startRound() {
roundStart = Date.now();;
bars.forEach(b => b.speed = rng.randInt(8192, 32768));
const sortedBars = [...bars];
sortedBars.sort((l, r) => l.speed - r.speed);
sortedBars.forEach((b, i) => {
switch (i) {
case 0:
b.multiplier = rng.randChoice([7, 5, 3]);
break;
case 1:
b.multiplier = rng.randChoice([5, 3, 3]);
break;
case 2:
b.multiplier = rng.randChoice([3, 1, 1]);
break;
default:
b.multiplier = 1;
break;
}
b.label.innerHTML = `${b.multiplier}x`;
b.div.style.background = multiplierColors[b.multiplier];
});
roundFrame();
}
async function collectPoints() {
if (animFrame) {
window.cancelAnimationFrame(animFrame);
animFrame = null;
}
button.disabled = true;
const points =
bars.map(b => Math.floor(b.meter.value / 128) * b.multiplier)
.reduce((t, i) => t + i, 0);
addScore(points);
if (round + 1 < maxRounds) {
setRound(round + 1);
await doPopup(0.8, `Round ${round + 1}`);
startRound();
} else {
if (score > best) {
best = score;
bestDisplay.innerHTML = 'Best ' +
best.toString().padStart(5, '0');
popup.innerHTML = `Best!\n${score}`;
} else {
popup.innerHTML = `${score}`;
}
popup.hidden = false;
button.innerHTML = "Retry?";
button.onclick = startGame;
await new Promise(resolve => setTimeout(resolve, 800));
button.disabled = false;
}
}
button.onclick = startGame;
});
|