-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
1432 lines (1277 loc) · 57.2 KB
/
app.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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const express = require("express");
const app = express();
const httpserver = require("http").createServer();
const WebSocket = require("ws");
const wss = new WebSocket.Server({server: httpserver, path: "/ws"});
const fs = require("fs");
const bodyParser = require("body-parser");
const path = require("path");
const jwt = require("jsonwebtoken");
const cookieParser = require("cookie-parser");
const { Pool } = require("pg");
const auth = require("./scripts/auth.js");
const gamehandler = require("./scripts/game.js");
const emitter = require("events").EventEmitter;
const compression = require("compression");
const { resolve } = require("path");
const { rateLimit } = require("express-rate-limit");
const nodemailer = require("nodemailer");
const crypto = require("crypto");
const badges = require("./scripts/badges.js");
const { response } = require("express");
const colorData = require("./scripts/colorData.js");
const badwords = require("bad-words");
const emblitzBot = require("./scripts/bot.js");
const configs = require("./configs.json");
/*Don't do it yourself, instead, be lazy and find a package that does it for you.
-Sun Tzu, The Art of War
Update 7/27/22: passport.js creates a lot of hosting compatibility issues
and requires a separate db so I'll have to ignore this advice just for this
one time.
*/
if(process.env.PRODUCTION !== "yes") {
console.log("Running in development environment!");
require("dotenv").config();
} else {
console.log("Running in production environment!");
}
//-- configs --
const authsecret = process.env.AUTHSECRET;
var port = process.env.SERVERPORT;
//GAME VERSION
const gameversion = configs.gameVersion + " | " + configs.gameLastUpdated;
//mapname, maxplayers
const allmaps = require("./scripts/mapconfig.js");
//-- end configs --
//-- version --
console.log("Using game version " + gameversion);
//-- end version --
//-- player colors --
const playercoloroptions = ["red", "orange", "yellow", "green", "blue", "purple"];
//-- end player colors --
//-- DEV SERVER MONITOR VARS --//
var dev_emails = 0;
var dev_server_starttime = new Date();
dev_server_starttime = dev_server_starttime.toString();
var dev_server_abs_starttime = Date.now();
//-- end dev server monitor vars --//
var hostname = process.env.HOSTNAME + ":" + port;
if(process.env.PRODUCTION === "yes") {
hostname = process.env.HOSTNAME;
if(process.env.PORT) {
port = process.env.PORT;
}
}
const badWordsFilter = new badwords();
const game = new gamehandler();
const gameevents = gamehandler.gameevents;
//database
var dbcredentials = null;
if(process.env.PRODUCTION === "yes") {
dbcredentials = {
connectionString: process.env.DATABASE_URL,
ssl: { rejectUnauthorized: false }
};
console.log("Database set to production mode");
} else {
dbcredentials = {
host: process.env.DATABASE_URL,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
port: process.env.DATABASE_PORT,
database: process.env.DATABASE_NAME
};
console.log("Database set to development mode");
}
const pool = new Pool(dbcredentials);
pool.connect(function(err) {
if (err) console.log(err);
//export db configs
module.exports.db = pool;
console.log("Connected to database!");
auth.initDB().then(function() {
console.log("Finished initializing database");
console.log("--- Server launched, begin logging. ---")
if(configs.accountManagement.forceEmailVerification) {
setInterval(function() {
auth.deleteUnusedAccounts();
}, configs.accountManagement.deleteUnusedAccountsInterval);
}
});
});
//--MAILER--
var mailTransport = null;
//if(process.env.PRODUCTION !== "yes") {
console.log("Using development mail server (Gmail)");
mailTransport = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.MAILUSER,
pass: process.env.MAILPASSWORD
}
});
/*} else {
//You got a production mail server?
console.log("Using production mail server");
}
*/
const clients = new Map();
const rooms = [];
var userids = [];
function escapestring(str) {
return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}
function escapeHTML(text) {
try {
return text.replace(/&/g, '&').replace(/</g, '<').replace(/"/g, '"').replace(/'/g, ''');
} catch {
return "";
}
}
function randomnumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function removeFromArray(arr, item) {
for (var i = arr.length; i--;) {
if (arr[i] === item) arr.splice(i, 1);
}
}
function requireHTTPS(req, res, next) {
//for Heroku only
if (!req.secure && req.get('x-forwarded-proto') !== 'https' && process.env.PRODUCTION === "yes") {
return res.redirect('https://' + req.get('host') + req.url);
}
next();
}
//-- RATELIMITS --//
const apiLimiter = rateLimit({
windowMs: 0.5 * 60000, //minutes
max: 40,
message: JSON.stringify({"error": 429, "message": "You are accessing the api too quickly (40 requests/30 sec)! Try again in a minute. Calm down my guy."}),
standardHeaders: true,
legacyHeaders: false
});
const adminApiLimiter = rateLimit({
windowMs: 1 * 60000, //minutes
max: 10,
message: JSON.stringify({"error": 429, "message": "You are accessing the auth api too quickly (10 requests/min)! Please go and bing chilling, and try again in a minute."}),
standardHeaders: true,
legacyHeaders: false
});
const mailApiLimiter = rateLimit({
windowMs: 5 * 60000, //minutes
max: 5,
message: JSON.stringify({"error": 429, "message": "You are accessing the auth 2 api too quickly (3 requests/5 min)! Please go and bing chilling, and try again in 5 minutes."}),
standardHeaders: true,
legacyHeaders: false
});
app.set("view engine", "html");
app.engine("html", require("ejs").renderFile);
app.set("views", path.join(__dirname, "./public"));
app.disable("x-powered-by");
app.use(requireHTTPS);
app.use(compression());
app.use("/api/", apiLimiter);
app.use("/authapi/", adminApiLimiter);
app.use("/auth2/", mailApiLimiter);
/*
App can't have pages caching because too much of it is dynamic
and changes very frequently. Plus, webpages are small enough
to not suffer a noticably slower load time as a result.
*/
//enable req.body to be used
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cookieParser());
//remove trailing forward slash
app.use((req, res, next) => {
if (req.path.substr(-1) === '/' && req.path.length > 1) {
const query = req.url.slice(req.path.length);
res.redirect(301, req.path.slice(0, -1) + query);
} else {
next();
}
});
app.all("/", (req, res) => {
let getuuid = req.cookies.uuid;
res.setHeader("Surrogate-Control", "no-store");
res.setHeader(
"Cache-Control",
"no-store, no-cache, must-revalidate, proxy-revalidate"
);
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "0");
//create temporary game id (GID) to combine with pubkey
let chars = "1234567890qwertyuiopasdfghjklzxcvbnm";
let id = "";
for(let i=0; i<30; i++) {
id += chars.charAt(crypto.randomInt(0, chars.length-1));
}
res.cookie("GID", id);
let profileoutput = `
<DIV CLASS="po-container">
<DIV CLASS="po-description">Login or register to set a username, earn medals, see stats, and more! Only takes about 30 seconds!</DIV>
<BUTTON CLASS="joinbutton pushdown jb_green" ONCLICK="window.location.href='./login?action=register'" STYLE="font-size: 18px; min-width: 150px; margin-top: 10px;">Register</BUTTON><BUTTON ONCLICK="window.location.href='./login'" STYLE="font-size: 18px; min-width: 150px; margin-top: 10px;" CLASS="joinbutton pushdown jb_gray">Login</BUTTON>
</DIV>
`
let adslot_1 = `<DIV STYLE="font-size: 13px; margin-bottom: 5px;" CLASS="ad-warning ad-warning-one">Advertisement</DIV>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2859288291436441"
></script>
<!-- Main menu ad -->
<ins class="adsbygoogle ad-type-responsive"
style="display:block"
data-ad-client="ca-pub-2859288291436441"
data-ad-slot="7138740914"
data-ad-format="horizontal"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>`;
let adslot_2 = `<DIV STYLE="font-size: 13px; margin-bottom: 5px;" CLASS="ad-warning">Advertisement</DIV>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2859288291436441"
></script>
<!-- Main menu ad -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-2859288291436441"
data-ad-slot="7138740914"
data-ad-format="horizontal"
data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>`;
let adslot_3 = `<DIV STYLE="font-size: 13px; margin-bottom: 5px; margin: auto; margin-top: 15px; text-align: center;" CLASS="ad-warning">Advertisement</DIV>
<DIV STYLE="text-align: center; width: 100%; min-height: 100px; min-width: 100vw; display: block;">
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2859288291436441"
></script>
<!-- lobby ad 1 -->
<ins class="adsbygoogle"
style="display:block"
data-ad-client="ca-pub-2859288291436441"
data-ad-slot="6903292412"
data-full-width-responsive="true"></ins>
<script>
window.addEventListener("load", function() {
(adsbygoogle = window.adsbygoogle || []).push({});
});
</script>
</DIV>`;
if(process.env.ADS_ENABLED === "false") {
adslot_1 = adslot_2 = adslot_3 = null;
}
if(!getuuid) {
//guest user
let guestuuid = "";
for(let i=0; i<50; i++) {
guestuuid += chars.charAt(crypto.randomInt(0, chars.length-1));
}
res.cookie("uuid", "guest-" + guestuuid, { expires: new Date(Date.now() + (10*24*3600000))});
guestuuid = "";
for(let i=0; i<50; i++) {
guestuuid += chars.charAt(crypto.randomInt(0, chars.length-1));
}
res.cookie("publickey", "guest-" + guestuuid, { expires: new Date(Date.now() + (10*24*3600000))});
res.render("index", {
host_name: hostname,
prod: process.env.PRODUCTION,
gameversion: gameversion,
timeoutDuration: configs.timeoutDuration,
profile_output: profileoutput,
adslot_1,
adslot_2,
adslot_3
});
} else if(!getuuid.startsWith("guest-")) {
auth.getUserInfo(getuuid).then(function(userinfo) {
//logged in user
res.cookie("publickey", userinfo.publickey, { expires: new Date(Date.now() + (100*24*3600000))});
let profileoutput = `
<DIV CLASS="po-container" STYLE="margin-bottom: 20px">
<DIV CLASS="po-description" STYLE="text-align: left">
<A CLASS="my_profile" HREF="./login?action=verify">Please verify your account by <U>clicking here</U>. Note that unverified accounts will be deleted in an hour.</A>
</DIV>
</DIV>
`;
if(userinfo.verified === "true") {
profileoutput = "";
}
profileoutput += `
<DIV CLASS="profile-outline" STYLE="background: ${colorData[userinfo.playercolor].normal}">
<DIV CLASS="glb_avatar-frame" STYLE="border: 5px solid ${userinfo.playercolor}"><IMG STYLE="width: 60px; height: 60px" SRC="./images/defaultpfp.png"></DIV>
<DIV CLASS="glb_p_info">
<DIV ID="p_name" CLASS="lb_p_name">${userinfo.username}</DIV>
<A CLASS="my_profile" HREF="./user/${userinfo.username}"><SPAN STYLE="text-decoration: underline">My profile</SPAN> <I CLASS="fa fa-external-link-square"></I></A>
<A CLASS="my_profile mp_settings" HREF="./settings"><SPAN STYLE="text-decoration: underline">Settings</SPAN> <I CLASS="fa fa-gear"></I></A>
</DIV>
</DIV>
<DIV CLASS="my_stats">
<SPAN CLASS="ms_stat ms_stat_one"><IMG CLASS="ms_medals" SRC="./images/medal.png"><SPAN ID="ms_medals">--</SPAN> Medals</SPAN>
<SPAN CLASS="ms_stat ms_stat_two"><IMG CLASS="ms_badges" SRC="./images/badgeicon.png"><SPAN ID="ms_badges">--</SPAN> Badges</SPAN>
</DIV>
`
res.render("index", {
host_name: hostname,
prod: process.env.PRODUCTION,
gameversion: gameversion,
timeoutDuration: configs.timeoutDuration,
profile_output: profileoutput,
adslot_1,
adslot_2,
adslot_3
});
}).catch(function() {
//guest user bc uuid is invalid
let guestuuid = "";
for(let i=0; i<50; i++) {
guestuuid += chars.charAt(crypto.randomInt(0, chars.length-1));
}
res.cookie("uuid", "guest-" + guestuuid, { expires: new Date(Date.now() + (10*24*3600000))});
guestuuid = "";
for(let i=0; i<50; i++) {
guestuuid += chars.charAt(crypto.randomInt(0, chars.length-1));
}
res.cookie("publickey", "guest-" + guestuuid, { expires: new Date(Date.now() + (10*24*3600000))});
res.render("index", {
host_name: hostname,
prod: process.env.PRODUCTION,
gameversion: gameversion,
timeoutDuration: configs.timeoutDuration,
profile_output: profileoutput,
adslot_1,
adslot_2,
adslot_3
});
});
} else {
//guest user
res.render("index", {
host_name: hostname,
prod: process.env.PRODUCTION,
gameversion: gameversion,
timeoutDuration: configs.timeoutDuration,
profile_output: profileoutput,
adslot_1,
adslot_2,
adslot_3
});
}
});
app.get("/*.html", (req, res) => {
let path = req.originalUrl.replace(/.html$/, "");
res.redirect(301, path);
});
httpserver.on("request", app);
app.get("/api", (req, res) => {
res.json({"error": "invalid form body"});
});
app.get("/authapi", (req, res) => {
res.json({"error": "invalid form body"});
});
app.get("/login", (req, res) => {
let uuid = req.cookies.uuid;
res.setHeader("Surrogate-Control", "no-store");
res.setHeader(
"Cache-Control",
"no-store, no-cache, must-revalidate, proxy-revalidate"
);
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "0");
if(uuid) {
auth.getUserInfo(uuid).then(function(userinfo) {
res.render("login", {
host_name: hostname,
currentuser: `<DIV CLASS="lg_headertext_acct">You're already logged in as <B>${userinfo.username}</B>, but you can log into a different account.</DIV>`
});
}).catch(function() {
res.render("login", {
host_name: hostname,
currentuser: ""
});
});
} else {
res.render("login", {
host_name: hostname,
currentuser: ""
});
}
});
app.get("/tutorial", (req, res) => {
res.render("tutorial")
});
app.get("/admin", (req, res) => {
res.setHeader("Surrogate-Control", "no-store");
res.setHeader(
"Cache-Control",
"no-store, no-cache, must-revalidate, proxy-revalidate"
);
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "0");
res.render("admin");
});
app.get("/settings", (req, res) => {
let uuid = req.cookies.uuid;
res.setHeader("Surrogate-Control", "no-store");
res.setHeader(
"Cache-Control",
"no-store, no-cache, must-revalidate, proxy-revalidate"
);
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "0");
if(uuid) {
auth.getUserInfo(uuid).then(function(userinfo) {
res.render("settings", {
playercolor: userinfo.playercolor,
playername: userinfo.username
});
}).catch(function() {
res.redirect("./login");
})
}
});
app.get("/privacy", (req, res) => {
res.render("privacy");
});
app.get("/tutorial/*", (req, res) => {
let filePath = path.join(__dirname, `./public/tutorial/${req.path.split("/")[2]}.html`);
fs.stat(filePath, (err) => {
if(err) {
res.render("./errorpages/404");
res.status(404);
return;
}
res.render("tutorial/" + req.path.split("/")[2]);
});
});
app.get("/user/*", (req, res) => {
let username = req.path.split("/")[2];
auth.getPublicUserInfo(username).then(function(result) {
res.render("user", {
username: result.username,
playercolor: result.playercolor,
playercolorbg: colorData[result.playercolor].normal
});
}).catch(function() {
res.render("./errorpages/404");
res.status(404);
});
});
app.get("/verify", (req, res) => {
let gettoken = req.query.token;
let outputresult = ``;
jwt.verify(gettoken, authsecret, function(err, decoded) {
//invalid token
if(err || decoded.iss != "dr. defario's grandson samuel") {
outputresult = `Oops... an error occured. Your link might've expired or broke; try getting another verification email. Sorry about that. <A CLASS="lg_register pushdown jb_green" STYLE="display: block; margin: auto; margin-top: 30px; text-decoration: none" HREF="https://www.emblitz.com">Back to Emblitz</A>`
res.render("verify", {
result: outputresult
});
} else {
pool.query(`SELECT * FROM users WHERE publickey=$1 AND email=$2 AND verified=$3`, [decoded.data[0].publickey, decoded.data[0].email, false], function(err, result) {
if(err || result.rows.length == 0) {
outputresult = `Oops... an error occured. Your link might've expired, been used, or broke; try getting another verification email. Sorry about that. <A CLASS="lg_register pushdown jb_green" STYLE="display: block; margin: auto; margin-top: 30px; text-decoration: none" HREF="https://www.emblitz.com">Back to Emblitz</A>`;
res.render("verify", {
result: outputresult
});
} else {
let getusername = result.rows[0].username;
pool.query(`UPDATE users SET verified=$1 WHERE publickey=$2 AND email=$3`, [true, decoded.data[0].publickey, decoded.data[0].email], function(err, result) {
auth.awardBadge(decoded.data[0].publickey, "verifiedaccount");
let outputresult = `Thanks for verifying your account! You can now login through the login page using your username and password. Enjoy the game!<DIV STYLE="display: block; margin-top: 20px;">Your username: <B>${getusername}</B></DIV><A CLASS="lg_register pushdown jb_green" STYLE="display: block; margin: auto; margin-top: 30px; text-decoration: none" HREF="https://emblitz.com">To Emblitz!</A>`;
res.render("verify", {
result: outputresult
});
});
}
});
}
});
});
app.post("/verify", (req, res) => {
res.json({"error": "please use GET"});
});
//id = roomid
function getroommap(id) {
for(let i=0; i<rooms.length; i++) {
if(rooms[i].id === id) {
return rooms[i].map.toString();
}
}
return false;
}
app.post("/auth2", (req, res) => {
if(req.body.action === "registeruser") {
let errors = [];
let emailformatted = req.body.email.match(
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
);
let usernameformatted = req.body.username.match(
/^[a-zA-Z0-9_]+$/
);
let wordtofilter = req.body.username.replace(/_/g, "");
let containsbadwords = badWordsFilter.isProfane(wordtofilter);
if(req.body.username.length < 2) {
errors.push("u1");
} else if(req.body.username.length > 12) {
errors.push("u2")
} else if(!usernameformatted) {
errors.push("u4");
} else if(containsbadwords) {
errors.push("u5");
}
if(!req.body.email) {
errors.push("e1");
} else if(!emailformatted) {
errors.push("e2");
}
if(!req.body.password) {
errors.push("p1")
} else if(req.body.password.length < 8 || req.body.password.length > 30) {
errors.push("p2");
}
auth.checkUserConflicts(req.body.username, req.body.email).then(function(conflicts) {
errors = errors.concat(conflicts);
if(errors.length > 0) {
res.json({"errors": errors});
return;
}
//passed all checks...
//add user to database
auth.registerUser(req.body.username, req.body.email, req.body.password).then(function(tokens) {
var mail = {
from: "'Emblitz Team' <[email protected]>",
to: req.body.email,
subject: "Please verify your new account!",
html: ` <BODY STYLE="background: #121212; padding-top: 45px; padding-bottom: 45px; width: 100%;">
<DIV STYLE="width: calc(100% - 40px); max-width: 700px; margin: auto; background: #303030; margin: auto; padding: 20px;">
<IMG SRC="https://www.emblitz.com/images/logo.png" STYLE="width: 100%; max-width: 350px;">
<DIV STYLE="margin-top: 10px; color: #ffffff; font-size: 18px; font-family: sans-serif; margin-left: 8px;"><DIV STYLE="display: block; margin-bottom: 5px;">Dear ${req.body.username},</DIV>Thanks for registering for an Emblitz account! Please verify your email first to finish setting up your account by clicking the button below. This email is valid for the next 10 minutes. Please note that unverified accounts will be deleted after an hour. If you didn't make this request, simply ignore this email.<A HREF="emblitz.com/verify?token=${tokens[1]}" TARGET="_blank" STYLE="display: block; margin-top: 30px; margin-bottom: 30px; -webkit-appearance: none; -moz-appearance: none; appearance: none; border: none; border-radius: 10px; font-size: 18px; padding: 10px; width: 120px; text-align: center; text-decoration: none; color: #ffffff; background: #00b00f; cursor: pointer">Verify Email</A>Thanks,<BR>-The Emblitz Team<DIV STYLE="margin-top: 60px;">© Emblitz</DIV></DIV>
</DIV>
</BODY>
`
};
mailTransport.sendMail(mail);
dev_emails++;
res.cookie("uuid", tokens[0], { expires: new Date(Date.now() + (2*24*3600000))}); //2 days expiry bc temporary and account isn't verified yet
res.cookie("publickey", tokens[2], { expires: new Date(Date.now() + (2*24*3600000))});
res.json({"ok": true});
});
});
} else if(req.body.action === "resendemail") {
let uuid = req.cookies.uuid;
if(uuid) {
auth.verifyUUID(uuid).then(function(data) {
var mail = {
from: "'Emblitz Team' <[email protected]>",
to: data[0],
subject: "Please verify your new account!",
html: ` <BODY STYLE="background: #121212; padding-top: 45px; padding-bottom: 45px; width: 100%;">
<DIV STYLE="width: calc(100% - 40px); max-width: 700px; margin: auto; background: #303030; margin: auto; padding: 20px;">
<IMG SRC="https://www.emblitz.com/images/logo.png" STYLE="width: 100%; max-width: 350px;">
<DIV STYLE="margin-top: 10px; color: #ffffff; font-size: 18px; font-family: sans-serif; margin-left: 8px;"><DIV STYLE="display: block; margin-bottom: 5px;">Dear ${data[1]},</DIV>Thanks for registering for an Emblitz account! Please verify your email first to finish setting up your account by clicking the button below. This email is valid for the next 10 minutes. Please note that unverified accounts will be deleted after an hour. If you didn't make this request, simply ignore this email.<A HREF="emblitz.com/verify?token=${data[2]}" TARGET="_blank" STYLE="display: block; margin-top: 30px; margin-bottom: 30px; -webkit-appearance: none; -moz-appearance: none; appearance: none; border: none; border-radius: 10px; font-size: 18px; padding: 10px; width: 120px; text-align: center; text-decoration: none; color: #ffffff; background: #00b00f; cursor: pointer">Verify Email</A>Thanks,<BR>-The Emblitz Team<DIV STYLE="margin-top: 60px;">© Emblitz</DIV></DIV>
</DIV>
</BODY>
`
};
mailTransport.sendMail(mail);
dev_emails++;
res.json({"email": data[0]});
}).catch(function(error) {
res.json({"error": "lookup_1"});
});
} else {
res.json({"error": "lookup_2"})
}
} else if(req.body.action === "resetpassword") {
}
})
app.post("/authapi", (req, res) => {
//admin functions
if(req.body.action === "createpost") {
if(req.body.auth !== process.env.ADMINMASTERPASSWORD) {
res.status(403);
res.json({"error": "403", "message": "You are not authorized to make this call!"});
return;
}
auth.postAnnouncement(req.body.title, req.body.content, req.body.submittedtime, req.body.image).then(function() {
res.json({"result": "post created successfully"});
});
} else if(req.body.action === "deletepost") {
if(req.body.auth !== process.env.ADMINMASTERPASSWORD) {
res.status(403);
res.json({"error": "403", "message": "You are not authorized to make this call!"});
return;
}
auth.deleteAnnouncement(req.body.postid).then(function() {
res.json({"result": "deleted post"})
});
} else if(req.body.action === "validatepassword") {
if(req.body.auth === process.env.ADMINMASTERPASSWORD) {
res.json({"result": true});
} else {
res.json({"result": false});
}
} if(req.body.action === "runsqlquery") {
if(req.body.auth !== process.env.ADMINMASTERPASSWORD) {
res.status(403);
res.json({"error": "403", "message": "You are not authorized to make this call!"});
return;
}
auth.runSQLQuery(req.body.query).then(function(result) {
res.json({"result": result.rows});
});
} else if(req.body.action === "login") {
if(req.body.username && req.body.password) {
auth.userLogin(req.body.username, req.body.password).then(function(userdata) {
userdata = userdata[0];
res.cookie("publickey", userdata.publickey, { expires: new Date(Date.now() + (30*24*3600000))});
res.cookie("uuid", userdata.token, { expires: new Date(Date.now() + (30*24*3600000))});
res.json({
"username": userdata.username,
"email": userdata.email,
"wins": userdata.wins,
"losses": userdata.losses,
"medals": userdata.medals,
"badges": userdata.badges,
"pfp": userdata.pfp,
"tournamentprogress": userdata.tournamentprogress,
"verified": userdata.verified,
"timecreated": userdata.timecreated,
"playercolor": userdata.playercolor,
"playersettings": userdata.playersettings,
"metadata": userdata.metadata,
"xp": userdata.xp
});
}).catch(function(error) {
res.json({"error": error});
});
} else if(!req.body.username && req.body.password) {
res.json({"error": "missing username"});
} else if(!req.body.password && req.body.username) {
res.json({"error": "missing password"});
} else {
res.json({"error": "missing username and password"})
}
}
});
app.post("/api", (req, res) => {
try {
if(req.body.action === "getdevstats") {
if(req.body.auth !== process.env.ADMINMASTERPASSWORD) {
res.status(403);
res.json({"error": "403", "message": "You are not authorized to make this call!"});
return;
}
res.json({"starttime": dev_server_starttime, "startms": dev_server_abs_starttime, "emailssent": dev_emails});
} else if (req.body.action === "editplayercolor") {
let getuuid = req.cookies.uuid;
let color = req.body.color;
if(color && getuuid && playercoloroptions.includes(color)) {
auth.changePlayerColor(getuuid, color).then(function() {
res.json({"success": "colorchanged"});
}).catch(function() {
res.json({"error": "not logged in"});
});
} else {
res.json({"error": "invalid parameters provided"});
}
} else if(req.body.action === "fetchposts") {
if(!isNaN(Number(req.body.startindex)) && !isNaN(Number(req.body.amount))) {
let startindex = req.body.startindex;
let amount = req.body.amount;
if(amount > 25) {
amount = 25;
} else if(amount < 1) {
amount = 1;
}
if(startindex < 0 || startindex > 99999999) {
startindex = 0
}
auth.fetchAnnouncements(startindex, amount).then(function(result) {
res.json({"posts": result});
});
} else {
res.status(400);
res.json({"error": 400, "message": "Malformed request"});
}
} else if(req.body.action === "getmap") {
var roommap = getroommap(req.body.roomid);
fs.readFile("./mapdata/" + roommap + "/" + roommap + ".txt", "utf8", function(err, data) {
fs.readFile("./mapdata/" + roommap + "/mapdict.json", "utf8", function(err, mapdict) {
fs.readFile("./mapdata/" + roommap + "/moves.json", "utf8", function(err, moves) {
fs.readFile("./mapdata/" + roommap + "/coordadjust.json", "utf8", function(err, coordadjust) {
fs.readFile("./mapdata/" + roommap + "/metadata.json", "utf8", function(err, metadata) {
res.json({"mapdata": data, "mapdict": mapdict, "moves": moves, "coordadjust": coordadjust, "metadata": metadata});
});
});
});
});
});
} else if(req.body.action === "joingame") {
//did player request for specific room?
if(req.body.preset) {
//does room exist?
let preset = req.body.preset;
let roomfound = false;
for(let i=0; i<rooms.length; i++) {
if(rooms[i].id === preset) {
//is room full?
if(rooms[i]["players"] >= rooms[i]["maxplayers"]) {
res.json({"error": "room " + preset + " is full"});
} else {
if(game.queryGameStatus(preset) !== "lobby") {
res.json({"error": "room " + preset + " has started"});
} else {
res.json({"uid": userid(), "room": preset});
}
}
roomfound = true;
break;
}
}
if(!roomfound) {
res.json({"error": "room " + preset + " does not exist"});
}
} else {
res.json({"uid": userid(), "room": joinroom(req.body.prefermap, req.body.createnewroom)});
}
} else if (req.body.action === "getmyinfo") {
let getuuid = req.cookies.uuid;
auth.getUserInfo(getuuid).then(function(userdata) {
res.json({
"username": userdata.username,
"email": userdata.email,
"wins": userdata.wins,
"losses": userdata.losses,
"medals": userdata.medals,
"badges": userdata.badges,
"pfp": userdata.pfp,
"tournamentprogress": userdata.tournamentprogress,
"verified": userdata.verified,
"timecreated": userdata.timecreated,
"playercolor": userdata.playercolor,
"playersettings": userdata.playersettings,
"metadata": userdata.metadata,
"xp": userdata.xp
});
}).catch(function(error) {
res.json({"error": "no user found"})
});
} else if(req.body.action === "getuserprofile") {
auth.getPublicUserInfo(req.body.username).then(function(userdata) {
res.json({
"username": userdata.username,
"wins": userdata.wins,
"losses": userdata.losses,
"medals": userdata.medals,
"badges": userdata.badges,
"pfp": userdata.pfp,
"tournamentprogress": userdata.tournamentprogress,
"timecreated": userdata.timecreated,
"playercolor": userdata.playercolor,
"xp": userdata.xp
});
}).catch(function(error) {
res.json({"error": "no user found"})
});
} else if(req.body.action === "badgedata") {
res.json(badges);
} else if(req.body.action === "logoutuser") {
res.clearCookie("uuid");
res.clearCookie("publickey");
res.send("logged out");
} else if(req.body.action === "getleaderboard") {
auth.fetchLeaderboard().then(function(result) {
res.send(result);
})
} else {
res.json({"error": "invalid form body"});
res.end();
}
} catch(e) {
console.log(e);
}
});
app.use(express.static(__dirname + "/public"));
app.use(function(req, res, next) {
res.status(404);
res.sendFile("./public/errorpages/404.html", {root: __dirname});
});
//public id generator
function userid() {
let chars = "1234567890qwertyuiopasdfghjklzxcvbnm";
let id = "u-";
for(let i=0; i<20; i++) {
id += chars.charAt(randomnumber(0, chars.length-1));
}
while(userids.includes(id)) {
let id = "u-"
for(let i=0; i<20; i++) {
id += chars.charAt(randomnumber(0, chars.length-1));
}
}
userids.push(id);
return id;
}
//if no username is specified, generate a random username
function genPname() {
return "Player " + randomnumber(1, 999);
//TODO: check and prevent duplicate names
}
//returns true if duplicate room ids exist
function checkDupeRoom(id) {
for(let i=0; i<rooms.length; i++) {
if(rooms[i].id === id) {
return true;
}
}
}
//Actually, several bots are attempted to be initiated
function initializeBot(roomid) {
let totalroomcount = rooms.length;
for(let i=0; i<totalroomcount; i++) {
if(rooms[i].id === roomid) {
let attempttimes = Math.round(rooms[i]["maxplayers"] - 2);
if (attempttimes < 1) {
attempttimes = randomnumber(1, 2);
}
for(let x=0; x < attempttimes; x++) {
setTimeout(function() {
if(!getroommap(roomid)) return;
if(!rooms[i]) return;
if(rooms[i]["isprivate"]) return;
fs.readFile("./mapdata/" + getroommap(roomid) + "/moves.json", "utf8", function(err, moves) {
if (err) console.log(err);
if(rooms[i]["maxplayers"] - rooms[i]["players"] > 1 && rooms[i]["ingame"] == false) {
rooms[i]["players"]++;
rooms[i]["bots"]++;
rooms[i]["playersready"]++; //bots are always ready
//same thing to start the lobby timer back up
if(rooms[i]["players"] > 1) {
if(game.queryGameStatus(rooms[i]["id"]) === "lobby") {
game.resumeLobbyTimer(rooms[i]["id"]);
}
}
//color assignment -- same algorithm as player color assignment
//default color = red
let pcolor = "red";
let takencolors = [];
let availablecolors = playercoloroptions;
let playerliststring = rooms[i]["playerslist"];
for(let i=0; i<playerliststring.length; i++) {
takencolors.push(playerliststring[i]["pcolor"]);
}
if(takencolors.includes(pcolor)) {
for(let i=0; i<takencolors.length; i++) {
availablecolors = availablecolors.filter(function(item) {
return item !== takencolors[i];
});
}
pcolor = availablecolors[(Math.random() * availablecolors.length) | 0];
}
//now create a bot instance
let newbot = new emblitzBot(roomid, "Player " + randomnumber(1, 999), pcolor, JSON.parse(moves));
newbot.joinGame().then(function(result) {
rooms[i]["playerslist"].push({"id": result[0], "name": result[1], "pcolor": result[2]});
sendRoomMsg(rooms[i].id, {"users": rooms[i]["playerslist"], "playersconfirmed": rooms[i]["playersconfirmed"], "isprivateroom": rooms[i]["isprivate"]});
//bot should "press" ready on its own, or at least mimic the behavior
setTimeout(function() {
if(!rooms[i]) return;
if(!rooms[i]["playersconfirmed"].includes(result[0])) {
rooms[i]["playersconfirmed"].push(result[0]);
sendRoomMsg(rooms[i].id, {"confirmedusers": rooms[i]["playersconfirmed"]});
}
if(rooms[i]["playersconfirmed"].length == rooms[i]["players"] && rooms[i]["players"] > 1) {
game.skipLobbyTimer(rooms[i].id);
}
}, randomnumber(3000, 6000));
newbot.initiateController();
});
}
});
}, randomnumber(1500 + (x*3000), 3000 + (x*5500)));
}
break;
}
}
}
function joinroom(map, createroom) {
let roommap = "";
let allmapnames = Object.keys(allmaps);
let randommap = false;
if(map !== "random" && allmapnames.includes(map)) {