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