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'}}, { song => -1, title => '' };
56 push @{$p->[$#$p]->{'songs'}}, $score;
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, rank => 1 };
67 push @{$pl->[$#$pl]->{'songs'}}, $score;
69 if (defined($score->{'score'})) {
70 $pl->[$#$pl]->{'total'} += $score->{'score'};
74 # Find the rank of all the players in every group, via simple insertion-like
77 for my $p (@{$r->{'parallels'}}) {
78 my $pls = $p->{'players'};
79 for my $i (0..$#$pls) {
81 for my $j (0..($i-1)) {
82 if ($pls->[$i]->{'total'} < $pls->[$j]->{'total'}) {
83 $pls->[$i]->{'rank'}++;
84 } elsif ($pls->[$i]->{'total'} > $pls->[$j]->{'total'}) {
85 $pls->[$j]->{'rank'}++;
92 # FIXME: In some odd cases, there _might_ be an empty group right at the end. Fix this when
93 # we are able to add/delete people in groups.
95 my $num_rounds = scalar @rounds;
96 my $num_rankings = scalar @$rankings;
98 # Lock all rounds but the last (active?) one
99 for my $r (0..$#rounds-1) {
100 $rounds[$r]->{'locked'} = 1;
103 # If there's only one group left and it's valid for closing, we can also finish
104 # the entire tournament if we'd like
105 if ($closing_valid && (scalar @rounds > 0 && scalar @{$rounds[$#rounds]->{'parallels'}}) == 1) {
106 $finishing_valid = 1;
109 # If there have been no rounds, check out the number of participants; if not, check the
110 # number of qualified from the last round
112 if ($num_rounds == 0) {
113 my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_participants FROM tournamentparticipation WHERE tournament=?', undef, $id);
114 $num_qualified = $ref->{'num_participants'};
116 my $ref = $dbh->selectrow_hashref('SELECT SUM(numqualifying) AS numqualifying FROM rounds NATURAL JOIN groups WHERE tournament=? AND round=?', undef, $id, $num_rounds);
117 $num_qualified = $ref->{'numqualifying'};
120 # And last: If there is a ranking list, the tournament is closed and we really can't
122 if ($num_rankings > 0) {
124 $finishing_valid = 0;
125 $rounds[$#rounds]->{'locked'} = 1;
128 ccbs::print_header();
129 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
130 tournament => $tournament,
131 rankings => $rankings,
132 num_rankings => $num_rankings,
134 num_rounds => $num_rounds,
135 num_qualified => $num_qualified,
137 closing_valid => $closing_valid,
138 finishing_valid => $finishing_valid