]> git.sesse.net Git - ccbs/blob - html/player.pl
0fbde00f445ba54436b7951367fe931c746bc722
[ccbs] / html / player.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 $player = $dbh->selectrow_hashref('SELECT * FROM players NATURAL JOIN countries NATURAL LEFT JOIN clubs WHERE player=?', undef, $id);
13 my $bestsongs = ccbs::db_fetch_all($dbh, 'SELECT song,title,score,tournament,tournamentname FROM ( SELECT DISTINCT ON (song) song,score,tournament FROM scores WHERE player=? AND score IS NOT NULL ORDER BY song,score DESC ) t1 NATURAL JOIN songs NATURAL JOIN tournaments ORDER BY score DESC LIMIT 10', $id);
14
15 # Fetch all scores and count a bit
16 my $columns = 0;
17 my @allsongs = ();
18 my $last_title = undef;
19
20 my $scores = ccbs::db_fetch_all($dbh, 'SELECT title,score FROM scores NATURAL JOIN songs WHERE player=? ORDER BY title,score DESC', $id);
21 for my $score (@$scores) {
22         my $title = $score->{'title'};
23         if (!defined($last_title) || $title ne $last_title) {
24                 push @allsongs, { title => $title, scores => [] };
25         }
26
27         push @{$allsongs[$#allsongs]->{'scores'}}, $score->{'score'};
28
29         my $this_columns = scalar @{$allsongs[$#allsongs]->{'scores'}};
30         $columns = $this_columns if ($this_columns > $columns);
31
32         $last_title = $title;
33 }
34
35 ccbs::print_header();
36 ccbs::process_template('player.tmpl', $player->{'title'}, {
37         player => $player,
38         bestsongs => $bestsongs,
39         allsongs => \@allsongs,
40         columns => $columns
41 });
42 $dbh->disconnect;