Skip to content

Commit 0e531f8

Browse files
committed
added more unit tests, fixed bug where godfather could not kill without mafioso decision
1 parent f142257 commit 0e531f8

File tree

8 files changed

+376
-219
lines changed

8 files changed

+376
-219
lines changed

app.ts

+30-41
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,13 @@ const redis = require("redis-server");
7272
const redisServer = new redis(6379);
7373
const grawlix = require("grawlix");
7474
const mysql = require("mysql");
75-
const nodemailer = require("nodemailer");
7675
const bcrypt = require("bcrypt");
7776
const saltNumber = 10;
78-
79-
//details of your email account go here - this needs to be amended for security reasons
80-
var transporter = nodemailer.createTransport({
81-
service: "gmail",
82-
auth: {
83-
user: "youremail@gmail.com",
84-
pass: "yourpassword",
85-
},
86-
});
87-
8877
let uGameid = 0;
8978
let uPlayerid = 0;
9079

9180
let con: any = undefined;
92-
redisServer.open((err: string) => { });
81+
redisServer.open((err: string) => {});
9382

9483
function connectDatabase() {
9584
//Details of your MySQL server go here (don't worry, these aren't my production details.)
@@ -142,11 +131,11 @@ let session = expressSession({
142131
function loginUser(req: any) {
143132
req.session.loggedIn = true;
144133
req.session.username = req.body.username;
145-
req.session.save(() => { });
134+
req.session.save(() => {});
146135
}
147136

148137
//use session cookie in sockets
149-
io.use(function (socket: any, next: any) {
138+
io.use(function(socket: any, next: any) {
150139
session(socket.request, socket.request.res, next);
151140
});
152141

@@ -171,16 +160,16 @@ app.use(
171160
);
172161
app.set("view engine", "pug");
173162
app.use(session);
174-
app.get("/imprint", function (req: any, res: any) {
163+
app.get("/imprint", function(req: any, res: any) {
175164
res.render("imprint");
176165
});
177-
app.get("/about", function (req: any, res: any) {
166+
app.get("/about", function(req: any, res: any) {
178167
res.render("about");
179168
});
180-
app.get("/mobile", function (req: any, res: any) {
169+
app.get("/mobile", function(req: any, res: any) {
181170
res.render("mobile");
182171
});
183-
app.get("/", function (req: any, res: any) {
172+
app.get("/", function(req: any, res: any) {
184173
let gameNames = [];
185174
for (let i = 0; i < server.games.length; i++) {
186175
gameNames.push(server.games[i].name);
@@ -208,7 +197,7 @@ app.get("/", function (req: any, res: any) {
208197
});
209198
});
210199
if (DATABASE && con) {
211-
app.post("/register", function (req: any, res: any) {
200+
app.post("/register", function(req: any, res: any) {
212201
let status = "success";
213202
//run validation
214203
let letters = /^[A-Za-z]+$/;
@@ -222,7 +211,7 @@ if (DATABASE && con) {
222211
let sql =
223212
"SELECT username FROM USERS where username=" +
224213
mysql.escape(req.body.username);
225-
con.query(sql, function (err: any, results: any) {
214+
con.query(sql, function(err: any, results: any) {
226215
if (results.length == 0) {
227216
if (
228217
typeof req.body.email == "string" ||
@@ -261,8 +250,8 @@ if (DATABASE && con) {
261250
}
262251

263252
if (status == "success") {
264-
bcrypt.genSalt(saltNumber, function (err: any, salt: any) {
265-
bcrypt.hash(req.body.password, salt, function (
253+
bcrypt.genSalt(saltNumber, function(err: any, salt: any) {
254+
bcrypt.hash(req.body.password, salt, function(
266255
err: any,
267256
hash: any,
268257
) {
@@ -276,7 +265,7 @@ if (DATABASE && con) {
276265
"," +
277266
mysql.escape(salt) +
278267
")";
279-
con.query(sql, function (err: any, result: any) {
268+
con.query(sql, function(err: any, result: any) {
280269
if (err) throw err;
281270
});
282271
});
@@ -307,7 +296,7 @@ if (DATABASE && con) {
307296
res.send('{ "result":' + JSON.stringify(status) + "}");
308297
}
309298
});
310-
app.post("/#", function (req: any, res: any) {
299+
app.post("/#", function(req: any, res: any) {
311300
let status = "failure";
312301
if (
313302
typeof req.body.username == "string" &&
@@ -316,12 +305,12 @@ if (DATABASE && con) {
316305
let sql =
317306
"SELECT encrypted_password FROM USERS WHERE username=" +
318307
mysql.escape(req.body.username);
319-
con.query(sql, function (err: any, result: any) {
308+
con.query(sql, function(err: any, result: any) {
320309
if (result.length != 0) {
321310
bcrypt.compare(
322311
req.body.password,
323312
result[0].encrypted_password,
324-
function (err: any, comparisonResult: any) {
313+
function(err: any, comparisonResult: any) {
325314
if (comparisonResult == true) {
326315
status = "success";
327316
loginUser(req);
@@ -341,12 +330,12 @@ if (DATABASE && con) {
341330
}
342331
});
343332
}
344-
app.post("/logout", function (req: any, res: any) {
333+
app.post("/logout", function(req: any, res: any) {
345334
req.session.loggedIn = false;
346335
req.session.username = "";
347336
res.send("{}");
348337
});
349-
app.post("/newGame", function (req: any, res: any) {
338+
app.post("/newGame", function(req: any, res: any) {
350339
let result = "success";
351340
if (
352341
typeof req.body.name == "string" &&
@@ -382,13 +371,13 @@ app.post("/newGame", function (req: any, res: any) {
382371
}
383372
res.send('{"result":' + JSON.stringify(result) + "}");
384373
});
385-
app.post("/forgottenPassword", function (req: any, res: any) {
374+
app.post("/forgottenPassword", function(req: any, res: any) {
386375
if (typeof req.body.username == "string") {
387376
//find email in the database matching username 'req.body.username'
388377
let sql =
389378
"SELECT email FROM USERS WHERE username=" +
390379
mysql.escape(req.body.username);
391-
con.query(sql, function (err: any, result: any) {
380+
con.query(sql, function(err: any, result: any) {
392381
if (result.length == 0) {
393382
res.send('{"result":"Your username is incorrect"}');
394383
} else {
@@ -401,13 +390,13 @@ app.post("/forgottenPassword", function (req: any, res: any) {
401390
//then send the email:
402391
}
403392
});
404-
app.get("*.png", function () { });
405-
app.get("*", function (req: any, res: any) {
393+
app.get("*.png", function() {});
394+
app.get("*", function(req: any, res: any) {
406395
res.render("404");
407396
});
408397

409398
//handle socket requests
410-
io.on("connection", function (socket: Socket) {
399+
io.on("connection", function(socket: Socket) {
411400
//set the session unless it is already set
412401
if (!socket.request.session.socketID) {
413402
socket.request.session.socketID = socket.id;
@@ -427,10 +416,10 @@ io.on("connection", function (socket: Socket) {
427416
if (oldPlayerId != undefined) {
428417
thisPlayerId = oldPlayerId;
429418
}
430-
socket.on("reloadClient", function () {
419+
socket.on("reloadClient", function() {
431420
server.reloadClient(thisPlayerId);
432421
});
433-
socket.on("message", function (msg: string) {
422+
socket.on("message", function(msg: string) {
434423
if (typeof msg === "string") {
435424
//exclude commands from filtering (they start with a forward slash):
436425
if (msg[0] === "/") {
@@ -446,19 +435,19 @@ io.on("connection", function (socket: Socket) {
446435
}
447436
}
448437
});
449-
socket.on("leaveGame", function () {
438+
socket.on("leaveGame", function() {
450439
server.leaveGame(thisPlayerId);
451440
});
452-
socket.on("disconnect", function () {
441+
socket.on("disconnect", function() {
453442
server.removeSocketFromPlayer(thisPlayerId, socket);
454443
server.kick(thisPlayerId);
455444
});
456-
socket.on("gameClick", function (gameId: string) {
445+
socket.on("gameClick", function(gameId: string) {
457446
if (parseInt(gameId) != NaN) {
458447
server.gameClick(thisPlayerId, gameId);
459448
}
460449
});
461-
socket.on("localGameClick", function (name: string, gameId: string) {
450+
socket.on("localGameClick", function(name: string, gameId: string) {
462451
server.receive(thisPlayerId, name);
463452
if (
464453
server.getUser(thisPlayerId) != undefined &&
@@ -467,7 +456,7 @@ io.on("connection", function (socket: Socket) {
467456
server.gameClick(thisPlayerId, gameId);
468457
}
469458
});
470-
socket.on("lobbyMessage", function (msg: string) {
459+
socket.on("lobbyMessage", function(msg: string) {
471460
if (typeof msg === "string") {
472461
if (Date.now() - time < 500) {
473462
time = Date.now();
@@ -487,6 +476,6 @@ io.on("connection", function (socket: Socket) {
487476

488477
//listen on port
489478
let port = 8081;
490-
http.listen(port, function () {
479+
http.listen(port, function() {
491480
console.log("Port is:" + port);
492481
});

core/game.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
UserColorArray,
2222
Utils,
2323
} from "./utils";
24-
import { DEBUGMODE } from "../app";
24+
//import { DEBUGMODE } from "../app";
2525

2626
export abstract class Game {
2727
protected endChat: MessageRoom = new MessageRoom();
@@ -73,9 +73,9 @@ export abstract class Game {
7373
license: string,
7474
) {
7575
//debug mode will start game faster
76-
if (DEBUGMODE) {
77-
this._startWait = 10000;
78-
}
76+
//if (DEBUGMODE) {
77+
//this._startWait = 10000;
78+
//}
7979
this._uid = uid;
8080
this._server = server;
8181
this._minPlayerCount = minPlayerCount;
@@ -108,7 +108,7 @@ export abstract class Game {
108108
return this._users;
109109
}
110110
//update is supplied by concrete child class
111-
protected update() { }
111+
protected update() {}
112112
//returns array of usernames and their colors, to put in the lobby chat
113113
get usernameColorPairs(): Array<NameColorPair> {
114114
let usernameColorPairs = [];
@@ -260,10 +260,10 @@ export abstract class Game {
260260
) {
261261
user.send(
262262
"The game will start in " +
263-
Math.floor(
264-
(this.startWait - this.startClock.time) / 1000,
265-
).toString() +
266-
" seconds",
263+
Math.floor(
264+
(this.startWait - this.startClock.time) / 1000,
265+
).toString() +
266+
" seconds",
267267
);
268268
user.send('Type "/start" to start the game immediately');
269269
user.setTime(this.startWait - this.startClock.time, 10000);
@@ -393,7 +393,7 @@ export abstract class Game {
393393
}
394394
}
395395
//to be overridden in child classes as necessary
396-
public customAdminReceive(user: User, msg: string): void { }
396+
public customAdminReceive(user: User, msg: string): void {}
397397
//generic admin commands
398398
public adminReceive(user: User, msg: string): void {
399399
if (user.admin == true && msg[0] == "!") {

0 commit comments

Comments
 (0)