-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
350 lines (277 loc) · 8.94 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
/* =============
// Data
============= */
// System variables
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 3000;
const connection = mongoose.connection;
// My varibles
let signedIn = false;
let signedInUser = "(not signed in)";
var namesList = [];
var dataDone = false;
// More system stuff
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + "/assets"));
app.set("view engine", "ejs");
mongoose.set("useFindAndModify", false);
// Mongoose things
mongoose.Promise = global.Promise;
// ACK TEXT PASSWORD
mongoose.connect("mongodb+srv://Squirrel:nCCJ0sQuQQ5qhGsn@test-user-data.daqv1.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", { useUnifiedTopology: true, useNewUrlParser: true });
// System things
connection.on("error", console.error.bind(console, "Connection error: "));
// Schemas
const useDataSchema = new mongoose.Schema({
name: String,
email: String,
passcode: String,
avatar: String,
dateAccountStarted: Number,
dashcoins: Number,
friends: [],
friendInvitesSent: [],
friendInvitesRecived: [],
gameSave: {}
});
const chatSchema = new mongoose.Schema({
input: String,
user: String,
avatar: Number,
datePosted: Object
});
const UserData = mongoose.model('UserData', useDataSchema);
const Chat = mongoose.model("Chat", chatSchema);
/* =============
// Processing
============= */
UserData.find((err, users) => {
if (err) return console.error(err);
for (i = 0; i < users.length; i++) {
if (!namesList.includes(users[i].name)) { namesList.push(users[i].name); }
}
}).then(item => { dataDone = true; });
setInterval(() => {
if (signedInUser != "(not signed in)") {
UserData.findOne({ name: signedInUser.name }, (err, user) => {
if (err) return console.error(err);
else { signedInUser = user; }
});
}
}, 2500);
/* =============
// Post requests
============= */
// Final destanations
app.get("/", (req, res) => {
UserData.find((err, users) => {
if (err) return console.error(err);
else {
Chat.find(function(err, found) {
if (err) { console.log(err); }
else { res.render("home", { user: signedInUser, UserData: UserData, users: users, foundItems: found }); }
});
}
});
});
app.get("/vegetable-dash", (req, res) => {
if (signedInUser !== "not signed in") {
UserData.find((err, users) => {
if (err) return console.error(err);
else {
Chat.find(function(err, found) {
if (err) { console.log(err); }
else { res.render("index", { user: signedInUser, UserData: UserData, users: users, foundItems: found }); }
});
}
});
}
else { res.render("signin", { error: { is: false } }); }
});
app.get("/chat", (req, res) => {
Chat.find(function(err, found) {
if (err) { console.log(err); }
else { res.render("chat", { user: signedInUser, UserData: UserData, foundItems: found }); }
})
});
// For video
const server = require("http").Server(app);
const { v4: uuidv4 } = require("uuid");
const io = require("socket.io")(server, { cors: { origin: "*" } });
const { ExpressPeerServer } = require("peer");
const peerServer = ExpressPeerServer(server, { debug: true, });
app.use("/peerjs", peerServer);
app.use(express.static("public"));
app.get("/start-video", (req, res) => {
res.redirect(`/video-chat/${uuidv4()}`);
});
app.get("/video-chat/:room", (req, res) => {
res.render("room", { roomId: req.params.room });
});
let listOfPeers = [];
peerServer.on("connection", (data) => {
listOfPeers.push(data.id);
});
io.on("connection", (socket) => {
// socket.broadcast.emit("user-connected", socket.id);
let userId;
socket.on("join-room", (roomId, usrId, userName) => {
userId = usrId;
socket.join(roomId);
socket.emit("listOfPeers", listOfPeers);
socket.broadcast.emit("user-connected", userId);
socket.on("message", (message) => {
io.to(roomId).emit("createMessage", message, userName);
});
});
socket.on("disconnect", () => {
if (listOfPeers.indexOf(5) > -1) { listOfPeers.splice(listOfPeers.indexOf(userId), 1); }
socket.broadcast.emit("user-disconnected", userId);
});
});
server.listen(port);
// Temporary landings
app.get("/account-created", (req, res) => {
res.send("You succesfully created your account!<br><a href='/'>Back to homepage</a>");
});
app.get("/create-account", (req, res) => {
res.render("signup");
});
app.get("/sign-in", (req, res) => {
if (!signedIn) { res.render("signin", { error: { is: false } }); }
else {
Chat.find(function(err, found) {
if (err) { console.log(err); }
else { res.redirect("/"); }
});
}
});
app.get("/sign-out", (req, res) => {
signedIn = false;
user = null;
signedInUser = "(not signed in)";
res.redirect("/");
});
/* =============
// Redirect Requests
============= */
// Create chat message
app.post("/chat-message", (req, res) => {
const newChat = new Chat({
input: req.body.message,
user: signedInUser.name,
avatar: signedInUser.avatar,
datePosted: new Date()
});
newChat.save(function(err){
if (err) {
console.log(err);
} else {
Chat.find(function(err, found) {
if (err) { console.log(err); }
else { res.redirect("back"); }
})
}
});
});
// Create account
app.post("/create-account", (req, res) => {
if (dataDone === false) { setTimeout(addNow, 250); }
else { addNow(); }
function addNow() {
if (namesList.includes(req.body.name)) { console.log("Choose a different name! (This one is taken!)"); return false; }
else if (parseInt(req.body.pscd) === NaN) { console.log("Passcode must contain only numbers!"); return false; }
else {
var newUser = new UserData({ name: req.body.name, email: req.body.emil, passcode: req.body.pscd, dateAccountStarted: Date.now() });
newUser.save(function (err, newUser) {
if (err) return console.error(err);
});
}
}
res.redirect("/account-created");
});
// Update avatar
app.post("/choose-avatar", (req, res) => {
UserData.findOneAndUpdate(
{ name: signedInUser.name },
{ avatar: req.body.avatar },
{ new: true },
(err, doc) => { if (err) return console.error(err); }
);
UserData.findOne({ name: signedInUser.name }, (err, user) => {
if (err) return console.error(err);
else {
signedInUser = user;
res.redirect("/");
}
});
});
// IMPORTANT: This is sign in
app.post("/play", (req, res) => {
UserData.findOne({ name: req.body.name, email: req.body.emil, passcode: req.body.pscd }, (err, user) => {
if (err) return console.error(err);
if (!user) { res.render("signin", { error: { is: true, more: "The data dosen't line up. Try again!" } }); }
else if (user) {
signedIn = true;
signedInUser = user;
Chat.find(function(err, found) {
if (err) { console.log(err); }
else { res.redirect("back"); }
});
}
});
});
// Add new friends - NEEDS FIXING
app.post("/send-friend-request", (req, res) => {
let thisUser = req.body.thisUser;
let thatUser = req.body.thatUser;
UserData.findOneAndUpdate(
{ name: thisUser },
{ $addToSet: { friendInvitesSent: thatUser } },
{ new: true },
(err, doc) => { if (err) return console.error(err); }
);
UserData.findOneAndUpdate(
{ name: thatUser },
{ $addToSet: { friendInvitesRecived: thisUser } },
{ new: true },
(err, doc) => { if (err) return console.error(err); }
);
res.redirect("/");
});
app.post("/accept-friend-request", (req, res) => {
let thisUser = req.body.thisUser;
let thatUser = req.body.thatUser;
UserData.findOneAndUpdate(
{ name: thisUser },
{ $push: { friends: thatUser }, $pull: { friendInvitesSent: thatUser, friendInvitesRecived: thatUser }, $inc: { dashcoins: 5 } },
{ new: true },
(err, doc) => { if (err) return console.error(err); }
);
UserData.findOneAndUpdate(
{ name: thatUser },
{ $push: { friends: thisUser }, $pull: { friendInvitesSent: thisUser, friendInvitesRecived: thisUser }, $inc: { dashcoins: 5 } },
{ new: true },
(err, doc) => { if (err) return console.error(err); }
);
res.redirect("/");
});
// Save Vegetable Dash data - NEEDS FIXING
app.post("/save-vegetable-dash", (req, res) => {
let inputSave = req.body;
UserData.findOneAndUpdate(
{ name: signedInUser.name },
{ gameSave: inputSave },
{ new: true },
function (err, doc) { if (err) { return console.error(err); } else { res.send("Misson Success!"); } }
);
});
// That's it
// app.listen(port);
// vegetable dash save
// fix friends (if broken)
// style friends modal