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 # Swoop all the data in in a big join, then order it over to quasi-sane Perl objects.
17 # (round -> parallel -> player -> songs -> title,chosen,score)
18 my $scores = ccbs::db_fetch_all($dbh,
19 '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',
24 my ($round, $parallel, $player) = (-1,-1,'');
25 for my $score (@$scores) {
26 if ($score->{'round'} != $round) {
27 $round = $score->{'round'};
28 push @rounds, { round => $round, parallels => [] };
31 my $p = $rounds[$#rounds]->{'parallels'};
32 if ($score->{'parallel'} != $parallel) {
33 $parallel = $score->{'parallel'};
34 push @$p, { parallel => $parallel, players => [], songs => [], num_songs => 0 };
37 # Information on songs is not selected from roundrandomsongs etc.,
38 # but is filled in the first time the song is seen for this round
41 if ($score->{'position'} == 1) {
42 if ($score->{'chosen'}) {
43 push @{$p->[$#$p]->{'songs'}}, '';
45 push @{$p->[$#$p]->{'songs'}}, $score->{'title'};
47 $p->[$#$p]->{'num_songs'}++;
50 my $pl = $p->[$#$p]->{'players'};
51 if ($score->{'nick'} ne $player) {
52 $player = $score->{'nick'};
53 push @$pl, { player => $score->{'player'}, nick => $player, songs => [], total => 0 };
56 push @{$pl->[$#$pl]->{'songs'}}, $score;
58 if (defined($score->{'score'})) {
59 $pl->[$#$pl]->{'total'} += $score->{'score'};
63 my $num_rounds = scalar @rounds;
64 my $num_rankings = scalar @$rankings;
66 # Lock all rounds but the last (active?) one
67 for my $r (0..$#rounds-1) {
68 $rounds[$r]->{'locked'} = 1;
70 $rounds[$#rounds]->{'locked'} = 0;
72 # If there have been no rounds, check out the number of participants; if not, check the
73 # number of qualified from the last round
75 if ($num_rounds == 0) {
76 my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_participants FROM tournamentparticipation WHERE tournament=?', undef, $id);
77 $num_qualified = $ref->{'num_participants'};
79 my $ref = $dbh->selectrow_hashref('SELECT SUM(numqualifying) AS numqualifying FROM rounds NATURAL JOIN groups WHERE tournament=? AND round=?', undef, $id, $num_rounds);
80 $num_qualified = $ref->{'numqualifying'};
84 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
85 tournament => $tournament,
86 rankings => $rankings,
87 num_rankings => $num_rankings,
89 num_rounds => $num_rounds,
90 num_qualified => $num_qualified,