]> git.sesse.net Git - foosball/blob - www/index.pl
a9f2d43b7d022a7dd133f424b004dc3436afba1d
[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         next if ($rating == 0 || $rating == 1500);
19
20         my ($oldrating) = foosball::find_single_rating($dbh, $username, 'AND ratetime::date < current_date ');
21
22         my $trend = "";
23         if (defined($oldrating)) {
24                 $trend = (sprintf "%+d", int($rating-$oldrating+0.5));
25         }
26
27         push @single_top, {
28                 'username' => $username,
29                 'rating' => int($rating+0.5),
30                 'rd' => int($rd+0.5),
31                 'lowerbound' => int($rating - 3.0*$rd + 0.5),
32                 'trend' => $trend,
33         };
34 }
35 @single_top = sort { $b->{'lowerbound'} <=> $a->{'lowerbound'} } @single_top;
36
37 # Double score board
38 my @double_top = ();
39 $q = $dbh->prepare('select username from users');
40 $q->execute();
41 while (my $ref = $q->fetchrow_hashref) {
42         my $username = $ref->{'username'};
43         my ($rating, $rd) = foosball::find_double_rating($dbh, $username);
44         next if ($rating == 0 || $rating == 1500);
45
46         my ($oldrating) = foosball::find_double_rating($dbh, $username, 'AND ratetime::date < current_date ');
47
48         my $trend = "";
49         if (defined($oldrating)) {
50                 $trend = (sprintf "%+d", int($rating-$oldrating+0.5));
51         }
52
53         push @double_top, {
54                 'username' => $username,
55                 'rating' => int($rating+0.5),
56                 'rd' => int($rd+0.5),
57                 'lowerbound' => int($rating - 3.0*$rd + 0.5),
58                 'trend' => $trend,
59         };
60 }
61 @double_top = sort { $b->{'lowerbound'} <=> $a->{'lowerbound'} } @double_top;
62
63 # Last games
64 my @last_games = ();
65 $q = $dbh->prepare('
66 select * from (
67     select
68         gametime as sort_gametime,
69         to_char(gametime, \'IYYY-MM-DD HH24:MI\') as gametime,
70         \'Double\' as type,
71         team1_username1 || \' / \' || team1_username2 as username1,
72         team2_username1 || \' / \' || team2_username2 as username2,
73         score1,
74         score2,
75         ra1.rating_diff as diff1, 
76         ra2.rating_diff as diff2, 
77         ra3.rating_diff as diff3, 
78         ra4.rating_diff as diff4 
79     from
80         double_results re
81         join double_rating ra1
82             on re.gametime=ra1.ratetime
83             and re.team1_username1=ra1.username
84         join double_rating ra2
85             on re.gametime=ra2.ratetime
86             and re.team1_username2=ra2.username
87         join double_rating ra3
88             on re.gametime=ra3.ratetime
89             and re.team2_username1=ra3.username
90         join double_rating ra4
91             on re.gametime=ra4.ratetime
92             and re.team2_username2=ra4.username
93     union all
94     select
95         gametime as sort_gametime,
96         to_char(gametime, \'IYYY-MM-DD HH24:MI\') as gametime,
97         \'Single\' as type,
98         username1,
99         username2,
100         score1,
101         score2,
102         ra1.rating_diff as diff1, 
103         null as diff2, 
104         ra2.rating_diff as diff3,
105         null as diff4 
106     from
107         single_results re
108         join single_rating ra1
109             on re.gametime=ra1.ratetime
110             and re.username1=ra1.username
111         join single_rating ra2
112             on re.gametime=ra2.ratetime
113             and re.username2=ra2.username
114 ) t1
115 order by sort_gametime desc limit 10');
116 $q->execute();
117 while (my $ref = $q->fetchrow_hashref) {
118         if (defined($ref->{'diff2'})) {
119                 $ref->{'diff1'} = sprintf "%+d / %+d",
120                         int($ref->{'diff1'} + 0.5),
121                         int($ref->{'diff2'} + 0.5);
122                 $ref->{'diff2'} = sprintf "%+d / %+d",
123                         int($ref->{'diff3'} + 0.5),
124                         int($ref->{'diff4'} + 0.5);
125         } else {
126                 $ref->{'diff1'} = sprintf "%+d", int($ref->{'diff1'} + 0.5);
127                 $ref->{'diff2'} = sprintf "%+d", int($ref->{'diff3'} + 0.5);
128         }
129         push @last_games, $ref;
130 }
131
132 $dbh->disconnect;
133
134 print CGI->header(-type=>'application/xhtml+xml');
135
136 my $doc = XML::Template::process_file('index.xml', {
137         '#singletop' => XML::Template::alternate('tr/class', \@single_top, 'odd', 'even'),
138         '#doubletop' => XML::Template::alternate('tr/class', \@double_top, 'odd', 'even'),
139         '#lastgames' => XML::Template::alternate('tr/class', \@last_games, 'odd', 'even'),
140 });
141 print $doc->toString;