-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetHighscore.php
More file actions
77 lines (65 loc) · 2.52 KB
/
getHighscore.php
File metadata and controls
77 lines (65 loc) · 2.52 KB
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
<?php
include_once('config.php');
$user_id_get = $_GET['user_id']?$_GET['user_id']:false;
$scores = array();
$db = new DB();
$db->verboseMode = false;
$db->query('SELECT boulder_id, avg(difficulty) FROM boulders_comments WHERE difficulty > 0 group by boulder_id');
while(list($boulder_id,$score) = $db->fetchRow())
{
$scores[$boulder_id] = $score;
}
//$db->query('SELECT id, username, facebook_id, count(bc.boulderid) AS finished FROM users u
//LEFT JOIN boulders_completed bc ON u.id = bc.userid WHERE id != 0 GROUP BY u.id ORDER BY finished DESC');
$user_scores = array();
$user_scores_real = array();
$user_finished_real = array();
$db->query('SELECT id, bc.boulderid FROM users u
JOIN boulders_completed bc ON u.id = bc.userid WHERE id != 0');
while(list($user_id, $boulder_id) = $db->fetchRow())
{
if($scores[$boulder_id]){
$user_scores_real[$user_id] += $scores[$boulder_id];
$user_finished_real[$user_id] += 1;
}
$user_scores[$user_id] += $scores[$boulder_id]?$scores[$boulder_id]:1;
}
if($user_id_get){
$db->query('SELECT id, username, facebook_id, count(bc.boulderid) AS finished FROM users u
LEFT JOIN boulders_completed bc ON u.id = bc.userid WHERE id != 0 AND (private = 0 OR u.id = '.$user_id_get.') GROUP BY u.id ORDER BY finished DESC');
}
else{
$db->query('SELECT id, username, facebook_id, count(bc.boulderid) AS finished FROM users u
LEFT JOIN boulders_completed bc ON u.id = bc.userid WHERE id != 0 AND private = 0 GROUP BY u.id ORDER BY finished DESC');
}
$first = false;
$highscores = array();
while(list($user_id, $user_name, $facebook_id, $finished) = $db->fetchRow())
{
$highscore = new Highscore();
$highscore->user_id = $user_id;
$highscore->user_name = $user_name;
$highscore->facebook_id = ($facebook_id?$facebook_id:'0');
$highscore->finished = $finished;
$highscore->score = ($user_scores[$user_id]?round($user_scores[$user_id],2):0);
if($user_finished_real[$user_id] > 0){
$highscore->diff_average = round($user_scores_real[$user_id]/$user_finished_real[$user_id],2);
}else{;
$highscore->diff_average = 0;
}
array_push($highscores, $highscore);
}
usort($highscores,cmpScores);
$rank = 1;
foreach($highscores as $h){
$h->rank = $rank++;
}
echo('{"highscores" : ' .json_encode($highscores) .'}');
function cmpScores($highscore1, $highscore2)
{
if ($highscore1->score == $highscore2->score) {
return 0;
}
return ($highscore1->score > $highscore2->score) ? -1 : 1;
}
?>