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