forked from wwwtyro/keyzen
-
Notifications
You must be signed in to change notification settings - Fork 5
/
keyzen.js
418 lines (337 loc) · 9.84 KB
/
keyzen.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
var KEEP_SCORES = 10;
var data = {};
data.chars = " jfkdlsahgyturieowpqbnvmcxz6758493021`-=[]\\;',./ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+{}|:\"<>?";
var save = {};
var sounds = {};
var words;
$(document).ready(function() {
sounds = {
"click": new Audio("click.wav"),
"clack": new Audio("clack.wav"),
"fail": new Audio("fail.wav"),
"ding" : new Audio("ding.wav")
};
loadData();
toggle("words" ,"Words" ,[1,2,3,4,5]);
toggle("mode" ,"Mode" ,["measure","speed"]);
toggle("target" ,"Target WPM",[30,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110]);
toggle("need_space" ,"Need space to advance" ,["Yes","No"]);
$.getJSON('wordlist.json', function(jsondata) {
words = new WeightedList(jsondata);
next_word();
render();
$(document).keypress(keyHandler);
$(document).keydown(function(e) {
if (e.which == 8) { // detect backspace
data.word_index = Math.max(0,data.word_index - 1);
data.keys_hit = data.keys_hit.substr(0, data.keys_hit.length - 1);
if(data.word_errors[data.word_index]) {
data.word_errors[data.word_index] = false;
data.num_errors -= 1;
}
render();
e.preventDefault();
}
});
window.setInterval(update_target_bar,42);
});
});
function generate_word() {
return words.peek(save.settings.words).join(" "); // weighted random pick
}
function word_finished() {
// satisfying ding ---
if(data.num_errors == 0) {
sounds["ding"].play();
}
// save data from previous word -----
var accuracy = ((data.word.length - data.num_errors) / data.word.length) * 100; // in percent
save.accuracies.push([accuracy,data.word.length]);
if(save.accuracies.length > KEEP_SCORES) save.accuracies.shift();
// calculate wpm
var minutes = (data.end_time - data.start_time) / (60.0 * 1000.0);
var words = data.word.length / 5.0;
var wpm = words / minutes;
save.wpms.push([wpm,data.word.length]);
if(save.wpms.length > KEEP_SCORES) save.wpms.shift();
}
function next_word() {
data.word = generate_word();
data.word_index = 0;
data.keys_hit = "";
data.num_errors = 0;
data.word_errors = {};
data.doneMode = false;
data.start_time = new Date().getTime();
}
function keyHandler(e) {
var key = String.fromCharCode(e.which);
if (data.chars.indexOf(key) < 0){
return;
}
e.preventDefault();
if(key == " " && data.doneMode) {
word_finished();
next_word();
render();
return;
}
if(data.doneMode) {
sounds["clack"].play();
return;
}
if(key == " " && data.word_index == 0) {
return;
}
data.keys_hit += key;
if(key == data.word[data.word_index]) {
//data.in_a_row[key] += 1;
sounds["click"].play();
}
else {
// data.in_a_row[data.word[data.word_index]] = 0;
// data.in_a_row[key] = 0;
sounds["clack"].play();
data.num_errors += 1;
data.word_errors[data.word_index] = true;
}
data.word_index += 1;
if (data.word_index >= data.word.length) {
data.end_time = new Date().getTime();
if(save.settings.need_space == "Yes") {
data.doneMode = true;
} else {
word_finished();
next_word();
render();
}
}
render();
saveData();
}
function saveData() {
localStorage.data = JSON.stringify(save);
}
function loadData() {
if(!localStorage.data)
save = {};
else {
save = JSON.parse(localStorage.data);
}
if(!save.wpms)
save.wpms = [];
if(!save.accuracies)
save.accuracies = [];
if(!save.settings) {
save.settings = {
words: 3,
mode: "measure",
target: 40,
need_space: "Yes"
};
}
}
function render() {
//render_level();
render_word();
render_info();
//render_level_bar();
//render_rigor();
}
function render_word() {
var word = "";
for (var i = 0; i < data.word.length; i++) {
sclass = "normalChar";
if (i > data.word_index) {
sclass = "normalChar";
}
else if (i == data.word_index) {
sclass = "currentChar";
}
else if(data.word_errors[i]) {
sclass = "errorChar";
}
else {
sclass = "goodChar";
}
word += "<span class='" + sclass + "'>";
//if(data.word[i] == " ") {
//word += "⎵"
//}
if(data.word[i] == "&") {
word += "&"
}
else {
word += data.word[i];
}
word += "</span>";
}
var keys_hit = "<span class='keys-hit'>";
for(var d in data.keys_hit) {
//if (data.keys_hit[d] == ' ') {
//keys_hit += "⎵";
//}
if (data.keys_hit[d] == '&') {
keys_hit += "&";
}
else {
keys_hit += data.keys_hit[d];
}
}
for(var i = data.word_index; i < data.word.length; i++) {
keys_hit += " ";
}
keys_hit += "</span>";
$("#word").html(word + "<br>" + keys_hit);
}
function update_target_bar() {
var fullWidth = $(".keys-hit").innerWidth();
var timeSoFar = new Date().getTime() - data.start_time;
var words = data.word.length / 5.0;
var fullTime = (words / save.settings.target) * 60 * 1000;
var curWidth = (timeSoFar/fullTime) * fullWidth;
$("#target-bar").width(Math.min(curWidth,fullWidth));
if(timeSoFar > fullTime && save.settings.mode == "speed") {
sounds["fail"].play();
next_word();
render();
}
}
function weighted_average(arr) {
var sum = 0;
var weight_total = 0;
for(var x in arr) {
var item = arr[x];
weight_total += item[1];
sum += item[0] * item[1];
}
return sum / weight_total;
}
function render_info() {
var text = "";
text += "Recent WPM: " + weighted_average(save.wpms).toPrecision(4) + " ";
text += "Recent Accuracy: " + weighted_average(save.accuracies).toPrecision(5) + "% ";
$("#info-bar").html(text);
}
function toggle(id,name,vals) {
var curval = 0;
if(save.settings[id] != undefined) {
for(var i in vals) {
if(vals[i] == save.settings[id]) {
curval = i;
}
}
}
var newToggle = $("<span>" + name +": <span class='toggle' id='settings-toggle-" + id + "'>" + vals[curval] + "</span> </span>");
$("#settings").append(newToggle);
//save.settings[id] = vals[curval];
$("#settings-toggle-" + id,newToggle).click(function() {
curval += 1;
curval = curval % vals.length;
$(this).html(vals[curval]);
// actually update the setting
save.settings[id] = vals[curval];
saveData();
});
}
/*
function set_level(l) {
data.in_a_row = {};
for(var i = 0; i < data.chars.length; i++) {
data.in_a_row[data.chars[i]] = data.consecutive;
}
data.in_a_row[data.chars[l]] = 0;
data.level = l;
data.word_index = 0;
data.word_errors = {};
data.word = generate_word();
data.keys_hit = "";
save();
render();
}
function level_up() {
if (data.level + 1 <= data.chars.length - 1) {
(new Audio('ding.wav')).play();
}
l = Math.min(data.level + 1, data.chars.length);
set_level(l);
}
function render_level() {
var chars = "<span id='level-chars-wrap'>";
var level_chars = get_level_chars();
var training_chars = get_training_chars();
for (var c in data.chars) {
if(training_chars.indexOf(data.chars[c]) != -1) {
chars += "<span style='color: #F00' onclick='set_level(" + c + ");'>"
}
else if (level_chars.indexOf(data.chars[c]) != -1) {
chars += "<span style='color: #000' onclick='set_level(" + c + ");'>"
}
else {
chars += "<span style='color: #AAA' onclick='set_level(" + c + ");'>"
}
if (data.chars[c] == ' ') {
chars += "⎵";
}
else {
chars += data.chars[c];
}
chars += "</span>";
}
chars += "</span>";
$("#level-chars").html('click to set level: ' + chars);
}
function render_rigor() {
chars = "<span id='rigor-number' onclick='inc_rigor();'>";
chars += '' + data.consecutive;
chars += '<span>';
$('#rigor').html('click to set required repititions: ' + chars);
}
function inc_rigor() {
data.consecutive += 1;
if (data.consecutive > 9) {
data.consecutive = 2;
}
render_rigor();
}
function render_level_bar() {
training_chars = get_training_chars();
if(training_chars.length == 0) {
m = data.consecutive;
}
else {
m = 1e100;
for(c in training_chars) {
m = Math.min(data.in_a_row[training_chars[c]], m);
}
}
m = Math.floor($('#level-chars-wrap').innerWidth() * Math.min(1.0, m / data.consecutive));
$('#next-level').css({'width': '' + m + 'px'});
}
function generate_word() {
word = '';
for(var i = 0; i < data.word_length; i++) {
c = choose(get_training_chars());
if(c != undefined && c != word[word.length-1]) {
word += c;
}
else {
word += choose(get_level_chars());
}
}
return word;
}
function get_level_chars() {
return data.chars.slice(0, data.level + 1).split('');
}
function get_training_chars() {
var training_chars = [];
var level_chars = get_level_chars();
for(var x in level_chars) {
if (data.in_a_row[level_chars[x]] < data.consecutive) {
training_chars.push(level_chars[x]);
}
}
return training_chars;
}
*/