]> git.sesse.net Git - wloh/blob - www/rating.pl
Do not show cheaters.
[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 AND NOT jukser
81 ORDER BY rating DESC');
82 $q->execute($locale);
83
84 my @players = ();
85
86 $i = 0;
87 while (my $ref = $q->fetchrow_hashref) {
88         my %player = ();
89
90         $player{'#rank'} = wloh_common::get_ordinal(++$i, $locale);
91         $player{'#user'} = Encode::decode_utf8($ref->{'navn'});
92         $player{'#user/href'} = sprintf "http://wordfeud.aasmul.net/bruker-%d", $ref->{'id'};
93         $player{'#rating'} = sprintf "%.1f", $ref->{'rating'};
94         $player{'#stddev'} = sprintf "%.1f", $ref->{'rating_stddev'};
95         $player{'#divlink'} = $ref->{'serie_navn'};
96         $player{'#divlink/href'} = sprintf "http://wordfeud.aasmul.net/serie-%d", $ref->{'serie_id'};
97
98         push @players, \%player;
99 }
100
101 print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+75m');
102 wloh_common::process_template('rating', $locale, {
103         '#navbar' => wloh_common::get_navbar($cgi, $dbh, $locale),
104         'iterations' => $aux_parms->{'num_iterations'},
105         'rating-prior-stddev' => sprintf("%.1f", $aux_parms->{'rating_prior_stddev'}),
106         'match-stddev' => sprintf("%.1f", $aux_parms->{'score_stddev'} * sqrt(2.0)),
107         '#divisions' => XML::Template::alternate('tr/class', \@divisions, 'even', 'odd'),
108         '#players' => XML::Template::alternate('tr/class', \@players, 'even', 'odd'),
109         'last-sync' => wloh_common::get_last_sync($dbh)
110 });
111
112 $dbh->rollback;