-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
229 lines (181 loc) · 6.93 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
var http = require('http');
var express = require('express');
var socket_io = require('socket.io');
var app = express();
app.use(express.static('public'));
var server = http.Server(app);
var io = socket_io(server);
var userHost;
var users = [];
var userHost;
var userPoints = {};
var words = [
"word", "letter", "number", "person", "pen", "police", "people",
"sound", "water", "breakfast", "place", "man", "men", "woman", "women", "boy",
"girl", "serial killer", "Oregon Trail", "week", "month", "name", "sentence", "line", "air",
"land", "home", "hand", "house", "picture", "animal", "mother", "father",
"big foot", "sister", "world", "head", "page", "country", "question",
"shiba inu", "school", "plant", "food", "sun", "state", "eye", "city", "tree",
"farm", "story", "sea", "night", "day", "life", "north", "south", "east",
"west", "child", "children", "example", "paper", "music", "river", "car",
"Power Rangers", "feet", "book", "science", "room", "friend", "idea", "fish",
"mountain", "horse", "watch", "color", "face", "wood", "list", "bird",
"body", "fart", "family", "song", "door", "forest", "wind", "ship", "area",
"rock", "Captain Planet", "fire", "problem", "airplane", "top", "bottom", "king",
"space", "whale", "unicorn", "narwhal", "furniture", "sunset", "sunburn", "Grumpy cat", "feather", "pigeon"
];
function newWord() {
wordcount = Math.floor(Math.random() * (words.length));
return words[wordcount];
};
var wordcount;
io.on('connection', function (socket) {
io.emit('userlist', {names: users, userPoints: userPoints});
socket.on('join', function(data) {
socket.username = data.username;
// user automatically joins a room under their own name
socket.join(data.username);
console.log(socket.username + ' has joined. ID: ' + socket.id);
// save the name of the user to an array called users
users.push(socket.username);
users.sort();
Object.assign(userPoints, data.userPoints);
if(users.length == 1)
{
userHost = socket.username;
io.in(socket.username).emit('new host');
}
// if the user is first to join OR 'drawer' room has no connections
if(users.length == 1)
{
userHost = socket.username;
io.in(socket.username).emit('new host');
}
if (users.length == 1 || typeof io.sockets.adapter.rooms['drawer'] === 'undefined') {
// place user into 'drawer' room
socket.join('drawer');
// server submits the 'drawer' event to this user
io.in(socket.username).emit('drawer', socket.username);
console.log(socket.username + ' is a drawer');
// send the random word to the user inside the 'drawer' room
io.in(socket.username).emit('draw word', newWord());
// console.log(socket.username + "'s draw word (join event): " + newWord());
}
// if there are more than one names in users
// or there is a person in drawer room..
else {
// additional users will join the 'guesser' room
socket.join('guesser');
// server submits the 'guesser' event to this user
io.in(socket.username).emit('guesser', socket.username);
console.log(socket.username + ' is a guesser');
}
// update all clients with the list of users
io.emit('userlist', {names: users, userPoints: userPoints});
});
// submit drawing on canvas to other clients
socket.on('draw', function(obj) {
socket.broadcast.emit('draw', obj);
});
// submit each client's guesses to all clients
socket.on('guessword', function(data) {
io.emit('guessword', { username: data.username, guessword: data.guessword})
console.log('guessword event triggered on server from: ' + data.username + ' with word: ' + data.guessword);
});
socket.on('disconnect', function() {
console.log("Disconnected: ", socket.username)
console.log("Users: ", users);
for (var i = 0; i < users.length; i++) {
// remove user from users list
if (users[i] == socket.username) {
users.splice(i, 1);
};
};
users.sort();
if(userHost == socket.username && users.length>0)
{
var x = Math.floor(Math.random() * (users.length));
userHost = users[x];
io.in(users[x]).emit('new host');
}
console.log("Users: ", users);
console.log(socket.username + ' has disconnected.');
// submit updated users list to all clients
io.emit('userlist', {names: users, userPoints: userPoints});
// if 'drawer' room has no connections..
if ( typeof io.sockets.adapter.rooms['drawer'] === "undefined") {
// generate random number based on length of users list
var x = Math.floor(Math.random() * (users.length));
console.log(users[x]);
// submit new drawer event to the random user in userslist
io.in(users[x]).emit('new drawer', users[x]);
};
});
socket.on('new drawer', function(name) {
// remove user from 'guesser' room
socket.leave('guesser');
// place user into 'drawer' room
socket.join('drawer');
console.log('new drawer emit: ' + name);
// submit 'drawer' event to the same user
socket.emit('drawer', name);
// send a random word to the user connected to 'drawer' room
io.in('drawer').emit('draw word', newWord());
});
socket.on('completed user', function(data){
userPoints[data.to] = data.firstUser ? userPoints[data.to]+2: userPoints[data.to]+1;
io.in(data.to).emit('show Completed', data.to);
})
socket.on('completed all user', function(data){
for(var i = 0;i < users.length; i++)
{
if(users[i]!=data.from)
{
io.in(users[i]).emit('reverse show Completed', users[i]);
}
}
io.emit('correct answer', {username: data.username, guessword: data.guessword, names: users, userPoints: userPoints});
})
socket.on('game stop', function(){
for(var i = 0;i < users.length; i++)
{
io.in(users[i]).emit('stop the game');
}
})
socket.on('game stop', function(){
for(var i = 0;i < users.length; i++)
{
io.in(users[i]).emit('stop the game');
}
})
socket.on('show Guess Word', function(data){
for(var i = 0;i < users.length; i++)
{
io.in(users[i]).emit('show guesses', {username: data.username, guessword: data.guessword});
}
})
// initiated from drawer's 'dblclick' event in Player list
socket.on('swap rooms', function(data) {
// drawer leaves 'drawer' room and joins 'guesser' room
socket.leave('drawer');
socket.join('guesser');
// submit 'guesser' event to this user
socket.emit('guesser', socket.username);
// submit 'drawer' event to the name of user that was doubleclicked
io.in(data.to).emit('drawer', data.to);
// submit random word to new user drawer
io.in(data.to).emit('draw word', newWord());
io.emit('reset', data.to);
});
socket.on('correct answer', function(data) {
userPoints[data.username]++;
io.emit('correct answer', {username: data.username, guessword: data.guessword, names: users, userPoints: userPoints});
console.log(data.username + ' guessed correctly with ' + data.guessword);
});
socket.on('clear screen', function(name) {
io.emit('clear screen', name);
});
})
server.listen(process.env.PORT || 8080, function() {
console.log('Server started at http://localhost:8080');
});