]> git.sesse.net Git - ccbs/blob - html/show-tournament.pl
Added a stored procedure for finding the min/max rank for an entire tournament.
[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
14 my $rankings;
15 if ($tournament->{'country'} == 1) {
16         $rankings = ccbs::db_fetch_all($dbh, 'SELECT ranking,player,nick || \' (\' || countrycode::varchar || \')\' AS nick,COALESCE(points,-1) AS points FROM tournamentrankings NATURAL JOIN players NATURAL JOIN countries WHERE tournament=? ORDER BY ranking', $id);
17 } else {
18         $rankings = ccbs::db_fetch_all($dbh, 'SELECT ranking,player,COALESCE(nick || \' (\' || clubcode::varchar || \')\', nick) AS nick,COALESCE(points,-1) AS points FROM tournamentrankings NATURAL JOIN players NATURAL LEFT JOIN clubs WHERE tournament=? ORDER BY ranking', $id);
19 }
20
21 my $songs = ccbs::db_fetch_all($dbh, 'SELECT song,title FROM machinesongs NATURAL JOIN songs WHERE machine=? ORDER BY LOWER(title)', $tournament->{'machine'});
22
23 # Check if the last round is valid for closing (by checking if all scores
24 # entered are valid)
25 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'});
26 my ($closing_valid,$finishing_valid);
27 $finishing_valid = 0;
28 if ($ref->{'num_incomplete'} == 0) {
29         $closing_valid = 1;
30 } else {
31         $closing_valid = 0;
32 }
33
34 # Check if this tournament is on the bigscreen or not.
35 my $ref = $dbh->selectrow_hashref('SELECT * FROM bigscreen.active_tournament');
36 my $bigscreen = ($ref->{'tournament'} == $id) ? 1 : 0;
37
38 my $active_groups;
39 if ($bigscreen) {
40         # Find all groups currently shown on the bigscreen.
41         $active_groups = ccbs::db_fetch_all($dbh, 'SELECT * FROM bigscreen.active_groups WHERE tournament=?', $id);
42 }
43
44 # Swoop all the data in in a big join, then order it over to quasi-sane Perl objects.
45 # (round -> parallel -> player -> songs -> title,chosen,score)
46 my $scores;
47 if ($tournament->{'country'} == 1) {
48         $scores = ccbs::db_fetch_all($dbh,
49         'SELECT round,parallel,position,playmode,difficulty,songnumber,player,nick || \' (\' || countrycode::varchar || \')\' AS nick,song,title,chosen,score FROM roundparticipation NATURAL JOIN players NATURAL JOIN countries NATURAL JOIN scores NATURAL LEFT JOIN songs WHERE tournament=? ORDER BY round,parallel,position,songnumber',
50         $id);
51 } else {
52         $scores = ccbs::db_fetch_all($dbh,
53         'SELECT round,parallel,position,playmode,difficulty,songnumber,player,COALESCE(nick || \' (\' || clubcode::varchar || \')\', nick) AS nick,song,title,chosen,score FROM roundparticipation NATURAL JOIN players NATURAL JOIN scores NATURAL LEFT JOIN songs NATURAL LEFT JOIN clubs WHERE tournament=? ORDER BY round,parallel,position,songnumber',
54         $id);
55 }
56
57 my @rounds = ();
58
59 my ($round, $parallel, $player) = (-1,-1,'');
60 for my $score (@$scores) {
61         if ($score->{'round'} != $round) {
62                 $round = $score->{'round'};
63                 push @rounds, { round => $round, parallels => [], locked => 0 };
64                 $parallel = -1;
65         }
66         my $p = $rounds[$#rounds]->{'parallels'};
67         if ($score->{'parallel'} != $parallel) {
68                 $parallel = $score->{'parallel'};
69                 push @$p, { parallel => $parallel, players => [], songs => [], num_songs => 0 };
70                 $player = '';
71
72                 if ($bigscreen) {
73                         # suboptimal, but heck :-)
74                         $p->[$#$p]->{'bigscreen'} = 0;
75                         for my $ag (@$active_groups) {
76                                 if ($ag->{'round'} == $round && $ag->{'parallel'} == $parallel) {
77                                         $p->[$#$p]->{'bigscreen'} = 1;
78                                         last;
79                                 }
80                         }
81                 }
82
83                 # Information on songs is not selected from roundrandomsongs etc.,
84                 # but is filled in the first time the song is seen for this round
85                 # (ie. below)
86         }
87         if ($score->{'position'} == 1) {
88                 if ($score->{'chosen'}) {
89                         push @{$p->[$#$p]->{'songs'}}, { song => -1, title => '' };
90                 } else {
91                         push @{$p->[$#$p]->{'songs'}}, $score;
92                 }
93                 $p->[$#$p]->{'num_songs'}++;
94         }
95         
96         my $pl = $p->[$#$p]->{'players'};
97         if ($score->{'nick'} ne $player) {
98                 $player = $score->{'nick'};
99                 push @$pl, { player => $score->{'player'}, nick => $player, songs => [], total => 0, rank => 1 };
100         }
101
102         push @{$pl->[$#$pl]->{'songs'}}, $score;
103
104         if (defined($score->{'score'})) {
105                 $pl->[$#$pl]->{'total'} += $score->{'score'};
106         }
107 }
108
109 # Find the rank of all the players in every group, via simple insertion-like
110 # sort
111 for my $r (@rounds) {
112         for my $p (@{$r->{'parallels'}}) {
113                 my $pls = $p->{'players'};
114                 for my $i (0..$#$pls) {
115                         my $rank = 1;
116                         for my $j (0..($i-1)) {
117                                 if ($pls->[$i]->{'total'} < $pls->[$j]->{'total'}) {
118                                         $pls->[$i]->{'rank'}++;
119                                 } elsif ($pls->[$i]->{'total'} > $pls->[$j]->{'total'}) {
120                                         $pls->[$j]->{'rank'}++;
121                                 }
122                         }
123                 }
124         }
125 }
126
127 # FIXME: In some odd cases, there _might_ be an empty group right at the end. Fix this when
128 #        we are able to add/delete people in groups.
129
130 my $num_rounds = scalar @rounds;
131 my $num_rankings = scalar @$rankings;
132
133 # Lock all rounds but the last (active?) one
134 for my $r (0..$#rounds-1) {
135         $rounds[$r]->{'locked'} = 1;
136 }
137
138 # If there's only one group left and it's valid for closing, we can also finish
139 # the entire tournament if we'd like
140 if ($closing_valid && (scalar @rounds > 0 && scalar @{$rounds[$#rounds]->{'parallels'}}) == 1) {
141         $finishing_valid = 1;
142 }
143
144 # If there have been no rounds, check out the number of participants; if not, check the
145 # number of qualified from the last round
146 my $num_qualified;
147 if ($num_rounds == 0) {
148         my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_participants FROM tournamentparticipation WHERE tournament=?', undef, $id);
149         $num_qualified = $ref->{'num_participants'};
150 } else {
151         my $ref = $dbh->selectrow_hashref('SELECT SUM(numqualifying) AS numqualifying FROM rounds NATURAL JOIN groups WHERE tournament=? AND round=?', undef, $id, $num_rounds);
152         $num_qualified = $ref->{'numqualifying'};
153 }
154
155 # And last: If there is a ranking list, the tournament is closed and we really can't
156 # do anything more
157 if ($num_rankings > 0) {
158         $closing_valid = 0;
159         $finishing_valid = 0;
160         if ($#rounds > -1) {
161                 $rounds[$#rounds]->{'locked'} = 1;
162         }
163 }
164
165 ccbs::print_header();
166 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
167         tournament => $tournament,
168         rankings => $rankings,
169         num_rankings => $num_rankings,
170         rounds => \@rounds,
171         num_rounds => $num_rounds,
172         num_qualified => $num_qualified,
173         songs => $songs,
174         closing_valid => $closing_valid,
175         finishing_valid => $finishing_valid,
176         bigscreen => $bigscreen
177 });
178 $dbh->disconnect;