forked from Murrium123/FriendFind
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
358 lines (317 loc) · 13.3 KB
/
server.js
File metadata and controls
358 lines (317 loc) · 13.3 KB
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
const express = require('express')
const session = require('express-session')
const mongoose = require('mongoose')
const path = require('path')
const port = process.env.PORT || 3019;
const app = express();
app.use(express.static(__dirname));
app.use(express.urlencoded({ extended: true }))
app.use(session({
secret: process.env.SESSION_SECRET || 'secret key',
resave: false,
saveUninitialized: false,
cookie: { secure: false },
name: process.env.SESSION_NAME || 'sessionId',
}));
mongoose.connect('mongodb+srv://adamchakour05:C5ygFeBcxpPDaNg2@ffcluster.8ojuf.mongodb.net/FFdb', {
useNewUrlParser: true,
useUnifiedTopology: true
})
const db = mongoose.connection
db.once('open', () => {
console.log("Connected to MongoDB")
})
const userSchema = new mongoose.Schema({
name: String,
pronoun: {
type: String,
enum: ['he/him', 'she/her', 'they/them'],
},
interest1: { type: String, default: '' },
interest2: { type: String, default: '' },
interest3: { type: String, default: '' },
question1: { type: String, default: '' },
answer1: { type: String, default: '' },
wrong1a: { type: String, default: '' },
wrong1b: { type: String, default: '' },
wrong1c: { type: String, default: '' },
question2: { type: String, default: '' },
answer2: { type: String, default: '' },
wrong2a: { type: String, default: '' },
wrong2b: { type: String, default: '' },
wrong2c: { type: String, default: '' },
question3: { type: String, default: '' },
answer3: { type: String, default: '' },
wrong3a: { type: String, default: '' },
wrong3b: { type: String, default: '' },
wrong3c: { type: String, default: '' },
fR: { type: [mongoose.Schema.Types.ObjectId], default: [] } //friend requests
});
const Users = mongoose.model("data", userSchema)
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'FFmainpage.html'))
})
app.post('/post', async (req, res) => {
const { name, pronoun } = req.body
const user = new Users({
name,
pronoun
})
try {
await user.save()
console.log(user)
req.session.currentUserId = user._id;
res.redirect(`FFInterestPage.html?currentUserId=${user._id}`)
} catch(error) {
console.log(error)
res.send("Form Submission Failed")
}
})
app.post('/submitAnswers', async (req, res) => {
console.log(req.body);
const { interest1, question1, answer1, wrong1a, wrong1b, wrong1c, interest2, question2, answer2, wrong2a, wrong2b, wrong2c, interest3, question3, answer3, wrong3a, wrong3b, wrong3c } = req.body;
const currentUserId = req.session.currentUserId;
try {
const user = await Users.findById(currentUserId);
if (!user) {
return res.status(404).send("User not found");
}
if (!interest1 || !interest2 || !interest3) {
return res.status(400).send("Missing interests in form");
}
user.interest1 = interest1;
user.question1 = question1;
user.answer1 = answer1;
user.wrong1a = wrong1a;
user.wrong1b = wrong1b;
user.wrong1c = wrong1c;
user.interest2 = interest2;
user.question2 = question2;
user.answer2 = answer2;
user.wrong2a = wrong2a;
user.wrong2b = wrong2b;
user.wrong2c = wrong2c;
user.interest3 = interest3;
user.question3 = question3;
user.answer3 = answer3;
user.wrong3a = wrong3a;
user.wrong3b = wrong3b;
user.wrong3c = wrong3c;
await user.save()
res.redirect(`/showuser?currentUserId=${currentUserId}`);
} catch(err) {
console.log(err)
res.status(500).send("Failed to submit answers");
}
});
app.get('/showuser', async (req, res) => {
const currentUserId = req.session.currentUserId;
try {
const currentUser = await Users.findById(currentUserId);
if (!currentUser) {
return res.status(404).send("Current user not found.");
}
// Check if there are friend requests
if (currentUser.fR.length > 0) {
const otherUsers = await Users.find({ _id: { $ne: currentUserId } });
// Check for mutual friend requests
for (const otherUser of otherUsers) {
if (currentUser.fR.includes(otherUser._id) && otherUser.fR.includes(currentUserId)) {
return res.redirect('/matchpage');
}
}
}
// Find random user (excluding the current user)
const otherUsers = await Users.find({ _id: { $ne: currentUserId } }); // Ensure this line is here
const randomUser = otherUsers[Math.floor(Math.random() * otherUsers.length)];
const otherUserId = randomUser._id;
req.session.otherUserId = otherUserId;
// Render HTML page with loading screen, then show the user
res.send(`
<html>
<head>
<title>FriendFind</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<script>
window.onload = function() {
setTimeout(function() {
document.getElementById('loading').style.display = 'none';
document.getElementById('user-info').style.display = 'block';
}, 2000);
};
</script>
<style>
#user-info { display: none; }
#loading { font-size: 24px; font-weight: bold; text-align: center; padding: 20px; }
</style>
</head>
<body>
<div id="loading">Looking for Friend...</div>
<div id="user-info">
<center>
<h1>FriendFind</h1>
<h2>Meet ${randomUser.name}!</h2>
<p>Pronouns: ${randomUser.pronoun}</p>
<p>Interests: ${randomUser.interest1}, ${randomUser.interest2}, ${randomUser.interest3}</p>
<h2>Questions:</h2>
<button class="question-button" onclick="window.location.href='/question?question=${encodeURIComponent(randomUser.question1)}&correctAnswer=${encodeURIComponent(randomUser.answer1)}&wrongAnswer1=${encodeURIComponent(randomUser.wrong1a)}&wrongAnswer2=${encodeURIComponent(randomUser.wrong1b)}&wrongAnswer3=${encodeURIComponent(randomUser.wrong1c)}'">${randomUser.question1}</button>
<br>
<button class="question-button" onclick="window.location.href='/question?question=${encodeURIComponent(randomUser.question2)}&correctAnswer=${encodeURIComponent(randomUser.answer2)}&wrongAnswer1=${encodeURIComponent(randomUser.wrong2a)}&wrongAnswer2=${encodeURIComponent(randomUser.wrong2b)}&wrongAnswer3=${encodeURIComponent(randomUser.wrong2c)}'">${randomUser.question2}</button>
<br>
<button class="question-button" onclick="window.location.href='/question?question=${encodeURIComponent(randomUser.question3)}&correctAnswer=${encodeURIComponent(randomUser.answer3)}&wrongAnswer1=${encodeURIComponent(randomUser.wrong3a)}&wrongAnswer2=${encodeURIComponent(randomUser.wrong3b)}&wrongAnswer3=${encodeURIComponent(randomUser.wrong3c)}'">${randomUser.question3}</button>
</center>
</div>
</body>
</html>
`);
} catch (err) {
console.error(err); // Log the error to console for debugging
res.status(500).send("Error finding users");
}
});
app.get('/question', (req, res) => {
const question = req.query.question;
const correctAnswer = req.query.correctAnswer;
const wrongAnswer1 = req.query.wrongAnswer1;
const wrongAnswer2 = req.query.wrongAnswer2;
const wrongAnswer3 = req.query.wrongAnswer3;
// Render the question page with dynamic data
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Question Detail</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
text-align: center;
}
.answer {
margin-top: 20px;
font-size: 18px;
font-weight: bold;
}
button {
display: block;
margin: 10px auto;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>FriendFind</h1>
<h2 id="question"></h2>
<div id="buttons"></div> <!-- Container for buttons -->
<script>
//get parameters from url
const urlParams = new URLSearchParams(window.location.search);
const question = urlParams.get('question');
const correctAnswer = urlParams.get('correctAnswer');
const wrongAnswers = [
urlParams.get('wrongAnswer1'),
urlParams.get('wrongAnswer2'),
urlParams.get('wrongAnswer3')
];
//add question and answers to page
document.getElementById('question').textContent = question;
//create buttons for answers
const buttonsDiv = document.getElementById('buttons');
buttonsDiv.innerHTML = ''; // Clear any existing content
//function to handle answer click
function handleAnswerClick(answer) {
const otherUserId = "${req.session.otherUserId}"; // Retrieve from session
if (answer === correctAnswer) {
// Redirect to the correct answer page
window.location.href = '/correct-answer?otherUserId=' + otherUserId; // Change to your correct answer page URL
} else {
// Redirect to the wrong answer page
window.location.href = '/wrong-answer'; // Change to your wrong answer page URL
}
}
//create buttons for each answer
const answers = [correctAnswer, ...wrongAnswers];
answers.forEach(answer => {
const button = document.createElement('button');
button.textContent = answer;
button.onclick = () => handleAnswerClick(answer); // Add click event handler
buttonsDiv.appendChild(button);
});
</script>
</body>
</html>
`);
});
app.get('/correct-answer', async (req, res) => {
const currentUserId = req.session.currentUserId;
const otherUserId = req.session.otherUserId;
console.log("Current User ID:", currentUserId);
console.log("Other User ID:", otherUserId);
try {
await Users.findByIdAndUpdate(currentUserId, { $addToSet: { fR: otherUserId } });
res.send(`
<html>
<head>
<title>Correct Answer</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<center>
<h1>FriendFind</h1>
<h2>Found Friend!</h2>
<h3>Request Sent</h3>
<button onclick="window.location.href='/showuser'">Find Another Friend</button>
</center>
</body>
</html>
`);
} catch (err) {
res.status(500).send("Error processing correct answer");
}
});
app.get('/wrong-answer', (req, res) => {
res.send(`
<html>
<head>
<title>Wrong Answer</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<center>
<h1>FriendFind</h1>
<h2>Sorry! Wrong Answer...</h2>
<button onclick="window.history.back()">Try New Question?</button><br>
<button onclick="window.location.href='/showuser'">Find Another Friend?</button>
</center>
</body>
</html>
`);
});
app.get('/matchpage', (req, res) => {
res.send(`
<html>
<head>
<title>Match Page</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<center>
<h1>FriendFind</h1>
<h2>It's a Match!</h2>
<h3>Join this user's chat room to start a conversation!</h3>
<a href="https://www.chatzy.com/52781656008329">
<button>Join!</button>
</a>
</center>
</body>
</html>
`);
});
app.listen(port, () => {
console.log("Server Started");
});