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.

player.js 3.3KB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. let load_url = "https://exmods.org/player"
  2. let test_url = "http://localhost:1337/player"
  3. //load_url = test_url // testing only
  4. function build_player() {
  5. // build
  6. let player = document.getElementById("player-name")
  7. let player_link = document.getElementById("player-link")
  8. let leaderboard = document.getElementById("leaderboard")
  9. let leaderboard_container = document.getElementById("leaderboard-container")
  10. let tmpEntry = {ID: -42, Rank:"?", BoardURL: "", BoardName:"Loading...", PlayerURL: "", PlayerName: "Loading...", Score:">9000"}
  11. let errEntry = build_entry(tmpEntry)
  12. leaderboard.appendChild(errEntry)
  13. let gen_url = load_url+"?"+window.location.href.split("?").slice(1).join("?")+"&entries=10"
  14. setTimeout(() => {document.getElementById("nameAnchor-42").innerText = "Something went wrong :(";}, 5000) // 5s
  15. fetch(gen_url).then(
  16. (response) => {
  17. if (!response.ok || (response.status) != 200) {
  18. document.getElementById("nameAnchor-42").innerText = "Something went wrong :("
  19. return
  20. }
  21. errEntry.style.display = "none"
  22. response.json().then(
  23. (result) => {
  24. if (result.Items.length!=1) {
  25. document.getElementById("nameAnchor-42").innerText = "Something went wrong :("
  26. return
  27. }
  28. errEntry.style.display = "none"
  29. leaderboard.removeChild(errEntry)
  30. // build player
  31. player_link.innerText = result.Items[0].Name
  32. player_link.href = ""// TODO use result.Items[0].Url
  33. // build leaderboard
  34. for (entry of result.Items[0].Entries) {
  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.BoardURL.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.BoardName
  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. }