-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
279 lines (238 loc) · 9.51 KB
/
index.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
/**
* PONGre Server Based PONG Implementation
* © 2018 Chiraag Bangera.
* This file contains all the Server side Networking code and also contains the main server side logic for the game.
*/
const express = require('express')
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const serverIP = require('ip');
// we are defining the port for our server to listen on
let PORT = process.env.PORT || 8080;
app.use(express.static('public'))
// A fix for the CORS Policy
io.set('origins', '*:*');
console.log("Initialized Server on port: " + PORT);
server.listen(PORT);
// this array stores all socket information pk: id
let sockets = [];
// this array stores all the connected client information pk: id
let clients = [];
// this array stores all the players information pk: id
let players = [];
// this array stores all the game sessions pk:id fk: client.id
let sessions = [];
let socketCount = 0;
let clientCount = 0;
let sessionCount = 0;
//incomming connection from a client
io.on('connection', function (socket) {
var clientIp = socket.request.connection.remoteAddress;
// a new Socket connection is opened
console.log('Connection request from ' + clientIp + ':' + socket.request.connection.remotePort);
// Connect request from client
socket.on('connectToServer', function (obj) {
// username validation
if (!validateNAME(obj.name)) {
sendError("Username is Invalid. Try again. No spaces and special characters Allowed.")
return;
}
let client = { id: clientCount++, ip: clientIp, name: obj.name };
clients.push(client);
sockets.push({ id: socketCount++, socket: socket, clientID: client.id });
// Sending handshake confirmation with necessary details
socket.emit('connected', { message: "Connected to Game Server", clientid: client.id, ip: serverIP.address(), clientip: client.ip, name: client.name });
// Sending updates to all cients but connecting client
updateClients();
console.log("Client# " + client.id + " Connected Successfully with IP: " + client.ip);
});
// when the client specifically requests for updates
socket.on('updates', function () {
console.log("Specific Request from Client for Updates");
socket.emit('update', { message: "Client list update from Game Server", clients: clients, players: players });
});
// Client hits play against another opponent
socket.on('startGame', function (data) {
let p1;
let p2;
let p1Socket;
let p2Socket;
// we need to move our two playing clients off from the available list to the playing list ie. clients array to players array
p1 = clientObjForID(data.client.id, true);
p2 = clientObjForID(data.opponent.id, true);
players.push(p1);
players.push(p2);
p1Socket = socketObjForClientWithID(p1.id).socket;
p2Socket = socketObjForClientWithID(p2.id).socket;
// we create a new session object with the required data
let session = { id: sessionCount++, p1: p1, p2: p2, p1score: 0, p2score: 0, p1ID: 0, p2ID: 1, pos: { ball: { x: 0.5, y: 0.5 }, paddles: [{ x: 0, y: 0.5 }, { x: 1, y: 0.5 }] }, p1Socket: p1Socket, p2Socket: p2Socket };
// client copy of the server session that we will be sending to both players
let syncData = { id: session.id, p1: session.p1, p2: session.p2, p1score: session.p1score, p2score: session.p2score, p1ID: session.p1ID, p2ID: session.p2ID, pos: session.pos };
// every client waiting for a game gets a new updated clients database and also a list of players currently active in a session
updateClients();
// we start the session for our two active players
p1Socket.emit('play', { session: syncData });
p2Socket.emit('play', { session: syncData });
// we store our session into our master database
sessions.push(session);
console.log("Starting Game Session#: " + session.id + " with " + session.p1.id + " and " + session.p2.id);
});
// here is where we sync the game with our two connected players
socket.on('sync', function (moveData) {
let session;
// First we find the game session where our client is playing
session = sessionObjForID(moveData.id, true);
if(!session){
return;
}
session.pos = moveData.pos;
if(moveData.gID == 0){
if (session.pos.ball.x < -0.1 || session.pos.ball.x > 1.1) {
if (session.pos.ball.x < -0.1) {
session.p2score++;
}
else if (session.pos.ball.x > 1.1) {
session.p1score++;
}
session.p1Socket.emit('reset');
}
}
// we synchronize the grid data from the client
let syncData = { id: session.id, p1: session.p1, p2: session.p2, p1score: session.p1score, p2score: session.p2score, p1ID: session.p1ID, p2ID: session.p2ID, pos: session.pos };
session.p1Socket.emit('syncClient', { session: syncData });
session.p2Socket.emit('syncClient', { session: syncData });
sessions.push(session);
});
// Logic to handle client disconnects
socket.on('disconnect', function () {
let removedClient;
let disconnectedSocket;
let session;
// get socket details
disconnectedSocket = socketObjForSocket(socket, true);
if (!disconnectedSocket) {
return;
}
// finding out if they quit from an active session
removedClient = playerObjForID(disconnectedSocket.clientID, true);
// if a player is removed find his session details
if (removedClient) {
console.log("Player with Client# " + removedClient.id + " with IP: " + removedClient.ip + " Got disconnected ");
session = sessionObjForClientWithID(removedClient.id, true);
}
// If session details found we do the appropriate updates to the database
if (session) {
// we transfer the opponent to active database and send him updates
let client = playerObjForID(removedClient.id === session.p1.id ? session.p2.id : session.p1.id, true);
clients.push(client);
socketObjForClientWithID(client.id).socket.emit('end', { p1score: session.p1score, p2score: session.p2score });
updateClients();
return;
};
// we do 2 things here if a player not in a game quit we just remove him and update the other clients with new databse
// if an active player had quit then we just need to update the clients cause we did the processing above
removedClient = clientObjForID(disconnectedSocket.clientID, true);
console.log("Client# " + removedClient.id + " with IP: " + removedClient.ip + " disconnected from server");
updateClients();
});
socket.on('error', function (e) {
console.log("Socket Error" + e);
})
function sendError(message) {
console.log("Sending error Message");
socket.emit('errorOccured', { message: message });
}
});
function updateClients() {
clientBroadcast('update', { message: "Client list update from Game Server", clients: clients, players: players });
}
function clientBroadcast(trigger, obj) {
console.log("Client Broadcasting");
clients.forEach(client => {
socketObjForClientWithID(client.id).socket.emit(trigger, obj);
})
}
function clientObjForID(id, splice = false) {
let client;
clients.forEach(obj => {
if (obj.id === id) {
if (splice) {
client = clients.splice(clients.indexOf(obj), 1)[0];
}
client = obj;
}
});
return client;
}
function playerObjForID(id, splice = false) {
let player;
players.forEach(obj => {
if (obj.id === id) {
if (splice) {
player = players.splice(players.indexOf(obj), 1)[0];
}
player = obj;
}
});
return player;
}
function sessionObjForID(id, splice = false) {
let session;
sessions.forEach(obj => {
if (obj.id === id) {
if (splice) {
session = sessions.splice(sessions.indexOf(obj), 1)[0];
}
session = obj;
}
});
return session;
}
function sessionObjForClientWithID(id, splice = false) {
let session;
sessions.forEach(obj => {
if (obj.p1.id === id || obj.p2.id === id) {
if (splice) {
session = sessions.splice(sessions.indexOf(obj), 1)[0];
}
session = obj;
}
});
return session;
}
function socketObjForClientWithID(id, splice = false) {
let socketObj;
sockets.forEach(obj => {
if (obj.clientID === id) {
if (splice) {
socketObj = sockets.splice(sockets.indexOf(obj), 1)[0];
}
socketObj = obj;
}
});
return socketObj;
}
function socketObjForSocket(socket, splice = false) {
let socketObj;
sockets.forEach(obj => {
if (obj.socket === socket) {
if (splice) {
socketObj = sockets.splice(sockets.indexOf(obj), 1)[0];
}
socketObj = obj;
}
});
return socketObj;
}
function validateNAME(str) {
if (str == undefined) {
return false;
}
str = str.trim();
var usernameRegex = /^[\wa-zA-Z]{3,12}$/;
if (str.match(usernameRegex)) {
return true;
}
return false;
}