forked from BuildTheEarth-Italia/Wordpress-Theme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
points.php
357 lines (288 loc) · 11.5 KB
/
points.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/*
Template Name: Punti classifica
Template Post Type: page
*/
define('DONOTCACHEPAGE', true);
// Importo lo stile della tabella
wp_enqueue_style('bte_points_style');
// Stampo l'header
get_header();
// Dico a Wordpress di caricare il gile Javascript per gli utenti online
wp_enqueue_script('bte_points_online_players_loader');
// Path al file di cache e durata massima, se sono rimossi non verrà eseguita la cache
define('CACHE_FILENAME', WP_CONTENT_DIR . '/cache/classifica-cached.json'); // Default: WP_CONTENT_DIR . '/cache/classifica-cached.json'
define('CACHE_MAX_LIFE', 'P2D'); // Default: P2D
// URL Per le richieste al plugin https://github.com/BuildTheEarth-Italia/DataList
define('URL', 'http://bteitalia.it:8000');
$leaderboard = array();
$permissions = new stdClass();
$playtime = new stdClass();
$gotCachedData = defined('CACHE_FILENAME') && defined('CACHE_MAX_LIFE') && obtainCachedDataIfAvailable($leaderboard, $permissions, $playtime);
// Se non sono riuscito a caricare la cache carico le risorse "fresche"
if (!$gotCachedData)
obtainFreshData($leaderboard, $permissions, $playtime);
// Prendo l'indice di partenza
$start = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
// Se l'input non è valido cado nel valore di default (zero)
if ($start === null || $start === false)
$start = 0;
?>
<div class="grass"></div>
<section class="panel commonPanel pointsPanel" id="classifica">
<h2 class="post-title"><?php the_title(); ?></h2>
<div class="post-content"><?php the_content(); ?></div>
<div class="table-wrapper">
<table>
<thead class="table-head">
<tr>
<th class="rank">#</th>
<th class="username">Username</th>
<th class="points">Punti</th>
</tr>
</thead>
<tbody class="table-body">
<?php
// Itero tutti gli elementi
if ($leaderboard != null)
for ($i = $start; $i < $start + 20 && $i < count($leaderboard); $i++) {
// Ottengo il gruppo a cui il player appartiene
$role_class = get_role_class($leaderboard[$i], $permissions);
?>
<tr data-index="<?= $i; ?>" data-username="<?= $leaderboard[$i]->name; ?>">
<td class="rank"><?= $i + 1; ?></td>
<td class="username bubble-hover-trigger">
<img class="avatar" loading="lazy" src="https://www.mc-heads.net/avatar/<?= $leaderboard[$i]->name; ?>/25/" onerror="this.src='https:\/\/www.mc-heads.net\/avatar\/MHF_steve\/25\/';">
<span class="name bubble-wrapper">
<span class="<?= $role_class; ?>"><?= $leaderboard[$i]->name; ?></span>
<div class="bubble<?= ($i < $start + 3) ? ' upside-down' : ''; ?>">
<img class="avatar" loading="lazy" src="https://www.mc-heads.net/avatar/<?= $leaderboard[$i]->name; ?>/80/" onerror="this.src='https:\/\/www.mc-heads.net\/avatar\/MHF_steve\/50';">
<h1 class="name <?= $role_class; ?>"><?= $leaderboard[$i]->name; ?></h1>
<div class="decorator">
<span class="decorator-title">Punti</span>
<span class="decorator-value"><?= $leaderboard[$i]->score; ?></span>
</div>
<div class="decorator">
<span class="decorator-title">Rank</span>
<span class="decorator-value <?= $role_class; ?>"><?= $role_class != null ? ucfirst(str_replace('role-', '', $role_class)) : "Starter"; ?></span>
</div>
<div class="decorator">
<span class="decorator-title">Nazione</span>
<span class="decorator-value"><?= get_national_flag($leaderboard[$i], $permissions); ?></span>
</div>
<?php
$currentPlayerPlaytime = $playtime->{$leaderboard[$i]->name};
if ($currentPlayerPlaytime !== null) { ?>
<div class="decorator playtime">
<span class="decorator-title">Tempo di gioco</span>
<span class="decorator-value"><?= $currentPlayerPlaytime; ?></span>
</div>
<?php } ?>
</div>
</span>
</td>
<td class="points"><?= $leaderboard[$i]->score; ?></td>
</tr>
<?php
}
?>
</tr>
</tbody>
</table>
</div>
<div class="buttons">
<?php
$uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2)[0];
if ($start >= 20)
echo '<a href="' . $uri_parts . '?start=' . ($start - 20 - $start % 20) . '" class="prev">Precedente</a>';
if ($start + 20 <= count($leaderboard))
echo '<a href="' . $uri_parts . '?start=' . ($start + 20 - $start % 20) . '" class="next">Successivo</a>';
?>
</div>
</section>
<?php
get_footer();
// Avvio lo script per il caricamento dei player online
echo '<script>loadOnlineUsers("' . URL . '");</script>';
flush();
if (!$gotCachedData)
saveFreshData($leaderboard, $permissions, $playtime);
function get_role_class($user, $groups)
{
if ($groups != null) {
$name = $user->name;
if (array_search($name, $groups->master) !== false) {
return 'role-master';
} else if (array_search($name, $groups->expert) !== false) {
return 'role-expert';
} else if (array_search($name, $groups->architect) !== false) {
return 'role-architect';
}
}
$points = $user->score;
if ($points >= 2000) {
return 'role-expert';
} else if ($points >= 1250 && $points < 2000) {
return 'role-architect';
} else if ($points >= 750 && $points < 1250) {
return 'role-builder';
} else if ($points >= 250 && $points < 750) {
return 'role-trainee';
}
return null;
}
function get_national_flag($user, $groups)
{
if ($groups != null) {
$name = $user->name;
if (array_search($name, $groups->yugoslavia) !== false) {
return '🇭🇷';
} else if (array_search($name, $groups->malta) !== false) {
return '🇲🇹';
}
}
return '🇮🇹';
}
function obtainCachedDataIfAvailable(&$points, &$groups, &$time)
{
$f = @fopen(CACHE_FILENAME, 'r');
// Se il file non esiste ritorno false
if ($f === false)
return false;
// Ottengo i dati dal JSON
$cachedData = json_decode(
fread($f, filesize(CACHE_FILENAME))
);
// Chiudo il file
fclose($f);
// Verifico che siano passati almeno 2 gorni
$isCacheExpired = DateTimeImmutable::createFromFormat('U', $cachedData->cacheDate)->add(new DateInterval(CACHE_MAX_LIFE)) < new DateTimeImmutable();
// Se la cache è scauta ritorno false
if ($isCacheExpired)
return false;
// Salvo i punti
$points = $cachedData->points;
// i ruoli...
$groups = $cachedData->groups;
// e il playtime
$time = $cachedData->playtime;
// Ritorno con successo
return true;
}
function obtainFreshData(&$points, &$groups, &$time)
{
// Ottengo la lista dei punti
$tmpPoints = fetchRemoteData('/points');
if ($tmpPoints != null) {
$tmpPoints = $tmpPoints->Leaderboard;
// Ordino la lista per numero di punti
function cmp($a, $b)
{
if ($a->score == $b->score) {
return 0;
}
return ($a->score > $b->score) ? -1 : 1;
}
usort($tmpPoints, 'cmp');
// Assegno il valore alla variabile globale
$points = $tmpPoints;
unset($tmpPoints);
}
// Ottengo la lista dei ruoli
$tmpGroups = fetchRemoteData('/permissions');
if ($tmpGroups != null) {
$tmpGroups = $tmpGroups->groups;
// Ordino la lista e rimuovo i ruoli inutili per la classifica
foreach ($tmpGroups as $role) {
switch ($role->name) {
case 'master':
case 'expert':
case 'architect':
case 'yugoslavia':
case 'malta':
// Assegno il gruppo a variable locale
$groups->{$role->name} = $role->members;
break;
}
}
// Rimuovo la variabile temporanea
unset($tmpGroups);
}
// Ottengo la lista dei ruoli
$tmpPlaytime = fetchRemoteData('/playtime');
if ($tmpPlaytime != null) {
$tmpPlaytime = $tmpPlaytime->playtime;
// Ordino la lista e modifico il tempo di gioco
foreach ($tmpPlaytime as $entry) {
$t = $entry->ticks;
// Converto il tempo da ticks al formato "umano"
$seconds = (int) ($t / 20);
$minutes = (int) ($seconds / 60);
$hours = (int) ($minutes / 60);
$days = (int) ($hours / 24);
// Creo la stringa
if ($days > 0)
$time->{$entry->name} = $days . ' giorni, ';
if (($hours % 24) > 0)
$time->{$entry->name} .= $hours % 24 . ' ore, ';
$time->{$entry->name} .= $minutes % 60 . ' minuti e ';
$time->{$entry->name} .= $seconds % 60 . ' secondi';
}
// Rimuovo la variabile temporanea
unset($tmpPlaytime);
}
}
function saveFreshData($points, $groups, $time)
{
// Creo la cartella se non esiste
wp_mkdir_p(dirname(CACHE_FILENAME));
// Apro il file cancellando tutto il contenuto
$f = fopen(CACHE_FILENAME, 'w');
// Se il file non puo essere creato ritorno false
if ($f === false)
return false;
// Ottengo i dati dal JSON
$serializedCachedData = json_encode(
array(
'cacheDate' => time(),
'points' => $points,
'groups' => $groups,
'playtime' => $time
)
);
fwrite($f, $serializedCachedData);
// Chiudo il file
fclose($f);
// Ritorno con successo
return true;
}
function fetchRemoteData($endpoint)
{
// Apro cURL
$ch = curl_init(URL . $endpoint);
// Parametri di configurazione di cURL
curl_setopt_array(
$ch,
array(
// Dico a cURL di darmi il risultato e non inviarmi gli headers di HTTP
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
// Impongo di seguire i redirect
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
// Imposto il formato di un risultato accettabile
CURLOPT_HTTPHEADER => array(
'Accept' => 'application/json'
),
)
);
// Parsing del JSON
$decodedJSON2Return = json_decode(
curl_exec($ch)
);
print_r(curl_error($ch));
// Chiudo la connessione
curl_close($ch);
return $decodedJSON2Return;
}
?>