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);
14 my $songs = ccbs::db_fetch_all($dbh, 'SELECT song,title FROM machinesongs NATURAL JOIN songs WHERE machine=? ORDER BY LOWER(title)', $tournament->{'machine'});
16 # Check if the last round is valid for closing (by checking if all scores
18 my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_incomplete FROM scores WHERE tournament=? AND (song IS NULL OR playmode IS NULL OR difficulty IS NULL OR chosen IS NULL or score IS NULL)', undef, $tournament->{'tournament'});
20 if ($ref->{'num_incomplete'} == 0) {
26 # Swoop all the data in in a big join, then order it over to quasi-sane Perl objects.
27 # (round -> parallel -> player -> songs -> title,chosen,score)
28 my $scores = ccbs::db_fetch_all($dbh,
29 'SELECT round,parallel,position,playmode,difficulty,songnumber,player,nick,song,title,chosen,score FROM roundparticipation NATURAL JOIN players NATURAL JOIN scores NATURAL LEFT JOIN songs WHERE tournament=? ORDER BY round,parallel,position,songnumber',
34 my ($round, $parallel, $player) = (-1,-1,'');
35 for my $score (@$scores) {
36 if ($score->{'round'} != $round) {
37 $round = $score->{'round'};
38 push @rounds, { round => $round, parallels => [], locked => 0 };
41 my $p = $rounds[$#rounds]->{'parallels'};
42 if ($score->{'parallel'} != $parallel) {
43 $parallel = $score->{'parallel'};
44 push @$p, { parallel => $parallel, players => [], songs => [], num_songs => 0 };
47 # Information on songs is not selected from roundrandomsongs etc.,
48 # but is filled in the first time the song is seen for this round
51 if ($score->{'position'} == 1) {
52 if ($score->{'chosen'}) {
53 push @{$p->[$#$p]->{'songs'}}, '';
55 push @{$p->[$#$p]->{'songs'}}, $score->{'title'};
57 $p->[$#$p]->{'num_songs'}++;
60 my $pl = $p->[$#$p]->{'players'};
61 if ($score->{'nick'} ne $player) {
62 $player = $score->{'nick'};
63 push @$pl, { player => $score->{'player'}, nick => $player, songs => [], total => 0 };
66 push @{$pl->[$#$pl]->{'songs'}}, $score;
68 if (defined($score->{'score'})) {
69 $pl->[$#$pl]->{'total'} += $score->{'score'};
73 my $num_rounds = scalar @rounds;
74 my $num_rankings = scalar @$rankings;
76 # Lock all rounds but the last (active?) one
77 for my $r (0..$#rounds-1) {
78 $rounds[$r]->{'locked'} = 1;
81 # If there have been no rounds, check out the number of participants; if not, check the
82 # number of qualified from the last round
84 if ($num_rounds == 0) {
85 my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_participants FROM tournamentparticipation WHERE tournament=?', undef, $id);
86 $num_qualified = $ref->{'num_participants'};
88 my $ref = $dbh->selectrow_hashref('SELECT SUM(numqualifying) AS numqualifying FROM rounds NATURAL JOIN groups WHERE tournament=? AND round=?', undef, $id, $num_rounds);
89 $num_qualified = $ref->{'numqualifying'};
93 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
94 tournament => $tournament,
95 rankings => $rankings,
96 num_rankings => $num_rankings,
98 num_rounds => $num_rounds,
99 num_qualified => $num_qualified,
101 closing_valid => $closing_valid