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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
import { useEffect, useState } from 'react';
import { TypingGame } from './components/TypingGame';
import MainMenu from './components/MainMenu';
import { EndScreen } from './components/EndScreen';
import { GameState, Stats } from './types';
import { ThemeProvider } from './contexts/ThemeContext';
import { useCoreGame } from './hooks/useCoreGame';
import './styles.css';
import textsData from './data/texts.json';
type TextItem = { category: string; content: string; attribution: string };
const LOCAL_TEXTS: TextItem[] = Array.isArray(textsData) ? (textsData as TextItem[]) : [];
function uniqueCategories(items: TextItem[]): string[] {
const set = new Set<string>();
for (const t of items) if (t.category) set.add(t.category);
return Array.from(set).sort();
}
function pickRandom<T>(arr: T[]): T {
return arr[Math.floor(Math.random() * arr.length)];
}
function getRandomTextItemFrom(items: TextItem[], category?: string): TextItem {
const pool = category && category !== 'random'
? items.filter(t => t.category === category)
: items;
if (!pool.length) return { category: 'general', content: "The quick brown fox jumps over the lazy dog.", attribution: 'Traditional pangram' };
return pickRandom(pool);
}
function calculateStats(input: string, text: string, elapsedTime: number): Stats {
let correct = 0;
let incorrect = 0;
let currentStreak = 0;
let bestStreak = 0;
let totalErrors = 0;
let hasStartedTyping = input.length > 0;
// Calculate raw WPM using MonkeyType's approach
const windowSize = 0.5; // 500ms window for more granular peak detection
let maxWpm = 0;
let windowChars = 0;
let windowStartTime = 0;
// Track character timings and errors
const charTimings: { time: number; isCorrect: boolean }[] = [];
for (let i = 0; i < input.length; i++) {
const charTime = (i / input.length) * elapsedTime;
const isCorrect = input[i] === text[i];
charTimings.push({ time: charTime, isCorrect });
// Update window
while (charTime - windowStartTime > windowSize && charTimings.length > 0) {
if (charTimings[0].isCorrect) {
windowChars--;
}
windowStartTime = charTimings[1]?.time ?? charTime;
charTimings.shift();
}
if (isCorrect) {
windowChars++;
const windowTime = charTime - windowStartTime;
if (windowTime > 0) {
const windowWpm = (windowChars / 5) / (windowTime / 60);
maxWpm = Math.max(maxWpm, windowWpm);
}
}
}
const rawWpm = maxWpm; // Peak typing speed
// Calculate other stats
let totalTyped = 0;
for (let i = 0; i < input.length; i++) {
totalTyped++;
if (i < text.length && input[i] === text[i]) {
correct++;
currentStreak++;
bestStreak = Math.max(bestStreak, currentStreak);
} else {
incorrect++;
totalErrors++;
currentStreak = 0;
}
}
const totalChars = text.length;
// Only show 100% accuracy before typing starts
const accuracy = !hasStartedTyping ? 100 : Math.max(0, Math.min(100, (correct / totalTyped) * 100));
const wpm = elapsedTime === 0 ? 0 : (correct / 5) / (elapsedTime / 60);
return {
wpm,
rawWpm,
accuracy,
time: elapsedTime,
correctChars: correct,
incorrectChars: totalErrors,
totalChars,
currentStreak,
bestStreak,
};
}
export type Screen = 'main-menu' | 'typing-game' | 'end-screen';
function App() {
const { game, resetGame, cleanupGame } = useCoreGame();
const [allTexts, setAllTexts] = useState<TextItem[]>(LOCAL_TEXTS);
const [categories, setCategories] = useState<string[]>(uniqueCategories(LOCAL_TEXTS));
const [selectedCategory, setSelectedCategory] = useState<string>('random');
const [gameState, setGameState] = useState<GameState>({
screen: 'main-menu',
currentText: '',
input: '',
startTime: null,
isRunning: false,
stats: {
wpm: 0,
rawWpm: 0,
accuracy: 100,
time: 0,
correctChars: 0,
incorrectChars: 0,
totalChars: 0,
currentStreak: 0,
bestStreak: 0,
},
});
const [gameKey, setGameKey] = useState<number>(0); // For remounting TypingGame
// Track last WPM history only for end-screen payloads (retain local var at finish)
const testStats: Stats = {
wpm: 85, rawWpm: 90, accuracy: 95, time: 60,
correctChars: 425, incorrectChars: 21, totalChars: 446,
currentStreak: 50, bestStreak: 100
};
const testWpmHistory = Array.from({ length: 60 }, (_, i) => ({
time: i, wpm: 80 + Math.sin(i / 5) * 10, raw: 85 + Math.sin(i / 5) * 10,
isError: Math.random() > 0.95
}));
const testText = "This is a test sentence for the end screen. It has some text to display and check for errors.";
const testUserInput = "This is a test sentance for the end screen. It has sone text to display and check for erors.";
const _testCharTimings = testText.split('').map((char, i) => ({
time: (i / testText.length) * 60,
isCorrect: char === (testUserInput[i] || ''),
char: char,
index: i
}));
const [lastTest, setLastTest] = useState<{ stats: Stats; wpmHistory: Array<{ time: number; wpm: number; raw: number; isError: boolean }>; text: string; userInput: string; charTimings?: Array<{ time: number; isCorrect: boolean; char: string; index: number }>; keypressHistory?: Array<{ time: number; index: number; isCorrect: boolean }> } | null>(null);
// Removed unused Enter key end-screen toggle handler
// Optional online dataset fetch (fallback to local). Configure URL via window.TYPERPUNK_TEXTS_URL.
useEffect(() => {
const url = (window as any).TYPERPUNK_TEXTS_URL as string | undefined;
if (!url) return; // no online dataset configured
(async () => {
try {
const res = await fetch(url, { cache: 'no-store' });
if (!res.ok) return;
const data = await res.json();
if (Array.isArray(data)) {
setAllTexts(data as TextItem[]);
setCategories(uniqueCategories(data as TextItem[]));
}
} catch {}
})();
}, []);
const handleStartGame = async () => {
try {
// Reset game state first
const item = getRandomTextItemFrom(allTexts, selectedCategory);
setGameState((prev: GameState) => ({
...prev,
screen: 'typing-game',
currentText: item.content,
currentAttribution: item.attribution,
input: '',
startTime: null,
isRunning: false,
stats: {
wpm: 0,
rawWpm: 0,
accuracy: 100,
time: 0,
correctChars: 0,
incorrectChars: 0,
totalChars: 0,
currentStreak: 0,
bestStreak: 0,
},
}));
// Then reset WASM game instance
await resetGame();
setGameKey((k: number) => k + 1); // Force remount
// Ensure focus after a short delay
setTimeout(() => {
const inputElement = document.querySelector('.typing-input') as HTMLInputElement;
if (inputElement) {
inputElement.disabled = false;
inputElement.focus();
}
}, 200);
} catch (err) {
console.error('Error starting game:', err);
// If start fails, stay in main menu
setGameState((prev: GameState) => ({
...prev,
screen: 'main-menu',
}));
}
};
const handleResetGame = async () => {
try {
// Reset game state first
const item = getRandomTextItemFrom(allTexts, selectedCategory);
setGameState((prev: GameState) => ({
...prev,
screen: 'typing-game',
currentText: item.content,
currentAttribution: item.attribution,
input: '',
startTime: null,
isRunning: false,
stats: {
wpm: 0,
rawWpm: 0,
accuracy: 100,
time: 0,
correctChars: 0,
incorrectChars: 0,
totalChars: 0,
currentStreak: 0,
bestStreak: 0,
},
}));
// Then reset WASM game instance
await resetGame();
setGameKey((k: number) => k + 1); // Force remount
// Ensure focus after a short delay
setTimeout(() => {
const inputElement = document.querySelector('.typing-input') as HTMLInputElement;
if (inputElement) {
inputElement.disabled = false;
inputElement.focus();
}
}, 200);
} catch (err) {
console.error('Error resetting game:', err);
// If reset fails, go back to main menu
handleMainMenu();
}
};
const handleInput = (input: string, accuracy: number, mistakes: number) => {
setGameState((prev: GameState) => {
// Don't update if input hasn't changed
if (prev.input === input) {
return prev;
}
const newState = {
...prev,
input,
stats: {
...prev.stats,
accuracy,
incorrectChars: mistakes
}
};
// Only update running state and start time once
if (!prev.isRunning) {
newState.isRunning = true;
newState.startTime = Date.now();
}
// Check if the game is finished using WASM game's is_finished method
if (game && game.is_finished() && prev.screen === 'typing-game') {
newState.isRunning = false;
newState.screen = 'end-screen';
// Calculate final stats but preserve WASM accuracy and mistakes
const elapsedTime = (Date.now() - (newState.startTime || Date.now())) / 1000;
const stats = calculateStats(input, prev.currentText, elapsedTime);
// Preserve WASM accuracy and mistakes instead of recalculating
stats.accuracy = accuracy;
stats.incorrectChars = mistakes;
newState.stats = stats;
}
return newState;
});
};
const handleFinish = (
finalStats: Stats,
wpmHistory: Array<{ time: number; wpm: number; raw: number; isError: boolean }>,
userInput: string,
charTimings: Array<{ time: number; isCorrect: boolean; char: string; index: number }>,
keypressHistory: Array<{ time: number; index: number; isCorrect: boolean }>
) => {
setLastTest({ stats: finalStats, wpmHistory, text: gameState.currentText, userInput, charTimings, keypressHistory });
setGameState(prev => ({ ...prev, isRunning: false, screen: 'end-screen' }));
};
const handleMainMenu = async () => {
try {
// Reset game state first
setGameState((prev: GameState) => ({
...prev,
screen: 'main-menu',
input: '',
currentText: '',
currentAttribution: undefined,
startTime: null,
isRunning: false,
stats: {
wpm: 0,
rawWpm: 0,
accuracy: 100,
time: 0,
correctChars: 0,
incorrectChars: 0,
totalChars: 0,
currentStreak: 0,
bestStreak: 0,
},
}));
// Then cleanup WASM game instance
cleanupGame();
setGameKey((k: number) => k + 1); // Force remount
} catch (err) {
console.error('Error going to main menu:', err);
// If cleanup fails, still try to go to main menu
setGameState((prev: GameState) => ({
...prev,
screen: 'main-menu',
input: '',
currentText: '',
startTime: null,
isRunning: false,
stats: {
wpm: 0,
rawWpm: 0,
accuracy: 100,
time: 0,
correctChars: 0,
incorrectChars: 0,
totalChars: 0,
currentStreak: 0,
bestStreak: 0,
},
}));
}
};
useEffect(() => {
let interval: ReturnType<typeof setInterval>;
if (gameState.isRunning && gameState.screen === 'typing-game' && gameState.startTime) {
interval = setInterval(() => {
setGameState((prev: GameState) => {
if (!prev.isRunning || !prev.startTime) return prev;
const elapsedTime = (Date.now() - prev.startTime) / 1000;
const stats = calculateStats(prev.input, prev.currentText, elapsedTime);
// Only update if stats have changed significantly
const hasSignificantChange =
Math.abs(prev.stats.wpm - stats.wpm) > 0.1 ||
Math.abs(prev.stats.rawWpm - stats.rawWpm) > 0.1 ||
Math.abs(prev.stats.time - stats.time) > 0.1;
if (!hasSignificantChange) {
return prev;
}
return {
...prev,
stats,
};
});
}, 100);
}
return () => {
if (interval) clearInterval(interval);
};
}, [gameState.isRunning, gameState.screen, gameState.startTime]);
return (
<ThemeProvider>
<div className="app">
{gameState.screen === 'main-menu' ? (
<MainMenu
onStartGame={handleStartGame}
categories={categories}
selectedCategory={selectedCategory}
onSelectCategory={setSelectedCategory}
/>
) : gameState.screen === 'end-screen' ? (
<EndScreen
stats={lastTest?.stats || testStats}
wpmHistory={lastTest?.wpmHistory || testWpmHistory}
text={lastTest?.text || testText}
userInput={lastTest?.userInput || testUserInput}
charTimings={lastTest?.charTimings}
keypressHistory={lastTest?.keypressHistory}
onPlayAgain={handleResetGame}
onMainMenu={handleMainMenu}
/>
) : (
<TypingGame
key={gameKey}
game={game as any}
text={gameState.currentText}
input={gameState.input}
stats={gameState.stats}
attribution={gameState.currentAttribution}
onInput={handleInput}
onFinish={handleFinish}
onReset={handleResetGame}
onMainMenu={handleMainMenu}
/>
)}
</div>
</ThemeProvider>
);
}
export default App;
|