forked from arcxingye/EatKano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rank.php
190 lines (186 loc) · 8.13 KB
/
rank.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
@require 'conn.php';
@session_start();
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// non-existent default en
$lang_file = file_exists('static/i18n/' . $lang . '.json') ? "static/i18n/" . $lang . ".json" : "static/i18n/en.json";
$lang_data = file_get_contents($lang_file);
$i18n = json_decode($lang_data, true);
//Maximum number of pages that can be displayed
$max_pages = 9;
$RankingType = isset($_GET['type']) ? $_GET['type'] : 'day';
//Number of items that can be displayed on each page
$num = 10;
$CurrentPage = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1;
if (isset($_GET['name'])) {
$_SESSION['name'] = $_GET['name'];
}
$offset = ($CurrentPage - 1) * $num;
if ($RankingType == 'query') {
$queryname = isset($_GET['query']) ? $_GET['query'] : '';
$title = $i18n['query-record'];
$cond1 = "WHERE `name` LIKE ?";
$cond2 = $cond1 . ";";
}
if ($RankingType == 'day') {
$title = $i18n['day-rank'];
$cond1 = "WHERE to_days(time) = to_days(now())";
$cond2 = $cond1 . " ORDER BY `score` DESC limit ?,?;";
}
if ($RankingType == 'week') {
$title = $i18n['week-rank'];
$cond1 = "WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(time)";
$cond2 = $cond1 . " ORDER BY `score` DESC limit ?,?;";
}
if ($RankingType == 'month') {
$title = $i18n['month-rank'];
$cond1 = "WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(time)";
$cond2 = $cond1 . " ORDER BY `score` DESC limit ?,?;";
}
if ($RankingType == 'all') {
$title = $i18n['all-rank'];
$cond1 = "";
$cond2 = "ORDER BY score DESC limit ?,?;";
}
?>
<!DOCTYPE html>
<html lang=<?php echo $i18n['lang']; ?>>
<head>
<title><?php echo $i18n['rank-title']; ?></title>
<meta item="description" content="EatKano" />
<meta charset="utf-8" />
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0" />
<link href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.staticfile.org/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="./"><?php echo $i18n['navbar-brand']; ?></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link <?php echo $RankingType == 'day' ? "active" : ""; ?>" href="?type=day"><?php echo $i18n['daily-ranking']; ?></a>
</li>
<li class="nav-item">
<a class="nav-link <?php echo $RankingType == 'week' ? "active" : ""; ?>" href="?type=week"><?php echo $i18n['weekly-ranking']; ?></a>
</li>
<li class="nav-item">
<a class="nav-link <?php echo $RankingType == 'month' ? "active" : ""; ?>" href="?type=month"><?php echo $i18n['monthly-ranking']; ?></a>
</li>
<li class="nav-item">
<a class="nav-link <?php echo $RankingType == 'all' ? "active" : ""; ?>" href="?type=all"><?php echo $i18n['all-ranking']; ?></a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://github.com/arcxingye/EatKano/"><?php echo $i18n['source-code']; ?></a>
</li>
</ul>
<form class="d-flex text-nowrap" action="" onsubmit="return func()">
<input class="form-control me-2" id="search" placeholder="<?php echo $i18n['query-input']; ?>">
<button class="btn btn-outline-success" onclick="local()"><?php echo $i18n['search-btn']; ?></button>
</form>
<script>
function func() {
return false;
}
function local() {
if ($('#search').val()) {
window.location.href = "?type=query&query=" + $('#search').val();
} else {
alert("Name not filled in")
}
}
</script>
</div>
</div>
</nav>
<div style="max-width:640px;margin:0 auto;">
<div class="page-header text-center">
<br />
<h1><?php echo $title; ?></h1><br />
</div>
<div class="list-group">
<?php
$rank = $offset;
$data_sql = "SELECT * FROM " . $ranking . " " . $cond2;
if ($data_stmt = $link->prepare($data_sql)) {
if ($RankingType == "query") {
$queryname = '%'.$queryname.'%';
$data_stmt->bind_param("s", $queryname);
} else {
$data_stmt->bind_param("ii", $offset, $num);
}
$data_stmt->execute();
$data_stmt->store_result();
$data_stmt->bind_result($id, $score, $name, $time, $system, $area, $message, $attempts);
if ($data_stmt->num_rows > 0) {
while ($data_stmt->fetch()) {
$rank += 1;
echo "<a href='#' class='list-group-item list-group-item-action'><div class='d-flex w-100 justify-content-between'>
<h5 class='mb-1'>" . (($rank == 1 || $rank % 10 == 1) ? ($rank . "st ") : (($rank == 2 || $rank % 10 == 2) ? ($rank . "rd ") : ($rank . "th "))) . $name . "</h5><small>" . $time . "</small></div>
<p class='mb-1'>SCORE: " . $score . " TRY: " . $attempts . " -" . $system . " -" . $area . "</p>
<small>" . ($message ? $message : $i18n['no-message']) . "</small></a>";
}
} else {
echo "<br/><br/><p class='text-center'>" . $i18n['no-data'] . "<p>";
}
$data_stmt->close();
}
?>
<nav aria-label="Page navigation example" style="margin-bottom:3em;">
<ul class="pagination">
<?php
if ($RankingType != "query") {
$rows_sql = "SELECT count(*) FROM " . $ranking . " " . $cond1 . ";";
$rows_data = mysqli_query($link, $rows_sql);
$rows = mysqli_fetch_row($rows_data)[0];
$rows = $rows > $num * $max_pages ? $num * $max_pages : $rows;
$total = ceil($rows / $num);
if ($total > 1) {
if ($CurrentPage > 1) {
echo "<li class='page-item'><a class='page-link' href='?type=" . $RankingType . "&page=" . ($CurrentPage - 1) . "' aria-label='Previous'><span aria-hidden='true'>«</span></a></li>";
}
for ($p = 1; $p <= $total; $p++) {
echo "<li class='page-item " . ($CurrentPage == $p ? "active" : "") . "'><a class='page-link' href='?type=" . $RankingType . "&page=" . $p . "'>" . $p . "</a></li>";
}
if ($total > $CurrentPage) {
echo "<li class='page-item'><a class='page-link' href='?type=" . $RankingType . "&page=" . ($CurrentPage + 1) . "' aria-label='Next'><span aria-hidden='true'>»</span></a></li>";
}
}
}
?>
</ul>
</nav>
</div>
<footer class='fixed-bottom container' style='max-width:640px;'>
<div class='row shadow rounded bg-light'>
<div style='padding:0.2em 1em;'>
<?php
if (isset($_SESSION['name'])) {
//Query current user history
$score_sql = "SELECT `score`,`time`,`attempts` FROM " . $ranking . " where name=?";
$score_stmt = $link->prepare($score_sql);
$score_stmt->bind_param("s", $_SESSION['name']);
$score_stmt->bind_result($score, $time, $attempts);
$score_stmt->execute();
if ($score_stmt->fetch()) {
echo strtr($i18n["self-record"], array("{name}" => $_SESSION['name'], "{attempts}" => $attempts, "{score}" => $score, "{time}" => $time));
} else {
echo strtr($i18n["no-self-record"], array("{name}" => $_SESSION['name']));
}
$score_stmt->close();
} else {
echo $i18n["no-name-tip"];
}
$link->close();
?>
</div>
</div>
</footer>
</div>
</body>
</html>