Misc. changes -- some formatting and add a preliminary "start new round" form.
[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 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
15 # Swoop all the data in in a big join, then order it over to quasi-sane Perl objects.
16 # (round -> parallel -> player -> songs -> title,chosen,score)
17 my $scores = ccbs::db_fetch_all($dbh,
18         'SELECT round,parallel,position,nick,title,chosen,score FROM scores NATURAL JOIN players NATURAL JOIN songs NATURAL JOIN roundparticipation WHERE tournament=? ORDER BY round,parallel,position,songnumber',
19         $id);
20
21 my @rounds = ();
22
23 my ($round, $parallel, $player) = (-1,-1,'');
24 for my $score (@$scores) {
25         if ($score->{'round'} != $round) {
26                 $round = $score->{'round'};
27                 push @rounds, { round => $round, parallels => [] };
28                 $parallel = -1;
29         }
30         my $p = $rounds[$#rounds]->{'parallels'};
31         if ($score->{'parallel'} != $parallel) {
32                 $parallel = $score->{'parallel'};
33                 push @$p, { parallel => $parallel, players => [], songs => [], num_songs => 0 };
34                 $player = '';
35
36                 # Information on songs is not selected from roundrandomsongs etc.,
37                 # but is filled in the first time the song is seen for this round
38                 # (ie. below)
39         }
40         if ($score->{'position'} == 1) {
41                 if ($score->{'chosen'}) {
42                         push @{$p->[$#$p]->{'songs'}}, '';
43                 } else {
44                         push @{$p->[$#$p]->{'songs'}}, $score->{'title'};
45                 }
46                 $p->[$#$p]->{'num_songs'}++;
47         }
48         
49         my $pl = $p->[$#$p]->{'players'};
50         if ($score->{'nick'} ne $player) {
51                 $player = $score->{'nick'};
52                 push @$pl, { nick => $player, songs => [], total => 0 };
53
54         }
55         
56         push @{$pl->[$#$pl]->{'songs'}}, {
57                 title => $score->{'title'},
58                 chosen => $score->{'chosen'},
59                 score => $score->{'score'}
60         };
61         $pl->[$#$pl]->{'total'} += $score->{'score'};
62 }
63
64 my $num_rounds = scalar @rounds;
65
66 ccbs::print_header();
67 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
68         tournament => $tournament,
69         rankings => $rankings,
70         rounds => \@rounds,
71         num_rounds => $num_rounds
72 });
73 $dbh->disconnect;