-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
423 lines (357 loc) · 15 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const checkwin = require('./sub_server/XO.js')
server.listen(process.env.PORT || 3000, notify());
// Serve static files
app.use("/", express.static('public'))
// Notify the listeing port
function notify()
{
//var host = server.address().address;
//var port = server.address().port;
console.log("Listening on port 3000");
}
// Objects store user data
var loneUser ={};
var RPSSloneUser = {};
var XOloneUser ={};
var allUser ={};
function reset_loneUser() {
// Reset loneUser
console.log("Reset loneUser");
loneUser={};
}
io.on('connection', function(socket) {
console.log("New socket id is ",socket.id);
// Save this socket in allUser
allUser[socket.id] = socket;
// Update number of sockets every 2 seconds
socket.on('socketsNum', function(){
console.log("Updating");
socket.emit('socketsNum', Object.keys(allUser).length);
});
// When user disconnect
socket.on('disconnect', function(){
// If is being matched then out match
if(allUser[socket.id].partner) {
console.log('This socket is matched breaking match');
io.in(socket.id).emit('breakMatch');
}
// Handle which game user is finding in
if(loneUser.id === socket.id) loneUser = {}
if(RPSSloneUser.id === socket.id) RPSSloneUser = {}
if(XOloneUser.id === socket.id) XOloneUser = {}
// Delete in from allUser list
delete allUser[socket.id];
console.log(socket.id, "Is deleted");
});
// Receive username from client input and match
socket.on('sendUsername', (data) => {
console.log("Username is",data, "from socket",socket.id);
// Save this username in allUser by socket.id
allUser[socket.id].username = data;
// If no one is alone then be alone
if(!loneUser.id)
{
console.log("No one is alone so make",data, "a loneUser");
// Update loneUser
loneUser.username = data;
loneUser.id = socket.id;
console.log("loneUser is", loneUser);
console.log("Number of sockets connected",Object.keys(allUser).length + '\n');
}
// If someone is alone then match
else
{
console.log("Someone is here i'll match ",data,"with",loneUser.username);
// Update partner in allUser and
allUser[socket.id].partner = allUser[loneUser.id];
allUser[loneUser.id].partner = allUser[socket.id];
// Initialize array choice inside object
allUser[socket.id].choice = [];
allUser[socket.id].partner.choice = [];
// Join in one room
socket.join(loneUser.id, () => {
console.log("Changing room");
console.log(socket.id, "now in room",allUser[socket.id].rooms);
});
allUser[loneUser.id].join(socket.id, () => {
console.log("loneUser joining");
console.log("loneUser now in room",allUser[loneUser.id].rooms);
// Both matching process is done
io.in(socket.id).emit('matching_done',loneUser.username,allUser[socket.id].username);
reset_loneUser();
})
console.log("Number of sockets connected",Object.keys(allUser).length + '\n');
}
});
// If cancel_finding_RPS
socket.on('cancel_finding_RPS', () => {
console.log('Canceling, delete from loneUser');
loneUser = {};
});
// If cancel_finding_RPSS
socket.on('cancel_finding_RPSS', () => {
console.log('Canceling, delete from loneUser');
RPSSloneUser = {};
});
// Receive username from client input and match a RPSS game
socket.on('sendUsernameRPSS', (data) => {
console.log("Username is",data, "from socket",socket.id);
// Save this username in allUser by socket.id
allUser[socket.id].username = data;
// If no one is alone then be alone
if(!RPSSloneUser.id)
{
console.log("No one is alone so make",data, "a RPSSloneUser");
// Update loneUser
RPSSloneUser.username = data;
RPSSloneUser.id = socket.id;
console.log("RPSSloneUser is", RPSSloneUser);
console.log("Number of sockets connected",Object.keys(allUser).length + '\n');
}
// If someone is alone then match
else
{
console.log("Someone is here i'll match ",data,"with",RPSSloneUser.username);
// Update partner in allUser and
allUser[socket.id].partner = allUser[RPSSloneUser.id];
allUser[RPSSloneUser.id].partner = allUser[socket.id];
// Initialize array choice inside object
allUser[socket.id].choice = [];
allUser[socket.id].partner.choice = [];
// Join in one room
socket.join(RPSSloneUser.id, () => {
console.log("Changing room");
console.log(socket.id, "now in room",allUser[socket.id].rooms);
});
allUser[RPSSloneUser.id].join(socket.id, () => {
console.log("RPSSloneUser joining");
console.log("RPSSloneUser now in room",allUser[RPSSloneUser.id].rooms);
// Both matching process is done
io.in(socket.id).emit('matching_done',RPSSloneUser.username,allUser[socket.id].username);
console.log('Reset RPSSloneUser');
RPSSloneUser = {};
})
console.log("Number of sockets connected",Object.keys(allUser).length + '\n');
}
});
// Check if both player is ready RPS id is socket.id
// It can be used for both RPS and RPSS
socket.on('readyRPS', (id) => {
console.log('Player',allUser[id].username,'is ready');
allUser[id].ready = 1;
// If the other is ready
if(allUser[id].partner.ready)
{
console.log('The other is ready');
io.to(id).emit('both ready');
}
else socket.emit('not ready');
});
// In RPSS game, inform partner about socket's choice
socket.on('giveEnemyHint', (choice) => {
console.log('Giving my choice');
socket.broadcast.to(socket.id).emit('showEChoice', choice);
});
// emochat
socket.on('emochat_why', () => {
console.log('Received emochat');
socket.broadcast.to(socket.id).emit('emochat_why');
});
socket.on('emochat_what', () => {
console.log('Received emochat');
socket.broadcast.to(socket.id).emit('emochat_what');
});
socket.on('emochat_happy', () => {
console.log('Received emochat');
socket.broadcast.to(socket.id).emit('emochat_happy');
});
socket.on('emochat_scared', () => {
console.log('Received emochat');
socket.broadcast.to(socket.id).emit('emochat_scared');
});
// Handle event from clients player here is socket.id
socket.on('select',(round, choice, player) => {
// Send to all clients in room including sender
console.log('Received', choice, "from", allUser[player].username);
allUser[socket.id].choice[round] = choice;
io.in(socket.id).emit('select', choice, allUser[player].username);
});
// Check result from two player
socket.on('checkResult', (round) => {
// var Choices in round
let socketChoice = allUser[socket.id].choice[round];
let partnerChoice = allUser[socket.id].partner.choice[round];
console.log('Round', round);
console.log(allUser[socket.id].username, "chose", socketChoice, allUser[socket.id].partner.username, "chose", partnerChoice);
// Params order : winner, loser, round
// socket win
if( socketChoice == 'Rock' && partnerChoice == 'Scissors') io.to(socket.id).emit('result', socket.id, allUser[socket.id].partner.id, round);
if( socketChoice == 'Scissors' && partnerChoice == 'Paper') io.to(socket.id).emit('result', socket.id, allUser[socket.id].partner.id,round);
if( socketChoice == 'Paper' && partnerChoice == 'Rock') io.to(socket.id).emit('result', socket.id, allUser[socket.id].partner.id, round);
// Draw
if( socketChoice == 'Rock' && partnerChoice == 'Rock') io.to(socket.id).emit('result', '0', '0', round);
if( socketChoice == 'Paper' && partnerChoice == 'Paper') io.to(socket.id).emit('result','0', '0', round);
if( socketChoice == 'Scissors' && partnerChoice == 'Scissors') io.to(socket.id).emit('result','0', '0', round);
// socket lose
if( socketChoice == 'Rock' && partnerChoice == 'Paper') io.to(socket.id).emit('result', allUser[socket.id].partner.id, socket.id, round);
if( socketChoice == 'Paper' && partnerChoice == 'Scissors') io.to(socket.id).emit('result', allUser[socket.id].partner.id, socket.id, round);
if( socketChoice == 'Scissors' && partnerChoice == 'Rock') io.to(socket.id).emit('result', allUser[socket.id].partner.id, socket.id, round);
});
// Win match
socket.on('win_match_RPS', () => {
socket.emit('win_match_RPS',);
});
// Lose match
socket.on('lose_match_RPS', () => {
socket.emit('lose_match_RPS',);
});
// Draw match
socket.on('draw_match_RPS', () => {
socket.emit('draw_match_RPS',);
});
// Rematch for RPS and RPSS?
socket.on('rematch', () => {
// Check partner's rematch val
socket.rematch = 1;
console.log('In allUser', allUser[socket.id].rematch);
// Rematch = yes
if(allUser[socket.id].partner.rematch)
{
io.to(socket.id).emit('rematch', 1);
// Reset choice (only for RPSS and RPS)
allUser[socket.id].choice = [];
allUser[socket.id].partner.choice = [];
}
// Rematch = no
else
{
// Not rematch yet
socket.emit('rematch', 0);
// Inform partner
allUser[socket.id].partner.emit('want_rematch',);
}
});
// Handle event outRoom
socket.on('outRoom', () =>{
// Notify the other user
console.log("Sending notification to the other user");
socket.broadcast.to(socket.id).emit('beOuted',);
// // Outting socket leave partner's room
// socket.leave(allUser[socket.id].partner.id, () => {
// console.log("Socket left room");
// });
// // Help the beOuted
// allUser[socket.id].partner.leave(socket.id, () => {
// console.log("Partner left room");
// });
});
/*
* ============= Handle XO game ===============
*/
// Receive username from client input and match a RPSS game
socket.on('sendUsernameXO', (data) => {
console.log("Username is",data, "from socket",socket.id);
// Save this username in allUser by socket.id
allUser[socket.id].username = data;
// If no one is alone then be alone
if(!XOloneUser.id)
{
console.log("No one is alone so make",data, "a XOloneUser");
// Update loneUser
XOloneUser.username = data;
XOloneUser.id = socket.id;
console.log("XOloneUser is", XOloneUser);
console.log("Number of sockets connected",Object.keys(allUser).length + '\n');
}
// If someone is alone then match
else
{
console.log("Someone is here i'll match ",data,"with",XOloneUser.username);
// Update partner in allUser and
allUser[socket.id].partner = allUser[XOloneUser.id];
allUser[XOloneUser.id].partner = allUser[socket.id];
// Initialize board inside object
allUser[socket.id].board = new Array(15);
allUser[socket.id].partner.board = new Array(15);
for (let i = 0; i < 15 ; i++)
{
allUser[socket.id].partner.board[i] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
allUser[socket.id].board[i] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
}
console.log(allUser[socket.id].partner.board)
// Join in one room
socket.join(XOloneUser.id, () => {
console.log("Changing room");
console.log(socket.id, "now in room",allUser[socket.id].rooms);
});
allUser[XOloneUser.id].join(socket.id, () => {
console.log("XOloneUser joining");
console.log("XOloneUser now in room",allUser[XOloneUser.id].rooms);
// Both matching process is done
io.in(socket.id).emit('matching_done',XOloneUser.username,allUser[socket.id].username);
// Decide X and O
socket.emit('decide_player', 'X')
socket.to(socket.id).emit('decide_player', 'O')
// Reset
console.log('Reset XOloneUser');
XOloneUser = {};
})
console.log("Number of sockets connected",Object.keys(allUser).length + '\n');
}
});
// If cancel_finding_XO
socket.on('cancel_finding_XO', () => {
console.log('Canceling, delete from XOloneUser');
XOloneUser = {};
});
// Receive choice
socket.on('XOChoice', (y, x, player) => {
console.log('Received XOChoice', y, x, player)
// Change the board
allUser[socket.id].board[y][x] = player
allUser[socket.id].partner.board[y][x] = player
// Show choice to both players
socket.broadcast.to(socket.id).emit('showChoice', y, x, player)
// Check if someone wins, checkwin() returns 'X' || 'O' || 0
let winner = checkwin(allUser[socket.id].partner.board, y, x)
console.log('winner = ', winner)
winner ?
// Announce winner
io.to(socket.id).emit('has_winner', player)
// Else changeTurn
: socket.to(socket.id).emit('changeTurn')
})
// Rematch for XO?
socket.on('rematchXO', () => {
// Check partner's rematch val
socket.rematch = 1;
console.log('In allUser', allUser[socket.id].rematch);
// Rematch = yes
if(allUser[socket.id].partner.rematch)
{
// Emit
io.to(socket.id).emit('rematch', 1);
// Reset rematch
allUser[socket.id].rematch = 0
allUser[socket.id].partner.rematch = 0
// Reset board
for (let i = 0; i < 15 ; i++)
{
allUser[socket.id].partner.board[i] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
allUser[socket.id].board[i] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
}
}
// Rematch = no
else
{
// Not rematch yet
socket.emit('rematch', 0);
// Inform partner
allUser[socket.id].partner.emit('want_rematch',);
}
});
});