-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
288 lines (229 loc) · 8.34 KB
/
index.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
const express = require('express');
const app = express();
const path = require("path");
const multer = require('multer');
const Data = require("./models/data.js");
app.use(express.urlencoded({ extended: true }));
const MethodOverride = require('method-override');
app.use(MethodOverride('_method'));
app.use(express.json());
app.set("view engine" , "ejs");
app.set("views",path.join(__dirname,"/views"));
app.use(express.static(path.join(__dirname , "/public")));
app.use('/uploads', express.static(path.join(__dirname, 'public/uploads')));
const upload = multer({ dest: 'public/uploads/' });
const bcrypt = require("bcryptjs");
const mongoose = require('mongoose');
main().then(()=> {console.log("connection succedded!")})
.catch(err=>{console.log(err)});
async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/facemash');
}
// i had initiate my database with 10 entries
let users =[
]
Data.insertMany(users);
const K = 32; // K-factor is a constant used in the Elo rating system
// elo algo to rank the players
function calculateNewRatings(winnerRating, loserRating, winnerWon) {
const winnerExpected = 1 / (1 + Math.pow(10, (loserRating - winnerRating) / 1400));
const loserExpected = 1 / (1 + Math.pow(10, (winnerRating - loserRating) / 1400));
const winnerNewRating = winnerRating + K * (1 - winnerExpected);
const loserNewRating = loserRating + K * (0 - loserExpected);
return [Math.round(winnerNewRating), Math.round(loserNewRating)];
}
// get two random user after comparisiobn btw two had completed
async function getRandomEntries(count) {
try {
const allEntries = await Data.find().exec();
if (allEntries.length < count) {
console.log("Not enough entries in the database.");
return [];
}
// Knuth Shuffle.
for (let i = allEntries.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[allEntries[i], allEntries[j]] = [allEntries[j], allEntries[i]];
}
return allEntries.slice(0, count);
} catch (error) {
console.error("Error getting random entries:", error);
return [];
}
}
getRandomEntries(2);
app.listen(8080,(req,res)=>{
console.log("listening on port 8080...");
})
app.get('/home', async (req, res) => {
try {
const randomEntries = await getRandomEntries(2);
if (randomEntries.length === 2) {
const [user1, user2] = randomEntries;
res.render('home.ejs', { user1, user2 });
} else {
res.render('home.ejs', { user1: null, user2: null });
}
} catch (err) {
console.error("Error getting random entries:", err);
res.status(500).send("Internal Server Error");
}
});
// app.get('/home', (req, res) => {
// const user1 = { name: 'Test User 1', image: '/images/image1.jpeg' };
// const user2 = { name: 'Test User 2', image: '/images/image2.jpeg' };
// res.render('home.ejs', { user1, user2 });
// });
app.get("/about",(req,res)=>{
res.render('about.ejs');
})
app.get("/submit", async (req, res) => {
try {
const allEntries = await Data.find().sort({ rating: -1 }).exec(); // Sort by rating in descending order
res.render("airs.ejs", { allEntries });
} catch (error) {
console.error("Error fetching entries:", error);
res.status(500).send("Internal Server Error");
}
});
app.get("/ranking", async (req, res) => {
try {
const allusers = await Data.find();
res.render("users.ejs", { allusers });
} catch (error) {
console.error("Error fetching users:", error);
res.status(500).send("Internal Server Error");
}
});
app.get("/previous",(req,res)=>{
res.redirect('/home');
})
app.get("/users/new" ,(req,res)=>{
res.render("new.ejs");
})
app.post("/users", upload.single('image'), async (req, res) => {
try {
const { name, email, password, description } = req.body;
const image = req.file ? req.file.filename : 'default.jpg'; // Default image if none is provided
// Hash the password before storing it
const saltRounds = 10; // You can adjust the salt rounds as needed
const hashedPassword = await bcrypt.hash(password, saltRounds);
// Create a new user with hashed password
const newUser = new Data({
name,
email,
password: hashedPassword,
description,
image: `/uploads/${image}`
});
// Save the new user to the database
await newUser.save();
res.redirect('/ranking'); // Redirect to the ranking page or another page
} catch (err) {
console.error("Error registering user:", err);
res.status(500).send("Error registering user");
}
});
// api
app.get('/new-entries', async (req, res) => {
try {
const randomEntries = await getRandomEntries(2);
res.json(randomEntries);
} catch (err) {
console.error("Error getting new entries:", err);
res.status(500).send("Internal Server Error");
}
});
app.post('/update-ratings', async (req, res) => {
try {
const { winnerId, loserId } = req.body;
// Fetch both users
const [winner, loser] = await Promise.all([
Data.findById(winnerId).exec(),
Data.findById(loserId).exec()
]);
if (!winner || !loser) {
return res.status(404).send("User not found");
}
// Calculate new ratings
const [newWinnerRating, newLoserRating] = calculateNewRatings(winner.rating, loser.rating, true);
// Update the users' ratings
winner.rating = newWinnerRating;
loser.rating = newLoserRating;
await Promise.all([
winner.save(),
loser.save()
]);
res.json({ success: true });
} catch (error) {
console.error("Error updating ratings:", error);
res.status(500).send("Internal Server Error");
}
});
app.get("/users/:id", async (req, res) => {
try {
let userId = req.params.id;
let entry = await Data.findById(userId);
if (entry) {
res.render("profile.ejs", { entry });
} else {
res.status(404).send("User not found");
}
} catch (error) {
res.status(500).send("Server Error");
}
});
app.get("/users/:id/edit" ,async (req ,res)=>{
try {
let userId = req.params.id;
let entry = await Data.findById(userId);
if (entry) {
res.render("edit.ejs", { entry });
} else {
res.status(404).send("User not found");
}
} catch (error) {
res.status(500).send("Server Error");
}
})
app.patch("/users/:id", upload.single('image'), async (req, res) => {
try {
const { name, description, password } = req.body;
const image = req.file ? req.file.filename : null;
const id = req.params.id;
// Find the user by ID
const user = await Data.findById(id);
if (user) {
// Compare provided password with hashed password in the database
const isMatch = await bcrypt.compare(password, user.password);
if (isMatch) {
// Apply changes if the password matches
user.name = name || user.name; // Update only if new value is provided
user.description = description || user.description;
if (image) {
user.image = `/uploads/${image}`;
}
// Save updated user
await user.save();
res.redirect(`/users/${id}`);
} else {
res.status(401).send("Invalid password");
}
} else {
res.status(404).send("User not found");
}
} catch (err) {
console.error("Error saving user:", err);
res.status(500).send("Error saving user");
}
});
// api for top 5 trending users
app.get('/trending-users', async (req, res) => {
try {
const trendingUsers = await Data.find().sort({ rating: -1 }).limit(5).exec(); // Fetch top 5 users by rating
res.json(trendingUsers);
} catch (error) {
console.error('Error fetching trending users:', error);
res.status(500).send('Server Error');
}
});