1+ <!DOCTYPE html>
2+ <html lang =" en" >
3+ <head >
4+ <meta charset =" UTF-8" >
5+ <meta name =" viewport" content =" width=device-width, initial-scale=1.0" >
6+ <title >TypeWord - <%= room %> </title >
7+ <link rel =" shortcut icon" href =" /logo" type =" image/x-icon" >
8+ <link rel =" stylesheet" href =" https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css"
9+ crossorigin =" anonymous" >
10+ </head >
11+ <body class =" text-center" >
12+ <h1 ><%= room %> </h1 >
13+ <p >Host: <%= host %> </p >
14+ <p >You: <%= user %> </p >
15+ <p id =" playerText" >Player: <%= player %> </p >
16+ <hr >
17+
18+ <p ><b >Word:</b ></p >
19+ <p id =" wordText" ></p >
20+ <div class =" d-flex justify-content-center mt-4" >
21+ <input type =" text" class =" form-control w-25" id =" wordInp" >
22+ </div >
23+ <br >
24+ <p id =" timeText" >Time: 0s</p >
25+
26+
27+ <br >
28+
29+ <div >
30+ <button id =" startGameBtn" class =" btn btn-success" disabled >Start</button >
31+ </div >
32+ <script src =" /socket.io/socket.io.js" ></script >
33+ <script >
34+ const you = " <%= user %>" ;
35+ const room = " <%= room %>" ;
36+ const host = " <%= host %>" ;
37+ const socket = io ();
38+ const playerText = document .getElementById (" playerText" );
39+ const wordText = document .getElementById (" wordText" );
40+ const startGameBtn = document .getElementById (" startGameBtn" );
41+ const timeText = document .getElementById (" timeText" );
42+ const wordInp = document .getElementById (" wordInp" );
43+ let word;
44+ let time = 0 ;
45+
46+ if (you == host){
47+ startGameBtn .disabled = false ;
48+ }
49+
50+ function startGame (){
51+ socket .emit (" typewordstart" , {" room" : room});
52+ }
53+
54+ function checkWord (){
55+ if (wordInp .value == word){
56+ const finishTime = time;
57+ time = undefined ;
58+ socket .emit (" typewordfinish" , {" room" : room, " time" : finishTime, " player" : you});
59+ }
60+ }
61+
62+ setInterval (() => {
63+ if (word){
64+ time = time + 0.01 ;
65+ timeText .textContent = ` Time: ${ time .toFixed (2 )} ` ;
66+ checkWord ();
67+ }
68+ }, 10 );
69+
70+
71+ socket .on (" typewordjoin" , (data ) => {
72+ if (data .room == room){
73+ playerText .textContent = ` Player: ${ data .username } ` ;
74+ }
75+ });
76+
77+ socket .on (" typewordwinner" , (data ) => {
78+ if (data .room = room){
79+ word = undefined ;
80+ time = 0 ;
81+ timeText .textContent = ` Winner: ${ data .player } ` ;
82+ }
83+ });
84+
85+ socket .on (" typewordstart" , (data ) => {
86+ if (data .room == room){
87+ wordText .textContent = " 3" ;
88+ setTimeout (() => {
89+ wordText .textContent = " 2" ;
90+ setTimeout (() => {
91+ wordText .textContent = " 1" ;
92+ setTimeout (() => {
93+ word = data .word ;
94+ wordText .textContent = word;
95+ }, 1000 );
96+ }, 1000 );
97+ }, 1000 );
98+ }
99+ });
100+
101+ startGameBtn .addEventListener (" click" , () => startGame ());
102+ </script >
103+ </body >
104+ </html >
0 commit comments