-
Notifications
You must be signed in to change notification settings - Fork 2
/
highscores.html
126 lines (116 loc) · 3.95 KB
/
highscores.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta property="og:locale" content="en-US">
<meta property="og:site_name" content="Trippin on Tubs Scores">
<meta property="og:title" content="Trippin on Tubs Scores">
<meta name="description" content="The website for Trippin on Tubs high scores.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" href="pico.min.css">
<title>High Scores</title>
<style>
a.active {
font-weight: bold;
}
</style>
</head>
<body>
<main class="container">
<nav>
<ul>
<li><a class="period active" href="#" id="a_alltime">All-Time</a></li>
<li><a class="period" href="#" id="a_today">Today</a></li>
</ul>
<ul>
<li><a class="version" id="v4" href="highscores.html?version=4">V1</a></li>
<li><a class="version" id="v5" href="highscores.html?version=5">V2</a></li>
<li><a class="version" id="v6" href="highscores.html?version=6">V3</a></li>
</ul>
</nav>
<table id="tbl_alltime" role="grid">
<thead>
<tr>
<th scope="col">Rank</th>
<th scope="col">Name</th>
<th scope="col">Score</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody id="tb_alltime">
</tbody>
</table>
<table id="tbl_today" role="grid" style="display: none">
<thead>
<tr>
<th scope="col">Rank</th>
<th scope="col">Name</th>
<th scope="col">Score</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody id="tb_today">
</tbody>
</table>
</main>
</body>
<script>
const latestVersion = "6";
const params = new URLSearchParams(window.location.search);
const versionParam = params.get("version");
const targetVersion = versionParam ? versionParam.toString() : latestVersion;
function setActive(type, id) {
document.querySelectorAll(`.${type}`).forEach(element => {
element.classList.remove('active');
});
document.getElementById(id).classList.add('active');
}
setActive('version', `v${targetVersion}`);
function addCell(row, text) {
const cell = row.insertCell();
cell.appendChild(document.createTextNode(text));
}
function addScores(scores, target) {
const tbody = document.getElementById(target);
scores.forEach((score, index) => {
const row = tbody.insertRow();
addCell(row, index + 1);
addCell(row, score['name']);
addCell(row, score['score']);
addCell(row, score['date']);
});
}
function loadScores(resource, fn) {
var xhr = new XMLHttpRequest();
xhr.open('GET', resource, true);
xhr.onload = function (e) {
if (xhr.readyState === 4 && xhr.status === 200) {
fn(JSON.parse(xhr.responseText));
}
};
xhr.send(null);
}
function addClickHandler(id, fn) {
document.getElementById(id).addEventListener("click", function(event) {
event.preventDefault();
fn();
});
}
function toggle(showId, hideId) {
document.getElementById(showId).style.display = '';
document.getElementById(hideId).style.display = 'none';
}
const qs = `?version=${targetVersion}&no_events=true&limit=100`;
loadScores(`/scores/alltime${qs}`, (scores) => {addScores(scores, 'tb_alltime')});
loadScores(`/scores/today${qs}`, (scores) => {addScores(scores, 'tb_today')});
addClickHandler('a_alltime', () => {
setActive('period', 'a_alltime');
toggle('tbl_alltime', 'tbl_today');
});
addClickHandler('a_today', () => {
setActive('period', 'a_today');
toggle('tbl_today', 'tbl_alltime');
});
</script>
</html>