This commit is contained in:
IM23a-spirgif
2025-05-06 15:51:21 +02:00
parent 687748ed85
commit 4a92b61930
+11 -7
View File
@@ -220,7 +220,7 @@ app.get("/lobby/join", authenticateToken, async (req, res) => {
const users = get_data(); const users = get_data();
const user = users.find(u => u.username === username); const user = users.find(u => u.username === username);
if (!user) { if (!user) {
return res.status(401).json({error: "User doesn't exist"}); return res.status(401).json({ error: "User doesn't exist" });
} }
if (lobbyId in playerMap) { if (lobbyId in playerMap) {
@@ -228,31 +228,35 @@ app.get("/lobby/join", authenticateToken, async (req, res) => {
if (!playerMap[lobbyId].includes(username)) { if (!playerMap[lobbyId].includes(username)) {
playerMap[lobbyId].push(username); playerMap[lobbyId].push(username);
} }
res.json({lobbyId, username}); res.json({ lobbyId, username });
} else { } else {
console.log("Lobby with " + lobbyId + " not found."); console.log("Lobby with " + lobbyId + " not found.");
return res.status(404).json({error: "Lobby not found"}); return res.status(404).json({ error: "Lobby not found" });
} }
}); });
app.delete("/lobby/leave", authenticateToken, async (req, res) => { app.delete("/lobby/leave", authenticateToken, async (req, res) => {
const lobbyId = req.query.lobbyId; const lobbyId = req.query.lobbyId;
const username = req.query.username; const username = req.query.username;
if (!lobbyId || !username) { if (!lobbyId || !username) {
return res.status(400).json({error: "Missing lobbyId or username"}); return res.status(400).json({ error: "Missing lobbyId or username" });
} }
if (lobbyId in playerMap) { if (lobbyId in playerMap) {
const index = playerMap[lobbyId].indexOf(username); const index = playerMap[lobbyId].indexOf(username);
if (index > -1) { if (index > -1) {
playerMap[lobbyId].splice(index, 1); playerMap[lobbyId].splice(index, 1);
return res.json({message: "Successfully left lobby"}); return res.json({ message: "Successfully left lobby" });
} else { } else {
return res.status(404).json({error: "User not in lobby"}); return res.status(404).json({ error: "User not in lobby" });
} }
} }
res.status(404).json({error: "Lobby not found"});
res.status(404).json({ error: "Lobby not found" });
}); });
app.get("/lobby/players", authenticateToken, async (req, res) => { app.get("/lobby/players", authenticateToken, async (req, res) => {
let lobbyId = req.query.lobbyId; let lobbyId = req.query.lobbyId;
let players = playerMap[lobbyId] || []; let players = playerMap[lobbyId] || [];