-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
269 lines (217 loc) · 8.2 KB
/
main.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
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
function scrollTo(element) {
const y = element.getBoundingClientRect().top + element.parentElement.scrollTop;
element.parentElement.scroll({
top: y - element.parentElement.offsetHeight,
behavior: 'smooth'
});
}
function countLetters(text) {
// Count the characters in a str without spaces (includes punctuation)
let length = 0;
for (let i = 0; i < text.length; i++) {
if (text[i] != ' ') {
length++;
}
}
return length;
}
class ChallengeTextLineGroup {
constructor(lines, selected = 0) {
this.lines = lines // Array of ChallengeTextLine
this.selected = selected // Int representing the index of the selected line (zero-indexed)
}
toHTML() {
let ret = "";
for (let i = 0; i < this.lines.length; i++) {
if /*(Math.abs(i - this.selected) < 3)*/ (true) {
ret += `<div class="challenge-text" id="challenge-line-${i+1}">${i+1}. <span class="${(i == this.selected) ? 'challenge-text-selected-line' : ''}">${this.lines[i]}</span></div><br>`
}
}
return ret;
}
toString() {
return this.lines.join(" ");
}
get words() {
return this.toString().split(" ");
}
}
function lineSplit(text) {
// Split a one-line string into an array of lines
const LINE_WORD_COUNT = 10;
let lines = [];
words = text.split(' ');
for (let i = 0; i < words.length; i++) {
if (lines.length == 0) {
lines.push("");
}
if (lines[lines.length - 1].split(' ').length < LINE_WORD_COUNT) {
lines[lines.length - 1] = lines[lines.length - 1] + words[i] + ' ';
}
else if (words[i] && words[i] != ' ' && words[i] != '\n') {
lines.push(words[i] + ' ');
}
}
return new ChallengeTextLineGroup(lines);
}
function renderChallengeText(lines) {
// Take a ChallengeTextLineGroup and display the lines in the challenge text div
let box = document.getElementById("challenge-text");
box.innerHTML = lines.toHTML();
}
var level_running = false;
var lines = null;
const LPS = 2.5; // Letters per second
var difficulty; // Difficulty is in seconds. Letter count = seconds * 2.5.
var countdownTarget;
var levelStart; // Date object representing start time of current level
function setDifficulty(d) {
// Take a number of seconds as input and update the difficulty text
difficulty = d;
document.getElementById("difficulty").textContent = `${Math.round(difficulty * LPS)} letters / ${difficulty + Math.round(0.05 * LPS * difficulty)} seconds`;
}
function genText() {
// Generate the text to be written
const REQUIRED_LEN = Math.round(difficulty * LPS);
text = txtgen.sentence();
while (countLetters(text) != REQUIRED_LEN) {
if (countLetters(text) > REQUIRED_LEN) {
text = text.slice(0, -1);
}
else {
text = text + " " + txtgen.sentence();
}
}
return text;
}
function displayWPM(time) {
// Count the words in the global line group and display WPM given the completion time in ms
const WPM = Math.round(lines.words.length / (time/1000))*60;;
document.getElementById('wpm').innerText = WPM.toString();
}
function toggleKeyHints(b) {
// Take in a boolean as input and enable/disable key hints
keyhints = document.getElementsByClassName('keyhint');
for (let i = 0; i < keyhints.length; i++) {
keyhints[i].style.visibility = b ? null : 'hidden';
//console.log(i.toString() + '. ' + keyhints[i].style.visibility);
}
}
function startTimer() {
startButton = document.getElementById("timer-start");
if (startButton.style.visibility == 'hidden') {
return;
}
startButton.style.visibility = 'hidden';
toggleKeyHints(true);
document.getElementById("timer-text").innerText = " Get ready! Starting in 3...";
let getReadyCountdownTarget = (new Date()).getTime() + 4000;
let getReadyCountdown = setInterval(function() {
levelStart = (new Date()).getTime();
let now = (new Date()).getTime();
let distance = Math.floor((getReadyCountdownTarget - now) / 1000);
document.getElementById("timer-text").innerText = " Get ready! Starting in " + distance.toString() + "...";
if (distance < 1) {
document.getElementById("timer-text").innerText = " Starting now!";
clearInterval(getReadyCountdown);
countdownTarget = (new Date()).getTime() + (1000 * difficulty) + (Math.round(0.05 * LPS * difficulty) * 1000);
level_running = true;
}
}, 1000);
}
function ding() {
document.getElementById('ding').play();
}
function toggleCompletionPrompt(toggle, allow_continue = false) {
document.getElementById('completion-prompt').style.display = toggle ? null : 'none';
document.getElementById('completion-prompt-continue').style.display = (toggle && allow_continue) ? null : 'none';
document.getElementById('completion-prompt-retry').style.display = toggle ? null : 'none';
}
function setupLevel(d) {
// Take a number of seconds as input and set up level
try {
window.clearTimer(" seconds");
}
catch (e) {
if (!(e instanceof TypeError)) {
throw e;
}
}
setDifficulty(d);
toggleKeyHints(false);
toggleCompletionPrompt(false);
level_running = false;
lines = lineSplit(genText());
renderChallengeText(lines);
scrollTo(document.getElementById(`challenge-line-1`));
console.log('Level starting with difficulty ' + d.toString());
document.getElementById("timer-seconds").innerText = (difficulty + Math.round(0.05 * LPS * difficulty)).toString().padStart(4, '0')
document.getElementById("timer-start").style.visibility = null;
let timer = setInterval(function() {
if (!level_running) {
return;
}
let now = (new Date()).getTime();
let distance = countdownTarget - now;
document.getElementById("timer-seconds").innerText = (Math.floor(distance/1000)).toString().padStart(4, '0')
//document.getElementById("timer-text").innerText = humanizeDuration(Math.floor(distance/1000) * 1000);
document.getElementById("timer-text").innerText = " seconds"
function clearTimer(message) {
clearInterval(timer);
document.getElementById("timer-seconds").innerText = '0000';
document.getElementById("timer-text").innerText = ' ' + message;
toggleKeyHints(false);
document.getElementsByClassName('keyhint'); // For some reason the visibility does not change properly when this is not present
}
window.clearTimer = clearTimer;
if (distance <= 1000) {
clearTimer("Time's up!");
ding();
toggleCompletionPrompt(true);
}
}, 1000);
document.onkeydown = (e) => {
if (e.repeat || !level_running) {
return;
}
if (e.key == " ") {
e.preventDefault();
}
if (e.key == "Enter" || e.key == " ") {
lines.selected++;
}
else if (e.key == 'Backspace' && lines.selected > 0) {
lines.selected--;
}
else {
return;
}
if (lines.selected + 1 > lines.lines.length) {
level_running = false;
document.onkeydown = null;
window.clearTimer("Challenge complete!");
toggleCompletionPrompt(true, true);
displayWPM((new Date()).getTime() - levelStart);
}
else {
//document.getElementById(`challenge-line-${lines.selected+1}`).scrollIntoView();
scrollTo(document.getElementById(`challenge-line-${lines.selected+1}`));
}
renderChallengeText(lines);
}
}
function increaseDifficulty() {
if (document.getElementById('completion-prompt-continue').style.display == 'none') {
return;
}
setupLevel(difficulty+10);
toggleCompletionPrompt(false);
}
function restartLevel() {
if (document.getElementById('completion-prompt-retry').style.display == 'none') {
return;
}
setupLevel(difficulty);
toggleCompletionPrompt(false);
}
setupLevel(20);