-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
1073 lines (858 loc) · 25.4 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************/
/* Set up the static file server */
/* Include the static file webserver library */
var static = require('node-static');
/* Include the http server library */
var http = require('http');
/* Assume that we are running on Heroku */
var port = process.env.PORT;
var directory =__dirname + '/public';
/* If aren't on Heroku, then we need to readjust the port and direcotry
* information and we know that because port won't be set */
if(typeof port == 'undefined' || !port){
directory = './public';
port = 8080;
}
/* Set up a static web-server that will deliver files from the filesystem */
var file = new static.Server(directory);
/* Construct an http server that gets file from the file server*/
var app= http.createServer(
function(request,response){
request.addListener('end',
function(){
file.serve(request,response);
}
).resume();
}
).listen(port);
console.log('The Server is running');
/**********************************************/
/* Set up the web socket server */
/* A registry of socket_ids and player information */
var players =[];
var io = require('socket.io').listen(app);
io.sockets.on('connection', function (socket) {
log('Client connection by '+socket.id);
function log(){
var array = ['*** Server Log Message:'];
for(var i = 0; i < arguments.length; i ++){
array.push(arguments[i]);
console.log(arguments[i]);
}
socket.emit('log',array);
socket.broadcast.emit('log',array);
}
/* join_room command */
/*payload:
{
'room': room to join,
'username': username of person joining
}
join_room_response:
{
'result': 'success',
'room': room joined,
'username': username that joined,
'socket_id': the socket id of the person that,
'membership': number of people in the room including the new one
}
or
{
'result':'fail',
'message': failure message
}
*/
socket.on('join_room',function(payload){
log('\'join_room\'command'+JSON.stringify(payload));
/* Check that the client sent a payload */
if(('undefined'=== typeof payload) || !payload){
var error_message ='join_room had no payload, command aborted';
log(error_message);
socket.emit('join_room_response', {
result: 'fail',
message: error_message
});
return;
}
/* Check that the payload has a room to join */
var room = payload.room;
if(('undefined'=== typeof room) || !room){
var error_message ='join_room didn\'t specify a room, command aborted';
log(error_message);
socket.emit('join_room_response', {
result: 'fail',
message: error_message
});
return;
}
/* Check that a username has been provided */
var username = payload.username;
if(('undefined'=== typeof username) || !username){
var error_message ='join_room didn\'t specify a username command aborted';
log(error_message);
socket.emit('join_room_response', {
result: 'fail',
message: error_message
});
return;
}
/* Store information about this new player */
players[socket.id] = {};
players[socket.id].username = username;
players[socket.id].room = room;
/* Actually have the user join the room */
socket.join(room);
/* Get the room object */
var roomObject = io.sockets.adapter.rooms[room];
/* Tell everyone that is already in the room that someone just joined.*/
var numClients = roomObject.length;
var success_data = {
result: 'success',
room: room,
username: username,
socket_id: socket.id,
membership: numClients
};
io.in(room).emit('join_room_response', success_data);
for(var socket_in_room in roomObject.sockets){
var success_data = {
result: 'success',
room: room,
username: players[socket_in_room].username,
socket_id: socket_in_room,
membership: numClients
};
socket.emit('join_room_response',success_data);
};
log('join_room success');
if(room !== 'lobby'){
send_game_update(socket,room,'initial update');
}
});
socket.on('disconnect',function(){
log('Client disconnected '+JSON.stringify(players[socket.id]));
if('undefined' !== typeof players[socket.id] && players[socket.id]){
var username = players[socket.id].username;
var room = players[socket.id].room;
var payload = {
username: username,
socket_id: socket.id
};
delete players[socket.id];
io.in(room).emit('player_disconnected',payload);
}
});
/* send_message command */
/*payload:
{
'room': room to join,
'message': the message to send
}
send_message_response:
{
'result': 'success',
'room': room joined,
'username': username of the person that spoke,
'message': the message spoken
}
or
{
'result':'fail',
'message': failure message
}
*/
socket.on('send_message',function(payload){
log('server received a command','send_message', payload);
if(('undefined'=== typeof payload) || !payload){
var error_message ='send_message had no payload, command aborted';
log(error_message);
socket.emit('send_message_response', {
result: 'fail',
message: error_message
});
return;
}
var room = payload.room;
if(('undefined'=== typeof room) || !room){
var error_message ='send_message didn\'t specify a room, command aborted';
log(error_message);
socket.emit('send_message_response', {
result: 'fail',
message: error_message
});
return;
}
var username = players[socket.id].username;
if(('undefined'=== typeof username) || !username){
var error_message ='send_message didn\'t specify a username command aborted';
log(error_message);
socket.emit('send_message_response', {
result: 'fail',
message: error_message
});
return;
}
var message = payload.message;
if(('undefined' === typeof message)|| !message){
var error_message ='send_message didn\'t specify a message command aborted';
log(error_message);
socket.emit('send_message_response', {
result: 'fail',
message: error_message
});
return;
}
var success_data = {
result: 'success',
room: room,
username: username,
message: message
};
io.in(room).emit('send_message_response', success_data);
log('Message sent to room '+ room + ' by ' + username);
});
/* invite command */
/*payload:
{
'requested_user': the socket id of the person to be invited
}
invite_response:
{
'result': 'success',
'socket_id': the socket id of the person being invited
}
or
{
'result':'fail',
'message': failure message
}
invited:
{
'result': 'success',
'socket_id': the socket id of the person being invited
}
or
{
'result':'fail',
'message': failure message
}
*/
socket.on('invite',function(payload){
log('invite with '+JSON.stringify(payload));
/* Check to make sure that a payload was sent */
if(('undefined' === typeof payload) || !payload){
var error_message ='invite had no payload, command aborted';
log(error_message);
socket.emit('invite_response', {
result: 'fail',
message: error_message
});
return;
}
/* Check that the message can be traced to a username */
var username = players[socket.id].username;
if(('undefined'=== typeof username) || !username){
var error_message = 'invite can\'t identify who sent the message';
log(error_message);
socket.emit('invite_response', {
result: 'fail',
message: error_message
});
return;
}
var requested_user = payload.requested_user;
if(('undefined'=== typeof requested_user) || !requested_user){
var error_message ='invite didn\'t specify a requested_user, command aborted';
log(error_message);
socket.emit('invite_response', {
result: 'fail',
message: error_message
});
return;
}
var room = players[socket.id].room;
var roomObject = io.sockets.adapter.rooms[room];
/*Make sure the user being invited is in the room */
if(!roomObject.sockets.hasOwnProperty(requested_user)){
var error_message = 'invite requested a user that wasn\'t in the room, command aborted'
log(error_message);
socket.emit('invite_response',{
result: 'fail',
message: error_message
});
return;
}
/* If everything is okay respond to the inviter that it was successful */
var success_data = {
result: 'success',
socket_id: requested_user
};
socket.emit('invite_response', success_data);
/* Tell the invitee that they have been invited */
var success_data = {
result: 'success',
socket_id: socket.id
};
socket.to(requested_user).emit('invited', success_data);
log('invite successful');
});
/* uninvite command */
/*payload:
{
'requested_user': the socket id of the person to be uninvited
}
invite_response:
{
'result': 'success',
'socket_id': the socket id of the person being uninvited
}
or
{
'result':'fail',
'message': failure message
}
uninvited:
{
'result': 'success',
'socket_id': the socket id of the person doing the uninviting
}
or
{
'result':'fail',
'message': failure message
}
*/
socket.on('uninvite',function(payload){
log('uninvite with '+JSON.stringify(payload));
/* Check to make sure that a payload was sent */
if(('undefined' === typeof payload) || !payload){
var error_message ='uninvite had no payload, command aborted';
log(error_message);
socket.emit('uninvite_response', {
result: 'fail',
message: error_message
});
return;
}
/* Check that the message can be traced to a username */
var username = players[socket.id].username;
if(('undefined'=== typeof username) || !username){
var error_message = 'uninvite can\'t identify who sent the message';
log(error_message);
socket.emit('uninvite_response', {
result: 'fail',
message: error_message
});
return;
}
var requested_user = payload.requested_user;
if(('undefined'=== typeof requested_user) || !requested_user){
var error_message ='uninvite didn\'t specify a requested_user, command aborted';
log(error_message);
socket.emit('uninvite_response', {
result: 'fail',
message: error_message
});
return;
}
var room = players[socket.id].room;
var roomObject = io.sockets.adapter.rooms[room];
/*Make sure the user being invited is in the room */
if(!roomObject.sockets.hasOwnProperty(requested_user)){
var error_message = 'invite requested a user that wasn\'t in the room, command aborted'
log(error_message);
socket.emit('invite_response',{
result: 'fail',
message: error_message
});
return;
}
/* If everything is okay respond to the uninviter that it was successful */
var success_data = {
result: 'success',
socket_id: requested_user
};
socket.emit('uninvite_response', success_data);
/* Tell the uninvitee that they have been uninvited */
var success_data = {
result: 'success',
socket_id: socket.id
};
socket.to(requested_user).emit('uninvited', success_data);
log('uninvite successful');
});
/* game start command */
/*payload:
{
'requested_user': the socket id of the person to play with
}
game_start_response:
{
'result': 'success',
'socket_id': the socket id of the person being uninvited
'game_id': id of the game session
}
or
{
'result':'fail',
'message': failure message
}
*/
socket.on('game_start',function(payload){
log('game_start with '+JSON.stringify(payload));
/* Check to make sure that a payload was sent */
if(('undefined' === typeof payload) || !payload){
var error_message ='game_start had no payload, command aborted';
log(error_message);
socket.emit('game_start_response', {
result: 'fail',
message: error_message
});
return;
}
/* Check that the message can be traced to a username */
var username = players[socket.id].username;
if(('undefined'=== typeof username) || !username){
var error_message = 'game_start can\'t identify who sent the message';
log(error_message);
socket.emit('game_start_response', {
result: 'fail',
message: error_message
});
return;
}
var requested_user = payload.requested_user;
if(('undefined'=== typeof requested_user) || !requested_user){
var error_message ='uninvite didn\'t specify a requested_user, command aborted';
log(error_message);
socket.emit('uninvite_response', {
result: 'fail',
message: error_message
});
return;
}
var room = players[socket.id].room;
var roomObject = io.sockets.adapter.rooms[room];
/*Make sure the user being invited is in the room */
if(!roomObject.sockets.hasOwnProperty(requested_user)){
var error_message = 'gamestart requested a user that wasn\'t in the room, command aborted'
log(error_message);
socket.emit('game_start_response',{
result: 'fail',
message: error_message
});
return;
}
/* If everything is okay respond to the game_starter that it was successful */
var game_id = Math.floor((1+Math.random()) *0x10000).toString(16).substring(1);
var success_data = {
result: 'success',
socket_id: requested_user,
game_id: game_id
};
socket.emit('game_start_response', success_data);
/* Tell the other player to play */
var success_data = {
result: 'success',
socket_id: socket.id,
game_id: game_id
};
socket.to(requested_user).emit('game_start_response', success_data);
log('game_start successful');
});
/* game token start command */
/*payload:
{
'row': 0-7 the row to play the token on
'column': 0-7 the column to play the token on
'color': 'white or black'
}
if successful a sucess message will be followed by a game_update message
play_token_response:
{
'result': 'success',
or
{
'result':'fail',
'message': failure message
}
*/
socket.on('play_token',function(payload){
log('play_token'+JSON.stringify(payload));
/* Check to make sure that a payload was sent */
if(('undefined' === typeof payload) || !payload){
var error_message ='play_token had no payload, command aborted';
log(error_message);
socket.emit('play_token_response', {
result: 'fail',
message: error_message
});
return;
}
/* Check that the player has previously registered */
var player = players[socket.id];
if(('undefined'=== typeof player) || !player){
var error_message = 'server doesn\'t recognize you (try going back one screen)';
log(error_message);
socket.emit('play_token_response', {
result: 'fail',
message: error_message
});
return;
}
var username = players[socket.id].username;
if(('undefined'=== typeof username) || !username){
var error_message = 'play_token can\'t identity who sent the message';
log(error_message);
socket.emit('play_token_response', {
result: 'fail',
message: error_message
});
return;
}
var game_id = players[socket.id].room;
if(('undefined'=== typeof game_id) || !game_id){
var error_message = 'play_token can\'t find oyour game board';
log(error_message);
socket.emit('play_token_response', {
result: 'fail',
message: error_message
});
return;
}
var row = payload.row;
if(('undefined'=== typeof row) || row < 0 || row > 7){
var error_message = 'play_token didn\'t specify a valid row, command aborted';
log(error_message);
socket.emit('play_token_response', {
result: 'fail',
message: error_message
});
return;
}
var column = payload.column;
if(('undefined'=== typeof column) || row < 0 || row > 7){
var error_message = 'play_token didn\'t specify a valid column, command aborted';
log(error_message);
socket.emit('play_token_response', {
result: 'fail',
message: error_message
});
return;
}
var color = payload.color;
if(('undefined'=== typeof color) || (color != 'white' && color !='black')){
var error_message = 'play_token didn\'t specify a valid color, command aborted';
log(error_message);
socket.emit('play_token_response', {
result: 'fail',
message: error_message
});
return;
}
var game = games[game_id];
if(('undefined'=== typeof game) || !game){
var error_message = 'play_token couldn\'t find your game board';
log(error_message);
socket.emit('play_token_response', {
result: 'fail',
message: error_message
});
return;
}
/* If the current attempt at playing a token is out of turn then error */
if(color !== game.whose_turn){
var error_message = 'play_token message played out of turn';
log(error_message);
socket.emit('play_token_response', {
result: 'fail',
message: error_message
});
return;
}
if(color !== game.whose_turn){
var error_message = 'play_token message played out of turn';
log(error_message);
socket.emit('play_token_response', {
result: 'fail',
message: error_message
});
return;
}
/* If the wrong socket is playing the color */
if( ((game.whose_turn === 'white') && (game.player_white.socket !==socket.id)) ||
((game.whose_turn === 'black') && (game.player_black.socket !==socket.id))){
var error_message = 'play_token turn played by wrong player';
log(error_message);
socket.emit('play_token_response', {
result: 'fail',
message: error_message
});
return;
}
/* Send response*/
var success_data = {
result: 'success'
};
socket.emit('play_token_response', success_data);
/* Execute the move */
if(color == 'white'){
game.board[row][column] = 'w';
flip_board('w',row,column,game.board);
game.whose_turn = 'black';
game.legal_moves = calculate_valid_moves('b', game.board);
}
else if(color == 'black'){
game.board[row][column] = 'b';
flip_board('b',row,column,game.board);
game.whose_turn = 'white';
game.legal_moves = calculate_valid_moves('w', game.board);
}
var d = new Date();
game.last_move_time = d.getTime();
send_game_update(socket,game_id, 'played a token');
});
});
/*********************************/
/* Code related to the game state */
var games = [];
function create_new_game(){
var new_game ={};
new_game.player_white ={};
new_game.player_black ={};
new_game.player_white.socket = '';
new_game.player_white.username = '';
new_game.player_black.socket = '';
new_game.player_black.username = '';
var d = new Date();
new_game.last_move_time = d.getTime();
new_game.whose_turn = 'black';
new_game.board = [
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ','w','b',' ',' ',' '],
[' ',' ',' ','b','w',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' ']
];
new_game.legal_moves = calculate_valid_moves('b',new_game.board);
return new_game;
}
/* Check if ther is a color 'who' on the line starting at (r,c) or
*anywhere further by adding dr and dc to (r,c)*/
function check_line_match(who,dr,dc,r,c,board){
if(board[r][c] === who){
return true;
}
if(board[r][c] === ' '){
return false;
}
if( (r+dr < 0) || (r+dr > 7)){
return false;
}
if( (c+dc < 0) || (c+dc > 7)){
return false;
}
return check_line_match(who,dr,dc,r+dr,c+dc,board);
}
/* Check if the position at r,c contains the opposite of 'who' on the board
* and if the line indicated by adding dr to r and dc to c eventually ends in
* the who color */
function valid_move(who, dr,dc,r,c,board){
var other;
if(who === 'b'){
other = 'w';
}
else if(who === 'w'){
other ='b';
}
else{
log('Houston we have a color problem: '+who);
return false;
}
if( (r+dr < 0) || (r+dr > 7)){
return false;
}
if( (c+dc < 0) || (c+dc > 7)){
return false;
}
if(board[r+dr][c+dc] != other){
return false;
}
if( (r+dr+dr < 0) || (r+dr+dr > 7)){
return false;
}
if( (c+dc+dc < 0) || (c+dc+dc > 7)){
return false;
}
return check_line_match(who,dr,dc,r+dr+dr,c+dc+dc,board);
}
function calculate_valid_moves(who,board){
var valid = [
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' ']
];
for(row = 0; row <8;row++){
for(var column = 0; column <8;column++){
if(board[row][column] === ' '){
nw = valid_move(who,-1,-1,row,column,board);
nn = valid_move(who,-1,0,row,column,board);
ne = valid_move(who,-1,1,row,column,board);
ww = valid_move(who,0,-1,row,column,board);
ee = valid_move(who,0,1,row,column,board);
sw = valid_move(who,1,-1,row,column,board);
ss = valid_move(who,1,0,row,column,board);
se = valid_move(who,1,1,row,column,board);
if( nw || nn || ne || ww || ee || sw || ss || se){
valid[row][column] = who;
}
}
}
}
return valid;
}
function flip_line(who,dr,dc,r,c,board){
if( (r+dr < 0) || (r+dr > 7) ){
return false;
}
if( (c+dc < 0) || (c+dc > 7) ){
return false;
}
if(board[r+dr][c+dc] === ' '){
return false;
}
if(board[r+dr][c+dc] === who){
return true;
}
else{
if(flip_line(who,dr,dc,r+dr,c+dc,board)){
board[r+dr][c+dc] = who;
return true;
}
else{
return false;
}
}
}
function flip_board(who,row,column,board){
flip_line(who,-1,-1,row,column,board);
flip_line(who,-1,0,row,column,board);
flip_line(who,-1,1,row,column,board);
flip_line(who,0,-1,row,column,board);
flip_line(who,0,1,row,column,board);
flip_line(who,1,-1,row,column,board);
flip_line(who,1,0,row,column,board);
flip_line(who,1,1,row,column,board);
}
function send_game_update(socket, game_id, message){
/* Check to see if ag ame with game_id already exists */
if(('undefined' === typeof games[game_id]) || !games[game_id]){
/* No game exists, so make one */
console.log('No game exists. Creating '+game_id+' for '+socket.id);
games[game_id] = create_new_game();
}
/* Make sure that only 2 people are in the game room */
var roomObject;
var numClients;
do{
roomObject = io.sockets.adapter.rooms[game_id];
numClients = roomObject.length;
if(numClients > 2){
console.log('Too many clients in room:'+game_id+' #:'+numClients);
if(games[game_id].player_white.socket == roomObject.sockets[0]){
game[game_id].player_white.socket = '';
game[game_id].player_white.username = '';
}
if(games[game_id].player_black.socket == roomObject.sockets[0]){
game[game_id].player_black.socket = '';
game[game_id].player_black.username = '';
}
/* kick one of the extra people out */
var sacrifice = Object.keys(roomObject.sockets)[0];
io.of('/').connected[sacrifice].leave(game_id);
}
}
while((numClients-1) > 2);
/* Assign this socket a color */
/* If the current player isn't assigned a color */
if((games[game_id].player_white.socket !=socket.id) && (games[game_id].player_black.socket !=socket.id)){
console.log('Player isn\'t assigned a color: '+socket.id);
/* and there isn't a color to give them */
if((games[game_id].player_black.socket != '') && (games[game_id].player_white.socket != '')){
games[game_id].player_white.socket = '';
games[game_id].player_white.username ='';
games[game_id].player_black.socket = '';
games[game_id].player_black.username ='';
}
}
/* Assign colors to the players if not already done */