-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
103 lines (93 loc) · 2.77 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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const Level = require("./models/Level");
const User = require("./models/User");
const levelInfo = require("./levelInfo");
const session = require("express-session");
mongoose.connect(
"mongodb+srv://aryan:[email protected]/?retryWrites=true&w=majority"
);
mongoose.connection.on(
"error",
console.error.bind(console, "connection error")
);
mongoose.connection.once("open", () => console.log("DB connected"));
const path = require("path");
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
app.use(session({ secret: "spicefarm" }));
app.use("/levels", (req, res, next) => {
if (!req.session.user_id) {
res.redirect("/");
} else next();
});
app.get("/", async (req, res) => {
res.render("home", { error: req.query.error });
});
app.get("/levels", async (req, res) => {
const user = await User.findById(req.session.user_id).populate("levels");
const levels = user.levels;
res.render("index", { levels });
});
app.post("/levels/:id", async (req, res) => {
const currentLevel = await Level.findByIdAndUpdate(req.params.id, {
completed: true,
});
if (levelInfo.length === currentLevel.number) {
res.redirect("/gamewon");
} else {
const nextLevel = await Level.findOneAndUpdate(
{
number: currentLevel.number + 1,
user: req.session.user_id,
},
{ locked: false }
);
res.redirect(`/levels/${nextLevel._id}`);
}
});
app.get("/levels/:id", async (req, res) => {
const level = await Level.findById(req.params.id);
res.render("level", { level });
});
app.post("/", async (req, res) => {
if (await User.findOne({ username: req.body.username }))
res.redirect("/?error=true");
else {
const user = new User({ username: req.body.username });
for (let i = 0; i < levelInfo.length; i++) {
const level = new Level({
number: i + 1,
difficulty: levelInfo[i].difficulty,
grid: levelInfo[i].grid,
correct: levelInfo[i].correct,
time: levelInfo[i].time,
locked: levelInfo[i].locked,
completed: false,
user: user._id,
});
user.levels.push(level);
await level.save();
}
await user.save();
req.session.user_id = user._id;
res.redirect("/levels");
}
});
app.get("/gamewon", async (req, res) => {
const lastLevel = await Level.findOne({
number: levelInfo.length,
user: req.session.user_id,
});
if (lastLevel && lastLevel.completed) {
res.render("gamewon");
} else {
res.redirect("/levels");
}
});
app.listen(3000, () => {
console.log("Serving on 3000");
});