]> git.sesse.net Git - foosball/blob - www/index.pl
Switch to more accurate double score calculation.
[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         to_char(gametime, \'IYYY-MM-DD HH24:MI\') as gametime,
69         \'Double\' as type,
70         team1_username1 || \' / \' || team1_username2 as username1,
71         team2_username1 || \' / \' || team2_username2 as username2,
72         score1,
73         score2,
74         ra1.rating_diff as diff1, 
75         ra2.rating_diff as diff2, 
76         ra3.rating_diff as diff3, 
77         ra4.rating_diff as diff4 
78     from
79         double_results re
80         join double_rating ra1
81             on re.gametime=ra1.ratetime
82             and re.team1_username1=ra1.username
83         join double_rating ra2
84             on re.gametime=ra2.ratetime
85             and re.team1_username2=ra2.username
86         join double_rating ra3
87             on re.gametime=ra3.ratetime
88             and re.team2_username1=ra3.username
89         join double_rating ra4
90             on re.gametime=ra4.ratetime
91             and re.team2_username2=ra4.username
92     union all
93     select
94         to_char(gametime, \'IYYY-MM-DD HH24:MI\') as gametime,
95         \'Single\' as type,
96         username1,
97         username2,
98         score1,
99         score2,
100         ra1.rating_diff as diff1, 
101         null as diff2, 
102         ra2.rating_diff as diff3,
103         null as diff4 
104     from
105         single_results re
106         join single_rating ra1
107             on re.gametime=ra1.ratetime
108             and re.username1=ra1.username
109         join single_rating ra2
110             on re.gametime=ra2.ratetime
111             and re.username2=ra2.username
112 ) t1
113 order by gametime desc limit 10');
114 $q->execute();
115 while (my $ref = $q->fetchrow_hashref) {
116         if (defined($ref->{'diff2'})) {
117                 $ref->{'diff1'} = sprintf "%+d / %+d",
118                         int($ref->{'diff1'} + 0.5),
119                         int($ref->{'diff2'} + 0.5);
120                 $ref->{'diff2'} = sprintf "%+d / %+d",
121                         int($ref->{'diff3'} + 0.5),
122                         int($ref->{'diff4'} + 0.5);
123         } else {
124                 $ref->{'diff1'} = sprintf "%+d", int($ref->{'diff1'} + 0.5);
125                 $ref->{'diff2'} = sprintf "%+d", int($ref->{'diff3'} + 0.5);
126         }
127         push @last_games, $ref;
128 }
129
130 $dbh->disconnect;
131
132 print CGI->header(-type=>'application/xhtml+xml');
133
134 my $doc = XML::Template::process_file('index.xml', {
135         '#singletop' => \@single_top,
136         '#doubletop' => \@double_top,
137         '#lastgames' => \@last_games,
138 });
139 print $doc->toString;