]> git.sesse.net Git - foosball/blobdiff - www/index.pl
More cleanups, and statify.
[foosball] / www / index.pl
index a9f2d43b7d022a7dd133f424b004dc3436afba1d..9d439751bde488156435cfd3f898a7a9c6b8878f 100755 (executable)
@@ -10,7 +10,7 @@ my $dbh = foosball::db_connect();
 
 # Single score board (whoa, inefficient)
 my @single_top = ();
-my $q = $dbh->prepare('select username from users');
+my $q = $dbh->prepare('select username,count(*) as num_games from single_rating group by username');
 $q->execute();
 while (my $ref = $q->fetchrow_hashref) {
        my $username = $ref->{'username'};
@@ -21,22 +21,23 @@ while (my $ref = $q->fetchrow_hashref) {
 
        my $trend = "";
        if (defined($oldrating)) {
-               $trend = (sprintf "%+d", int($rating-$oldrating+0.5));
+               $trend = (sprintf "%+d", foosball::round($rating-$oldrating));
        }
 
        push @single_top, {
                'username' => $username,
-               'rating' => int($rating+0.5),
-               'rd' => int($rd+0.5),
-               'lowerbound' => int($rating - 3.0*$rd + 0.5),
+               'rating' => foosball::round($rating),
+               'rd' => foosball::round($rd),
+               'lowerbound' => foosball::round($rating - 3.0*$rd),
                'trend' => $trend,
+               'numgames' => $ref->{'num_games'},
        };
 }
 @single_top = sort { $b->{'lowerbound'} <=> $a->{'lowerbound'} } @single_top;
 
 # Double score board
 my @double_top = ();
-$q = $dbh->prepare('select username from users');
+$q = $dbh->prepare('select username,count(*) as num_games from double_rating group by username');
 $q->execute();
 while (my $ref = $q->fetchrow_hashref) {
        my $username = $ref->{'username'};
@@ -47,15 +48,16 @@ while (my $ref = $q->fetchrow_hashref) {
 
        my $trend = "";
        if (defined($oldrating)) {
-               $trend = (sprintf "%+d", int($rating-$oldrating+0.5));
+               $trend = (sprintf "%+d", foosball::round($rating-$oldrating));
        }
 
        push @double_top, {
                'username' => $username,
-               'rating' => int($rating+0.5),
-               'rd' => int($rd+0.5),
-               'lowerbound' => int($rating - 3.0*$rd + 0.5),
+               'rating' => foosball::round($rating),
+               'rd' => foosball::round($rd),
+               'lowerbound' => foosball::round($rating - 3.0*$rd),
                'trend' => $trend,
+               'numgames' => $ref->{'num_games'},
        };
 }
 @double_top = sort { $b->{'lowerbound'} <=> $a->{'lowerbound'} } @double_top;
@@ -117,14 +119,14 @@ $q->execute();
 while (my $ref = $q->fetchrow_hashref) {
        if (defined($ref->{'diff2'})) {
                $ref->{'diff1'} = sprintf "%+d / %+d",
-                       int($ref->{'diff1'} + 0.5),
-                       int($ref->{'diff2'} + 0.5);
+                       foosball::round($ref->{'diff1'}),
+                       foosball::round($ref->{'diff2'});
                $ref->{'diff2'} = sprintf "%+d / %+d",
-                       int($ref->{'diff3'} + 0.5),
-                       int($ref->{'diff4'} + 0.5);
+                       foosball::round($ref->{'diff3'}),
+                       foosball::round($ref->{'diff4'});
        } else {
-               $ref->{'diff1'} = sprintf "%+d", int($ref->{'diff1'} + 0.5);
-               $ref->{'diff2'} = sprintf "%+d", int($ref->{'diff3'} + 0.5);
+               $ref->{'diff1'} = sprintf "%+d", foosball::round($ref->{'diff1'});
+               $ref->{'diff2'} = sprintf "%+d", foosball::round($ref->{'diff3'});
        }
        push @last_games, $ref;
 }