-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
88 lines (78 loc) · 2.74 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
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="preconnect" href="https://api.alt-mp.com">
<title>alt:V</title>
<style>
body {
color: white;
font-size: 225%;
text-align: right;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
</style>
</html>
<body>
<span id="p_count">0</span> / <span id="max_pcount">0</span>
</body>
<footer>
<script>
// config
const server_id = "EnLAA0O";
// refresh time in minuets
// don`t go below 1
const refresh_time = 3;
// end config
let fetch_interval = undefined;
async function fetch_players() {
const request_url = `https://api.alt-mp.com/servers/${server_id}`;
console.info("Requesting server data.");
try {
const response = await fetch(request_url);
if (!response.ok) {
console.error("Couln`t fetch api data!");
return [0, 0]
} else {
let json = await response.json();
if (!json.available) {
console.error("The server is offline!");
return [0, 0]
} else {
let players = json.playersCount;
let max_players = json.maxPlayersCount;
return [players, max_players]
}
}
} catch (error) {
console.error(error);
return [0, 0]
}
return [0, 0]
}
function set_new_stats(players, maxPlayers) {
console.info("settings new stats");
const player_counter = document.getElementById("p_count");
const max_player_counter = document.getElementById("max_pcount");
player_counter.innerHTML = players;
max_player_counter.innerHTML = maxPlayers;
}
async function update_stats() {
const current_data = await fetch_players();
set_new_stats(current_data[0], current_data[1]);
}
// initial fetch
update_stats();
// we don`t go below one
if (typeof(refresh_time) != "number" || refresh_time < 1) {
fetch_interval = window.setInterval(update_stats, (1 * 60 * 1000));
} else {
fetch_interval = window.setInterval(update_stats, (refresh_time * 60 * 1000));
}
window.onbeforeunload = function() {
window.clearInterval(fetch_interval);
}
</script>
</footer>
</html>