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.

67 lines
2.6KB

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