Show ranking list on the web interface.
[ccbs] / html / show-tournament.pl
1 #! /usr/bin/perl
2
3 use ccbs;
4 use strict;
5 use warnings;
6
7 my $cgi = new CGI;
8 my $id = $cgi->param('id');
9
10 my $dbh = ccbs::db_connect();
11
12 my $tournament = $dbh->selectrow_hashref('SELECT * FROM tournaments NATURAL JOIN seasons NATURAL JOIN countries NATURAL JOIN machines NATURAL JOIN scoringsystems WHERE tournament=?', undef, $id);
13 my $rankings = ccbs::db_fetch_all($dbh, 'SELECT ranking,nick,COALESCE(points,-1) AS points FROM tournamentrankings NATURAL JOIN players WHERE tournament=? ORDER BY ranking', $id);
14
15 # Swoop all the data in in a big join, then order it over to quasi-sane Perl objects.
16 # (round -> parallel -> player -> songs -> title,chosen,score)
17 my $scores = ccbs::db_fetch_all($dbh,
18         'SELECT round,parallel,position,nick,title,chosen,score FROM scores NATURAL JOIN players NATURAL JOIN songs NATURAL JOIN roundparticipation WHERE tournament=? ORDER BY round,parallel,position,songnumber',
19         $id);
20
21 my @rounds = ();
22
23 my ($round, $parallel, $player) = (-1,-1,'');
24 for my $score (@$scores) {
25         if ($score->{'round'} != $round) {
26                 $round = $score->{'round'};
27                 push @rounds, { round => $round, parallels => [] };
28                 $parallel = -1;
29         }
30         my $p = $rounds[$#rounds]->{'parallels'};
31         if ($score->{'parallel'} != $parallel) {
32                 $parallel = $score->{'parallel'};
33                 push @$p, { parallel => $parallel, players => [], songs => [] };
34                 $player = '';
35
36                 # Information on songs is not selected from roundrandomsongs etc.,
37                 # but is filled in the first time the song is seen for this round
38                 # (ie. below)
39         }
40         if ($score->{'position'} == 1) {
41                 if ($score->{'chosen'}) {
42                         push @{$p->[$#$p]->{'songs'}}, '';
43                 } else {
44                         push @{$p->[$#$p]->{'songs'}}, $score->{'title'};
45                 }
46         }
47         
48         my $pl = $p->[$#$p]->{'players'};
49         if ($score->{'nick'} ne $player) {
50                 $player = $score->{'nick'};
51                 push @$pl, { nick => $player, songs => [] };
52
53         }
54         
55         push @{$pl->[$#$pl]->{'songs'}}, {
56                 title => $score->{'title'},
57                 chosen => $score->{'chosen'},
58                 score => $score->{'score'}
59         };
60 }
61
62 ccbs::print_header();
63 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
64         tournament => $tournament,
65         rankings => $rankings,
66         rounds => \@rounds
67 });
68 $dbh->disconnect;