From 3f9c08a43c7ed36212e75ab146f3879e1781a83d Mon Sep 17 00:00:00 2001 From: IM23a-cernik Date: Mon, 28 Apr 2025 14:50:11 +0200 Subject: [PATCH] another attempt at fixing ability to join non-existent lobbies --- lobby.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/lobby.js b/lobby.js index 16b67ec..1d1da9d 100644 --- a/lobby.js +++ b/lobby.js @@ -16,10 +16,10 @@ async function createLobby() { } async function joinLobby(lobbyId, player_name) { - if (playerMap[lobbyId]) { - let response = await fetch(`https://www.deckofcardsapi.com/api/deck/${lobbyId}/pile/${player_name}/add/?cards=`) - console.log(response); - } else console.log("Lobby with "+lobbyId+" not found."); + + let response = await fetch(`https://www.deckofcardsapi.com/api/deck/${lobbyId}/pile/${player_name}/add/?cards=`) + console.log(response); + } app.get("/lobby/create", async (req, res) => { @@ -42,18 +42,21 @@ app.get("/lobby/join", async (req, res) => { if (!user) { return res.status(401).json({error: "User doesn't exist"}); } + for (let lobby of playerMap) { + if (playerMap[lobby.lobbyId] == lobbyId) { + await joinLobby(lobbyId, user.username); - await joinLobby(lobbyId, user.username); + if (!playerMap[lobbyId]) { + playerMap[lobbyId] = []; + } - if (!playerMap[lobbyId]) { - playerMap[lobbyId] = []; + if (!playerMap[lobbyId].includes(username)) { + playerMap[lobbyId].push(username); + } + + res.json({lobbyId, username}); + } else console.log("Lobby with " + lobbyId + " not found."); } - - if (!playerMap[lobbyId].includes(username)) { - playerMap[lobbyId].push(username); - } - - res.json({lobbyId, username}); }); app.get("/lobby/players", async (req, res) => {