-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
224 lines (176 loc) · 6.69 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
//Setup server with express
let express = require('express');
let app = express();
const PORT = process.env.PORT || 3000;
let server = app.listen(PORT);
let path = require('path');
let socket = require('socket.io');
let io = socket(server);
app.use(express.static(path.join(process.cwd(), 'public')));
let connectedUsers = [];
let gamePlayers = [];
let playerId = 0;
let scoreGoal = 50;
let words = [];
//Read from list of words into words array
const readline = require('readline');
const fs = require('fs');
const rl = readline.createInterface({
input: fs.createReadStream(path.join(process.cwd(), 'data/words.txt'))
});
rl.on('line', function (line) {
words.push(line);
});
//rl.on('close', function() { console.log(words); });
function getRandomInt(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
console.log("Listening on port " + PORT + ". Please port forward if you wish to connect over the internet.\n");
io.sockets.on('connection', function(socket) {
//On connect, add the current connection to client list
connectedUsers.push({id: socket.id, name: "👌", score: 0});
//Log user array and connections length
console.log("New user connected!\nUsers connected: " + Object.keys(io.sockets.connected).length);
console.log("Users:");
console.log(connectedUsers);
console.log('');
//Request canvas for the new client
if (connectedUsers.length > 1) {
socket.to(connectedUsers[0].id).emit('requestCanvas', { from: socket.id });
io.emit('loading', true);
}
else {
io.emit('loading', false);
}
//On user disconnect, remove them from the client list
socket.on('disconnect', function () {
connectedUsers.splice(connectedUsers.findIndex(x => x.id === socket.id), 1);
//if the disconnected player is in the gamePlayers
let playerInGame = gamePlayers.findIndex(x => x.id === socket.id);
if (playerInGame !== -1) {
gamePlayers.splice(playerInGame, 1);
//if the current player (drawer) disconnected go to the next player
if (socket.id === playerId) {
nextPlayer();
}
}
console.log("User disconnected!");
console.log("Users:");
console.log(connectedUsers);
console.log('');
});
//On 'name', change players name
socket.on('name', function(data) {
connectedUsers[connectedUsers.findIndex(x => x.id === socket.id)].name = data;
let userInGameIndex = gamePlayers.findIndex(x => x.id === socket.id);
if (userInGameIndex !== -1) gamePlayers[userInGameIndex].name = data;
});
//On 'mouse' message, broadcast data to everyone but the sender
socket.on('mouse', function(data) {
socket.broadcast.emit('mouse', data);
});
//On 'image' -> everyone but sender
socket.on('image', function(data) {
socket.broadcast.emit('image', data);
});
//On 'clearCanvas' -> everyone but sender
socket.on('clearCanvas', function() {
socket.broadcast.emit('clearCanvas');
});
//On 'sendCanvas', send the canvas to the new client
socket.on('sendCanvas', function(data) {
socket.to(data.to).emit('sendCanvas', data);
});
socket.on('loading', function(data) {
io.emit('loading', data);
});
//Drawing game functions
//On 'startGame' -> everyone
socket.on('startGame', function() {
if (gamePlayers.length > 0) return;//if joining game wait until next round
gamePlayers = connectedUsers.slice(0);
playerId = socket.id;
let data = {
players: gamePlayers,
currentPlayerId: playerId,
word1: words[getRandomInt(0,words.length - 1)],
word2: words[getRandomInt(0,words.length - 1)],
word3: words[getRandomInt(0,words.length - 1)],
};
io.emit('startGame', data);
});
//On 'guess' -> player who is currently drawing
socket.on('guess', function(data) {
socket.to(gamePlayers[gamePlayers.findIndex(x => x.id === playerId)].id).emit('guess', data);
});
//On 'guessReply' -> everyone but sender
socket.on('guessReply', function(data) {
//Correct guess, assign points
if (data.result) {
connectedUsers[connectedUsers.findIndex(x => x.id === data.player)].score += data.score;
//Half score goes to the drawer
connectedUsers[connectedUsers.findIndex(x => x.id === playerId)].score += Math.round(data.score / 2);
}
socket.broadcast.emit('guessReply', data);
});
//On 'hint' -> everyone but sender
socket.on('hint', function(data) {
socket.broadcast.emit('hint', data);
});
//On 'nextPlayer' -> everyone
socket.on('nextPlayer', function() {
nextPlayer();
});
function nextPlayer() {
if (gamePlayers.length === 0) return;
if (checkForWinner()) return;
gamePlayers = connectedUsers.slice(0);
let currPlayerIndex = gamePlayers.findIndex(x => x.id === socket.id);
currPlayerIndex++;
currPlayerIndex %= gamePlayers.length;
playerId = gamePlayers[currPlayerIndex].id;
let data = {
players: gamePlayers,
currentPlayerId: playerId,
word1: words[getRandomInt(0,words.length - 1)],
word2: words[getRandomInt(0,words.length - 1)],
word3: words[getRandomInt(0,words.length - 1)],
};
io.emit('startGame', data);
}
function checkForWinner() {
let winners = [];
//Check for any players with a score higher than the goal
for (let i = 0; i < connectedUsers.length; ++i) {
if (connectedUsers[i].score >= scoreGoal) {
winners.push(connectedUsers[i]);
}
}
if (winners.length > 0) {
//Since the guesser and the drawer gain score at the same time, they could both reach the goal at the same time
if (winners.length > 1) {
//Sort highest to lowest score
winners.sort(function(x,y) { return x.score < y.score });
//Equal score no winner yet
if (winners[0].score === winners[1].score) return false;
}
let data = {
winnerId: winners[0].id,
winnerName: winners[0].name,
score: winners[0].score
};
winGame(data);
return true;
}
return false;
}
function winGame(data) {
io.emit('winGame', data);
//Reset scores
for (let i = 0; i < connectedUsers.length; ++i) {
connectedUsers[i].score = 0;
}
gamePlayers = [];
playerId = 0;
}
});