-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
77 lines (65 loc) · 2.68 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Treasure Game Hunter</title>
</head>
<body>
<h1>Welcome To Treasure Hunter Game Multiplayer</h1>
<p id="username"></p>
<a href="#" id="matchmaking">MATCHMAKING</a>
<a href="login.html" id="btnLogin">LOGIN</a>
<a href="register.html" id="btnRegist">REGISTER</a>
<a href="rankings.html" id="btnRanking">List Users Ranking</a>
<a href="matchs.html" id="btnMatch">History Match</a>
<script>
async function fetchData() {
let user;
let userId;
const btnLogin = document.getElementById("btnLogin");
const btnRegist = document.getElementById("btnRegist");
const username = document.getElementById("username");
const matchmaking = document.getElementById("matchmaking");
if (localStorage.getItem("user")) {
user = JSON.parse(localStorage.getItem("user"));
userId = user.user.id;
} else {
user = null;
userId = null;
}
if (!user) {
btnLogin.style.display = "block";
btnRegist.style.display = "block";
matchmaking.style.display = "none";
return;
};
const response = await fetch(`http://localhost:3019/api/users/${userId}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${localStorage.getItem("accessToken")}`,
},
}).catch((err) => console.log(err));
const data = await response.json();
if (data.meta.code == 200) {
btnLogin.style.display = "none";
btnRegist.style.display = "none";
matchmaking.style.display = "block";
username.innerHTML = user.user.name;
} else if (data.meta.code == 401 || data.meta.code == 404) {
btnLogin.style.display = "block";
btnRegist.style.display = "block";
matchmaking.style.display = "none";
}
}
fetchData();
const matchmaking = document.getElementById("matchmaking");
matchmaking.addEventListener("click", () => {
const user = JSON.parse(localStorage.getItem("user"));
window.location.href = `game.html?name=${user.user.name}`;
});
</script>
</body>
</html>