]> git.sesse.net Git - ccbs/blob - html/show-tournament.pl
Add a (dummy) do-edit-scores.pl backend. (Nevermind, it crept into the previous commi...
[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, locked => 1 };
54         }
55         
56         push @{$pl->[$#$pl]->{'songs'}}, $score;
57
58         if (defined($score->{'score'})) {
59                 $pl->[$#$pl]->{'total'} += $score->{'score'};
60         } else {
61                 $pl->[$#$pl]->{'locked'} = 0;
62         }
63 }
64
65 my $num_rounds = scalar @rounds;
66 my $num_rankings = scalar @$rankings;
67
68 # If there have been no rounds, check out the number of participants; if not, check the
69 # number of qualified from the last round
70 my $num_qualified;
71 if ($num_rounds == 0) {
72         my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_participants FROM tournamentparticipation WHERE tournament=?', undef, $id);
73         $num_qualified = $ref->{'num_participants'};
74 } else {
75         my $ref = $dbh->selectrow_hashref('SELECT SUM(numqualifying) AS numqualifying FROM rounds NATURAL JOIN groups WHERE tournament=? AND round=?', undef, $id, $num_rounds);
76         $num_qualified = $ref->{'numqualifying'};
77 }
78
79 ccbs::print_header();
80 ccbs::process_template('show-tournament.tmpl', $tournament->{'tournamentname'}, {
81         tournament => $tournament,
82         rankings => $rankings,
83         num_rankings => $num_rankings,
84         rounds => \@rounds,
85         num_rounds => $num_rounds,
86         num_qualified => $num_qualified,
87         songs => $songs
88 });
89 $dbh->disconnect;