-
Notifications
You must be signed in to change notification settings - Fork 0
/
trivia-server.js
816 lines (706 loc) · 22.9 KB
/
trivia-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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
const express = require('express');
var bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
const path = require("path");
const model = require("./trivia-logic-module.js")
const fs = require("fs");
let curGameID;// GLOBAL GAME ID, USED TO LOAD GAME
let curUser;
//3 arrays for create game queue
let friendsonlyarr=[]
let publicarr=[]
let privatearr=[]
const session = require('express-session');
app.use(express.static(path.join(__dirname, "css")));
app.use(express.static(path.join(__dirname, "js")));
app.use(express.static(path.join(__dirname, 'c4')));
app.use(express.static('c4'));
//main page
app.use('/', express.static('c4'))
//max age of 100 minutes
app.use(session({ secret: 'KQLKPJNGMNUEAM0203', cookie:{maxAge: 6000000}}))
app.use(express.json())
//app.use(express.static(".");
app.set("view engine", "pug");
app.get("/",function(req,res) {
if(req.session.loggedin){//if logged in send game file
if(curGameID) {
res.sendFile(__dirname + '/c4/c4.html');
}
else {
res.redirect("/profile")//if no gameID then redirect to profile, select a game to load
}
}
else {
res.redirect('/login')
}
});
app.get("/games/:gid", function(req,res) {//get gameboard with gameID
let gID = req.params.gid;
console.log("gID: " + gID)
let gameList = model.games;
let reqGame;
gameList.forEach(value => {//filter games and get gameID
if(req.session.loggedin){
if(value.gameID == gID) {
reqGame = value;
curGameID = gID;
console.log(curGameID)
}
}
});
if(req.session.loggedin){//load for gameID
res.sendFile(__dirname + '/c4/c4.html');
}
else {
res.redirect('/login')
}
})
app.post("/games/:gid", function(req,res) {//post new game object and replace it,check for win and tie
let gID = req.params.gid;
let gameList = model.games;
let reqGame;
let username = req.session.username;
gameList.forEach(value => {
if(req.session.loggedin){
if(value.gameID == gID) {
reqGame = value;
//curGameID = gID;
}
}
});
let indexofG = model.games.findIndex(i => i.gameID == gID);
model.games[indexofG].isGameDone=true;
if(model.games[indexofG].players[0]==username) {
model.games[indexofG].winner=model.games[indexofG].players[1];
}
else if(model.games[indexofG].players[1]==username) {
model.games[indexofG].winner=model.games[indexofG].players[0];
}
if(req.session.loggedin){
res.redirect("/profile");
}
else {
res.redirect('/login')
}
})
app.get("/search",function(req,res){//get search page, display all public users
if(!req.session.loggedin) {
res.redirect("/login")
return;
}
let username = req.session.username;
let userlist = model.users;
let requestingUser;
userlist.forEach(value => {
if(req.session.loggedin){
if(value.username === username) {
requestingUser = value;
}
}
});
res.render("search.pug", {
reqUser:requestingUser,
userArr:model.users
})
})
app.post("/search",function(req,res){//return all that match search parameters
if(!req.session.loggedin) {
res.redirect("/login");
return;
}
let username = req.session.username;
let reqUser = req.body.user;
let userlist = model.users;
let holderArr = [];
userlist.forEach(value => {
if(req.session.loggedin){
if(value.username === username) {
requestingUser = value;
}
if(value.username.includes(reqUser)) {
holderArr.push(value)
}
}
});
res.render("search.pug", {
reqUser:requestingUser,
userArr:holderArr
})
})
app.get("/game",function(req,res){//initial GET for connect4.js, sends current game object with username
//let gameObj = req.body;
if(!curGameID) {
//console.log("GAME ID NOT INIT!")
res.redirect("/profile")
return;
}
//console.log("User Func curGameID: " + curGameID)
let indexofG = model.games.findIndex(i => i.gameID == curGameID);
res.send([req.session.username,model.games[indexofG]]);
})
app.get("/creategame",function(req,res) {//sends createGame page
if(!req.session.loggedin) {
res.redirect("/login")
return;
}
let username = req.session.username;
let userlist = model.users;
let requestingUser;
userlist.forEach(value => {
if(req.session.loggedin){
if(value.username === username) {
requestingUser = value;
}
}
});
res.render("creategame.pug", {
reqUser:requestingUser,
userArr:model.users
})
})
app.post("/creategame",function(req,res) {//creates game and handles good/bad inputs
let username = req.session.username;
let username2;
let pub = req.body.isPublic;
let idHolder = 0;
if(!pub) {
res.status(404).send("Please select game privateness...")
return;
}
if(!req.session.loggedin) {
res.redirect("/login")
return;
}
model.games.forEach(value => {
if(value.gameID > idHolder) {
idHolder=value.gameID;
}
})
idHolder++;//this is the new gameID incase we need to create a new game!
let userHolder;
if(!req.body.user) {
//if queue == 1, create game with other, else if queue == 0, just add them
if(pub=="public") {
if(publicarr.length==0) {
publicarr.push(username);
res.redirect("/creategame")
return;
}
else if(!publicarr.includes(username)){
username2=publicarr[0];
publicarr = []
//create game
let gameC = {gameID:idHolder,isPublic:true,isFO:false,players:[username,username2],currentTurn:1,isGameDone:false,winner:null,gameBoard:[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]}
model.games.push(gameC);
res.redirect("/profile")
return;
}
else {
res.status(404).send("You are already in the public queue! Wait for a game to appear on your profile page")
}
}
else if(pub=="private") {
if(privatearr.length==0) {
privatearr.push(username);
res.redirect("/creategame")
return;
}
else if(!privatearr.includes(username)){
username2=privatearr[0];
privatearr = []
//create game
let gameC = {gameID:idHolder,isPublic:false,isFO:false,players:[username,username2],currentTurn:1,isGameDone:false,winner:null,gameBoard:[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]}
model.games.push(gameC);
res.redirect("/profile")
return;
}
else {
res.status(404).send("You are already in the private queue! Wait for a game to appear on your profile page")
return;
}
}
else if(pub=="fo") {
if(friendsonlyarr.length==0) {
friendsonlyarr.push(username);
res.redirect("/creategame")
return;
}
else if(!friendsonlyarr.includes(username)){
username2=friendsonlyarr[0];
friendsonlyarr = []
//create game
let gameC = {gameID:idHolder,isPublic:false,isFO:true,players:[username,username2],currentTurn:1,isGameDone:false,winner:null,gameBoard:[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]}
model.games.push(gameC);
res.redirect("/profile")
return;
}
else {
res.status(404).send("You are already in the friends only queue! Wait for a game to appear on your profile page")
return;
}
//res.redirect("/creategame")
}
}
else {
let user = req.body.user;
model.users.forEach(value =>{
if(value.username==user) {
userHolder=value;
}
});
if(!userHolder) {
res.send("User does not exist!")
return;
}
else if(userHolder) {
//send game request
let indexofp = model.users.findIndex(i => i === userHolder);
model.users[indexofp].gameRequests.push([username,pub]);
res.redirect("/profile")
return;
}
}
// let userlist = model.users;
// let holderArr = [];
// userlist.forEach(value => {
// if(req.session.loggedin){
// if(value.username === username) {
// requestingUser = value;
// }
// if(value.username.includes(reqUser)) {
// holderArr.push(value)
// }
// }
// });
//res.redirect("/profile")
})
app.post("/acceptG", function(req, res, next){//accept game request
let user = req.session.username;
let userlist = model.users;
let holder = JSON.parse(req.body.user);
let requestUser = holder[0];
let pub = holder[1];
let requestToFriend;
let newID = 0;
model.games.forEach(value => {
if(value.gameID > newID) {
newID = value.gameID;
}
})
let gameC;
newID++;
if(pub=="public") {
gameC = {gameID:newID,isPublic:true,isFO:false,players:[user,requestUser],currentTurn:1,isGameDone:false,winner:null,gameBoard:[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]}
}
else if(pub=="private") {
gameC = {gameID:newID,isPublic:true,isFO:false,players:[user,requestUser],currentTurn:1,isGameDone:false,winner:null,gameBoard:[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]}
}
else if(pub=="fo") {
gameC = {gameID:newID,isPublic:false,isFO:true,players:[user,requestUser],currentTurn:1,isGameDone:false,winner:null,gameBoard:[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]}
}
model.games.push(gameC)
let indexofP = model.users.findIndex(i => i.username === user);
let indexofP2 = model.users[indexofP].gameRequests.findIndex(i => i[0] == requestUser);
model.users[indexofP].gameRequests.splice(indexofP2,1)
res.redirect("/profile")
})
app.post("/declineG", function(req, res, next){//decline game request
let user = req.session.username;
let requestUser = JSON.parse(req.body.user);
let indexofP = model.users.findIndex(i => i.username === user);
let indexofP2 = model.users[indexofP].gameRequests.findIndex(i => i[0] == requestUser[0]);
model.users[indexofP].gameRequests.splice(indexofP2,1)
res.redirect("/profile");
})
app.post("/game",function(req,res){//connect4.js POSTS information here, checks for win and returns appropriate array
let recObj = req.body;
let gameID = recObj.gameID;
let gameList = model.games;
let indexofG = gameList.findIndex(i => i.gameID === gameID);
if(!indexofG) {
res.status(404).send("Game Not Found")
}
model.games[indexofG]=recObj;
if(model.winCheck(recObj.gameBoard) && !model.games[indexofG].isGameDone){
//find winner
if(recObj.currentTurn==1) {
model.games[indexofG].winner = model.games[indexofG].players[1];
let indexofp1 = model.users.findIndex(i => i.username == model.games[indexofG].players[1]);//winner
let indexofp2 = model.users.findIndex(i => i.username == model.games[indexofG].players[0]);//loser
model.users[indexofp1].GamesWon+=1;
model.users[indexofp2].GamesLost+=1;
console.log(model.users[indexofp2].GamesWon + " " + model.users[indexofp1].GamesLost)
}
else {
model.games[indexofG].winner = model.games[indexofG].players[0];
let indexofp1 = model.users.findIndex(i => i.username == model.games[indexofG].players[1]);//loser
let indexofp2 = model.users.findIndex(i => i.username == model.games[indexofG].players[0]);//winner
model.users[indexofp2].GamesWon+=1;
model.users[indexofp1].GamesLost+=1;
console.log(model.users[indexofp2].GamesWon + " " + model.users[indexofp1].GamesLost)
}
model.games[indexofG].isGameDone=true;
return;
}
else if(model.drawCheck(recObj.gameBoard)) {
//draw
model.games[indexofG].isGameDone=true
model.games[indexofG].winner="tie";
console.log("TIE! ")
}
})
app.post("/profilePub",function(req,res) {//changes profile privateness
let username;
let pub = req.body.isPublic;
if(req.session.loggedin){
username = req.session.username;
console.log("user requesting: " + username)
}
else {
res.redirect("/login")
return;
}
let userlist = model.users;
let requestingUser;
let indexUser1 = userlist.findIndex(i => i.username == username);
if(model.users[indexUser1].isPublic && pub=="private") {
model.users[indexUser1].isPublic = false;
}
else if(!(model.users[indexUser1].isPublic) && pub=="public") {
model.users[indexUser1].isPublic = true;
}
res.redirect("/profile");
})
app.get("/users/:user",function(req,res){//returns profile of user if public or friends
let username;
if(req.session.loggedin){
username = req.session.username;
console.log("user requesting: " + username)
}
let username2 = req.params.user;
let userlist = model.users;
let requestingUser, displayUser;
userlist.forEach(value => {
if(req.session.loggedin){
if(value.username === username) {
requestingUser = value;
}
}
if(value.username == username2) {
displayUser = value;
}
});
if(!displayUser) {//if user doesnt exist
res.status(404).send("User does not exist!");
return;
}
let gameList = model.games;
let holder = [];
let holder2 = [];
gameList.forEach(value => {
if(value.players.includes(displayUser.username)) {
if(value.isGameDone) {//if games done, add to hodler2
if(!value.isPublic && !value.isFO) {//if its private and reqUser is one of the participants, add it to holder2
if(value.players.includes(requestingUser.username)) {
holder2.push(value)
}
}
else if(value.isPublic) {
holder2.push(value)
}
else if(value.isFO && displayUser.friends.includes(requestingUser.username)) {
holder2.push(value)
}
}
else {
if(!value.isPublic && !value.isFO) {//if its private and reqUser is one of the participants, add it to holder2
if(value.players.includes(requestingUser.username)) {
holder.push(value)
console.log("1")
}
}
else if(value.isPublic) {
holder.push(value);
console.log("2")
}
else if(value.isFO && displayUser.friends.includes(requestingUser.username)) {
holder.push(value)
console.log("3")
}
}
}
})
//if game array is empty(means no active games for this user)
//give a string empty for pug to display a empty array
if(holder.length==0) {
holder = "empty";
}
if(holder2.length==0) {
holder2="empty"
}
let holder3 = (displayUser.GamesWon/(displayUser.GamesWon+displayUser.GamesLost))*100
if(displayUser.isPublic){
res.render("viewprofile.pug", {
user:displayUser,
gameArr:holder,
gameArr2:holder2,
percentage:holder3
})
return;
}
else if(!displayUser.isPublic && displayUser.friends.includes(requestingUser)) {
res.render("viewprofile.pug", {
user:displayUser,
gameArr:holder,
gameArr2:holder2
})
return;
}
// else condition if the user exists but is private
else {
res.status(401).send("Not authorized. User is private!");
}
})
app.get("/users",function(req,res){//returns to profile or login
if(req.session.loggedin) {
res.redirect("/profile")
}
else {
res.redirect('/login');
}
})
app.get("/profile",function(req,res) {//loads profile with all info
if(req.session.loggedin) {
let username = req.session.username;
console.log("PROFILE: " + username)
let userlist = model.users;
let requestingUser;
let gameList = model.games;
let holder = [];
let holder2 = [];
gameList.forEach(value => {
if(value.players.includes(username)) {
if(value.isGameDone) {
holder2.push(value)//gameArr2
}
else {
holder.push(value);//gameArr
}
}
})
userlist.forEach(value => {
if(value.username == username) {
requestingUser = value;
}
});
let holder3 = (requestingUser.GamesWon/(requestingUser.GamesWon+requestingUser.GamesLost))*100
res.render('profile.pug', {
user:requestingUser,
gameArr:holder,
gameArr2:holder2,
percentage:holder3
}
)}
else{
res.redirect('/login')
}
});
app.get("/login",function(req,res) {//renders login page
res.render('login.pug')});
app.post("/login",login)//renders login info
app.get("/logout", logout)//logs out
app.get("/forgotpass",function(req,res) {
res.render('forgotpass.pug')});//forgotpass apge
app.get("/signup",function(req,res) {//loads signup page
if(req.session.loggedin) {
res.redirect("/profile")
}
res.render('signup.pug')});
app.post("/signup",signup);
// Auth function that can be passed to
// For a new session, loggedin does not exist
// until we define it in signup, so req.session.loggedin returns null
function auth(req, res, next) {
if(!req.session.loggedin) {
res.redirect("login.pug", {
loggedin:false
})
return;
}
}
// Signup function get username and password from req.body
// and find user in users array to validate the user does not
// already exists (prevent username duplicates)
function signup(req, res, next) {
let username = req.body.username;
let password = req.body.password;
let user;
model.users.forEach((value) => {
if(value.username == username) {
user = value;
}
})
if(user) {
res.status(401).send("User already exists.");
return;
} else {
//https://i.pinimg.com/originals/f4/97/71/f497716cb0550463349750966e41070f.png
// please use this url for signup
user = {username: username, password: password, profilePic:req.body.url, email:req.body.email, GamesWon:0,GamesLost:0,FriendRequests:[],friends:[],isPublic:true, isOnline:true,gameRequests:[]};
model.users.push(user);
req.session.loggedin = true;
req.session.username = username;
res.status(200);
res.redirect("/")
//res.status(200).send("Signed up.");
}
}
// Login function get username and password from req.body
// and find user in users array
function login(req, res, next) {
let username = req.body.username;
curUser = username;
let password = req.body.password;
let user;
let userlist = model.users;
userlist.forEach(value => {
if(value.username == username) {
user = value;
}
});
// console.log("username " + username + " password " + password)
// console.log("USER " + user)
if(user) {
if(user.password == password) {
req.session.loggedin = true;
req.session.username = username;
let indexUser1 = userlist.findIndex(i => i.username === username);
model.users[indexUser1].isOnline = true;
console.log(model.users[indexUser1].username)
res.status(200);
res.redirect("/profile");
}
}
else {
res.status(401).send("Not authorized. Invalid username or incorrect password.");
}
}
// Logout just set the session variable loggedin to false
// to fail the auth function.
function logout(req, res, next) {
if(req.session.loggedin) {
let userlist = model.users;
let username = req.session.username;
let indexUser1 = userlist.findIndex(i => i.username === username);
model.users[indexUser1].isOnline = false;
req.session.loggedin = false;
res.status(200);
}
res.status(404);//need to login
res.redirect("/login")
}
//this should add friends if valid
app.post("/addF", function(req, res, next){
console.log("Getting user with name: " + req.body.user);
let user = req.session.username;
let userlist = model.users;
let requestUser = req.body.user;
let requestToFriend;
userlist.forEach(value => {
if(value.username == user) {
user = value;//person thats logged in
}
if(value.username == requestUser) {
requestToFriend = value;//person we're trying to add
}
});
if(!requestToFriend){
res.status(404).send("Unknown user")
}
else {
if(user.friends.includes(requestToFriend)) {
res.status(404).send("You are already friends!")
//this should prevent people that are already friends to become friends again
}
else if(user.FriendRequests.includes(requestToFriend)) {
model.makeFriends(user.username, requestToFriend.username);
console.log(user.username + " and " + requestToFriend.username + " are now friends!")
res.status(200).redirect("/profile");//they are now friends
}
else {//need to send friend request
let indexUser1 = model.users.findIndex(i => i.username === user.username);
let indexUser2 = model.users.findIndex(i => i.username === requestToFriend.username);
if(!model.users[indexUser2].FriendRequests.includes(user)) {
model.users[indexUser2].FriendRequests.push(user);
res.status(200).redirect("/profile");;
}
//res.redirect("/profile")
}
}
//res.redirect("/profile")
})
//remove friends if valid
app.post("/remF", function(req, res, next){
console.log("Getting user with name: " + req.body.user);
let user = req.session.username;
let userlist = model.users;
let requestUser = req.body.user;
let requestToFriend;
userlist.forEach(value => {
if(value.username == user) {
user = value;//person thats logged in
}
if(value.username == requestUser) {
requestToFriend = value;//person we're trying to add
}
});
if(!requestToFriend){
res.status(404).send("Unknown user")
return;
}
else {
if(user.friends.includes(requestToFriend)) {
model.deleteFriends(user.username,requestToFriend.username);
res.status(200);
res.redirect("/profile")
//this should prevent people that are already friends to become friends again
}
else if(user.FriendRequests.includes(requestToFriend)) {
let indexUser1 = userlist.findIndex(i => i.username === user.username);
let index2 = model.users[indexUser1].FriendRequests.findIndex(i => i.username === requestToFriend.username);
model.users[indexUser1].FriendRequests.splice(index2,1);
res.status(200)
res.redirect("/profile")
}
else {//need to send friend request
res.status(404).send("Error, you cannot delete a friend that you are not friends with!")
}
}
})
const http = require("http");
let server = http.createServer(app);
const io = require("socket.io")(server);
let msgs = []
//socket.io for global chatbox
io.on('connection', socket =>{
console.log("A connection was made.");
//add events for that socket
socket.on('disconnect', () => {
console.log("Somebody left.");
})
socket.on("register", name => {
socket.username = curUser;
console.log("User joined: " + socket.username);
socket.emit('init', JSON.stringify({messages: msgs}));
io.emit("newuser", name);
})
socket.on("newmsg", message => {
message = socket.username + ": " + message
msgs.push(message);
io.emit("newmsg", message);
})
})
server.listen(3000);