Reduce lobby cleanup interval to 10 seconds and remove redundant comments.
This commit is contained in:
+2
-12
@@ -180,32 +180,25 @@ async function createLobby() {
|
|||||||
}
|
}
|
||||||
app.post("/leave-lobby", (req, res) => {
|
app.post("/leave-lobby", (req, res) => {
|
||||||
const { lobbyId, username } = req.body;
|
const { lobbyId, username } = req.body;
|
||||||
|
|
||||||
if (!lobbyId || !username) {
|
if (!lobbyId || !username) {
|
||||||
return res.status(400).json({ error: "Lobby ID und Username sind erforderlich!" });
|
return res.status(400).json({ error: "Lobby ID und Username sind erforderlich!" });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!playerMap[lobbyId]) {
|
if (!playerMap[lobbyId]) {
|
||||||
return res.status(404).json({ error: "Lobby nicht gefunden!" });
|
return res.status(404).json({ error: "Lobby nicht gefunden!" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entferne den Nutzer aus der Lobby
|
|
||||||
playerMap[lobbyId] = playerMap[lobbyId].filter(player => player !== username);
|
playerMap[lobbyId] = playerMap[lobbyId].filter(player => player !== username);
|
||||||
|
|
||||||
// Lösche die Lobby, wenn sie leer ist
|
|
||||||
if (playerMap[lobbyId].length === 0) {
|
if (playerMap[lobbyId].length === 0) {
|
||||||
delete playerMap[lobbyId];
|
delete playerMap[lobbyId];
|
||||||
console.log(`Leere Lobby ${lobbyId} wurde gelöscht.`);
|
console.log(`Leere Lobby ${lobbyId} wurde gelöscht.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.json({ message: `Lobby ${lobbyId} aktualisiert.` });
|
res.json({ message: `Lobby ${lobbyId} aktualisiert.` });
|
||||||
});
|
});
|
||||||
setInterval(() => removeEmptyLobbies(playerMap), 30000); // Alle 60 Sekunden
|
setInterval(() => removeEmptyLobbies(playerMap), 10000);
|
||||||
|
|
||||||
function removeEmptyLobbies(playerMap) {
|
function removeEmptyLobbies(playerMap) {
|
||||||
for (const lobby in playerMap) {
|
for (const lobby in playerMap) {
|
||||||
if (playerMap[lobby].length === 0) {
|
if (playerMap[lobby].length === 0) {
|
||||||
delete playerMap[lobby]; // Entfernt die leere Lobby
|
delete playerMap[lobby];
|
||||||
console.log(`Lobby ${lobby} wurde gelöscht.`);
|
console.log(`Lobby ${lobby} wurde gelöscht.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -227,7 +220,6 @@ async function deleteLobby(id) {
|
|||||||
|
|
||||||
async function joinLobby(lobbyId, player_name) {
|
async function joinLobby(lobbyId, player_name) {
|
||||||
let response = await fetch(`https://www.deckofcardsapi.com/api/deck/${lobbyId}/pile/${player_name}/add/?cards=`)
|
let response = await fetch(`https://www.deckofcardsapi.com/api/deck/${lobbyId}/pile/${player_name}/add/?cards=`)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get("/lobby/create", authenticateToken, async (req, res) => {
|
app.get("/lobby/create", authenticateToken, async (req, res) => {
|
||||||
@@ -260,7 +252,6 @@ app.get("/lobby/join", authenticateToken, async (req, res) => {
|
|||||||
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) {
|
||||||
await joinLobby(lobbyId, user.username);
|
await joinLobby(lobbyId, user.username);
|
||||||
if (!playerMap[lobbyId].includes(username)) {
|
if (!playerMap[lobbyId].includes(username)) {
|
||||||
@@ -292,7 +283,6 @@ app.delete("/lobby/leave", authenticateToken, async (req, res) => {
|
|||||||
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" });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user