-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
502 lines (461 loc) · 14.8 KB
/
server.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
const express = require("express");
const CryptoJS = require("crypto-js");
const stringifySafe = require("json-stringify-safe");
const { MongoClient, ServerApiVersion } = require("mongodb");
const axios = require("axios");
const path = require('path');
const session = require("express-session");
const http = require("http");
const { Server } = require("socket.io");
const fs = require("fs");
const app = express();
const server = http.createServer(app);
const io = new Server(server, { pingInterval: 2000, pingTimeout: 5000 });
app.use(express.static('public'));
app.use(express.static(path.join(__dirname, 'public', 'site')));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
name: "cookie",
secret: process.env['cookieSecret'],
resave: false,
saveUninitialized: true,
cookie: { maxAge: 3 * 24 * 60 * 60 * 1000 },
}));
app.use((req, res, next) => {
console.log('Request URL:', req.url);
next();
});
app.use((req, res, next) => {
if (req.path !== '/' && req.path.startsWith('/site/')) {
req.url = req.url.replace('/site', '');
}
if (req.path !== '/' && req.path.startsWith('/panel/')) {
req.url = req.url.replace('/panel', '');
}
if (req.path.endsWith('.html')) {
return res.redirect(301, req.path.slice(0, -5));
}
next();
});
function formatDateTime(dateTime) {
const options = {
year: "numeric", month: "numeric", day: "numeric",
hour: "numeric", minute: "numeric", hour12: true,
};
return dateTime.toLocaleString(undefined, options);
}
const byte = (str) => {
let size = new Blob([str]).size;
return size;
};
function encrypt(text, pass) {
return CryptoJS.AES.encrypt(text, pass).toString();
}
function decrypt(text, pass) {
return CryptoJS.AES.decrypt(text, pass).toString(CryptoJS.enc.Utf8);
}
async function hashPassword(password) {
const saltRounds = 10;
return await bcrypt.hash(password, saltRounds);
}
async function validatePassword(password, hashedPassword) {
return await bcrypt.compare(password, hashedPassword);
}
function rand(min, max) {
return Math.random() * (max - min + 1) + min;
}
const uri = process.env["mongoURL"];
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
const db_name = "pixelit";
const db = client.db(db_name);
const users = db.collection("users");
const badges = db.collection("badges");
const news = db.collection("news");
const chatm = db.collection("chat");
const packs = db.collection("packs");
async function run() {
try {
await client.connect();
await client.db(db_name).command({ ping: 1 });
requests = await client.db(db_name).collection("requests").find().toArray();
console.log("Successfully connected to the database");
} catch (e) {
console.log(e);
}
}
run().catch(console.dir);
const timezoneOffset = new Date().getTimezoneOffset();
const localTime = new Date(Date.now() - timezoneOffset * 60 * 1000);
const router = require("./routes.js");
app.use(router);
const port = 3000;
const encpass = process.env["encpass"];
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/home.");
});
io.on("connection", (socket) => {
console.log("a user connected");
socket.on("disconnect", () => {
console.log("user disconnected");
});
socket.on("getTokens", async (name) => {
const user = await users.findOne({ username: name });
if (user === null) return;
io.to(socket.id).emit("tokens", user.tokens, user.sent, user.packsOpened);
});
socket.on("message", async (message) => {
console.log("sending message")
try {
if (byte(message) > 1000 || message.trim() === "") {
console.log("message too long")
return;
}
const cookief = socket.handshake.headers.cookie;
console.log("getting response")
const response = await axios.get(
"https://pixelit.replit.app/user",
{
headers: {
Cookie: cookief,
},
validateStatus: function (status) {
return (status >= 200 && status < 300) || status === 500;
},
withCredentials: true,
},
);
console.log(response)
if (response.status !== 500) {
const name = response.data.username;
const d = new Date();
d.setHours(d.getHours() - 4); // todo: don't hardwire timezone
const user = await users.findOne({ username: name });
const chatmessage = {
sender: name,
msg: message,
badges: user.badges,
pfp: user.pfp,
};
await chatm.insertOne(chatmessage);
await users.updateOne(
{ username: name },
{ $set: { sent: user.sent + 1 } },
);
await users.updateOne(
{ username: name },
{ $set: { tokens: user.tokens + 1 } },
);
io.emit("chatupdate", "get");
console.log("message sent")
} else {
socket.emit("error", response.data);
}
} catch (error) {
console.error("Error during message handling:", error);
}
});
socket.on("getChat", async () => {
socket.emit("chatupdate", await chatm.find().toArray());
});
socket.on("getNews", async () => {
//await client.connect();
const newsPosts = await news.find().toArray();
io.to(socket.id).emit("getNews", newsPosts);
});
socket.on("newspost", async (info) => {
//await client.connect();
const newsPost = {
title: info.title,
content: info.content,
author: info.author,
date: info.date,
};
const sender = await users.findOne({ username: info.author });
if (
validatePassword(info.pass, sender.password, sender.salt) &&
admins.includes(info.author)
) {
await news.insertOne(newsPost);
console.log("posted news: " + info.title);
}
});
socket.on("addPack", async (pack, user) => {
//await client.connect();
const newpack = {
name: pack.name,
image: pack.image,
cost: pack.cost,
blooks: [],
};
const person = await users.findOne({ username: user.name });
if (
validatePassword(user.pass, person.password, person.salt) &&
admins.includes(user.name)
) {
try {
await packs
.insertOne(newpack)
.then((result) => {
console.log("Update operation result:", result);
})
.catch((error) => {
console.error("Error updating database:", error);
});
await users
.updateMany(
{ packs: { $nin: [pack.name] } },
{ $push: { packs: newpack } },
)
.then((result) => {
console.log("Update operation result:", result);
})
.catch((error) => {
console.error("Error updating database:", error);
});
} catch (e) {
console.log(e);
}
console.log("added new pack: " + newpack.name);
io.to(socket.id).emit("getPacks", "get");
} else console.log("addpack verification denied " + user.name);
});
socket.on("addBlook", async (blook, user) => {
const person = await users.findOne({ username: user.name });
if (
validatePassword(user.pass, person.password, person.salt) &&
admins.includes(user.name)
) {
await packs
.updateOne(
{ name: blook.parent },
{
$push: {
blooks: {
name: blook.name,
imageUrl: blook.image,
rarity: blook.rarity,
chance: blook.chance,
parent: blook.parent,
color: blook.color,
owned: 0,
},
},
},
)
.then((result) => {
console.log("Update operation result:", result);
})
.catch((error) => {
console.error("Error updating database:", error);
});
await users
.updateMany(
{ "packs.name": blook.parent },
{ $addToSet: { "packs.$[pack].blooks": blook } },
{ arrayFilters: [{ "pack.name": blook.parent }] },
)
.then((result) => {
console.log("Update operation result:", result);
})
.catch((error) => {
console.error("Error updating database:", error);
});
console.log(`added new blook to ${blook.parent}: ` + blook.name);
io.to(socket.id).emit("getPacks", "get");
} else console.log("addblook verification denied " + user.name);
});
socket.on("removePack", async (pack, user) => {
const newpack = {
name: pack.name,
image: pack.image,
cost: pack.cost,
blooks: [],
};
const person = await users.findOne({ username: user.name });
if (
validatePassword(user.pass, person.password, person.salt) &&
admins.includes(user.name)
) {
await packs
.deleteOne({ name: pack.name })
.then((result) => {
console.log("Update operation result:", result);
})
.catch((error) => {
console.error("Error updating database:", error);
});
await users
.updateMany(
{ "packs.name": pack.name },
{ $pull: { packs: { name: pack.name } } },
)
.then((result) => {
console.log("Update operation result:", result);
})
.catch((error) => {
console.error("Error updating database:", error);
});
console.log("deleted pack: " + pack.name);
io.to(socket.id).emit("getPacks", "get");
} else console.log("removepack verification denied " + user.name);
});
socket.on("removeBlook", async (blook, user) => {
const person = await users.findOne({ username: user.name });
if (
validatePassword(user.pass, person.password, person.salt) &&
admins.includes(user.name)
) {
await packs
.updateOne(
{ name: blook.parent },
{
$pull: {
blooks: {
name: blook.name,
},
},
},
)
.then((result) => {
console.log("Update operation result:", result);
})
.catch((error) => {
console.error("Error updating database:", error);
});
await users
.updateMany(
{ "packs.name": blook.parent, "packs.blooks.name": blook.name },
{ $pull: { "packs.$[pack].blooks": { name: blook.name } } },
{ arrayFilters: [{ "pack.name": blook.parent }] },
)
.then((result) => {
console.log("Update operation result:", result);
})
.catch((error) => {
console.error("Error updating database:", error);
});
console.log(`removed blook from ${blook.parent}: ` + blook.name);
io.to(socket.id).emit("getPacks", "get");
} else console.log("removeblook verification denied " + user.name);
});
socket.on("getPacks", async () => {
const packsArray = await packs.find().toArray();
io.to(socket.id).emit("getPacks", packsArray);
});
socket.on("openPack", async (opack, user) => {
const person = await users.findOne({ username: user.name });
if (person === null) return;
if (!validatePassword(user.pass, person.password, person.salt)) {
console.log("False password");
return;
}
const pack = await packs.findOne({ name: opack.name });
if (pack === null) {
console.log("Invalid pack");
return;
}
if (person.tokens < pack.cost) return;
const blooks = pack.blooks;
let totalchance = 0;
for (const b of blooks) {
totalchance += Number(b.chance);
}
const randnum = rand(0, totalchance);
let currentchance = 0;
for (const b of blooks) {
const blook = b;
if (
randnum >= currentchance &&
randnum <= currentchance + Number(blook.chance)
) {
const result = await users.updateOne(
{
username: user.name,
"packs.name": pack.name,
"packs.blooks.name": blook.name,
},
{
$inc: { "packs.$[pack].blooks.$[blook].owned": 1, packsOpened: 1 },
},
{
arrayFilters: [
{ "pack.name": pack.name },
{ "blook.name": blook.name },
],
},
);
await users.updateOne(
{ username: user.name },
{ $inc: { tokens: -pack.cost } },
);
console.log(
`${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`,
);
io.to(socket.id).emit("openPack", {
pack: pack.name,
blook: blook,
});
const testuser = await users.findOne({ username: user.name });
io.to(socket.id).emit("tokens", await testuser.tokens);
console.log(`${user.name} opened ${pack.name} and got ${blook.name}`);
}
currentchance += Number(blook.chance);
}
});
socket.on("getUserPacks", async (name) => {
const user = await users.findOne({ username: name });
if (user === null) return;
io.to(socket.id).emit("getUserPacks", user.packs);
});
socket.on("getAccounts", async () => {
const accounts = await users.find().toArray();
io.to(socket.id).emit("getAccounts", accounts);
});
socket.on("getBadges", async () => {
//await client.connect();
const badgeese = await badges.find().toArray();
io.to(socket.id).emit("getBadges", badgeese);
});
socket.on("getUserBadges", async (name) => {
//await client.connect();
const user = await users.findOne({ username: name });
if (user === null) return;
io.to(socket.id).emit("getUserBadges", user.badges);
});
socket.on("addBadge", async (data) => {
//await client.connect();
const { username, badge } = data;
await users.updateOne({ username }, { $addToSet: { badges: badge } });
const updatedUser = await users.find().toArray();
io.emit("badgeUpdate", updatedUser);
});
socket.on("removeBadge", async (data) => {
//await client.connect();
const { username, badge } = data;
await users.updateOne({ username }, { $pull: { badges: badge } });
const updatedUser = await users.find().toArray();
io.emit("badgeUpdate", updatedUser);
});
socket.on("createBadge", async (badge) => {
//await client.connect();
await badges.insertOne(badge);
});
socket.on("deleteBadge", async (badge) => {
//await client.connect();
await badges.deleteOne(badge);
await users.updateMany({}, { $pull: { badges: badge } });
});
});
server.listen(port, () => {
console.log(`Server started successfully on port ${port}`);
console.log(`Server is running at ${process.env["DEV_LINK"]}:${port}`);
});
console.log("Initializing server...");