]> git.sesse.net Git - ccbs/blob - html/show-tournament.pl
Don't show the "start next round" form until we can actually do so.
[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 my $songs = ccbs::db_fetch_all($dbh, 'SELECT song,title FROM machinesongs NATURAL JOIN songs WHERE machine=? ORDER BY LOWER(title)', $tournament->{'machine'});
15
16 # Check if the last round is valid for closing (by checking if all scores
17 # entered are valid)
18 my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_incomplete FROM scores WHERE tournament=? AND (song IS NULL OR playmode IS NULL OR difficulty IS NULL OR chosen IS NULL or score IS NULL)', undef, $tournament->{'tournament'});
19 my $closing_valid;
20 if ($ref->{'num_incomplete'} == 0) {
21         $closing_valid = 1;
22 } else {
23         $closing_valid = 0;
24 }
25
26 # Swoop all the data in in a big join, then order it over to quasi-sane Perl objects.
27 # (round -> parallel -> player -> songs -> title,chosen,score)
28 my $scores = ccbs::db_fetch_all($dbh,
29         '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',
30         $id);
31
32 my @rounds = ();
33
34 my ($round, $parallel, $player) = (-1,-1,'');
35 for my $score (@$scores) {
36         if ($score->{'round'} != $round) {
37                 $round = $score->{'round'};
38                 push @rounds, { round => $round, parallels => [], locked => 0 };
39                 $parallel = -1;
40         }
41         my $p = $rounds[$#rounds]->{'parallels'};
42         if ($score->{'parallel'} != $parallel) {
43                 $parallel = $score->{'parallel'};
44                 push @$p, { parallel => $parallel, players => [], songs => [], num_songs => 0 };
45                 $player = '';
46
47                 # Information on songs is not selected from roundrandomsongs etc.,
48                 # but is filled in the first time the song is seen for this round
49                 # (ie. below)
50         }
51         if ($score->{'position'} == 1) {
52                 if ($score->{'chosen'}) {
53                         push @{$p->[$#$p]->{'songs'}}, '';
54                 } else {
55                         push @{$p->[$#$p]->{'songs'}}, $score->{'title'};
56                 }
57                 $p->[$#$p]->{'num_songs'}++;
58         }
59         
60         my $pl = $p->[$#$p]->{'players'};
61         if ($score->{'nick'} ne $player) {
62                 $player = $score->{'nick'};
63                 push @$pl, { player => $score->{'player'}, nick => $player, songs => [], total => 0 };
64         }
65         
66         push @{$pl->[$#$pl]->{'songs'}}, $score;
67
68         if (defined($score->{'score'})) {
69                 $pl->[$#$pl]->{'total'} += $score->{'score'};
70         }
71 }
72
73 my $num_rounds = scalar @rounds;
74 my $num_rankings = scalar @$rankings;
75
76 # Lock all rounds but the last (active?) one
77 for my $r (0..$#rounds-1) {
78         $rounds[$r]->{'locked'} = 1;
79 }
80
81 # If there have been no rounds, check out the number of participants; if not, check the
82 # number of qualified from the last round
83 my $num_qualified;
84 if ($num_rounds == 0) {
85         my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_participants FROM tournamentparticipation WHERE tournament=?', undef, $id);
86         $num_qualified = $ref->{'num_participants'};
87 } else {
88         my $ref = $dbh->selectrow_hashref('SELECT SUM(numqualifying) AS numqualifying FROM rounds NATURAL JOIN groups WHERE tournament=? AND round=?', undef, $id, $num_rounds);
89         $num_qualified = $ref->{'numqualifying'};
90 }
91
92 ccbs::print_header();
93 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
94         tournament => $tournament,
95         rankings => $rankings,
96         num_rankings => $num_rankings,
97         rounds => \@rounds,
98         num_rounds => $num_rounds,
99         num_qualified => $num_qualified,
100         songs => $songs,
101         closing_valid => $closing_valid
102 });
103 $dbh->disconnect;