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.

board.js 3.2KB

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