-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
115 lines (99 loc) · 3.56 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
const express = require("express");
const app = express();
const cookieParser = require("cookie-parser");
const mongoose = require("mongoose");
const Room = require("./models/Room");
const User = require("./models/User");
const roomRoutes = require("./routes/room");
const roomAdminRoutes = require("./routes/roomAdmin");
mongoose.connect(
process.env.MONGO_URI || "mongodb://localhost:27017/assassin",
{ useNewUrlParser: true, useUnifiedTopology: true },
function () {
console.log("MongoDB Connected");
}
);
mongoose.Promise = Promise;
app.set("view engine", "ejs");
app.use(cookieParser());
app.use(express.static(__dirname + "/public"));
app.use(express.urlencoded({ extended: true }));
app.get("/", (req, res) => {
return res.render("index", { title: "Assassin Online Game" });
});
app.get("/createRoom", (req, res) => {
let roomCode = Math.floor(Math.random() * 100000);
const roomKey = Math.floor(Math.random() * 10000000);
// Check if room already exists
Room.findOne({ roomCode }, (err, foundRoom) => {
if (err || foundRoom) return res.send("Please try again...");
const newRoom = {
roomCode,
participants: [],
roomKey,
acceptingParticipants: true,
isPlaying: false,
};
Room.create(newRoom, (err, createdRoom) => {
if (err) return res.send("An error occurred");
console.log(`Room Created: ${roomCode}`);
// TODO: set timeout to delete room after 150 mins
res.cookie("roomId", createdRoom._id, {
expires: new Date(Date.now() + 9000000),
});
res.cookie("roomKey", roomKey, {
expires: new Date(Date.now() + 9000000),
});
return res.redirect("/roomAdmin");
});
});
});
app.post("/joinRoom", (req, res) => {
const { roomCode, username } = req.body;
if (!username) return res.send("Bad Username");
Room.findOne({ roomCode }, (err, foundRoom) => {
if (err) return res.send("Internal Server Error");
if (!foundRoom || !foundRoom.acceptingParticipants)
return res.send("Room not found or is no longer accepting participants");
User.findOne({ username, roomCode }, (err, foundUser) => {
if (err) return res.send("Internal Server Error");
if (foundUser) {
const { userSecret } = req.cookies;
if (userSecret && userSecret === foundUser.userSecret)
return res.redirect("/room");
return res.send("User already exists in this room");
}
const newUser = {
username,
userSecret: Math.floor(Math.random() * 10000000),
roomCode,
roomId: foundRoom._id,
role: foundRoom.rolesAssigned ? "villager" : null,
};
User.create(newUser, (err, createdUser) => {
if (err) return res.send("Internal Server Error");
console.log(`${username} joined room ${roomCode}`);
res.cookie("username", username, {
expires: new Date(Date.now() + 9000000),
});
res.cookie("userSecret", createdUser.userSecret, {
expires: new Date(Date.now() + 9000000),
});
res.cookie("roomId", foundRoom._id, {
expires: new Date(Date.now() + 9000000),
});
res.cookie("userId", createdUser._id, {
expires: new Date(Date.now() + 9000000),
});
return res.redirect("/room");
});
});
});
});
app.use("/room", roomRoutes);
app.use("/roomAdmin", roomAdminRoutes);
// TODO: reset room, auto delete room, optional password for room
const listenPort = process.env.PORT || 8080;
app.listen(listenPort, () => {
console.log(`Server running on http://localhost:${listenPort}`);
});