]> git.sesse.net Git - foosball/blob - www/index.pl
8e0b8f59814e8de16aef435f2e0b1eacf532dc5d
[foosball] / www / index.pl
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 use lib qw(/srv/bzr.sesse.net/www/xml-template/perl/);
5 use XML::Template;
6 use CGI;
7 require '../foosball.pm',
8
9 my $dbh = foosball::db_connect();
10
11 # Single score board (whoa, inefficient)
12 my @single_top = ();
13 my $q = $dbh->prepare('select username from users');
14 $q->execute();
15 while (my $ref = $q->fetchrow_hashref) {
16         my $username = $ref->{'username'};
17         my ($rating, $rd) = foosball::find_single_rating($dbh, $username);
18         my ($oldrating) = foosball::find_single_rating($dbh, $username, 'AND ratetime::date < current_date ');
19
20         my $trend = "";
21         if (defined($oldrating)) {
22                 $trend = (sprintf "%+d", int($rating-$oldrating+0.5));
23         }
24
25         push @single_top, {
26                 'username' => $username,
27                 'rating' => int($rating+0.5),
28                 'rd' => int($rd+0.5),
29                 'lowerbound' => int($rating - 3.0*$rd + 0.5),
30                 'trend' => $trend,
31         };
32 }
33 @single_top = sort { $b->{'lowerbound'} <=> $a->{'lowerbound'} } @single_top;
34
35 # Double score board
36 my @double_top = ();
37 $q = $dbh->prepare('select username from users');
38 $q->execute();
39 while (my $ref = $q->fetchrow_hashref) {
40         my $username = $ref->{'username'};
41         my ($rating, $rd) = foosball::find_double_rating($dbh, $username);
42         my ($oldrating) = foosball::find_double_rating($dbh, $username, 'AND ratetime::date < current_date ');
43
44         my $trend = "";
45         if (defined($oldrating)) {
46                 $trend = (sprintf "%+d", int($rating-$oldrating+0.5));
47         }
48
49         push @double_top, {
50                 'username' => $username,
51                 'rating' => int($rating+0.5),
52                 'rd' => int($rd+0.5),
53                 'lowerbound' => int($rating - 3.0*$rd + 0.5),
54                 'trend' => $trend,
55         };
56 }
57 @double_top = sort { $b->{'lowerbound'} <=> $a->{'lowerbound'} } @double_top;
58
59 # Last games
60 my @last_games = ();
61 $q = $dbh->prepare('select * from ( select to_char(gametime, \'IYYY-MM-DD HH24:MI\') as gametime,\'Double\' as type,team1_username1 || \' / \' || team1_username2 as username1, team2_username1 || \' / \' || team2_username2 as username2,score1,score2 from double_results union all select to_char(gametime, \'IYYY-MM-DD HH24:MI\') as gametime,\'Single\' as type,username1,username2,score1,score2 from single_results ) t1 order by gametime desc limit 10');
62 $q->execute();
63 while (my $ref = $q->fetchrow_hashref) {
64         push @last_games, $ref;
65 }
66
67 $dbh->disconnect;
68
69 print CGI->header(-type=>'application/xhtml+xml');
70
71 my $doc = XML::Template::process_file('index.xml', {
72         '#singletop' => \@single_top,
73         '#doubletop' => \@double_top,
74         '#lastgames' => \@last_games,
75 });
76 print $doc->toString;