-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
216 lines (185 loc) · 5.34 KB
/
app.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
'use strict';
/**
* Multiplayer Tic-Tac-Toe game using react and socketio libraries.
*/
var express = require('express'),
app = express(),
server = require('http').Server(app),
io = require('socket.io')(server),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
_ = require('underscore'),
games = [],
players = [],
numPlayers = 0,
gamesCount = 0,
playersWaiting = [];
/**
* CONFIGURING EXPRESS
*/
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));
app.use(bodyParser.json({ limit: '50mb' }));
app.use(methodOverride());
/**
* ROUTES
*/
app.get('/', function(req, res, next){
res.render('index.ejs');
});
io.on('connection', function (socket){
var addedPlayer = false,
room = null;
// New player
socket.on('add player', function (playerName) {
console.log(playerName + ' added to players list');
socket.playerName = playerName;
// Add the client's player name to the global list
players.push({
playerName: playerName,
socketID: socket.id
});
++numPlayers;
if(playersWaiting.length > 0){
// Not available anymore!
socket.leave('available');
playersWaiting = _.without(playersWaiting, socket.id);
// Joining game room
room = 'game-' + gamesCount;
socket.emit('join game', room);
var opponent = io.sockets.connected[playersWaiting.splice(0,1)];
opponent.emit('join game', room);
}else{
socket.room = 'available';
socket.join('available');
playersWaiting.push(socket.id);
console.log(socket.playerName + ' joined the available room');
}
});
// Join Game Room
socket.on('join room', function(room){
console.log(socket.playerName + ' joining ' + room);
var game = {
turn : 'X',
score : { X: 0, O: 0 },
moves : 0,
tiles : {
1: '',
2: '',
4: '',
8: '',
16: '',
32: '',
64: '',
128: '',
256: ''
},
wins : [7, 56, 448, 73, 146, 292, 273, 84],
players : [],
id: room
};
// Leaving room if in one.
if('room' in socket){
// If leaving from available room, remove player from available list
if(socket.room === 'available'){
playersWaiting = _.without(playersWaiting, socket.id);
}
socket.leave(socket.room);
}
if(room === 'available'){
// find if there is a player available and join him by creating a game
if(playersWaiting.length > 0){
// Joining game room
room = 'game-' + gamesCount;
socket.join(room);
socket.room = room;
game.players.push(socket.playerName);
socket.emit('joined room', game);
game.id = room;
games.push(game);
++gamesCount;
var opponent = io.sockets.connected[playersWaiting.splice(0,1)];
opponent.emit('join game', room);
}else{
playersWaiting.push(socket.id);
socket.join(room);
socket.room = room;
}
}else{
var gameFound = false;
// If game exists, join that game.
for(var i = 0; i < games.length; i++){
if(games[i].id === room){
game = games[i];
gameFound = true;
break;
}
}
// Add playerName
game.players.push(socket.playerName);
socket.join(room);
socket.room = room;
if(gameFound){
socket.emit('joined room', game);
io.to(socket.room).emit('joined room', game);
}else{
game.id = 'game-' + gamesCount;
socket.emit('joined room', game);
io.to(socket.room).emit('joined room', game);
games.push(game);
++gamesCount;
}
}
});
// Player move
socket.on('move', function(state){
io.to(socket.room).emit('move', state);
});
// Leave room
socket.on('leave room', function(){
socket.leave(socket.room);
});
// End Game
socket.on('end game', function(result){
console.log('Game ended: ' + result);
io.sockets.in(socket.room).emit('end game', result);
socket.leave(socket.room);
});
// when the user disconnects.. perform this
socket.on('disconnect', function () {
console.log('*********** Player Left ****************');
console.log(socket.id + ' | ' + socket.playerName);
console.log('****************************************');
// Remove player from available list
playersWaiting = _.without(playersWaiting, socket.id);
// If player has joined a room.
if('room' in socket){
// if available room
if(socket.room !== 'available'){
// Remove game from games list
for(var i = 0; i < games.length; i++){
if(socket.room === games[i].id){
games.splice(i,1);
break;
}
}
// Tell opponent you left
io.to(socket.room).emit('player left');
}
}
// Remove player from global players list
for(var i = 0; i < players.length; i++){
if(players[i].playerName === socket.playerName){
players.splice(i,1);
--numPlayers;
break;
}
}
});
});
// Starting server
server.listen(3003, function(){
console.log('Server started at http://localhost:3003');
});