]> git.sesse.net Git - wloh/blob - www/index.pl
Make a rating page.
[wloh] / www / index.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 POSIX;
9 use Devel::Peek;
10 use HTML::Entities;
11 use Encode;
12 use locale;
13 require '../config.pm';
14
15 my $cgi = CGI->new;
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 my $trials = 25_000;
23
24 binmode STDOUT, ':utf8';
25
26 my %players = ();
27 my %ratings = ();
28 my @matches = ();
29 my %parms = ();
30 my $match_stddev;
31
32 sub sanitize {
33         return HTML::Entities::encode_entities(shift);
34 }
35
36 sub color {
37         my $x = shift;
38         return int(255.0 * ($x ** (1.80)));
39 }
40
41 sub make_table {
42         my ($lowest_division, $used_ratings) = @_;
43
44         print <<"EOF";
45
46     <table>
47       <tr>
48         <th></th>
49 EOF
50
51         POSIX::setlocale(&POSIX::LC_ALL, 'C');
52
53         my $tmpnam = POSIX::tmpnam();
54         open MCCALC, ">", $tmpnam
55                 or die "$tmpnam: $!";
56
57         printf MCCALC "%f\n", $match_stddev;
58         printf MCCALC "%d\n", scalar keys %players;
59
60         for my $id (keys %players) {
61                 if (exists($used_ratings->{$id})) {
62                         printf MCCALC "%s %f\n", $id, $used_ratings->{$id};
63                 } else {
64                         printf MCCALC "%s %f\n", $id, 1500.0;
65                 }
66         }
67
68         for my $match (@matches) {
69                 printf MCCALC "%s %s %d %d\n", $match->[0], $match->[1], $match->[2], $match->[3];
70         }
71         close MCCALC;
72
73         POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
74
75         my %prob = ();
76
77         open MCCALC, "$config::base_dir/mcwordfeud $trials < $tmpnam |"
78                 or die "mccalc: $!";
79         while (<MCCALC>) {
80                 chomp;
81                 my @x = split /\s+/;
82                 my $id = $x[0];
83                 my $player = $players{$id};
84                 $prob{$player} = [ @x[1..$#x] ];
85         }
86         close MCCALC;
87         #unlink $tmpnam;
88
89         my $num_games = scalar keys %prob;
90         for my $i (1..$num_games) {
91                 print "        <th>$i.</th>\n";
92         }
93         print "        <th>NEDRYKK</th>\n" unless ($lowest_division);
94         print "      </tr>\n";
95
96         for my $player (sort { $a cmp $b } keys %prob) {
97                 print "      <tr>\n";
98                 print "        <th>$player</th>\n";
99
100                 for my $i (1..$num_games) {
101                         my $pn = $prob{$player}->[$i - 1] / $trials;
102
103                         my $r = color(1.0 - $pn / 3);
104                         my $g = color(1.0 - $pn / 3);
105                         my $b = color(1.0);
106
107                         if ($i == 1) {
108                                 ($g, $b) = ($b, $g);
109                         } elsif ($i >= $num_games - 1 && !$lowest_division) {
110                                 ($r, $b) = ($b, $r);
111                         }
112
113                         printf "        <td style=\"background-color: rgb($r, $g, $b)\">%.1f%%</td>\n", $pn * 100.0;
114                 }
115
116                 unless ($lowest_division) {
117                         my $pn = ($prob{$player}->[$num_games - 1] + $prob{$player}->[$num_games - 2]) / $trials;
118
119                         my $r = color(1.0);
120                         my $g = color(1.0 - $pn / 3);
121                         my $b = color(1.0 - $pn / 3);
122                         printf "        <td style=\"background-color: rgb($r, $g, $b)\">%.1f%%</td>\n", $pn * 100.0;
123                 }
124                 print "      </tr>\n";
125         }
126
127         print << "EOF";
128     </table>
129 EOF
130 }
131
132 # Get auxillary parameters
133 my $q = $dbh->prepare('SELECT * FROM ratings WHERE id < 0');
134 $q->execute;
135
136 while (my $ref = $q->fetchrow_hashref) {
137         $parms{$ref->{'id'}} = $ref->{'rating'};
138 }
139 $match_stddev = $parms{-2} * sqrt(2.0);
140
141 my $season;
142 my $division = $cgi->param('divisjon') // -1;
143 my $subdivision = $cgi->param('avdeling') // -1;
144 my $last_division = 0;
145
146 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
147
148 print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
149 printf <<"EOF", $match_stddev;
150 <html>
151   <head>
152     <title>WLoH-plasseringsannsynlighetsberegning</title>
153     <style type="text/css">
154 body {
155         color: black;
156         background: white;
157         font-family: sans-serif;
158 }
159 table {
160         border-collapse: collapse;
161         border: 1px solid black;
162 }
163 td, th {
164         border: 1px solid black;
165         padding: 5px;
166 }
167 td {
168         text-align: right;
169 }
170     </style>
171   </head>
172   <body>
173     <h1>WLoH-plasseringsannsynlighetsberegning</h1>
174
175     <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
176       den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
177       %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
178       Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.</p>
179
180     <p>Spillerne er sortert etter nick.</p>
181
182     <form method="get" action="/">
183 EOF
184
185 $q = $dbh->prepare('SELECT MAX(sesong) AS max_sesong FROM fotballserier');
186 $q->execute;
187 my $season;
188 while (my $ref = $q->fetchrow_hashref) {
189         $season = $ref->{'max_sesong'};
190 }
191
192 print <<"EOF";
193      <p>Divisjon:
194         <select name="divisjon" onchange="form.submit();">
195 EOF
196
197 $q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier WHERE sesong=? ORDER BY divisjon');
198 $q->execute($season);
199
200 my $found_division = 0;
201 my $max_division;
202
203 while (my $ref = $q->fetchrow_hashref) {
204         my $d = $ref->{'divisjon'};
205         if ($d == $division) {
206                 print "        <option value=\"$d\" selected=\"selected\">$d</option>\n";
207                 $found_division = 1;
208         } else {
209                 print "        <option value=\"$d\">$d</option>\n";
210         }
211         $max_division = $d;
212 }
213
214 $division = 1 if (!$found_division);
215
216 print <<"EOF";
217         </select>
218         Avdeling:
219         <select name="avdeling" onchange="form.submit();">
220 EOF
221
222 $q = $dbh->prepare('SELECT DISTINCT(avdeling) FROM fotballserier WHERE sesong=? AND divisjon=? ORDER BY avdeling');
223 $q->execute($season, $division);
224
225 my $found_subdivision = 0;
226
227 while (my $ref = $q->fetchrow_hashref) {
228         my $sd = $ref->{'avdeling'};
229         if ($sd == $subdivision) {
230                 print "        <option value=\"$sd\" selected=\"selected\">$sd</option>\n";
231                 $found_subdivision = 1;
232         } else {
233                 print "        <option value=\"$sd\">$sd</option>\n";
234         }
235 }
236
237 $subdivision = 1 if (!$found_subdivision);
238
239 print <<"EOF";
240         </select>
241         <input type="submit" value="Vis" />
242       </p>
243     </form>
244 EOF
245
246 # Get players and ratings
247 $q = $dbh->prepare('SELECT fotballdeltagere.id,fotballdeltagere.navn,rating FROM fotballdeltagere JOIN fotballserier ON fotballdeltagere.serie=fotballserier.nr JOIN ratings ON fotballdeltagere.id=ratings.id AND sesong=? AND divisjon=? AND avdeling=?');
248 $q->execute($season, $division, $subdivision);
249
250 while (my $ref = $q->fetchrow_hashref) {
251         my $id = $ref->{'id'};
252         $players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
253         $ratings{$id} = $ref->{'rating'};
254 }
255 $q->finish;
256
257 $q = $dbh->prepare('
258 SELECT
259   d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
260 FROM fotballresultater r
261   JOIN fotballserier s ON r.serie=s.nr
262   JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
263   JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
264 WHERE
265   sesong=? AND divisjon=? AND avdeling=?
266   AND lagrecno > motstander
267 ');
268 $q->execute($season, $division, $subdivision);
269
270 while (my $ref = $q->fetchrow_hashref) {
271         push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
272 }
273 $q->finish;
274
275 my $lowest_division = ($division == $max_division);
276 make_table($lowest_division, {});
277
278 print <<"EOF";
279     <p>Under er en variant som tar relativ spillestyrke med i beregningen;
280       se <a href="/rating">ratingsiden</a>.</p>
281 EOF
282
283 make_table($lowest_division, \%ratings);
284
285 print << "EOF";
286     </table>
287   </body>
288 </html>
289 EOF