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