-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
231 lines (196 loc) · 6.84 KB
/
index.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
// require("dotenv").config();
const { Socket } = require("dgram");
const express= require("express")
const http= require("http")
const mongoose= require("mongoose");
// const { setInterval } = require("timers/promises");
const getSentence = require("./api/getSentance");
const Game= require("./models/Games")
// const { setIntervalAsync, clearIntervalAsync, SetIntervalAsyncTimer } = require('set-interval-async');
//create server
const app = express();
const port =process.env.port || 3000;
var server= http.createServer(app);
var io=require("socket.io")(server);
//middleware
app.use(express.json());
//connect to mongodb
const DB= process.env.Database;;
//listening to socket io event from the client(flutter code)
io.on('connection', (socket)=>{
console.log(socket.id);
//for create
socket.on('create-game', async ({nickname})=>{
try{
let game = new Game();
const sentence = await getSentence();
game.words = sentence;
//new object
let player={
socketID:socket.id,
nickname,
isPartyLeader:true,
}
game.players.push(player)
game= await game.save();
const gameId = game._id.toString();
socket.join(gameId);
io.to(gameId).emit('updateGame', game);
}
catch(e){
console.log(e);
}
// console.log(nickname);
});
socket.on('join-game', async({nickname,gameId})=>{
try{
if(!gameId.match(/^[0-9a-fA-F]{24}$/)){
socket.emit('notCorrectGame',
"please enter vaild game ID")
return;
}
let game= await Game.findById(gameId);
if(game.isJoin){
const id=game._id.toString();
let player={
nickname,
socketID:socket.id,
}
socket.join(id);
game.players.push(player);
game= await game.save();
io.to(gameId).emit('updateGame', game);
}else{
socket.emit("notCorrectGame",
"The game is in progress ,please try again later")
}
}catch(e){
console.log(e);
}
});
socket.on("userInput",async({userInput,gameID})=>{
let game =await Game.findById(gameID);
console.log(gameID);
console.log(`userInput is ${userInput}`);
//console.log(game);
if(!game.isJoin && !game.isOver){
let player = game.players.find((playerr) => playerr.socketID === socket.id);
//let player = game.players.find((playerr) => playerr.socketID === socket.id);
console.log(`player.socketID is ${player}`);
console.log(`player.currentWordIndex is ${player.currentWordIndex}`);
if(game.words[player.currentWordIndex] === userInput.trim() ){
player.currentWordIndex = player.currentWordIndex+1;
if( player.currentWordIndex !== game.words.length){
game= await game.save();
io.to(gameID).emit('updateGame', game);
}
else{
let endTime = new Date().getTime();
let {startTime}=game;
player.WPM=calculateWPM(endTime, startTime,player);
game= await game.save();
socket.emit('done');
io.to(gameID).emit('updateGame', game);
}
}
}
}
);
// timer listener
socket.on("timer", async ({ playerId, gameID }) => {
let countDown = 5;
let game = await Game.findById(gameID);
// console.log(" WORking 2");
// console.log(` gameID is ${gameID}`);
let player = game.players.id(playerId);
// console.log(" WORking 21");
// console.log(` playerId is ${playerId}`);
if (player.isPartyLeader) {
// console.log(" WORking 23");
let timerId =setInterval(async () => {
// console.log(" WORking 24");
if (countDown >= 0) {
io.to(gameID).emit("timer", {
countDown,
msg: "Game Starting",
});
console.log(countDown);
countDown--;
} else {
game.isJoin = false;
// console.log(" WORking 212");
game = await game.save();
console.log(game);
io.to(gameID).emit("updateGame", game);
// console.log(" WORking 24");
startGameClock(gameID);
// console.log(" WORking 25");
clearInterval(timerId);
// console.log(" WORking 26");
// console.log('game START!');
}
}, 1000);
}
});
});
const startGameClock= async (gameID)=>{
let game= await Game.findById(gameID);
console.log(game);
game.startTime=new Date().getTime();
console.log(game.startTime);
game = await game.save();
let time=120;
let TimerId= setInterval(( function gameIntervalFunc(){
if(time>=0){
const timeFormat=calculateTime(time);
io.to(gameID).emit('timer',{
countDown:timeFormat,
msg:"time Remaining"
});
console.log(time);
time--;
} else {
(async()=>{
try{
let endTime = new Date().getTime();
let game= await Game.findById(gameID);
let {startTime}=game;
game.isOver=true;
game.players.foreach((player, index) => {
if(player.WPM=== -1){
game.players[index].WPM=calculateWPM(endTime, startTime,player); }
});
game= await game.save();
io.to(gameID).emit('updateGame', game);
clearInterval(TimerId);
} catch(e){
}
})();
}
return gameIntervalFunc;
// }));
})(),
1000
)
};
const calculateTime= (time)=>{
let min= Math.floor( time/60);
let sec = time%60;
return `${min}:${sec <10 ? "0"+sec:sec}`;
}
const calculateWPM = (endTime, startTime,player)=>{
const timeTakenInSec= (endTime-startTime)/1000;
const timeTaken= timeTakenInSec/60;
let wordsTyped= player.currentWordIndex;
const WPM = Math.floor(wordsTyped/timeTaken);
return WPM;
}
mongoose.connect(DB).then(()=>{
console.log("connection Successful!");
}).catch((e)=>{
console.log(e);
})
// listen to server
server.listen(port,"0.0.0.0",()=>{
console.log("server started running on port " +port);
});