8 my $id = $cgi->param('id');
10 my $dbh = ccbs::db_connect();
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);
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 roundparticipation NATURAL LEFT JOIN roundrandomsongs NATURAL LEFT JOIN scores NATURAL JOIN songs NATURAL JOIN players WHERE tournament=? ORDER BY round,parallel,position,songnumber',
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 => [] };
30 my $p = $rounds[$#rounds]->{'parallels'};
31 if ($score->{'parallel'} != $parallel) {
32 $parallel = $score->{'parallel'};
33 push @$p, { parallel => $parallel, players => [], songs => [], num_songs => 0 };
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
40 if ($score->{'position'} == 1) {
41 if ($score->{'chosen'}) {
42 push @{$p->[$#$p]->{'songs'}}, '';
44 push @{$p->[$#$p]->{'songs'}}, $score->{'title'};
46 $p->[$#$p]->{'num_songs'}++;
49 my $pl = $p->[$#$p]->{'players'};
50 if ($score->{'nick'} ne $player) {
51 $player = $score->{'nick'};
52 push @$pl, { nick => $player, songs => [], total => 0 };
56 push @{$pl->[$#$pl]->{'songs'}}, {
57 title => $score->{'title'},
58 chosen => $score->{'chosen'},
59 score => $score->{'score'}
61 $pl->[$#$pl]->{'total'} += $score->{'score'};
64 my $num_rounds = scalar @rounds;
65 my $num_rankings = scalar @$rankings;
67 # If there have been no rounds, check out the number of participants; if not, check the
68 # number of qualified from the last round
70 if ($num_rounds == 0) {
71 my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_participants FROM tournamentparticipation WHERE tournament=?', undef, $id);
72 $num_qualified = $ref->{'num_participants'};
74 my $ref = $dbh->selectrow_hashref('SELECT numqualifying FROM rounds WHERE tournament=? AND round=?', undef, $id, $num_rounds);
75 $num_qualified = $ref->{'numqualifying'};
79 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
80 tournament => $tournament,
81 rankings => $rankings,
82 num_rankings => $num_rankings,
84 num_rounds => $num_rounds,
85 num_qualified => $num_qualified