Design and logic for user-facing site https://exmods.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.9KB

  1. let load_url = "https://leadercraft.exmods.org/s/load";
  2. let test_url = "http://localhost:1337/load?board=main-test&count=15&start=0";
  3. //load_url = test_url // testing only
  4. let default_args = "?board=main-test&count=50&start=0";
  5. let always_args = "&count=50&start=0";
  6. function build_board() {
  7. let leaderboard = document.getElementById("leaderboard");
  8. let leaderboard_container = document.getElementById("leaderboard-container");
  9. let tmpEntry = {ID: -42, Rank:"?", PlayerURL: "/player?id=1", PlayerName: "Loading...", Score:">9000"}
  10. let errEntry = build_entry(tmpEntry);
  11. leaderboard.appendChild(errEntry);
  12. let gen_url = load_url;
  13. if (window.location.href.split("?").length < 2) { // no args
  14. gen_url += default_args;
  15. } else {
  16. gen_url += "?"+window.location.href.split("?").slice(1).join("&")+always_args;
  17. }
  18. setTimeout(() => {document.getElementById("nameAnchor-42").innerText = "Something went wrong :(";}, 5000); // 5s
  19. fetch(gen_url).then(
  20. (response) => {
  21. if (!response.ok || (response.status) != 200) {
  22. document.getElementById("nameAnchor-42").innerText = "Something went wrong :(";
  23. return;
  24. }
  25. errEntry.style.display = "none";
  26. response.json().then(
  27. (result) => {
  28. leaderboard.removeChild(errEntry)
  29. for (entry of result.Items) {
  30. let newEntry = build_entry(entry);
  31. // end
  32. leaderboard.appendChild(newEntry);
  33. }
  34. }
  35. );
  36. }
  37. );
  38. }
  39. function build_entry(entry) {
  40. let newEntry = document.createElement("div")
  41. newEntry.setAttribute("id", "entry"+entry.ID)
  42. newEntry.classList.add("board-entry")
  43. // rank
  44. let rankEntry = document.createElement("span")
  45. rankEntry.setAttribute("id", "rank"+entry.ID)
  46. rankEntry.innerText = entry.Rank
  47. rankEntry.classList.add("board-rank")
  48. newEntry.appendChild(rankEntry)
  49. // name and url
  50. let nameElem = document.createElement("span")
  51. nameElem.setAttribute("id", "name"+entry.ID)
  52. nameElem.classList.add("board-name")
  53. let nameAElem = document.createElement("a")
  54. nameAElem.setAttribute("id", "nameAnchor"+entry.ID)
  55. nameAElem.classList.add("board-name-a")
  56. let splitUrl = entry.PlayerURL.split("?")
  57. if (load_url === test_url) {
  58. nameAElem.href = "../board"+splitUrl[0]+".html?"+splitUrl.slice(1).join("?")
  59. } else {
  60. nameAElem.href = "../board"+splitUrl[0]+".html?"+splitUrl.slice(1).join("?")
  61. }
  62. nameAElem.innerText = entry.PlayerName
  63. nameElem.appendChild(nameAElem)
  64. newEntry.appendChild(nameElem)
  65. // score
  66. let scoreElem = document.createElement("span")
  67. scoreElem.setAttribute("id", "score"+entry.ID)
  68. scoreElem.classList.add("board-score")
  69. scoreElem.innerText = entry.Score
  70. newEntry.appendChild(scoreElem)
  71. return newEntry
  72. }