-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
190 lines (168 loc) · 5.74 KB
/
main.js
File metadata and controls
190 lines (168 loc) · 5.74 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
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
const greetingContainer = document.getElementById('greeting');
const textInput = document.getElementById('text-input');
const wpmCounter = document.getElementById('wpm-counter');
const commandBarContainer = document.getElementById('command-bar-container');
const commandInput = document.getElementById('command-input');
const commandOutput = document.getElementById('command-output');
const suggestionBox = document.getElementById('suggestion-box');
let currentWord = '';
let wordStartTime = null;
let suggestionIndex = -1;
const commands = [
{ name: 'restart', description: 'Restart the game' },
{ name: 'help', description: 'Show available commands' }
];
function restartGame() {
wpmCounter.textContent = '';
commandOutput.innerHTML = '';
getNewWord();
showMessage('Game restarted');
}
async function getNewWord() {
try {
const response = await fetch('/random-word');
const word = await response.text();
currentWord = word;
textInput.value = '';
wordStartTime = null;
renderWord();
} catch (error) {
console.error('Failed to fetch new word:', error);
greetingContainer.textContent = 'Error loading word.';
}
}
function renderWord() {
greetingContainer.innerHTML = '';
const typedValue = textInput.value;
currentWord.split('').forEach((char, index) => {
const span = document.createElement('span');
span.textContent = char;
span.classList.add('letter');
if (index < typedValue.length) {
if (typedValue[index] === char) {
span.classList.add('correct');
} else {
span.classList.add('incorrect');
}
} else if (index === typedValue.length) {
span.classList.add('current-letter');
}
greetingContainer.appendChild(span);
});
}
function calculateLastWordWPM() {
if (!wordStartTime) {
return;
}
const elapsedMinutes = (Date.now() - wordStartTime) / 60000;
if (elapsedMinutes === 0) {
wpmCounter.textContent = '--- WPM';
return;
}
const wpm = (currentWord.length / 5) / elapsedMinutes;
wpmCounter.textContent = `${Math.round(wpm)} WPM`;
showMessage(`${Math.round(wpm)} WPM`);
}
function toggleCommandBar(forceState) {
const isVisible = commandBarContainer.classList.contains('visible');
const shouldBeVisible = forceState !== undefined ? forceState : !isVisible;
if (shouldBeVisible) {
commandBarContainer.classList.add('visible');
commandInput.focus();
renderSuggestions();
} else {
commandBarContainer.classList.remove('visible');
suggestionBox.innerHTML = '';
textInput.focus();
}
}
function renderSuggestions() {
const inputValue = commandInput.value.toLowerCase();
const filteredCommands = commands.filter(cmd => cmd.name.startsWith(inputValue));
suggestionBox.innerHTML = '';
filteredCommands.forEach((cmd, index) => {
const div = document.createElement('div');
div.textContent = `${cmd.name} - ${cmd.description}`;
div.classList.add('suggestion-item');
if (index === suggestionIndex) {
div.style.backgroundColor = '#333';
}
div.addEventListener('click', () => {
commandInput.value = cmd.name;
suggestionBox.innerHTML = '';
commandInput.focus();
});
suggestionBox.appendChild(div);
});
}
textInput.addEventListener('input', () => {
if (wordStartTime === null && textInput.value.length > 0) {
wordStartTime = Date.now();
}
const typedValue = textInput.value;
if (typedValue === currentWord) {
calculateLastWordWPM();
getNewWord();
} else {
renderWord();
}
});
textInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault();
restartGame();
}
});
commandInput.addEventListener('input', () => {
suggestionIndex = -1;
renderSuggestions();
});
commandInput.addEventListener('keydown', (event) => {
const suggestions = suggestionBox.querySelectorAll('.suggestion-item');
if (event.key === 'ArrowDown') {
event.preventDefault();
if (suggestionIndex < suggestions.length - 1) {
suggestionIndex++;
renderSuggestions();
}
} else if (event.key === 'ArrowUp') {
event.preventDefault();
if (suggestionIndex > 0) {
suggestionIndex--;
renderSuggestions();
}
} else if (event.key === 'Tab' || event.key === 'Enter') {
event.preventDefault();
if (suggestionIndex !== -1) {
commandInput.value = commands.filter(cmd => cmd.name.startsWith(commandInput.value.toLowerCase()))[suggestionIndex].name;
suggestionBox.innerHTML = '';
} else {
const command = commandInput.value.trim().toLowerCase();
switch (command) {
case 'restart':
restartGame();
showMessage('Game restarted');
break;
case 'help':
commandOutput.innerHTML = '<div>Commands: restart, help</div>';
showMessage('Help is displayed below the WPM counter');
break;
}
commandInput.value = '';
toggleCommandBar(false);
}
}
});
window.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
event.preventDefault();
toggleCommandBar();
} else if (event.key === 'CapsLock' && event.target.tagName !== 'INPUT') {
event.preventDefault();
toggleCommandBar();
}
});
window.addEventListener('load', () => {
textInput.focus();
restartGame();
});