@@ -72,24 +72,13 @@ const redis = require("redis-server");
72
72
const redisServer = new redis ( 6379 ) ;
73
73
const grawlix = require ( "grawlix" ) ;
74
74
const mysql = require ( "mysql" ) ;
75
- const nodemailer = require ( "nodemailer" ) ;
76
75
const bcrypt = require ( "bcrypt" ) ;
77
76
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
-
88
77
let uGameid = 0 ;
89
78
let uPlayerid = 0 ;
90
79
91
80
let con : any = undefined ;
92
- redisServer . open ( ( err : string ) => { } ) ;
81
+ redisServer . open ( ( err : string ) => { } ) ;
93
82
94
83
function connectDatabase ( ) {
95
84
//Details of your MySQL server go here (don't worry, these aren't my production details.)
@@ -142,11 +131,11 @@ let session = expressSession({
142
131
function loginUser ( req : any ) {
143
132
req . session . loggedIn = true ;
144
133
req . session . username = req . body . username ;
145
- req . session . save ( ( ) => { } ) ;
134
+ req . session . save ( ( ) => { } ) ;
146
135
}
147
136
148
137
//use session cookie in sockets
149
- io . use ( function ( socket : any , next : any ) {
138
+ io . use ( function ( socket : any , next : any ) {
150
139
session ( socket . request , socket . request . res , next ) ;
151
140
} ) ;
152
141
@@ -171,16 +160,16 @@ app.use(
171
160
) ;
172
161
app . set ( "view engine" , "pug" ) ;
173
162
app . use ( session ) ;
174
- app . get ( "/imprint" , function ( req : any , res : any ) {
163
+ app . get ( "/imprint" , function ( req : any , res : any ) {
175
164
res . render ( "imprint" ) ;
176
165
} ) ;
177
- app . get ( "/about" , function ( req : any , res : any ) {
166
+ app . get ( "/about" , function ( req : any , res : any ) {
178
167
res . render ( "about" ) ;
179
168
} ) ;
180
- app . get ( "/mobile" , function ( req : any , res : any ) {
169
+ app . get ( "/mobile" , function ( req : any , res : any ) {
181
170
res . render ( "mobile" ) ;
182
171
} ) ;
183
- app . get ( "/" , function ( req : any , res : any ) {
172
+ app . get ( "/" , function ( req : any , res : any ) {
184
173
let gameNames = [ ] ;
185
174
for ( let i = 0 ; i < server . games . length ; i ++ ) {
186
175
gameNames . push ( server . games [ i ] . name ) ;
@@ -208,7 +197,7 @@ app.get("/", function (req: any, res: any) {
208
197
} ) ;
209
198
} ) ;
210
199
if ( DATABASE && con ) {
211
- app . post ( "/register" , function ( req : any , res : any ) {
200
+ app . post ( "/register" , function ( req : any , res : any ) {
212
201
let status = "success" ;
213
202
//run validation
214
203
let letters = / ^ [ A - Z a - z ] + $ / ;
@@ -222,7 +211,7 @@ if (DATABASE && con) {
222
211
let sql =
223
212
"SELECT username FROM USERS where username=" +
224
213
mysql . escape ( req . body . username ) ;
225
- con . query ( sql , function ( err : any , results : any ) {
214
+ con . query ( sql , function ( err : any , results : any ) {
226
215
if ( results . length == 0 ) {
227
216
if (
228
217
typeof req . body . email == "string" ||
@@ -261,8 +250,8 @@ if (DATABASE && con) {
261
250
}
262
251
263
252
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 (
266
255
err : any ,
267
256
hash : any ,
268
257
) {
@@ -276,7 +265,7 @@ if (DATABASE && con) {
276
265
"," +
277
266
mysql . escape ( salt ) +
278
267
")" ;
279
- con . query ( sql , function ( err : any , result : any ) {
268
+ con . query ( sql , function ( err : any , result : any ) {
280
269
if ( err ) throw err ;
281
270
} ) ;
282
271
} ) ;
@@ -307,7 +296,7 @@ if (DATABASE && con) {
307
296
res . send ( '{ "result":' + JSON . stringify ( status ) + "}" ) ;
308
297
}
309
298
} ) ;
310
- app . post ( "/#" , function ( req : any , res : any ) {
299
+ app . post ( "/#" , function ( req : any , res : any ) {
311
300
let status = "failure" ;
312
301
if (
313
302
typeof req . body . username == "string" &&
@@ -316,12 +305,12 @@ if (DATABASE && con) {
316
305
let sql =
317
306
"SELECT encrypted_password FROM USERS WHERE username=" +
318
307
mysql . escape ( req . body . username ) ;
319
- con . query ( sql , function ( err : any , result : any ) {
308
+ con . query ( sql , function ( err : any , result : any ) {
320
309
if ( result . length != 0 ) {
321
310
bcrypt . compare (
322
311
req . body . password ,
323
312
result [ 0 ] . encrypted_password ,
324
- function ( err : any , comparisonResult : any ) {
313
+ function ( err : any , comparisonResult : any ) {
325
314
if ( comparisonResult == true ) {
326
315
status = "success" ;
327
316
loginUser ( req ) ;
@@ -341,12 +330,12 @@ if (DATABASE && con) {
341
330
}
342
331
} ) ;
343
332
}
344
- app . post ( "/logout" , function ( req : any , res : any ) {
333
+ app . post ( "/logout" , function ( req : any , res : any ) {
345
334
req . session . loggedIn = false ;
346
335
req . session . username = "" ;
347
336
res . send ( "{}" ) ;
348
337
} ) ;
349
- app . post ( "/newGame" , function ( req : any , res : any ) {
338
+ app . post ( "/newGame" , function ( req : any , res : any ) {
350
339
let result = "success" ;
351
340
if (
352
341
typeof req . body . name == "string" &&
@@ -382,13 +371,13 @@ app.post("/newGame", function (req: any, res: any) {
382
371
}
383
372
res . send ( '{"result":' + JSON . stringify ( result ) + "}" ) ;
384
373
} ) ;
385
- app . post ( "/forgottenPassword" , function ( req : any , res : any ) {
374
+ app . post ( "/forgottenPassword" , function ( req : any , res : any ) {
386
375
if ( typeof req . body . username == "string" ) {
387
376
//find email in the database matching username 'req.body.username'
388
377
let sql =
389
378
"SELECT email FROM USERS WHERE username=" +
390
379
mysql . escape ( req . body . username ) ;
391
- con . query ( sql , function ( err : any , result : any ) {
380
+ con . query ( sql , function ( err : any , result : any ) {
392
381
if ( result . length == 0 ) {
393
382
res . send ( '{"result":"Your username is incorrect"}' ) ;
394
383
} else {
@@ -401,13 +390,13 @@ app.post("/forgottenPassword", function (req: any, res: any) {
401
390
//then send the email:
402
391
}
403
392
} ) ;
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 ) {
406
395
res . render ( "404" ) ;
407
396
} ) ;
408
397
409
398
//handle socket requests
410
- io . on ( "connection" , function ( socket : Socket ) {
399
+ io . on ( "connection" , function ( socket : Socket ) {
411
400
//set the session unless it is already set
412
401
if ( ! socket . request . session . socketID ) {
413
402
socket . request . session . socketID = socket . id ;
@@ -427,10 +416,10 @@ io.on("connection", function (socket: Socket) {
427
416
if ( oldPlayerId != undefined ) {
428
417
thisPlayerId = oldPlayerId ;
429
418
}
430
- socket . on ( "reloadClient" , function ( ) {
419
+ socket . on ( "reloadClient" , function ( ) {
431
420
server . reloadClient ( thisPlayerId ) ;
432
421
} ) ;
433
- socket . on ( "message" , function ( msg : string ) {
422
+ socket . on ( "message" , function ( msg : string ) {
434
423
if ( typeof msg === "string" ) {
435
424
//exclude commands from filtering (they start with a forward slash):
436
425
if ( msg [ 0 ] === "/" ) {
@@ -446,19 +435,19 @@ io.on("connection", function (socket: Socket) {
446
435
}
447
436
}
448
437
} ) ;
449
- socket . on ( "leaveGame" , function ( ) {
438
+ socket . on ( "leaveGame" , function ( ) {
450
439
server . leaveGame ( thisPlayerId ) ;
451
440
} ) ;
452
- socket . on ( "disconnect" , function ( ) {
441
+ socket . on ( "disconnect" , function ( ) {
453
442
server . removeSocketFromPlayer ( thisPlayerId , socket ) ;
454
443
server . kick ( thisPlayerId ) ;
455
444
} ) ;
456
- socket . on ( "gameClick" , function ( gameId : string ) {
445
+ socket . on ( "gameClick" , function ( gameId : string ) {
457
446
if ( parseInt ( gameId ) != NaN ) {
458
447
server . gameClick ( thisPlayerId , gameId ) ;
459
448
}
460
449
} ) ;
461
- socket . on ( "localGameClick" , function ( name : string , gameId : string ) {
450
+ socket . on ( "localGameClick" , function ( name : string , gameId : string ) {
462
451
server . receive ( thisPlayerId , name ) ;
463
452
if (
464
453
server . getUser ( thisPlayerId ) != undefined &&
@@ -467,7 +456,7 @@ io.on("connection", function (socket: Socket) {
467
456
server . gameClick ( thisPlayerId , gameId ) ;
468
457
}
469
458
} ) ;
470
- socket . on ( "lobbyMessage" , function ( msg : string ) {
459
+ socket . on ( "lobbyMessage" , function ( msg : string ) {
471
460
if ( typeof msg === "string" ) {
472
461
if ( Date . now ( ) - time < 500 ) {
473
462
time = Date . now ( ) ;
@@ -487,6 +476,6 @@ io.on("connection", function (socket: Socket) {
487
476
488
477
//listen on port
489
478
let port = 8081 ;
490
- http . listen ( port , function ( ) {
479
+ http . listen ( port , function ( ) {
491
480
console . log ( "Port is:" + port ) ;
492
481
} ) ;
0 commit comments