-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
237 lines (199 loc) · 6.14 KB
/
server.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
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const fs = require('fs');
const PORT = process.env.PORT || 3000;
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/client/index.html');
})
const getWord = () => {
const wordsList = fs.readFileSync("words.txt", 'utf8').split("\n");
const randomNumber = Math.floor(Math.random() * 150);
return wordsList[randomNumber];
}
let sockets = [];
let players = [];
let currentPlayer = -1;
let currentWord;
let currentSocketId;
let timeLeft;
let correctGuessedCount;
let intervalVar;
let guessedArray = [];
let gameOnGoing = false;
let activity = [];
const initialiseRound = () => {
console.log("Initialise Round");
if (players.length < 2) {
return "Waiting for more people";
}
currentPlayer += 1;
if (currentPlayer >= players.length) {
currentPlayer = 0;
}
guessedArray = [];
currentWord = getWord();
timeLeft = 85;
correctGuessedCount = 0;
gameOnGoing = true;
startRound();
}
const startRound = () => {
console.log("Start Round");
activity.unshift("Round started");
currentSocketId = players[currentPlayer].id;
activity.unshift(`${players[currentPlayer].name} is drawing!`);
io.emit('activity_server', activity.slice(0, 6));
// Find the socket connection for the required player
for (let i = 0; i < sockets.length; i++) {
if (sockets[i].id == currentSocketId) {
sockets[i].emit('gamestart_server', currentWord);
} else {
sockets[i].emit('gamestart_server_guess', "All the Best");
}
}
intervalVar = setInterval(() => {
console.log("One second passed");
timeLeft -= 1;
if (timeLeft == 0) {
stopGame();
return;
}
io.emit("timeleft_server", timeLeft);
}, 1000);
}
const stopGame = () => {
console.log("Stop the game");
clearInterval(intervalVar);
gameOnGoing = false;
activity.unshift("Round over");
io.emit("activity_server", activity.slice(0, 6));
// Send all the new scores of players
io.emit("players_list", players);
io.emit("clearcanvas");
if (players.length > 1) {
io.emit("nextround_server");
initialiseRound();
} else {
io.emit("gameover_server");
currentPlayer = -1;
}
}
const playerGuess = (id, guess) => {
let localSocket;
let playerName;
for (let i = 0; i < sockets.length; i++) {
if (sockets[i].id == id) {
localSocket = sockets[i];
}
if (players[i].id == id) {
playerName = players[i].name;
}
}
if (id == currentSocketId) {
localSocket.emit("guess_result", "You cannot guess");
return;
}
if (guessedArray.includes(id)) {
localSocket.emit("guess_result", "Already guessed");
return;
}
if (guess.toLowerCase() == currentWord.toLowerCase()) {
for (let i = 0; i < players.length; i++) {
if (players[i].id == id) {
players[i].score += 1;
playerName = players[i].name;
guessedArray.push(players[i].id);
localSocket.emit("guess_result", "Correct");
break;
}
}
activity.unshift(`${playerName} guessed the answer correct!`);
io.emit("activity_server", activity.slice(0, 6));
correctGuessedCount += 1;
} else {
localSocket.emit("guess_result", "Wrong");
activity.unshift(`${playerName} guessed the answer wrong :/`);
io.emit("activity_server", activity.slice(0, 6));
}
if (correctGuessedCount == players.length - 1) {
stopGame();
}
}
io.on('connection', (socket) => {
sockets.push(socket);
console.log(socket.id);
console.log("Connection formed");
players.push({
id: socket.id,
name: "Player" + (players.length + 1),
score: 0
});
activity.unshift(`New player connected!!`);
io.emit("activity_server", activity.slice(0, 6));
io.emit("players_list", players);
if (players.length > 1 && !gameOnGoing) {
initialiseRound();
}
socket.on('changename', (name) => {
for (let i = 0; i < players.length; i++) {
if (players[i].id == socket.id) {
players[i].name = name;
break;
}
}
io.emit("players_list", players);
});
socket.on('disconnect', () => {
if (socket.id == currentSocketId) {
activity.unshift("Person drawing left the game");
io.emit("activity_server", activity.slice(0, 6));
}
let playerName;
for (let i = 0; i < players.length; i++) {
if (players[i].id == socket.id) {
playerName = players[i].name;
}
}
activity.unshift(`${playerName} disconnected`);
io.emit("activity_server", activity.slice(0, 6));
sockets = sockets.filter(el => {
return el.id != socket.id;
})
players = players.filter(el => {
return el.id != socket.id;
})
if (players.length < 2) {
stopGame();
}
io.emit("players_list", players);
if (socket.id == currentSocketId) {
activity.unshift("Person drawing left the game");
io.emit("activity_server", activity.slice(0, 6));
stopGame();
}
console.log(`User disconnected: ${socket.id}`);
});
// socket.on('message1', (data) => {
// console.log(data);
// io.emit('message2', {
// arrayName: [1, 2, 3, 4, 5, 6]
// });
// });
socket.on('redraw', (data) => {
console.log(data);
socket.broadcast.emit("draw", data);
});
socket.on('guess', (data) => {
console.log("Guess function called: ", data);
playerGuess(socket.id, data);
});
socket.on('getword', () => {
socket.emit("currentword_server", currentWord);
})
})
http.listen(PORT, () => {
console.log(`Listening at port ${PORT}`);
});