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'});
19 my ($closing_valid,$finishing_valid);
21 if ($ref->{'num_incomplete'} == 0) {
27 # Swoop all the data in in a big join, then order it over to quasi-sane Perl objects.
28 # (round -> parallel -> player -> songs -> title,chosen,score)
29 my $scores = ccbs::db_fetch_all($dbh,
30 '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',
35 my ($round, $parallel, $player) = (-1,-1,'');
36 for my $score (@$scores) {
37 if ($score->{'round'} != $round) {
38 $round = $score->{'round'};
39 push @rounds, { round => $round, parallels => [], locked => 0 };
42 my $p = $rounds[$#rounds]->{'parallels'};
43 if ($score->{'parallel'} != $parallel) {
44 $parallel = $score->{'parallel'};
45 push @$p, { parallel => $parallel, players => [], songs => [], num_songs => 0 };
48 # Information on songs is not selected from roundrandomsongs etc.,
49 # but is filled in the first time the song is seen for this round
52 if ($score->{'position'} == 1) {
53 if ($score->{'chosen'}) {
54 push @{$p->[$#$p]->{'songs'}}, '';
56 push @{$p->[$#$p]->{'songs'}}, $score->{'title'};
58 $p->[$#$p]->{'num_songs'}++;
61 my $pl = $p->[$#$p]->{'players'};
62 if ($score->{'nick'} ne $player) {
63 $player = $score->{'nick'};
64 push @$pl, { player => $score->{'player'}, nick => $player, songs => [], total => 0 };
67 push @{$pl->[$#$pl]->{'songs'}}, $score;
69 if (defined($score->{'score'})) {
70 $pl->[$#$pl]->{'total'} += $score->{'score'};
74 # FIXME: In some odd cases, there _might_ be an empty group right at the end. Fix this when
75 # we are able to add/delete people in groups.
77 my $num_rounds = scalar @rounds;
78 my $num_rankings = scalar @$rankings;
80 # Lock all rounds but the last (active?) one
81 for my $r (0..$#rounds-1) {
82 $rounds[$r]->{'locked'} = 1;
85 # If there's only one group left and it's valid for closing, we can also finish
86 # the entire tournament if we'd like
87 if ($closing_valid && (scalar @rounds > 0 && scalar @{$rounds[$#rounds]->{'parallels'}}) == 1) {
91 # If there have been no rounds, check out the number of participants; if not, check the
92 # number of qualified from the last round
94 if ($num_rounds == 0) {
95 my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_participants FROM tournamentparticipation WHERE tournament=?', undef, $id);
96 $num_qualified = $ref->{'num_participants'};
98 my $ref = $dbh->selectrow_hashref('SELECT SUM(numqualifying) AS numqualifying FROM rounds NATURAL JOIN groups WHERE tournament=? AND round=?', undef, $id, $num_rounds);
99 $num_qualified = $ref->{'numqualifying'};
102 # And last: If there is a ranking list, the tournament is closed and we really can't
104 if ($num_rankings > 0) {
106 $finishing_valid = 0;
107 $rounds[$#rounds]->{'locked'} = 1;
110 ccbs::print_header();
111 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
112 tournament => $tournament,
113 rankings => $rankings,
114 num_rankings => $num_rankings,
116 num_rounds => $num_rounds,
117 num_qualified => $num_qualified,
119 closing_valid => $closing_valid,
120 finishing_valid => $finishing_valid