-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
161 lines (150 loc) · 4.42 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
const express = require("express");
const mongoose = require("mongoose");
const helmet = require("helmet");
const adminRouter = require("./admin");
const Puzzle = require("./models/puzzle");
const Team = require("./models/team");
const Winner = require("./models/winner");
require("dotenv").config();
const app = express();
app.set("view engine", "ejs");
const port = process.env.PORT || 8080;
const dbURL = process.env.DB_URL || "mongodb://127.0.0.1:27017/scavenger-hunt";
mongoose
.connect(dbURL)
.then(() => {
console.log("CONNECTED TO DATABASE");
app.listen(port, () => {
console.log(`LISTENING AT PORT ${port}`);
});
})
.catch((err) => {
console.log(err);
});
if (process.env.NODE_ENV !== "development") {
console.log("HELMET SECURED");
app.use(
helmet({
contentSecurityPolicy: {
directives: { "img-src": ["'self'", "res.cloudinary.com"] },
},
})
);
}
app.use(express.static("public"));
app.use(express.urlencoded({ extended: false }));
app.use("/admin", adminRouter);
app.get("/", async (req, res) => {
const winners = await Winner.find({}).sort({ createdAt: 1 });
res.render("home", { winners });
});
app.get("/:id", async (req, res) => {
try {
const puzzle = await Puzzle.findById(req.params.id);
if (puzzle.number <= 5) {
return res.render(String(puzzle.number), {
id: puzzle.id,
number: puzzle.number,
title: puzzle.title,
question: puzzle.question,
input: req.query.input || null,
incorrect: req.query.incorrect || null,
});
}
return res.render("cipher", {
id: puzzle.id,
number: puzzle.number,
title: puzzle.title,
question: puzzle.question,
code: puzzle.code,
input: req.query.input || null,
incorrect: req.query.incorrect || null,
});
} catch (e) {
return res.status(404).send("Not Found");
}
});
// Route for posting team ID
app.post("/win", async (req, res) => {
try {
const { questionID, id, answer, name } = req.body;
const puzzle = await Puzzle.findById(questionID);
const teamExists = await Team.exists({ _id: id });
const winnerExists = await Winner.exists({ teamID: id });
const nameTaken = (await Winner.find({}))
.map((winner) => winner.name)
.includes(name);
if (!puzzle || !puzzle.answer.includes(answer)) {
return res.status(401).send("UNAUTHORIZED REQUEST");
}
if (!teamExists || winnerExists || nameTaken) {
return res.render("win", {
questionID,
answer,
teamNameInput: name,
teamIDInput: id,
invalidID: !teamExists || winnerExists || null,
nameTaken: nameTaken || null,
});
}
const winner = new Winner({ teamID: req.body.id, name: req.body.name });
await winner.save();
return res.render("success");
} catch {
return res.send("ERROR");
}
});
// Route to check string-answer against an array of correct answers
app.post("/:id/check-string", async (req, res) => {
try {
const puzzle = await Puzzle.findById(req.params.id);
if (!puzzle) return res.send("NOT FOUND");
const { id, number, answer, reward } = puzzle;
if (answer.includes(req.body.answer.toLowerCase())) {
if (number === 8) {
return res.render("win", {
questionID: id,
answer: req.body.answer,
teamNameInput: null,
teamIDInput: null,
nameTaken: null,
invalidID: null,
});
}
return res.render("reward", {
reward: reward[Math.floor(Math.random() * 3)],
});
}
return res.redirect(
`/${req.params.id}?incorrect=true&input=${req.body.answer}`
);
} catch (e) {
return res.status(500).send("SERVER ERROR");
}
});
// Route to check answer as an array
app.post("/:id/check-array", async (req, res) => {
try {
const puzzle = await Puzzle.findById(req.params.id);
if (!puzzle) return res.send("NOT FOUND");
const { answer, reward } = puzzle;
const incorrect = [];
for (let i = 0; i < answer.length; i += 1) {
if (!(req.body[i] === answer[i])) {
incorrect.push(i);
}
}
if (incorrect.length) {
return res.redirect(
`/${req.params.id}?incorrect=${incorrect}&input=${Object.values(
req.body
)}`
);
}
return res.render("reward", {
reward: reward[Math.floor(Math.random() * 3)],
});
} catch {
return res.status(500).send("SERVER ERROR");
}
});