-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcommunity.js
83 lines (72 loc) · 3.23 KB
/
community.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
document.addEventListener("DOMContentLoaded", () => {
const modeSelector = document.getElementById("modeSelector");
const presetSelector = document.getElementById("presetSelector");
const leaderboardTable = document.getElementById("leaderboardTable");
const tableTimeElo = document.getElementById("tableTimeElo");
const seedColumn = document.getElementById("seedColumn");
const apiUrl = "https://sotnrandoapi.duckdns.org";
// Fetch Presets
const fetchPresets = async () => {
try {
const response = await fetch(`${apiUrl}/ranked/presets`);
const data = await response.json();
const presets = data.presets; // Access the 'presets' key
if (!Array.isArray(presets)) {
throw new Error("Invalid presets format");
}
console.log("Presets:", presets); // Log the presets for debugging
presets.forEach(preset => {
const option = document.createElement("option");
option.value = preset;
option.textContent = preset;
presetSelector.appendChild(option);
});
loadLeaderboard();
} catch (error) {
console.error("Error fetching presets:", error);
}
};
// Load Leaderboard
const loadLeaderboard = async () => {
const mode = modeSelector.value;
const preset = presetSelector.value;
const endpoint = mode === "elo" ? `${apiUrl}/leaderboards/elo/${preset}` : `${apiUrl}/leaderboards/time/${preset}`;
try {
const response = await fetch(endpoint);
const data = await response.json();
// Adjust table columns
if (mode === "elo") {
tableTimeElo.textContent = "Elo";
seedColumn.classList.add("d-none");
} else {
tableTimeElo.textContent = "Time";
seedColumn.classList.remove("d-none");
}
// Populate the table
const rows = data.leaderboards.map(player => {
if(player.seed_name === null){
player.seed_name = "Custom";
player.seed_url = "#";
}
const seedData = mode === "time" ? `<td><a href="${player.seed_url}" class="text-decoration-none text-info">${player.seed_name}</a></td>` : '';
return `
<tr>
<td>${player.rank}</td>
<td>${player.username}</td>
<td>${mode === "elo" ? player.elo : player.time}</td>
${seedData}
</tr>
`;
}).join('');
leaderboardTable.innerHTML = rows || `<tr><td colspan="4" class="text-center">No data available</td></tr>`;
} catch (error) {
console.error("Error loading leaderboard:", error);
leaderboardTable.innerHTML = `<tr><td colspan="4" class="text-center text-danger">Failed to load leaderboard</td></tr>`;
}
};
// Event Listeners
modeSelector.addEventListener("change", loadLeaderboard);
presetSelector.addEventListener("change", loadLeaderboard);
// Initialize
fetchPresets();
});