]> git.sesse.net Git - ccbs/blob - html/show-tournament.pl
Added preliminary backend for creating rounds and groups.
[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 my $num_rankings = scalar @$rankings;
66
67 # If there have been no rounds, check out the number of participants; if not, check the
68 # number of qualified from the last round
69 my $num_qualified;
70 if ($num_rounds == 0) {
71         my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_participants FROM tournamentparticipation WHERE tournament=?', undef, $id);
72         $num_qualified = $ref->{'num_participants'};
73 } else {
74         my $ref = $dbh->selectrow_hashref('SELECT numqualifying FROM roundparticipation WHERE tournament=? AND round=?', undef, $id, $num_rounds);
75         $num_qualified = $ref->{'numqualifying'};
76 }
77
78 ccbs::print_header();
79 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
80         tournament => $tournament,
81         rankings => $rankings,
82         num_rankings => $num_rankings,
83         rounds => \@rounds,
84         num_rounds => $num_rounds,
85         num_qualified => $num_qualified
86 });
87 $dbh->disconnect;