Disconnect the database handle cleanly on exit.
[ccbs] / html / show-tournament.pl
1 #! /usr/bin/perl
2
3 use ccbs;
4 use strict;
5 use warnings;
6
7 my $cgi = new CGI;
8 my $id = $cgi->param('id');
9
10 my $dbh = ccbs::db_connect();
11
12 my $tournament = $dbh->selectrow_hashref('SELECT * FROM tournaments WHERE tournament=?', undef, $id);
13
14 # Swoop all the data in in a big join, then order it over to quasi-sane Perl objects.
15 # (round -> parallel -> player -> songs -> title,chosen,score)
16 my $scores = ccbs::db_fetch_all($dbh,
17         'SELECT round,parallel,nick,title,chosen,score FROM scores NATURAL JOIN players NATURAL JOIN songs NATURAL JOIN roundparticipation WHERE tournament=? ORDER BY round,parallel,position,songnumber',
18         $id);
19
20 my @rounds = ();
21
22 my ($round, $parallel, $player) = (-1,-1,'');
23 for my $score (@$scores) {
24         if ($score->{'round'} != $round) {
25                 $round = $score->{'round'};
26                 push @rounds, { round => $round, parallels => [] };
27                 $parallel = -1;
28         }
29         my $p = $rounds[$#rounds]->{'parallels'};
30         if ($score->{'parallel'} != $parallel) {
31                 $parallel = $score->{'parallel'};
32                 push @$p, { parallel => $parallel, players => [] };
33                 $player = '';
34         }
35         my $pl = $p->[$#$p]->{'players'};
36         if ($score->{'nick'} ne $player) {
37                 $player = $score->{'nick'};
38                 push @$pl, { nick => $player, songs => [] };
39         }
40         
41         push @{$pl->[$#$pl]->{'songs'}}, {
42                 title => $score->{'title'},
43                 chosen => $score->{'chosen'},
44                 score => $score->{'score'}
45         };
46 }
47
48 ccbs::print_header();
49 ccbs::process_template('show-tournament.tmpl', 'Turnering', { tournament => $tournament, rounds => \@rounds });
50 $dbh->disconnect;