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.

62 lines
1.5 KiB

  1. const token = localStorage.getItem("token");
  2. console.log(token);
  3. if (localStorage.getItem("token") === null) {
  4. // redirect to dashboard
  5. window.location.href = "/index.html";
  6. }
  7. function logout() {
  8. localStorage.removeItem("token");
  9. window.location.href = "/index.html";
  10. }
  11. let config = {
  12. "crossdomain": true,
  13. headers: {
  14. "Content-Type": "application/json",
  15. "Authorization": "Bearer " + token
  16. }
  17. };
  18. let user = {};
  19. // get user data
  20. function getUser() {
  21. axios.get(url + "/", config).then(function(res) {
  22. console.log(res.data);
  23. user = res.data.user;
  24. console.log(user);
  25. document.getElementById("name").innerHTML = user.Name;
  26. document.getElementById("points").innerHTML = Math.floor(user.Points/1000);
  27. // if (localStorage.getItem("mainplanet") === null) {
  28. localStorage.setItem("mainplanet", res.data.user.Planets[0]);
  29. // }
  30. getPlanet();
  31. }).catch(function(error) {
  32. console.log(error);
  33. });
  34. }
  35. let planetid = localStorage.getItem("mainplanet");
  36. let planet = {};
  37. // get user data
  38. function getPlanet() {
  39. planetid = localStorage.getItem("mainplanet");
  40. axios.get(url + "/planets/" + planetid, config).then(function(res) {
  41. console.log("planet", res.data);
  42. planet = res.data.planet;
  43. console.log("planet", planet);
  44. printResources(planet.Resources);
  45. if (window.location.href.includes("buildings.html")) {
  46. printBuildings(planet);
  47. }
  48. }).catch(function(error) {
  49. console.log(error);
  50. });
  51. }
  52. getUser();
  53. setInterval(getUser, 10000);