-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
291 lines (254 loc) · 8.52 KB
/
script.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
const typing={
Beginner:"asdf jkl; asdf jkl; asdf jkl; asdf jkl; qwer poiu ty zxcv .,mn b gh",
intermediate:"The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs. Jinxed wizards pluck ivy from the big quilt.",
advanced:"Photosynthesis occurs in the chloroplasts of plant cells, which contain the pigment chlorophyll. Chlorophyll absorbs light energy, primarily in the blue and red wavelengths, and converts it into chemical energy through a series of reactions known as the light-dependent and light-independent Calvin cycle reactions. This process is crucial for life on Earth."
};
const form = document.querySelector('form');
const data = document.getElementById('data');
//Game mode
const Restart = document.querySelector('.reset');
const Back = document.querySelector('.back');
let dropdownMenu = document.querySelector('.dropdown-menu');
let dropup = document.querySelector('.dropup');
let dropdown = document.querySelector('.dropdown');
const Level1 = document.querySelector('.Level1');
const Level2 = document.querySelector('.Level2');
const Level3 = document.querySelector('.Level3');
const a_type = document.getElementById('a_type');
const a_back = document.getElementById('a_back');
const a_error = document.getElementById('a_error');
const a_done = document.getElementById('a_done');
const a_next = document.getElementById('a_next');
const a_wait = document.getElementById('a_wait');
let TotalWords = document.getElementById("TotalWords");
let TotalTime = document.getElementById("TimeCount");
let TotalSpeed = document.getElementById("SpeedCount");
const keydata = document.getElementById("keydata");
const Console = document.getElementById("console");
const info = document.querySelector('.info');
let error = document.getElementById("error");
let text; //text is an array that store text char
let i=0;
let isOpen = false;//to level dropup/dropdown
let words=0;
let Time = 0;
let Speed = 0;
//Back to previous action
Back.addEventListener('click',()=>{
a_type.currentTime=0;
a_type.play();
setTimeout(()=>{
window.location.reload();
Back.style.display='none';
},220);
})
//Restarting the game
Restart.addEventListener('click',(e)=>{
a_type.currentTime=0;
a_type.play();
form.style.display='flex';
Back.style.display='block';
Restart.style.display='none';
dropup.style.display='none';
isOpen=false;
dropdown.style.display='none';
});
// Taking text from user
form.addEventListener('submit',(e)=>{
e.preventDefault();
if(data.value===""){
form.style.display='flex';
data.style.borderRight='5px solid #f32013';
a_error.currentTime='0';
a_error.play();
}else{
if (data.value.length > 360) {
data.value = data.value.substring(0, 360);
}
keydata.innerHTML=data.value;
text=keydata.innerHTML.split("");
data.value='';
a_type.play();
form.style.display='none';
Start();
}
});
Level1.addEventListener('click',(e)=>{
a_type.currentTime=0;
a_type.play();
isOpen=false;
dropdown.style.display='none';
Restart.style.display='none';
Back.style.display='block';
dropup.style.display='none';
keydata.innerHTML=typing.Beginner;
text=keydata.innerHTML.split("");
a_type.play();
Start();
})
Level2.addEventListener('click',(e)=>{
a_type.currentTime=0;
a_type.play();
isOpen=false;
dropdown.style.display='none';
Restart.style.display='none';
Back.style.display='block';
dropup.style.display='none';
keydata.innerHTML=typing.intermediate;
text=keydata.innerHTML.split("");
a_type.play();
Start();
})
Level3.addEventListener('click',(e)=>{
a_type.currentTime=0;
a_type.play();
isOpen=false;
dropdown.style.display='none';
Restart.style.display='none';
Back.style.display='block';
dropup.style.display='none';
keydata.innerHTML=typing.advanced;
text=keydata.innerHTML.split("");
a_type.play();
Start();
})
//Control full alog after text come to the text array..
function Start(){
const countTime = setInterval(()=>{
Time++;
let min = Math.floor(Time/60);
let sec = Math.floor(Time%60);
let speed =Math.floor(words/(Time/60));
TotalSpeed.innerHTML=`${speed} WPM`;
if(Time/60<1){
TotalTime.innerHTML=`${sec}s`;
}else{
TotalTime.innerHTML=`${min} : ${sec}s`;
}
},1000);
Console.style.display='block';
window.addEventListener("keydown",function (e){
error.innerHTML="";
let char = String(text[i]);
keyCheck(char,e.key);
console.log(char);
console.log(i);
if(e.key===char || e.key==="CapsLock"){
if(e.key===char){
Console.innerHTML+=e.key;
i++;
if(text.length==i){
this.clearInterval(countTime);
end();
}
a_type.currentTime=0;
a_type.play();
}
Console.style.color='white';
}else{
if(e.key==='Backspace'){
TEXT = Console.innerHTML.split("");
if(i>0){
i--;
}
//Update the no of words
if(String(text[i])==" "){
words--;
TotalWords.innerHTML=`${words} Words`;
}
Console.innerHTML='';
for(let j=0;j<TEXT.length-1;j++){
Console.innerHTML+=TEXT[j];
}
a_back.currentTime=0;
a_back.play();
}else{
error.innerHTML=`💡:${text[i]}`;
a_error.currentTime=0;
a_error.play();
}
}
}
);
}
//Words count and check the pressed char is correct or not
function keyCheck(key,KEY){
if(key==" " && KEY==" "){
++words;
TotalWords.innerHTML=`${words} Words`;
}
}
function end(){
i=0;
info.style.display='none';
keydata.innerHTML='';
Console.innerHTML='';
TotalWords.innerHTML='';
keydata.style.color='white';
Console.style.display='none';
keydata.innerHTML='Congratulations🏆';
a_done.currentTime=0;
a_done.play();
window.addEventListener('keydown',(e)=>{
a_back.pause();
a_error.pause();
a_type.pause();
a_done.pause();
});
let m = document.querySelector('i');
m.innerHTML='wait';
let j=0;
setInterval(()=>{
if(j==0){
m.innerHTML='wait.';
j=1;
}else if(j==1){
m.innerHTML='wait..<BR>New Game';
j=2;
}else if(j==2){
m.innerHTML='wait...<br>New Game<br>Reset All Data<br>Done:30%';
j=3;
}else if(j==3){
m.innerHTML='wait..<br>New Game<br>Reset All Data<br>Done:60%';
j=4;
}else if(j==4){
m.innerHTML='wait.<br>New Game<br>Almost Complete<br>Done:80%';
j=5;
}else if(j==5){
m.innerHTML='Done 100%';
m.style.width='100px';
m.style.borderColor='#074242';
m.style.background='#3b7a60';
j=0;
}
},2000);
setTimeout(()=>{
a_next.play();
},12000);
setTimeout(()=>{
Restart.style.display='block';
window.location.reload();
},13500)
}
//Menu
dropdownMenu.style.transitionTimingFunction = 'linear';
dropdownMenu.style.transitionDuration = '0.15s';
dropup.addEventListener('click', (e) => {
if(!isOpen){
a_type.currentTime=0;
a_type.play();
dropdownMenu.style.display = 'flex';
setTimeout(() => {
dropdownMenu.style.bottom = '0px';
},10);
isOpen=true;
}else{
a_back.currentTime=0;
a_back.play();
dropdownMenu.style.bottom='-50px';
setTimeout(()=>{
dropdownMenu.style.display='none';
},100);
isOpen=false;
}
});