-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
435 lines (385 loc) · 13.7 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
//get external packages
var fs = require("fs");
var path = require("path");
var express = require("express");
var exphbs = require("express-handlebars");
var socket = require("socket.io");
const localtunnel = require('localtunnel');
//set up express
var app = express();
const port = process.env.PORT || 3000;
//initialize express-handlebars with no main layout and "/views/partials" as the partials directory
app.engine("handlebars", exphbs({ defaultLayout: null, partialsDir: [path.join(__dirname + "/views/partials")]}));
app.set("view engine", "handlebars");
//initialize localtunnel
var Gameurl = "null";
(async () =>
{
const tunnel = await localtunnel({ port: port });
Gameurl = tunnel.url;
console.log("url for the game:", tunnel.url)
tunnel.on('close', () => {
// tunnels are closed
});
})();
//middleware functions
//if the client asks for one of the rooms, give them the most updated version of that board
app.get("/room:n", function(req, res, next)
{
var n = parseInt(req.params.n);
if(n !== NaN && n >= 1 && n <= 4)
{
var dataToSend = {url: Gameurl, roomNum: n};
for(const [key, value] of Object.entries(rooms[n - 1].boardData))
{
dataToSend[key] = value;
}
res.status(200).render("room", dataToSend);
return;
}
next();
});
//send everything in the public directory by name
app.use(express.static("public"));
//if the user asks for the homepage, give them the home page
app.get("/", function(req, res)
{
res.status(200).sendFile(path.join(__dirname + "/html/MainChess.html"));
});
//if the user asks for the creators page
app.get("/Creators", function(req, res)
{
res.status(200).sendFile(path.join(__dirname + "/html/Creators.html"));
});
//if the user asks for the how to play page
app.get("/HowToPlay", function(req, res)
{
res.status(200).sendFile(path.join(__dirname + "/html/HowToPlay.html"));
});
//if the user asks for the room view page
app.get("/Rooms", function(req, res)
{
res.status(200).render("rooms", {url: Gameurl});
});
//tell the server to begin listening
var server = app.listen(port, function()
{
console.log("server is now set up on port", port);
});
//game variables used by socket, different for each room
var rooms = [
{
//room1
numPeopleInRoom: 0,
player1id: "",
player2id: "",
spectatorQueue: [],
whoseTurn: -1,
boardData: {}
},
{
//room2
numPeopleInRoom: 0,
player1id: "",
player2id: "",
spectatorQueue: [],
whoseTurn: -1,
boardData: {}
},
{
//room3
numPeopleInRoom: 0,
player1id: "",
player2id: "",
spectatorQueue: [],
whoseTurn: -1,
boardData: {}
},
{
//room4
numPeopleInRoom: 0,
player1id: "",
player2id: "",
spectatorQueue: [],
whoseTurn: -1,
boardData: {}
}
];
//fill the data for all the boards at the initial state of the game
var initialBoardData = JSON.parse(fs.readFileSync("initialBoardState.json"));
for(var i = 0; i < rooms.length; i++)
{
rooms[i].boardData = initialBoardData;
}
//an array of all people looking at the room view
var roomViewids = [];
//initialize socket.io
var io = socket(server);
//function to call when the game ends, it's here because it happens on multiple occasions
function endGame(data)
{
//tell all players and spectators in current room who won
io.to(rooms[data.roomNum - 1].player1id).emit("game-over", {winner: data.winner}); //tell player 1 the game is over
if(rooms[data.roomNum - 1].player2id !== "")
{
io.to(rooms[data.roomNum - 1].player2id).emit("game-over", {winner: data.winner}); //tell player 2 the game is over
}
for(var i = 0; i < rooms[data.roomNum - 1].spectatorQueue.length; i++)
{
io.to(rooms[data.roomNum - 1].spectatorQueue[i]).emit("game-over", {winner: data.winner}); //spectators the game is over
}
//move the current players 1 and 2 back to the end of the queue, the winner first
if(data.winner === 1) //player 1 won
{
rooms[data.roomNum - 1].spectatorQueue.push(rooms[data.roomNum - 1].player1id);
rooms[data.roomNum - 1].player1id = "";
if(rooms[data.roomNum - 1].player2id !== "") //do extra check just in case player 2 left
{
rooms[data.roomNum - 1].spectatorQueue.push(rooms[data.roomNum - 1].player2id);
rooms[data.roomNum - 1].player2id = "";
}
}
else //player 2 won
{
if(rooms[data.roomNum - 1].player2id !== "") //do extra check just in case player 2 left
{
rooms[data.roomNum - 1].spectatorQueue.push(rooms[data.roomNum - 1].player2id);
rooms[data.roomNum - 1].player2id = "";
}
if(rooms[data.roomNum - 1].player1id !== "") //do extra check just in case player 1 left
{
rooms[data.roomNum - 1].spectatorQueue.push(rooms[data.roomNum - 1].player1id);
rooms[data.roomNum - 1].player1id = "";
}
}
//pop the next two players off the front of the queue, and make them players 1 and 2
if(rooms[data.roomNum - 1].spectatorQueue.length > 0) //do extra check since there's no one to make player 1
{
rooms[data.roomNum - 1].player1id = rooms[data.roomNum - 1].spectatorQueue.shift();
}
if(rooms[data.roomNum - 1].spectatorQueue.length > 0) //do extra check since there's no one else to make player 2
{
rooms[data.roomNum - 1].player2id = rooms[data.roomNum - 1].spectatorQueue.shift();
}
//function to set up the next game, to be called after 5 seconds
function restartGame()
{
//tell player1 and player2 that they're player1 and player2
if(rooms[data.roomNum - 1].player1id !== "")
{
io.to(rooms[data.roomNum - 1].player1id).emit("player-join-game", {playerNum: 1});
}
if(rooms[data.roomNum - 1].player2id !== "") //do extra check just in case there is no player 2
{
io.to(rooms[data.roomNum - 1].player2id).emit("player-join-game", {playerNum: 2});
}
//tell the spectators that they're spectators
for(var i = 0; i < rooms[data.roomNum - 1].spectatorQueue.length; i++)
{
io.to(rooms[data.roomNum - 1].spectatorQueue[i]).emit("player-join-game", {playerNum: 0});
}
updateQueuePositions(data.roomNum); //tell spectators their updated queue positions
//reset everyone's boards
rooms[data.roomNum - 1].boardData = initialBoardData;
if(rooms[data.roomNum - 1].player1id !== "")
{
io.to(rooms[data.roomNum - 1].player1id).emit("update-board", {board: rooms[data.roomNum - 1].boardData}); //update player 1's board
}
if(rooms[data.roomNum - 1].player2id !== "")
{
io.to(rooms[data.roomNum - 1].player2id).emit("update-board", {board: rooms[data.roomNum - 1].boardData}); //update player 2's board
}
for(var i = 0; i < rooms[data.roomNum - 1].spectatorQueue.length; i++)
{
io.to(rooms[data.roomNum - 1].spectatorQueue[i]).emit("update-board", {board: rooms[data.roomNum - 1].boardData}); //update spectator's boards
}
//tell player1 and player 2 to begin playing
if(rooms[data.roomNum - 1].player1id !== "" && rooms[data.roomNum - 1].player2id !== "") //if there is a player1 and player2
{
io.to(rooms[data.roomNum - 1].player1id).emit("begin-game", {whoseTurn: 1});
io.to(rooms[data.roomNum - 1].player2id).emit("begin-game", {whoseTurn: 1});
rooms[data.roomNum - 1].whoseTurn = 1;
}
else if(rooms[data.roomNum - 1].player1id !== "") //if there's a player1 but not a player2
{
io.to(rooms[data.roomNum - 1].player1id).emit("begin-game", {whoseTurn: -1});
rooms[data.roomNum - 1].whoseTurn = -1;
}
}
setTimeout(restartGame, 5000);
}
//function to tell everyone in the queue their position
function updateQueuePositions(roomNum)
{
for(var i = 0; i < rooms[roomNum - 1].spectatorQueue.length; i++)
{
//loop through spectatorQueue and tell each one their updated position in the queue
io.to(rooms[roomNum - 1].spectatorQueue[i]).emit("queue-position", {position: i + 1});
}
}
//function to update the capacities of the rooms to all the people on the room view page
function updateRoomCapacities()
{
//calculate the number of people in each room by looping through every room
var roomCounts = [0, 0, 0, 0];
for(var i = 0; i < rooms.length; i++)
{
if(rooms[i].player1id !== "")
{
roomCounts[i]++;
}
if(rooms[i].player2id !== "")
{
roomCounts[i]++;
}
roomCounts[i] += rooms[i].spectatorQueue.length;
}
//for every person on the room view page
for(var i = 0; i < roomViewids.length; i++)
{
io.to(roomViewids[i]).emit("update-room-capacities", {roomCounts: roomCounts});
}
}
//all the socket.io functions to do when someone joins
io.on("connection", function(socket)
{
//function to run when someone joins the room view page
socket.on("room-view-join", function(data)
{
roomViewids.push(socket.id); //push their id in the room view page
//emit to all room view people how many people there are in each room
updateRoomCapacities();
});
//function to run right when the player connects (the client calls enter-room as soon as it connects)
socket.on("enter-room", function(data)
{
//tell this player what player they are
if(rooms[data.roomNum - 1].numPeopleInRoom == 0)
{
//this player is player 1
console.log("player 1 has joined room", data.roomNum, socket.id);
rooms[data.roomNum - 1].player1id = socket.id;
io.to(rooms[data.roomNum - 1].player1id).emit("player-join-game", {playerNum: 1});
rooms[data.roomNum - 1].numPeopleInRoom++;
//send a code to tell player 1 to wait for a player 2 to join
io.to(rooms[data.roomNum - 1].player1id).emit("begin-game", {whoseTurn: -1});
rooms[data.roomNum - 1].whoseTurn = -1;
}
else if(rooms[data.roomNum - 1].numPeopleInRoom == 1)
{
//this player is player 2
console.log("player 2 has joined room", data.roomNum, socket.id);
rooms[data.roomNum - 1].player2id = socket.id;
io.to(rooms[data.roomNum - 1].player2id).emit("player-join-game", {playerNum: 2});
rooms[data.roomNum - 1].numPeopleInRoom++;
//start the game, tell everyone that it's player 1's turn
io.to(rooms[data.roomNum - 1].player1id).emit("begin-game", {whoseTurn: 1});
io.to(rooms[data.roomNum - 1].player2id).emit("begin-game", {whoseTurn: 1});
rooms[data.roomNum - 1].whoseTurn = 1;
}
else
{
//since there are already too many players in this game, the incoming connection is a spectator
console.log("spectator has joined room", data.roomNum, socket.id);
rooms[data.roomNum - 1].spectatorQueue.push(socket.id);
io.to(socket.id).emit("player-join-game", {playerNum: 0});
//io.to(socket.id).emit("update-board", {board: rooms[data.roomNum - 1].boardData});
updateQueuePositions(data.roomNum);
rooms[data.roomNum - 1].numPeopleInRoom++;
}
updateRoomCapacities();
});
//function to update the board, emitted to every player and spectator
socket.on("update-board", function(data)
{
//update the boardData variable, stored here on the server
rooms[data.roomNum - 1].boardData = data.board;
//update the board for every player and spectator in given room
io.to(rooms[data.roomNum - 1].player1id).emit("update-board", {board: rooms[data.roomNum - 1].boardData});
io.to(rooms[data.roomNum - 1].player2id).emit("update-board", {board: rooms[data.roomNum - 1].boardData});
for(var i = 0; i < rooms[data.roomNum - 1].spectatorQueue.length; i++)
{
io.to(rooms[data.roomNum - 1].spectatorQueue[i]).emit("update-board", {board: rooms[data.roomNum - 1].boardData});
}
});
//function to update the server's version of whose turn it is
socket.on("next-turn", function(data)
{
rooms[data.roomNum - 1].whoseTurn = data.whoseTurn;
//tell every player and spectator in the given room whose turn it is
io.to(rooms[data.roomNum - 1].player1id).emit("next-turn", {whoseTurn: rooms[data.roomNum - 1].whoseTurn});
io.to(rooms[data.roomNum - 1].player2id).emit("next-turn", {whoseTurn: rooms[data.roomNum - 1].whoseTurn});
for(var i = 0; i < rooms[data.roomNum - 1].spectatorQueue.length; i++)
{
io.to(rooms[data.roomNum - 1].spectatorQueue[i]).emit("next-turn", {whoseTurn: rooms[data.roomNum - 1].whoseTurn});
}
});
//function to distribute to everyone who won and who lost
socket.on("game-over", function(data)
{
endGame(data);
});
//function to deal with someone leaving the room
socket.on("disconnect", function(data)
{
//check if this person was on the room view page
for(var i = 0; i < roomViewids.length; i++)
{
if(roomViewids[i] === socket.id) //if we found the client that left as part of the roomViewids array
{
roomViewids.splice(i, 1); //remove them from the array
return; //don't do anything else in this function
}
}
//first try to figure out which room the current user was in when they left
var roomNum;
for(var i = 0; i < 4; i++) //loop through each room, comparing to players and spectators
{
if(rooms[i].player1id === socket.id || rooms[i].player2id === socket.id)
{
//we found socket.id as player1 or player2 in room i
roomNum = i + 1;
break;
}
for(var j = 0; j < rooms[i].spectatorQueue.length; j++)
{
if(rooms[i].spectatorQueue[j] === socket.id)
{
//we found socket.id as a spectator in room i
roomNum = i + 1;
break;
}
}
}
//do special things for each type of player leaving
console.log(socket.id, "left room", roomNum);
if(socket.id === rooms[roomNum - 1].player1id) //player 1 left
{
//declare player 2 as the winner
rooms[roomNum - 1].player1id = "";
endGame({winner: 2, roomNum: roomNum});
}
else if(socket.id === rooms[roomNum - 1].player2id) //player 2 left
{
//declare player 1 as the winner
rooms[roomNum - 1].player2id = "";
endGame({winner: 1, roomNum: roomNum});
}
else //a spectator left
{
//remove them from spectatorQueue
for(var i = 0; i < rooms[roomNum - 1].spectatorQueue.length; i++)
{
if(rooms[roomNum - 1].spectatorQueue[i] === socket.id)
{
rooms[roomNum - 1].spectatorQueue.splice(i, 1);
break;
}
}
updateQueuePositions(roomNum);
}
rooms[roomNum - 1].numPeopleInRoom--;
//update room capacities for people in the rooms page
updateRoomCapacities();
});
});