Add leaderboard component to UI and API endpoint to fetch player rankings.
This commit is contained in:
+35
-11
@@ -87,6 +87,29 @@ app.post("/register", (request, response) => {
|
||||
response.json({lower_username, token});
|
||||
});
|
||||
|
||||
app.get("/leaderboard", authenticateToken, (req, res) => {
|
||||
const users = get_data();
|
||||
const sorted = users.sort((a, b) => b.money - a.money);
|
||||
|
||||
const top10 = sorted.slice(0, 10).map(u => ({
|
||||
username: u.username,
|
||||
money: u.money,
|
||||
}));
|
||||
|
||||
const username = req.user.username;
|
||||
const position = sorted.findIndex(u => u.username === username) + 1;
|
||||
const self = sorted.find(u => u.username === username);
|
||||
|
||||
res.json({
|
||||
leaderboard: top10,
|
||||
self: {
|
||||
username: self.username,
|
||||
money: self.money,
|
||||
position
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/login", (request, response) => {
|
||||
response.sendFile(path.join(__dirname, "./public/login.html"));
|
||||
})
|
||||
@@ -173,20 +196,21 @@ async function createLobby() {
|
||||
await fetch(`https://www.deckofcardsapi.com/api/deck/${lobbyId}/pile/players/add/?cards=`)
|
||||
return lobbyId;
|
||||
}
|
||||
|
||||
app.post("/leave-lobby", (req, res) => {
|
||||
const { lobbyId, username } = req.body;
|
||||
const {lobbyId, username} = req.body;
|
||||
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]) {
|
||||
return res.status(404).json({ error: "Lobby nicht gefunden!" });
|
||||
return res.status(404).json({error: "Lobby nicht gefunden!"});
|
||||
}
|
||||
playerMap[lobbyId] = playerMap[lobbyId].filter(player => player !== username);
|
||||
if (playerMap[lobbyId].length === 0) {
|
||||
delete playerMap[lobbyId];
|
||||
console.log(`Leere Lobby ${lobbyId} wurde gelöscht.`);
|
||||
}
|
||||
res.json({ message: `Lobby ${lobbyId} aktualisiert.` });
|
||||
res.json({message: `Lobby ${lobbyId} aktualisiert.`});
|
||||
});
|
||||
setInterval(() => removeEmptyLobbies(playerMap), 10000);
|
||||
|
||||
@@ -245,7 +269,7 @@ app.get("/lobby/join", authenticateToken, async (req, res) => {
|
||||
const users = get_data();
|
||||
const user = users.find(u => u.username === username);
|
||||
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) {
|
||||
await joinLobby(lobbyId, user.username);
|
||||
@@ -254,10 +278,10 @@ app.get("/lobby/join", authenticateToken, async (req, res) => {
|
||||
}
|
||||
user.lobbyId = lobbyId;
|
||||
save_data(users);
|
||||
res.json({ lobbyId, username });
|
||||
res.json({lobbyId, username});
|
||||
} else {
|
||||
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"});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -266,19 +290,19 @@ app.delete("/lobby/leave", authenticateToken, async (req, res) => {
|
||||
const username = req.query.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) {
|
||||
const index = playerMap[lobbyId].indexOf(username);
|
||||
if (index > -1) {
|
||||
playerMap[lobbyId].splice(index, 1);
|
||||
return res.json({ message: "Successfully left lobby" });
|
||||
return res.json({message: "Successfully left lobby"});
|
||||
} 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"});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user