|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- let load_url = "https://leadercraft.exmods.org/s/load";
- let test_url = "http://localhost:1337/load?board=main-test&count=15&start=0";
- //load_url = test_url // testing only
-
- let default_args = "?id=1&count=50&start=0";
- let always_args = "&count=50&start=0";
- let leaderboard_name_set = false;
-
- function build_board() {
- let leaderboard = document.getElementById("leaderboard");
- let leaderboard_container = document.getElementById("leaderboard-container");
- let tmpEntry = {ID: -42, Rank:"?", PlayerURL: "/player?id=1", PlayerName: "Loading...", Score:">9000"}
- let errEntry = build_entry(tmpEntry);
- leaderboard.appendChild(errEntry);
- let gen_url = load_url;
- if (window.location.href.split("?").length < 2) { // no args
- gen_url += default_args;
- } else {
- gen_url += "?"+window.location.href.split("?").slice(1).join("&")+always_args;
- }
- setTimeout(() => {document.getElementById("nameAnchor-42").innerText = "Something went wrong :(";}, 5000); // 5s
- fetch(gen_url).then(
- (response) => {
- if (!response.ok || (response.status) != 200) {
- document.getElementById("nameAnchor-42").innerText = "Something went wrong :(";
- return;
- }
- errEntry.style.display = "none";
- response.json().then(
- (result) => {
- leaderboard.removeChild(errEntry)
- for (entry of result.Items) {
- if (!leaderboard_name_set) {
- leaderboard_name_set = true;
- document.getElementById("leaderboard-name").innerText = entry.BoardName + " (id: " + entry.BoardID + ")";
- }
- let newEntry = build_entry(entry);
- // end
- leaderboard.appendChild(newEntry);
- }
- }
- );
- }
- );
- }
-
- function build_entry(entry) {
- let newEntry = document.createElement("div")
- newEntry.setAttribute("id", "entry"+entry.ID)
- newEntry.classList.add("board-entry")
- // rank
- let rankEntry = document.createElement("span")
- rankEntry.setAttribute("id", "rank"+entry.ID)
- rankEntry.innerText = entry.Rank
- rankEntry.classList.add("board-rank")
- newEntry.appendChild(rankEntry)
- // name and url
- let nameElem = document.createElement("span")
- nameElem.setAttribute("id", "name"+entry.ID)
- nameElem.classList.add("board-name")
- let nameAElem = document.createElement("a")
- nameAElem.setAttribute("id", "nameAnchor"+entry.ID)
- nameAElem.classList.add("board-name-a")
- let splitUrl = entry.PlayerURL.split("?")
- if (load_url === test_url) {
- nameAElem.href = "../board"+splitUrl[0]+".html?"+splitUrl.slice(1).join("?")
- } else {
- nameAElem.href = "../board"+splitUrl[0]+".html?"+splitUrl.slice(1).join("?")
- }
- nameAElem.innerText = entry.PlayerName
- nameElem.appendChild(nameAElem)
- newEntry.appendChild(nameElem)
- // score
- let scoreElem = document.createElement("span")
- scoreElem.setAttribute("id", "score"+entry.ID)
- scoreElem.classList.add("board-score")
- scoreElem.innerText = entry.Score
- newEntry.appendChild(scoreElem)
- return newEntry
- }
|