]> git.sesse.net Git - wloh/blob - www/rating.pl
48d985939c8a16918df02d095ba29d3f114526a1
[wloh] / www / rating.pl
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 no warnings qw(once);
5 use CGI;
6 use CGI::Carp qw( fatalsToBrowser );
7 use DBI;
8 use Encode;
9 use POSIX;
10 use HTML::Entities;
11 use utf8;
12 use locale;
13 use lib qw(../include);
14 require 'config.pm';
15 require 'common.pm';
16
17 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
18         or die "connect: " . $DBI::errstr;
19 $dbh->{AutoCommit} = 0;
20 $dbh->{RaiseError} = 1;
21
22 binmode STDOUT, ':utf8';
23
24 my $cgi = CGI->new;
25 my $locale = wloh_common::get_locale($cgi);
26
27 my $aux_parms = wloh_common::get_auxillary_parameters($dbh, $locale);
28
29 wloh_common::set_locale($locale);
30
31 my $season = wloh_common::get_max_season($dbh, $locale);
32
33 # Pick up all the subdivisions' ratings.
34 my %subdivision_ratings = ();
35 my $q = $dbh->prepare('SELECT divisjon, avdeling, serie_id, AVG(rating) AS avg_rating FROM ratings NATURAL JOIN siste_divisjon NATURAL JOIN spiller_kultur WHERE kultur=? AND sesong=? AND NOT jukser GROUP BY divisjon, avdeling, serie_id ORDER BY divisjon, avdeling');
36 $q->execute($locale, $season);
37
38 while (my $ref = $q->fetchrow_hashref) {
39         my $division = $ref->{'divisjon'};
40         my $rating = $ref->{'avg_rating'};
41         my $id = $ref->{'serie_id'};
42
43         push @{$subdivision_ratings{$division}}, [ $id, $rating ];
44 }
45
46 $q = $dbh->prepare('SELECT divisjon,AVG(rating) AS avg_rating,STDDEV(rating) AS stddev_rating FROM ratings NATURAL JOIN siste_divisjon NATURAL JOIN spiller_kultur WHERE kultur=? AND sesong=? AND NOT jukser GROUP BY divisjon ORDER BY divisjon');
47 $q->execute($locale, $season);
48
49 my @divisions = ();
50
51 my $i = 0;
52 while (my $ref = $q->fetchrow_hashref) {
53         my %division = ();
54
55         $division{'#rank'} = wloh_common::get_ordinal($ref->{'divisjon'}, $locale);
56         $division{'#average'} = sprintf "%.1f", $ref->{'avg_rating'};
57         $division{'#stddev'} = sprintf "%.1f", $ref->{'stddev_rating'};
58
59         my @subdivisions = ();
60         for my $arr (@{$subdivision_ratings{$ref->{'divisjon'}}}) {
61                 my ($id, $rating) = @$arr;
62                 push @subdivisions, {
63                         '#divlink' => sprintf("%.1f", $rating),
64                         '#divlink/href' => sprintf("http://wordfeud.aasmul.net/serie-%d", $id)
65                 };
66         }
67
68         $division{'subdivisions'} = \@subdivisions;
69
70         push @divisions, \%division;
71 }
72
73 $q = $dbh->prepare('
74 SELECT *
75 FROM ratings
76   NATURAL JOIN kanonisk_navn
77   NATURAL JOIN siste_divisjon
78   NATURAL JOIN spiller_kultur
79 WHERE kultur=?
80 ORDER BY rating DESC');
81 $q->execute($locale);
82
83 my @players = ();
84
85 $i = 0;
86 while (my $ref = $q->fetchrow_hashref) {
87         my %player = ();
88
89         $player{'#rank'} = wloh_common::get_ordinal(++$i, $locale);
90         $player{'#user'} = Encode::decode_utf8($ref->{'navn'});
91         $player{'#user/href'} = sprintf "http://wordfeud.aasmul.net/bruker-%d", $ref->{'id'};
92         $player{'#rating'} = sprintf "%.1f", $ref->{'rating'};
93         $player{'#stddev'} = sprintf "%.1f", $ref->{'rating_stddev'};
94         $player{'#divlink'} = $ref->{'serie_navn'};
95         $player{'#divlink/href'} = sprintf "http://wordfeud.aasmul.net/serie-%d", $ref->{'serie_id'};
96
97         push @players, \%player;
98 }
99
100 print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+75m');
101 wloh_common::process_template('rating', $locale, {
102         '#navbar' => wloh_common::get_navbar($cgi, $dbh, $locale),
103         'iterations' => $aux_parms->{'num_iterations'},
104         'rating-prior-stddev' => sprintf("%.1f", $aux_parms->{'rating_prior_stddev'}),
105         'match-stddev' => sprintf("%.1f", $aux_parms->{'score_stddev'} * sqrt(2.0)),
106         '#divisions' => XML::Template::alternate('tr/class', \@divisions, 'even', 'odd'),
107         '#players' => XML::Template::alternate('tr/class', \@players, 'even', 'odd'),
108         'last-sync' => wloh_common::get_last_sync($dbh)
109 });
110
111 $dbh->rollback;