-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
678 lines (568 loc) · 26.3 KB
/
index.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
const Express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const { Password, cacher, createDatabase, useJwt } = require("./utils");
require("dotenv").config();
if (!process.env.JWT_SECRET) throw new Error("Missing Environment Variable JWT_SECRET");
const app = Express();
const nginxFormat =
':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status';
app.use(morgan(nginxFormat));
const db = createDatabase("zypher.sqlite3");
app.use(Express.json());
app.use(cors('*'));
// cors for zypher.cyscomvit.com
// app.use(cors({
// origin: 'https://zypher.cyscomvit.com',
// optionsSuccessStatus: 200
// }));
app.set('view engine', 'ejs');
app.get("/k3qfqzo0hgde2lgfblgwm45pp/forms", async (req, res) => {
res.render('forms')
})
const protection = 'Zypher$202'
app.get('/k3qfqzo0hgde2lgfblgwm45pp/index', async (req, res) => {
let regTeamDetails = db.prepare("SELECT username, member_1_name, member_1_regno, member_2_name, member_2_regno, member_3_name, member_3_regno, answered_levels FROM users").all();
// we will get the challenge completed users based on answered_levels
console.log(regTeamDetails)
let challenge_1_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("1");
});
let challenge_2_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("2");
});
let challenge_3_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("3");
});
let challenge_4_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("4");
});
let challenge_5_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("5");
});
let challenge_6_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("6");
});
let challenge_7_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("7");
});
let challenge_8_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("8");
});
let challenge_9_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("9");
});
let challenge_10_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("10");
});
let challenge_11_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("11");
});
let challenge_12_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("12");
});
let challenge_13_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("13");
});
let challenge_14_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("14");
});
let challenge_15_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("15");
});
let challenge_16_completed = regTeamDetails.filter((team) => {
return team.answered_levels.includes("16");
});
res.render('index', {
challenge_1_completed: challenge_1_completed,
challenge_2_completed: challenge_2_completed,
challenge_3_completed: challenge_3_completed,
challenge_4_completed: challenge_4_completed,
challenge_5_completed: challenge_5_completed,
challenge_6_completed: challenge_6_completed,
challenge_7_completed: challenge_7_completed,
challenge_8_completed: challenge_8_completed,
challenge_9_completed: challenge_9_completed,
challenge_10_completed: challenge_10_completed,
challenge_11_completed: challenge_11_completed,
challenge_12_completed: challenge_12_completed,
challenge_13_completed: challenge_13_completed,
challenge_14_completed: challenge_14_completed,
challenge_15_completed: challenge_15_completed,
challenge_16_completed: challenge_16_completed,
regTeamDetails: regTeamDetails,
})
})
app.post("/register", async (req, res) => {
const { username = "", password = "", avatar = "", member_1_name, member_1_regno, member_2_name, member_2_regno, member_3_name, member_3_regno } = req.body;
console.log(req.body);
try {
// so 3 cases
// one case is where only one member name and regno is ther
// second case is where two member name and regno is there
// third case is where all three member name and regno is there
if (member_1_name && member_1_regno && member_2_name && member_2_regno && member_3_name && member_3_regno) {
const user = db
.prepare(
"INSERT INTO users (username, password, avatar, member_1_name, member_1_regno, member_2_name, member_2_regno, member_3_name, member_3_regno) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING username, avatar, level"
)
.get(username, await Password.hash(password), avatar, member_1_name, member_1_regno, member_2_name, member_2_regno, member_3_name, member_3_regno);
console.log(user)
res.json(user);
}
else if (member_1_name && member_1_regno && member_2_name && member_2_regno) {
const user = db
.prepare(
"INSERT INTO users (username, password, avatar, member_1_name, member_1_regno, member_2_name, member_2_regno) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING username, avatar, level"
)
.get(username, await Password.hash(password), avatar, member_1_name, member_1_regno, member_2_name, member_2_regno);
console.log(user)
res.json(user);
}
else if (member_1_name && member_1_regno) {
const user = db
.prepare(
"INSERT INTO users (username, password, avatar, member_1_name, member_1_regno) VALUES (?, ?, ?, ?, ?) RETURNING username, avatar, level"
)
.get(username, await Password.hash(password), avatar, member_1_name, member_1_regno);
console.log(user)
res.json(user);
}
else {
res.status(400).json({ error: "Please enter atleast one member name and regno" });
}
// const user = db
// .prepare(
// "INSERT INTO users (username, password, avatar) VALUES (?, ?, ?) RETURNING username, avatar, level"
// )
// .get(username, await Password.hash(password), avatar);
// res.json(user);
} catch (err) {
if (err.code === "SQLITE_CONSTRAINT_CHECK")
return res.status(400).json({ error: "Username must be between 3 and 20 characters" });
if (err instanceof Password.PasswordLengthError)
return res.status(400).json({ error: "Password must be between 8 and 100 characters" });
if (err.code === "SQLITE_CONSTRAINT")
return res.status(400).json({ error: "Username already taken" });
res.status(500).json({ error: "Internal server error" });
console.log(err);
}
});
app.post("/registerBackend", async (req, res) => {
const { username = "", password = "", avatar = "", member_1_name, member_1_regno, member_2_name, member_2_regno, member_3_name, member_3_regno, protection_client } = req.body;
console.log(req.body);
if (protection_client != protection) {
console.log("Failed here")
return res.status(401).json({ error: "Invalid password" });
}
try {
// so 3 cases
// one case is where only one member name and regno is ther
// second case is where two member name and regno is there
// third case is where all three member name and regno is there
if (member_1_name && member_1_regno && member_2_name && member_2_regno && member_3_name && member_3_regno) {
const user = db
.prepare(
"INSERT INTO users (username, password, avatar, member_1_name, member_1_regno, member_2_name, member_2_regno, member_3_name, member_3_regno) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING username, avatar, level"
)
.get(username, await Password.hash(password), avatar, member_1_name, member_1_regno, member_2_name, member_2_regno, member_3_name, member_3_regno);
console.log(user)
res.json(user);
}
else if (member_1_name && member_1_regno && member_2_name && member_2_regno) {
const user = db
.prepare(
"INSERT INTO users (username, password, avatar, member_1_name, member_1_regno, member_2_name, member_2_regno) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING username, avatar, level"
)
.get(username, await Password.hash(password), avatar, member_1_name, member_1_regno, member_2_name, member_2_regno);
console.log(user)
res.json(user);
}
else if (member_1_name && member_1_regno) {
const user = db
.prepare(
"INSERT INTO users (username, password, avatar, member_1_name, member_1_regno) VALUES (?, ?, ?, ?, ?) RETURNING username, avatar, level"
)
.get(username, await Password.hash(password), avatar, member_1_name, member_1_regno);
console.log(user)
res.json(user);
}
else {
res.status(400).json({ error: "Please enter atleast one member name and regno" });
}
// const user = db
// .prepare(
// "INSERT INTO users (username, password, avatar) VALUES (?, ?, ?) RETURNING username, avatar, level"
// )
// .get(username, await Password.hash(password), avatar);
// res.json(user);
} catch (err) {
if (err.code === "SQLITE_CONSTRAINT_CHECK")
return res.status(400).json({ error: "Username must be between 3 and 20 characters" });
if (err instanceof Password.PasswordLengthError)
return res.status(400).json({ error: "Password must be between 8 and 100 characters" });
if (err.code === "SQLITE_CONSTRAINT")
return res.status(400).json({ error: "Username already taken" });
res.status(500).json({ error: "Internal server error" });
console.err(err);
}
});
const [login, authorize] = useJwt(process.env.JWT_SECRET, "HS256", "2d");
app.post("/login", async (req, res) => {
// team username and password for authentication
const { username = "", password = "" } = req.body;
const user = db.prepare("SELECT * FROM users WHERE username = ?").get(username);
if (!user || !(await Password.verify({ hashed: user.password, password }))) {
return res.status(401).json({ error: "Invalid username or password" });
}
const token = login({ username });
res.json({ token });
});
app.get("/me", authorize, (req, res) => {
let data = db.prepare("SELECT username, level, scene_reached, points, answered_levels, answered_special_challenges FROM users WHERE username = ?").get(req.username);
console.log(data.answered_levels)
res.json(data);
});
app.post("/question", authorize, (req, res) => {
// res.json(
// db
// .prepare(
// "SELECT level, text, image FROM questions WHERE level = (SELECT level FROM users WHERE username = ?)"
// )
// .get(req.username)
// );
const { question_level = "" } = req.body;
res.json(
db
.prepare(
"SELECT level, scene, points, hidden, text, url FROM questions WHERE level = ?"
)
.get(question_level)
)
});
app.get("/special-question", authorize, (req, res) => {
let question1 = db.prepare("SELECT level, text, link, points FROM special_challenges WHERE level = 1").get();
let question2 = db.prepare("SELECT level, text, link, points FROM special_challenges WHERE level = 2").get();
let question3 = db.prepare("SELECT level, text, link, points FROM special_challenges WHERE level = 3").get();
let question4 = db.prepare("SELECT level, text, link, points FROM special_challenges WHERE level = 4").get();
let question5 = db.prepare("SELECT level, text, link, points FROM special_challenges WHERE level = 5").get();
console.log(question4)
res.json([question1, question2, question3, question4, question5]);
})
app.post("/add-special-question", (req, res) => {
let { level, text, url, points, answer, protection_client } = req.body;
if (protection_client != protection) {
return res.status(401).json({ error: "Invalid password" });
}
let link = url
console.log(req.body)
res.json(
db
.prepare(
"INSERT INTO special_challenges (level, text, link, points, answer) VALUES (?, ?,?,?, ?) RETURNING *"
)
.get(level, text, link, points, answer)
);
});
app.post("/delete-special-question", (req, res) => {
// if (req.query.password != process.env.ADMIN_PASSWORD) {
// return res.status(401).json({ error: "Invalid password" });
// }
let level = req.body.level;
console.log(level)
res.json(db.prepare("DELETE FROM special_challenges WHERE level = ?").run(level));
});
app.post("/special-answer", authorize, (req, res) => {
// console.log("The details are:", req.body)
const { answer = "", question_level = "" } = req.body;
const user = db.prepare("SELECT * FROM users WHERE username = ?").get(req.username);
const question = db.prepare("SELECT * FROM special_challenges WHERE level = ?").get(question_level);
// console.log("User details are:", user)
let points = user.points;
points = points + question.points;
// getting the correct levels
const currentLevels = (user.answered_special_challenges || "").split(",").map(Number);
console.log(currentLevels)
if (!question) {
return res.json({ error: "No more questions" });
}
if (answer !== question.answer) {
return res.json({ correct: false });
}
// adding the current level to the list of answered levels since the answer is correct
if (!currentLevels.includes(question.level)) {
currentLevels.push(question.level);
db.prepare("UPDATE users SET answered_special_challenges = ?, points = ? WHERE username = ?").run(
currentLevels.join(","), points, user.username
);
console.log("answer is correct, points are: ", points)
return res.json({ correct: true });
}
else {
console.log("already answered")
return res.json({ correct: "already answered" });
}
});
app.post("/answer", authorize, (req, res) => {
// this section will work only if user submits answer
const { answer = "", question_level = "" } = req.body;
const user = db.prepare("SELECT * FROM users WHERE username = ?").get(req.username);
const question = db.prepare("SELECT * FROM questions WHERE level = ?").get(question_level);
let points = user.points;
points = points + question.points;
// getting the correct levels
const currentLevels = (user.answered_levels || "").split(",").map(Number);
if (!question) {
return res.json({ error: "No more questions" });
}
// db.prepare("INSERT INTO attempts (username, level, attempt) VALUES (?, ?, ?)").run(
// user.username,
// user.level,
// answer
// );
if (answer !== question.answer) {
return res.json({ correct: false });
}
// adding the current level to the list of answered levels since the answer is correct
if (!currentLevels.includes(question.level)) {
currentLevels.push(question.level);
}
else if (currentLevels.includes(question.level)) {
return res.json({ correct: "already answered" });
}
console.log(currentLevels.join(","))
db.prepare("UPDATE users SET answered_levels = ?, points = ? WHERE username = ?").run(
currentLevels.join(","), points,
user.username
);
// now updating the user scene and level
if (user.level === 1 && user.scene_reached === 1 && question.scene === 1) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 2, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
// he/she solves either chest level 2 or 3, which increments the user level
else if (user.scene_reached === 2 && question.scene === 2) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 3, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
// similarly for others
else if (user.scene_reached === 3 && question.scene === 3) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 4, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
else if (user.scene_reached === 4 && question.scene === 4) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 5, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
else if (user.scene_reached === 5 && question.scene === 5) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 6, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
console.log("scene 6 reached")
}
else if (user.scene_reached === 6 && question.scene === 6) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 7, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
console.log("scene 7 reached")
}
else if (user.scene_reached === 7 && question.scene === 7) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 8, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
else if (user.scene_reached === 8 && question.scene === 8) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 9, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
else if (user.scene_reached === 9 && question.scene === 9) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 10, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
// this logic is for user coming back after completing the game, to climb up the leaderboard. only level is updated, scene remains same
else if (user.scene_reached != question.scene) {
db.prepare(
"UPDATE users SET level = level + 1, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
// for now scene 5 is finish line, if scene 5 is reached, game ends
// db.prepare(
// "UPDATE users SET level = level + 1, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
// ).run(user.username);
res.json({ correct: true });
});
const leaderboardStmt = db.prepare(
"SELECT username, level FROM users ORDER BY level DESC, reachedAt ASC"
);
const leaderboard = cacher(60)(() => leaderboardStmt.all());
// app.get("/leaderboard", (_, res) => res.json(leaderboard()));
app.get('/leaderboard', (req, res) => {
const data = db.prepare("SELECT username, points FROM users ORDER BY points DESC, reachedAt ASC").all();
res.json(data);
})
app.post("/add-question", (req, res) => {
// if (req.query.password != process.env.ADMIN_PASSWORD) {
// return res.status(401).json({ error: "Invalid password" });
// }
const { level, scene, points, text, url, answer, protection_client } = req.body;
if (protection_client != protection) {
return res.status(401).json({ error: "Invalid password" });
}
res.json(
db
.prepare(
"INSERT INTO questions (level, scene, points, text, url, answer) VALUES (?, ?, ?, ?, ?, ?) RETURNING *"
)
.get(level, scene, points, text, url, answer)
);
});
app.post("/delete-question", (req, res) => {
// if (req.query.password != process.env.ADMIN_PASSWORD) {
// return res.status(401).json({ error: "Invalid password" });
// }
const { level, protection_client } = req.body;
if (protection_client != protection) {
return res.status(401).json({ error: "Invalid password" });
}
res.json(db.prepare("DELETE FROM questions WHERE level = ?").run(level));
});
app.get("/all-questions", (req, res) => {
// if (req.query.password != process.env.ADMIN_PASSWORD) {
// return res.status(401).json({ error: "Invalid password" });
// }
res.json(db.prepare("SELECT * FROM questions").all());
});
app.post("/delete-team", (req, res) => {
// if (req.query.password != process.env.ADMIN_PASSWORD) {
// return res.status(401).json({ error: "Invalid password" });
// }
const { username, protection_client } = req.body;
if (protection_client != protection) {
return res.status(401).json({ error: "Invalid password" });
}
res.json(db.prepare("DELETE FROM users WHERE username = ?").run(username));
});
app.post("/edit-username", (req, res) => {
// if (req.query.password != process.env.ADMIN_PASSWORD) {
// return res.status(401).json({ error: "Invalid password" });
// }
const { username, newUsername } = req.body;
res.json(
db.prepare("UPDATE users SET username = ? WHERE username = ?").run(newUsername, username)
);
});
app.post("/CompleteChallengeBackend", (req, res) => {
// this section will work only if user submits answer
const { username = "", question_level = "", protection_client } = req.body;
if (protection_client != protection) {
return res.status(401).json({ error: "Invalid password" });
}
const user = db.prepare("SELECT * FROM users WHERE username = ?").get(username);
const question = db.prepare("SELECT * FROM questions WHERE level = ?").get(question_level);
console.log("The user details are:", user)
let points = user.points;
points = points + question.points;
// getting the correct levels
const currentLevels = (user.answered_levels || "").split(",").map(Number);
if (!question) {
return res.json({ error: "No more questions" });
}
// db.prepare("INSERT INTO attempts (username, level, attempt) VALUES (?, ?, ?)").run(
// user.username,
// user.level,
// answer
// );
// adding the current level to the list of answered levels since the answer is correct
if (!currentLevels.includes(question.level)) {
currentLevels.push(question.level);
}
else if (currentLevels.includes(question.level)) {
return res.json({ correct: "already answered" });
}
console.log(currentLevels.join(","))
db.prepare("UPDATE users SET answered_levels = ?, points = ? WHERE username = ?").run(
currentLevels.join(","), points,
user.username
);
// now updating the user scene and level
if (user.level === 1 && user.scene_reached === 1 && question.scene === 1) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 2, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
// he/she solves either chest level 2 or 3, which increments the user level
else if (user.scene_reached === 2 && question.scene === 2) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 3, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
// similarly for others
else if (user.scene_reached === 3 && question.scene === 3) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 4, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
else if (user.scene_reached === 4 && question.scene === 4) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 5, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
else if (user.scene_reached === 5 && question.scene === 5) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 6, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
console.log("scene 6 reached")
}
else if (user.scene_reached === 6 && question.scene === 6) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 7, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
console.log("scene 7 reached")
}
else if (user.scene_reached === 7 && question.scene === 7) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 8, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
else if (user.scene_reached === 8 && question.scene === 8) {
db.prepare(
"UPDATE users SET level = level + 1, scene_reached = 9, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
// this logic is for user coming back after completing the game, to climb up the leaderboard. only level is updated, scene remains same
else if (user.scene_reached != question.scene) {
db.prepare(
"UPDATE users SET level = level + 1, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
).run(user.username);
}
// for now scene 5 is finish line, if scene 5 is reached, game ends
// db.prepare(
// "UPDATE users SET level = level + 1, reachedAt = CURRENT_TIMESTAMP WHERE username = ?"
// ).run(user.username);
res.json({ correct: true });
});
app.post("/edit-password", (req, res) => {
// if (req.query.password != process.env.ADMIN_PASSWORD) {
// return res.status(401).json({ error: "Invalid password" });
// }
const { username, password } = req.body;
res.json(
db.prepare("UPDATE users SET password = ? WHERE username = ?").run(password, username)
);
});
app.get("/attempts", (req, res) => {
// if (req.query.password != process.env.ADMIN_PASSWORD) {
// return res.status(401).json({ error: "Invalid password" });
// }
res.json(db.prepare("SELECT * FROM attempts").all());
});
app.get("/test", (_, res) => {
res.send({ Message: "Server is up and running" });
});
app.listen(8080, console.log("Server started on port 8080."));