8ff04c347c9e22a703691f79167e6b2495a28143
[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 roundparticipation NATURAL LEFT JOIN roundrandomsongs NATURAL LEFT JOIN scores NATURAL JOIN songs NATURAL JOIN players 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         push @{$pl->[$#$pl]->{'songs'}}, {
56                 title => $score->{'title'},
57                 chosen => $score->{'chosen'},
58                 score => $score->{'score'}
59         };
60         $pl->[$#$pl]->{'total'} += $score->{'score'};
61 }
62
63 my $num_rounds = scalar @rounds;
64 my $num_rankings = scalar @$rankings;
65
66 # If there have been no rounds, check out the number of participants; if not, check the
67 # number of qualified from the last round
68 my $num_qualified;
69 if ($num_rounds == 0) {
70         my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_participants FROM tournamentparticipation WHERE tournament=?', undef, $id);
71         $num_qualified = $ref->{'num_participants'};
72 } else {
73         my $ref = $dbh->selectrow_hashref('SELECT SUM(numqualifying) AS numqualifying FROM rounds NATURAL JOIN groups WHERE tournament=? AND round=?', undef, $id, $num_rounds);
74         $num_qualified = $ref->{'numqualifying'};
75 }
76
77 ccbs::print_header();
78 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
79         tournament => $tournament,
80         rankings => $rankings,
81         num_rankings => $num_rankings,
82         rounds => \@rounds,
83         num_rounds => $num_rounds,
84         num_qualified => $num_qualified
85 });
86 $dbh->disconnect;