]> git.sesse.net Git - ccbs/blobdiff - html/show-tournament.pl
Replace the web frontend's rank calculations with the stored procedures.
[ccbs] / html / show-tournament.pl
index d69dc3219989f35a425eb4df2a4d52100335dbc6..d5173bac97239d730777de776e215f9401fffa37 100755 (executable)
@@ -13,9 +13,9 @@ my $tournament = $dbh->selectrow_hashref('SELECT * FROM tournaments NATURAL JOIN
 
 my $rankings;
 if ($tournament->{'country'} == 1) {
-       $rankings = ccbs::db_fetch_all($dbh, 'SELECT ranking,nick || \' (\' || countrycode || \')\' AS nick,COALESCE(points,-1) AS points FROM tournamentrankings NATURAL JOIN players NATURAL JOIN countries WHERE tournament=? ORDER BY ranking', $id);
+       $rankings = ccbs::db_fetch_all($dbh, 'SELECT ranking,player,nick || \' (\' || countrycode::varchar || \')\' AS nick,COALESCE(points,-1) AS points FROM tournamentrankings NATURAL JOIN players NATURAL JOIN countries WHERE tournament=? ORDER BY ranking', $id);
 } else {
-       $rankings = ccbs::db_fetch_all($dbh, 'SELECT ranking,COALESCE(nick || \' (\' || clubcode || \')\', nick) AS nick,COALESCE(points,-1) AS points FROM tournamentrankings NATURAL JOIN players NATURAL LEFT JOIN clubs WHERE tournament=? ORDER BY ranking', $id);
+       $rankings = ccbs::db_fetch_all($dbh, 'SELECT ranking,player,COALESCE(nick || \' (\' || clubcode::varchar || \')\', nick) AS nick,COALESCE(points,-1) AS points FROM tournamentrankings NATURAL JOIN players NATURAL LEFT JOIN clubs WHERE tournament=? ORDER BY ranking', $id);
 }
 
 my $songs = ccbs::db_fetch_all($dbh, 'SELECT song,title FROM machinesongs NATURAL JOIN songs WHERE machine=? ORDER BY LOWER(title)', $tournament->{'machine'});
@@ -46,12 +46,12 @@ if ($bigscreen) {
 my $scores;
 if ($tournament->{'country'} == 1) {
        $scores = ccbs::db_fetch_all($dbh,
-       'SELECT round,parallel,position,playmode,difficulty,songnumber,player,nick || \' (\' || countrycode || \')\' AS nick,song,title,chosen,score FROM roundparticipation NATURAL JOIN players NATURAL JOIN countries NATURAL JOIN scores NATURAL LEFT JOIN songs WHERE tournament=? ORDER BY round,parallel,position,songnumber',
-       $id);
+       'SELECT round,parallel,position,playmode,difficulty,songnumber,player,nick || \' (\' || countrycode::varchar || \')\' AS nick,song,title,chosen,score,best_rank,worst_rank FROM roundparticipation NATURAL JOIN players NATURAL JOIN countries NATURAL JOIN scores NATURAL LEFT JOIN songs NATURAL JOIN get_minmax_rank_for_players(?,\'single\') WHERE tournament=? ORDER BY round,parallel,position,songnumber',
+       $id, $id);
 } else {
        $scores = ccbs::db_fetch_all($dbh,
-       'SELECT round,parallel,position,playmode,difficulty,songnumber,player,COALESCE(nick || \' (\' || clubcode || \')\', nick) AS nick,song,title,chosen,score FROM roundparticipation NATURAL JOIN players NATURAL JOIN scores NATURAL LEFT JOIN songs NATURAL LEFT JOIN clubs WHERE tournament=? ORDER BY round,parallel,position,songnumber',
-       $id);
+       'SELECT round,parallel,position,playmode,difficulty,songnumber,player,COALESCE(nick || \' (\' || clubcode::varchar || \')\', nick) AS nick,song,title,chosen,score,best_rank,worst_rank FROM roundparticipation NATURAL JOIN players NATURAL JOIN scores NATURAL LEFT JOIN songs NATURAL LEFT JOIN clubs NATURAL JOIN get_minmax_rank_for_players(?,\'single\') WHERE tournament=? ORDER BY round,parallel,position,songnumber',
+       $id, $id);
 }
 
 my @rounds = ();
@@ -96,7 +96,8 @@ for my $score (@$scores) {
        my $pl = $p->[$#$p]->{'players'};
        if ($score->{'nick'} ne $player) {
                $player = $score->{'nick'};
-               push @$pl, { player => $score->{'player'}, nick => $player, songs => [], total => 0, rank => 1 };
+               push @$pl, { player => $score->{'player'}, nick => $player, songs => [], total => 0, best_rank => $score->{'best_rank'},
+                       worst_rank => $score->{'worst_rank'} };
        }
 
        push @{$pl->[$#$pl]->{'songs'}}, $score;
@@ -106,24 +107,6 @@ for my $score (@$scores) {
        }
 }
 
-# Find the rank of all the players in every group, via simple insertion-like
-# sort
-for my $r (@rounds) {
-       for my $p (@{$r->{'parallels'}}) {
-               my $pls = $p->{'players'};
-               for my $i (0..$#$pls) {
-                       my $rank = 1;
-                       for my $j (0..($i-1)) {
-                               if ($pls->[$i]->{'total'} < $pls->[$j]->{'total'}) {
-                                       $pls->[$i]->{'rank'}++;
-                               } elsif ($pls->[$i]->{'total'} > $pls->[$j]->{'total'}) {
-                                       $pls->[$j]->{'rank'}++;
-                               }
-                       }
-               }
-       }
-}
-
 # FIXME: In some odd cases, there _might_ be an empty group right at the end. Fix this when
 #        we are able to add/delete people in groups.
 
@@ -157,7 +140,9 @@ if ($num_rounds == 0) {
 if ($num_rankings > 0) {
        $closing_valid = 0;
        $finishing_valid = 0;
-       $rounds[$#rounds]->{'locked'} = 1;
+       if ($#rounds > -1) {
+               $rounds[$#rounds]->{'locked'} = 1;
+       }
 }
 
 ccbs::print_header();