-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
163 lines (146 loc) · 6.34 KB
/
script.js
File metadata and controls
163 lines (146 loc) · 6.34 KB
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
const words = 'Underneath shimmering starlight, ethereal beings traverse silent meadows where fragrant blossoms sway under moonlit skies. Whispers of ancient tales float upon serene winds, weaving enchanting narratives through shadowed groves. Luminescent orbs illuminate winding paths, guiding wanderers toward hidden realms filled with wonder and magic untold.'.split(' ');
const wordsCount = words.length;
const gameTime = 30 * 1000;
window.timer = null;
window.gameStart = null;
window.pauseTime = 0;
function addClass(el, name) {
el.className += ' ' + name;
}
function removeClass(el, name) {
el.className = el.className.replace(name, '');
}
function randomWord() {
const randomIndex = Math.ceil(Math.random() * wordsCount);
return words[randomIndex - 1];
}
function formatWord(word) {
return `<div class="word"><span class="letter">${word.split('').join('</span><span class="letter">')}</span></div>`;
}
function newGame() {
document.getElementById('words').innerHTML = '';
for (let i = 0; i < 200; i++) {
document.getElementById('words').innerHTML += formatWord(randomWord());
}
addClass(document.querySelector('.word'), 'current');
addClass(document.querySelector('.letter'), 'current');
document.getElementById('info').innerHTML = (gameTime / 1000) + '';
window.timer = null;
}
function getWpm() {
const words = [...document.querySelectorAll('.word')];
const lastTypedWord = document.querySelector('.word.current');
const lastTypedWordIndex = words.indexOf(lastTypedWord) + 1;
const typedWords = words.slice(0, lastTypedWordIndex);
const correctWords = typedWords.filter(word => {
const letters = [...word.children];
const incorrectLetters = letters.filter(letter => letter.className.includes('incorrect'));
const correctLetters = letters.filter(letter => letter.className.includes('correct'));
return incorrectLetters.length === 0 && correctLetters.length === letters.length;
});
return correctWords.length / gameTime * 60000;
}
function gameOver() {
clearInterval(window.timer);
addClass(document.getElementById('game'), 'over');
const result = getWpm();
document.getElementById('info').innerHTML = `WPM: ${result}`;
}
document.getElementById('game').addEventListener('keyup', ev => {
const key = ev.key;
const currentWord = document.querySelector('.word.current');
const currentLetter = document.querySelector('.letter.current');
const expected = currentLetter?.innerHTML || ' ';
const isLetter = key.length === 1 && key !== ' ';
const isSpace = key === ' ';
const isBackspace = key === 'Backspace';
const isFirstLetter = currentLetter === currentWord.firstChild;
if (document.querySelector('#game.over')) {
return;
}
console.log({ key, expected });
if (!window.timer && isLetter) {
window.timer = setInterval(() => {
if (!window.gameStart) {
window.gameStart = (new Date()).getTime();
}
const currentTime = (new Date()).getTime();
const msPassed = currentTime - window.gameStart;
const sPassed = Math.round(msPassed / 1000);
const sLeft = Math.round((gameTime / 1000) - sPassed);
if (sLeft <= 0) {
gameOver();
return;
}
document.getElementById('info').innerHTML = sLeft + '';
}, 1000);
}
if (isLetter) {
if (currentLetter) {
addClass(currentLetter, key === expected ? 'correct' : 'incorrect');
removeClass(currentLetter, 'current');
if (currentLetter.nextSibling) {
addClass(currentLetter.nextSibling, 'current');
}
} else {
const incorrectLetter = document.createElement('span');
incorrectLetter.innerHTML = key;
incorrectLetter.className = 'letter incorrect extra';
currentWord.appendChild(incorrectLetter);
}
}
if (isSpace) {
if (expected !== ' ') {
const lettersToInvalidate = [...document.querySelectorAll('.word.current .letter:not(.correct)')];
lettersToInvalidate.forEach(letter => {
addClass(letter, 'incorrect');
});
}
removeClass(currentWord, 'current');
addClass(currentWord.nextSibling, 'current');
if (currentLetter) {
removeClass(currentLetter, 'current');
}
addClass(currentWord.nextSibling.firstChild, 'current');
}
if (isBackspace) {
if (currentLetter && isFirstLetter) {
// make prev word current, last letter current
removeClass(currentWord, 'current');
addClass(currentWord.previousSibling, 'current');
removeClass(currentLetter, 'current');
addClass(currentWord.previousSibling.lastChild, 'current');
removeClass(currentWord.previousSibling.lastChild, 'incorrect');
removeClass(currentWord.previousSibling.lastChild, 'correct');
}
if (currentLetter && !isFirstLetter) {
// move back one letter, invalidate letter
removeClass(currentLetter, 'current');
addClass(currentLetter.previousSibling, 'current');
removeClass(currentLetter.previousSibling, 'incorrect');
removeClass(currentLetter.previousSibling, 'correct');
}
if (!currentLetter) {
addClass(currentWord.lastChild, 'current');
removeClass(currentWord.lastChild, 'incorrect');
removeClass(currentWord.lastChild, 'correct');
}
}
// move lines / words
if (currentWord.getBoundingClientRect().top > 250) {
const words = document.getElementById('words');
const margin = parseInt(words.style.marginTop || '0px');
words.style.marginTop = (margin - 35) + 'px';
}
// move cursor
const nextLetter = document.querySelector('.letter.current');
const nextWord = document.querySelector('.word.current');
const cursor = document.getElementById('cursor');
cursor.style.top = (nextLetter || nextWord).getBoundingClientRect().top + 2 + 'px';
cursor.style.left = (nextLetter || nextWord).getBoundingClientRect()[nextLetter ? 'left' : 'right'] + 'px';
});
document.getElementById('newGameBtn').addEventListener('click', () => {
gameOver();
newGame();
});
newGame();