-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
136 lines (136 loc) · 6.69 KB
/
main.ts
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
const game = () => {
let score: number; //game score
let scoreMultiplier: number; //score multiplier based on game mode
let streak: number; //game streak
let time: number; //game time remaining
let timeMultiplier: number; //time multiplier for pressing correct key quickly
let lastTimePressed: number; //last time key was pressed correctly
let timePressed: number; //time last key was pressed correctly
let isGameStarted = false; //is game started
let isGameEnded = true; //is game ended
let characterToType: string; //character to type
const characterToTypeText = document.getElementById(
"character-to-type-text",
) as HTMLSpanElement; //character display text
const scoreText = document.getElementById("score") as HTMLSpanElement; //score text
const timeText = document.getElementById("time-remaining") as HTMLSpanElement; //time remaining text
const typingText = document.getElementById("typing-text") as HTMLInputElement; //typing text field
const startButton = document.getElementById(
"start-button",
) as HTMLButtonElement; //start button
const gameMode = document.getElementById("mode") as HTMLSelectElement; //game mode
const timeLimit = document.getElementById("time-limit") as HTMLSelectElement; //time limit selection
const startGame: () => void = (): void => {
//start the game
score = 0; //set score to 0
streak = 0; //set streak to 0
time = parseInt(timeLimit.value, 10); //set time remaining based on selected time limit value
timeMultiplier = 1; //set time multiplier to 1
isGameStarted = true; //set is game started to true
isGameEnded = false; //set is game ended to false
lastTimePressed = performance.now(); //set last time key was pressed to current performance time
generateRandomCharacter(); //generate random character
scoreText.textContent = score.toString(); //update text contents
timeText.textContent = time.toString();
typingText.focus(); //focus typing text
startButton.disabled = true; //disable start button
typingText.disabled = false; //enable typing text
gameMode.disabled = true; //disable game mode selection
timeLimit.disabled = true; //disable time limit selection
const updateTime: () => void = (): void => {
//update time every second
time--; //decrease time by 1
timeText.textContent = time.toString(); //update time text content
if (time === 0) {
//if time is 0
clearInterval(updateTimeInterval); //clear interval
isGameStarted = false; //set is game started to false
isGameEnded = true; //set is game ended to true
startButton.disabled = false; //enable start button
typingText.disabled = true; //disable typing text
gameMode.disabled = false; //enable game mode selection
timeLimit.disabled = false; //enable time limit selection
}
};
const updateTimeInterval = setInterval(updateTime, 1000); //update time every second
};
const generateRandomCharacter: () => void = (): void => {
//generate random character
const lowercaseAlphabet = "abcdefghijklmnopqrstuvwxyz"; //lowercase letters
const uppercaseAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //uppercase letters
const numbers = "0123456789"; //numeric digits
const symbols = "`~!@#$%^&*()_+-={}|[]\\:\";'><?,./"; //symbols (special characters)
const alphabet = lowercaseAlphabet + uppercaseAlphabet; //lowercase and uppercase letters of alphabet
const lettersAndNumbers = alphabet + numbers; //alphabet and numeric digits
const charactersList = lettersAndNumbers + symbols; //alphabet, numeric digits, and symbols
switch (
gameMode.value //set character to type based on selected game mode
) {
case "lowercase-letters": //lowercase letters
characterToType =
lowercaseAlphabet[
Math.floor(Math.random() * lowercaseAlphabet.length)
];
scoreMultiplier = 1; //1x score multiplier
break;
case "all-letters": //all letters (uppercase and lowercase letters)
characterToType = alphabet[Math.floor(Math.random() * alphabet.length)];
scoreMultiplier = 2; //2x score multiplier
break;
case "letters-and-numbers": //letters and numbers
characterToType =
lettersAndNumbers[
Math.floor(Math.random() * lettersAndNumbers.length)
];
scoreMultiplier = 3; //3x score multiplier
break;
case "letters-numbers-and-symbols": //all supported characters (letters, numbers and symbols)
characterToType =
charactersList[Math.floor(Math.random() * charactersList.length)];
scoreMultiplier = 4; //4x score multiplier
break;
default: //default case
//use the same lowercase letters case
characterToType =
lowercaseAlphabet[
Math.floor(Math.random() * lowercaseAlphabet.length)
];
scoreMultiplier = 1; //1x score multiplier
break;
}
characterToTypeText.textContent = characterToType; //set character to type to random character
};
startButton.addEventListener("click", (): void => {
//when start button is clicked
if (!isGameStarted || isGameEnded) {
//if game is not started or game is ended
startGame(); //start the game
startButton.disabled = true; //disable start button
typingText.disabled = false; //enable typing text
gameMode.disabled = true; //disable game mode selection
timeLimit.disabled = true; //disable time limit selection
}
});
typingText.addEventListener("input", (): void => {
//when key is pressed in typing text field
if (isGameStarted && !isGameEnded) {
//if game is started and game is not ended
const text: string = typingText.value; //set text to input text
if (text === characterToType) {
//if input text is equal to character to type (correct key is pressed)
timePressed = performance.now(); //set time last key was pressed to current performance time
timeMultiplier = 1 + 1 / (timePressed - lastTimePressed + 1000); //set time multiplier based on how fast the key was pressed correctly
lastTimePressed = performance.now(); //set last time key was pressed to current performance time
streak++; //increase streak by 1
score += Math.round(streak * scoreMultiplier); //increase score by streak times score multiplier
typingText.value = ""; //clear typing text input value
scoreText.textContent = score.toLocaleString("en-US"); //update score text content with commas as thousands seperator
generateRandomCharacter(); //generate random character
} else {
//if wrong key is pressed
streak = 0; //reset streak to 0
}
}
});
};
game();