little change
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
const crypto = require("crypto");
|
||||
|
||||
const secret = crypto.randomBytes(64).toString('hex')
|
||||
|
||||
console.log(secret)
|
||||
@@ -1,4 +1,6 @@
|
||||
const express = require("express");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
@@ -27,7 +29,13 @@ app.get("/lobby/join", async (req, res) => {
|
||||
let lobbyId = req.query.lobbyId;
|
||||
let name = req.query.name;
|
||||
|
||||
await joinLobby(lobbyId, name);
|
||||
const users = get_data();
|
||||
const user = (users.find(u => u.username === name))
|
||||
if (!user) {
|
||||
res.sendStatus(401).json({ error: "User doesnt exist are required" });
|
||||
}
|
||||
|
||||
await joinLobby(lobbyId, user.username);
|
||||
|
||||
if (!playerMap[lobbyId]) {
|
||||
playerMap[lobbyId] = [];
|
||||
@@ -44,6 +52,25 @@ app.get("/lobby/players", async (req, res) => {
|
||||
res.json({ players });
|
||||
});
|
||||
|
||||
function get_data() {
|
||||
const dataPath = path.join(__dirname, "data.json");
|
||||
if (!fs.existsSync(dataPath)) {
|
||||
fs.writeFileSync(dataPath, JSON.stringify([]));
|
||||
}
|
||||
const data = fs.readFileSync(dataPath);
|
||||
return JSON.parse(data);
|
||||
}
|
||||
|
||||
function save_data(data) {
|
||||
fs.writeFileSync(path.join(__dirname, "data.json"), JSON.stringify(data, null, 2));
|
||||
}
|
||||
|
||||
function saveUser(user) {
|
||||
const users = get_data();
|
||||
users.push(user);
|
||||
save_data(users);
|
||||
}
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log("Server is running on port " + port);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user