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