-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
470 lines (402 loc) · 13.2 KB
/
index.js
File metadata and controls
470 lines (402 loc) · 13.2 KB
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
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var bodyParser = require("body-parser");
var validator = require("express-validator");
var session = require("express-session");
var flash = require("connect-flash");
var pg = require("pg");
const crypto = require("crypto");
const {parse} = require("pg-connection-string");
/*var rooms = [
{
title: "Test Title",
type: "jeopardy",
gameCode: "ABCD",
registeredNames: ["Michael"],
state: {},
}
];*/
//pg.defaults.ssl = true;
const dbConfig = Object.assign(parse(process.env.DATABASE_URL), {max: 9});
var pool = new pg.Pool(dbConfig);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(validator());
app.use(session({
secret: "There were times when I thought I'd never live to see the Bulldogs win the Premiership...",
resave: true,
saveUninitialized: false
}));
app.use(flash());
app.use(express.static("icons"));
app.get("/", function(req, res) {
res.sendFile(__dirname + "/public/index.html");
});
app.get("/start", function(req, res) {
res.sendFile(__dirname + "/public/startgame.html");
});
app.post("/host",
function createGameAndRedirect(req, res, next) {
var gameTitle = req.body.gametitle;
// generate four-letter unique code for game
var gameCode = generateGameCode();
// add new room with given details
pool.query("INSERT INTO rooms(gamecode, title, type, registerednames, state, password) VALUES($1,$2,$3,$4,$5,crypt($6, gen_salt($7, 8)))",
[gameCode, gameTitle, req.body.format, [], {}, req.body.password, "bf"], (err) => {
if (err) {
next(err);
} else {
res.send("/" + req.body.format + "/host?gamecode=" + gameCode);
}
}
);
}
);
app.post("/hostresume",
function checkRoomExists(req, res, next) {
var roomCode = req.body.gamecode.toUpperCase();
if (!roomCode || roomCode === "") {
next("blankRoom");
}
pool.query("SELECT * FROM rooms WHERE gameCode = $1", [roomCode], (err, dbRes) => {
if (err) {
next(err);
} else {
if (dbRes.rows.length === 0) {
next("roomNotFound");
} else {
next();
}
}
});
},
function checkPassword(req, res, next) {
pool.query("SELECT * FROM rooms WHERE gameCode = $1 AND password = crypt($2, password)",
[req.body.gamecode.toUpperCase(), req.body.password], (err, dbRes) => {
if (err) {
next(err);
} else {
if (dbRes.rows.length === 0) {
next("wrongPassword");
} else {
next();
}
}
}
);
},
function redirect(req, res, next) {
pool.query("SELECT type FROM rooms WHERE gameCode = $1", [req.body.gamecode.toUpperCase()], (err, dbRes) => {
if (err) {
next(err);
} else {
if (dbRes.rows.length === 0) {
next("roomNotFound");
} else {
var type = dbRes.rows[0].type;
res.send("/" + type + "/host?gamecode=" + req.body.gamecode.toUpperCase());
}
}
});
},
function handleErrors(err, req, res) {
res.status(400).send(err);
}
);
app.get("/join", function(req, res) {
res.sendFile(__dirname + "/public/joingame.html");
});
app.post("/play",
function checkRoomExists(req, res, next) {
var roomCode = req.body.gamecode.toUpperCase();
if (!roomCode || roomCode === "") {
next("blankRoom");
}
pool.query("SELECT * FROM rooms WHERE gameCode = $1", [roomCode], (err, dbRes) => {
if (err) {
next(err);
} else {
if (dbRes.rows.length === 0) {
next("roomNotFound");
} else {
next();
}
}
});
},
function checkNameNotTaken(req, res, next) {
var screenName = req.body.name;
if (screenName === "") {
next("blankScreenName");
} else {
pool.query("SELECT * FROM rooms WHERE gameCode = $1", [req.body.gamecode.toUpperCase()], (err, dbRes) => {
if (err) {
next(err);
} else {
if (dbRes.rows.length === 0) {
next("roomNotFound");
} else {
var registeredNames = dbRes.rows[0].registerednames;
var currentNames = dbRes.rows[0].state.players.map(p => p.screenName);
// check if any other user is using this name (this can include the same person reconnecting)
var preExistingUserArray = registeredNames.filter(function(player) { return player === screenName; });
if (preExistingUserArray.length === 0) {
// no other users with this name, so continue on
next();
} else {
// check if this user last played in the same room with the exact same name (i.e. left and came back in)
var matchingString = dbRes.rows[0].gameCode + "|" + screenName;
if (req.session.lastRoomAndName === matchingString) {
if (currentNames.some(p => p.screenName == screenName)) {
next();
} else {
// TODO user has entered once, had their screen name changed, left, and tried
// to enter again using their original username.
// This will now result in them being locked out, which should at least prevent
// malicious circumventing of the change of username, but there has to be a better
// way to handle this
next("deadUsernameAttempt");
}
// if they didn't, reject them
} else {
next("screenNameTaken");
}
}
}
}
});
}
},
function assignCookieAndRedirect(req, res, next) {
req.session.lastRoomAndName = req.body.gamecode + "|" + req.body.name;
pool.query("SELECT type FROM rooms WHERE gameCode = $1", [req.body.gamecode.toUpperCase()], (err, dbRes) => {
if (err) {
next(err);
} else {
if (dbRes.rows.length === 0) {
next("roomNotFound");
} else {
var type = dbRes.rows[0].type;
res.send("/" + type + "/play?gamecode=" + req.body.gamecode.toUpperCase() + "&name=" + req.body.name);
}
}
});
},
function handleErrors(err, req, res) {
console.log("handleErrors called: " + err);
res.status(400).send(err);
}
);
app.get("/displaygame", function(req, res) {
res.sendFile(__dirname + "/public/displaygame.html");
});
app.post("/display",
function checkRoomExists(req, res, next) {
var roomCode = req.body.gamecode.toUpperCase();
if (!roomCode || roomCode === "") {
next("blankRoom");
}
pool.query("SELECT * FROM rooms WHERE gameCode = $1", [roomCode], (err, dbRes) => {
if (err) {
next(err);
} else {
if (dbRes.rows.length === 0) {
next("roomNotFound");
} else {
next();
}
}
});
},
function redirect(req, res, next) {
var roomCode = req.body.gamecode.toUpperCase();
pool.query("SELECT type FROM rooms WHERE gameCode = $1", [roomCode], (err, dbRes) => {
if (err) {
next(err);
} else {
if (dbRes.rows.length === 0) {
next("roomNotFound");
} else {
var type = dbRes.rows[0].type;
res.send("/" + type + "/display?gamecode=" + roomCode);
}
}
});
},
function handleErrors(err, req, res) {
res.status(400).send(err);
}
);
app.get("/minigames", function(req, res) {
res.sendFile(__dirname + "/public/minigames.html");
});
app.get("/:game/host", function(req, res) {
res.sendFile(__dirname + "/public/" + req.params.game + "/host.html");
});
app.get("/:game/play", function(req, res) {
res.sendFile(__dirname + "/public/" + req.params.game + "/player.html");
});
app.get("/:game/display", function(req, res) {
res.sendFile(__dirname + "/public/" + req.params.game + "/display.html");
});
app.use(express.static("public"));
app.use(express.static("icons"));
io.on("connection", function(socket) {
socket.on("join request", function(info) {
var gameCode = "code unknown";
var screenName = "screen name unknown";
gameCode = info.gameCode.toUpperCase();
screenName = info.screenName;
try {
pool.query("SELECT * FROM rooms WHERE gameCode = $1", [gameCode], (err, dbRes) => {
if (err) {
socket.emit("room not found");
} else {
if (dbRes.rows.length === 0) {
socket.emit("room not found");
} else {
const room = dbRes.rows[0];
var id;
// check if this user is reconnecting from earlier
var preExistingUserArray = room.registerednames.filter(function(player) { return player === screenName; });
if (preExistingUserArray.length === 0) {
// Generate random ID for player
id = crypto.randomBytes(8).toString("hex");
// Safeguard against the same random ID being generated twice in the same game
while (room.state.players.some(p => p.id === id)) {
id = crypto.randomBytes(8).toString("hex");
}
room.registerednames.push(screenName);
pool.query("UPDATE rooms SET registerednames = $1 WHERE gamecode = $2", [room.registerednames, room.gamecode]);
socket.broadcast.to(room.gamecode).emit("new player", {screenName: screenName, id: id});
socket.join(gameCode);
socket.emit("accepted", {state: room.state, id: id});
} else {
if (room.state.players.some(p => p.screenName == screenName)) {
id = room.state.players.find(p => p.screenName == screenName).id;
socket.emit("accepted", {state: room.state, id: id});
socket.join(gameCode);
} else {
// TODO user has entered once, had their screen name changed, left, and tried
// to enter again using their original username.
// This will now result in them being locked out, which should at least prevent
// malicious circumventing of the change of username, but there has to be a better
// way to handle this
socket.emit("dead username attempt");
}
}
socket.on("send answer", function(details) {
var answer = {
player: id,
answer: details.submittedAnswer,
time: details.timeTaken,
};
socket.broadcast.to(room.gamecode).emit("new answer", answer);
});
socket.on("send message to host", function(details) {
socket.broadcast.to(room.gamecode).emit("new message", {
player: id,
details: details
});
});
socket.on("disconnect", function() {
console.log("User " + id + " disconnected from room " + gameCode);
});
}
}
});
} catch (err) {
socket.emit("room not found");
}
});
socket.on("display request", function(details) {
var gameCode = "code unknown";
gameCode = details.gameCode.toUpperCase();
console.log("Display Request: code " + gameCode);
pool.query("SELECT * FROM rooms WHERE gameCode = $1", [gameCode], (err, dbRes) => {
if (err) {
console.log(err);
} else {
if (dbRes.rows.length === 0) {
console.log("room not found");
} else {
const room = dbRes.rows[0];
socket.join(gameCode);
socket.emit("accepted");
socket.emit("new game state", room.state);
socket.on("set state", function(newState) {
for (var propertyName in newState) {
if (newState.hasOwnProperty(propertyName)) {
room.state[propertyName] = newState[propertyName];
}
}
console.log("new game state from display:");
console.log(room.state);
pool.query("UPDATE rooms SET state = $1 WHERE gamecode = $2", [room.state, room.gamecode]);
socket.broadcast.to(details.gameCode).emit("new game state", room.state);
});
}
}
});
});
socket.on("host request", function(details) {
try {
pool.query("SELECT * FROM rooms WHERE gamecode = $1", [details.gameCode], (err, dbRes) => {
if (err) {
console.log(err);
} else {
if (dbRes.rows.length === 0) {
console.log("room not found");
} else {
const thisRoom = dbRes.rows[0];
console.log(thisRoom);
socket.join(details.gameCode);
// send the host back the details of this game
socket.emit("game details", {
gameTitle: thisRoom.title,
gameCode: details.gameCode,
gameSettings: thisRoom.settings,
gameState: thisRoom.state
});
socket.on("send private message", function(messageDetails) {
socket.broadcast.to(details.gameCode).emit("private message", messageDetails);
});
socket.on("set state", function(newState) {
for (var propertyName in newState) {
if (newState.hasOwnProperty(propertyName)) {
thisRoom.state[propertyName] = newState[propertyName];
}
}
pool.query("UPDATE rooms SET state = $1 WHERE gamecode = $2", [thisRoom.state, thisRoom.gamecode]);
socket.broadcast.to(details.gameCode).emit("new game state", thisRoom.state);
});
socket.on("end game", function() {
console.log("host has ended game in room " /* + gameCode */);
// send message to all players that game has ended
socket.broadcast.to(details.gameCode).emit("game over", {});
// TODO remove this room from list
});
socket.on("play sound", function(id) {
socket.broadcast.to(details.gameCode).emit("play sound", id);
});
socket.on("update settings", function(newSettings) {
thisRoom.settings = newSettings;
});
}
}
});
} catch (err) {
socket.emit(err);
}
});
});
var listenPort = process.env.PORT || 3000;
http.listen(listenPort, function() {
console.log("SocketBuzzer server is listening on port " + listenPort);
});
var s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function generateGameCode() {
return Array(4).join().split(",").map(function() { return s.charAt(Math.floor(Math.random() * s.length)); }).join("");
}