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 # Check if this tournament is on the bigscreen or not.
28 my $ref = $dbh->selectrow_hashref('SELECT * FROM bigscreen.active_tournament');
29 my $bigscreen = ($ref->{'tournament'} == $id) ? 1 : 0;
33 # Find all groups currently shown on the bigscreen.
34 $active_groups = ccbs::db_fetch_all($dbh, 'SELECT * FROM bigscreen.active_groups WHERE tournament=?', $id);
37 # Swoop all the data in in a big join, then order it over to quasi-sane Perl objects.
38 # (round -> parallel -> player -> songs -> title,chosen,score)
39 my $scores = ccbs::db_fetch_all($dbh,
40 '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',
45 my ($round, $parallel, $player) = (-1,-1,'');
46 for my $score (@$scores) {
47 if ($score->{'round'} != $round) {
48 $round = $score->{'round'};
49 push @rounds, { round => $round, parallels => [], locked => 0 };
52 my $p = $rounds[$#rounds]->{'parallels'};
53 if ($score->{'parallel'} != $parallel) {
54 $parallel = $score->{'parallel'};
55 push @$p, { parallel => $parallel, players => [], songs => [], num_songs => 0 };
59 # suboptimal, but heck :-)
60 $p->[$#$p]->{'bigscreen'} = 0;
61 for my $ag (@$active_groups) {
62 if ($ag->{'round'} == $round && $ag->{'parallel'} == $parallel) {
63 $p->[$#$p]->{'bigscreen'} = 1;
69 # Information on songs is not selected from roundrandomsongs etc.,
70 # but is filled in the first time the song is seen for this round
73 if ($score->{'position'} == 1) {
74 if ($score->{'chosen'}) {
75 push @{$p->[$#$p]->{'songs'}}, { song => -1, title => '' };
77 push @{$p->[$#$p]->{'songs'}}, $score;
79 $p->[$#$p]->{'num_songs'}++;
82 my $pl = $p->[$#$p]->{'players'};
83 if ($score->{'nick'} ne $player) {
84 $player = $score->{'nick'};
85 push @$pl, { player => $score->{'player'}, nick => $player, songs => [], total => 0, rank => 1 };
88 push @{$pl->[$#$pl]->{'songs'}}, $score;
90 if (defined($score->{'score'})) {
91 $pl->[$#$pl]->{'total'} += $score->{'score'};
95 # Find the rank of all the players in every group, via simple insertion-like
98 for my $p (@{$r->{'parallels'}}) {
99 my $pls = $p->{'players'};
100 for my $i (0..$#$pls) {
102 for my $j (0..($i-1)) {
103 if ($pls->[$i]->{'total'} < $pls->[$j]->{'total'}) {
104 $pls->[$i]->{'rank'}++;
105 } elsif ($pls->[$i]->{'total'} > $pls->[$j]->{'total'}) {
106 $pls->[$j]->{'rank'}++;
113 # FIXME: In some odd cases, there _might_ be an empty group right at the end. Fix this when
114 # we are able to add/delete people in groups.
116 my $num_rounds = scalar @rounds;
117 my $num_rankings = scalar @$rankings;
119 # Lock all rounds but the last (active?) one
120 for my $r (0..$#rounds-1) {
121 $rounds[$r]->{'locked'} = 1;
124 # If there's only one group left and it's valid for closing, we can also finish
125 # the entire tournament if we'd like
126 if ($closing_valid && (scalar @rounds > 0 && scalar @{$rounds[$#rounds]->{'parallels'}}) == 1) {
127 $finishing_valid = 1;
130 # If there have been no rounds, check out the number of participants; if not, check the
131 # number of qualified from the last round
133 if ($num_rounds == 0) {
134 my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_participants FROM tournamentparticipation WHERE tournament=?', undef, $id);
135 $num_qualified = $ref->{'num_participants'};
137 my $ref = $dbh->selectrow_hashref('SELECT SUM(numqualifying) AS numqualifying FROM rounds NATURAL JOIN groups WHERE tournament=? AND round=?', undef, $id, $num_rounds);
138 $num_qualified = $ref->{'numqualifying'};
141 # And last: If there is a ranking list, the tournament is closed and we really can't
143 if ($num_rankings > 0) {
145 $finishing_valid = 0;
146 $rounds[$#rounds]->{'locked'} = 1;
149 ccbs::print_header();
150 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
151 tournament => $tournament,
152 rankings => $rankings,
153 num_rankings => $num_rankings,
155 num_rounds => $num_rounds,
156 num_qualified => $num_qualified,
158 closing_valid => $closing_valid,
159 finishing_valid => $finishing_valid,
160 bigscreen => $bigscreen