]> git.sesse.net Git - wloh/blob - www/index.pl
Convert rating.pl to XML::Template. It is dog-slow, but much cleaner.
[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 utf8;
13 use locale;
14 use lib qw(../include);
15 require 'config.pm';
16 require 'common.pm';
17
18 my $cgi = CGI->new;
19
20 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
21         or die "connect: " . $DBI::errstr;
22 $dbh->{AutoCommit} = 0;
23 $dbh->{RaiseError} = 1;
24
25 my $trials = 25_000;
26
27 binmode STDOUT, ':utf8';
28
29 my %players = ();
30 my %ratings = ();
31 my %ratings_stddev = ();
32 my @matches = ();
33
34 sub sanitize {
35         return HTML::Entities::encode_entities(shift);
36 }
37
38 sub color {
39         my $x = shift;
40         return int(255.0 * ($x ** (1.80)));
41 }
42
43 sub get_divisions {
44         my ($dbh, $locale, $season) = @_;
45
46         my @divisions = ();
47
48         my $q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier se JOIN fotballspraak sp ON se.spraak=sp.id WHERE kultur=? AND sesong=? ORDER BY divisjon');
49         $q->execute($locale, $season);
50
51         while (my $ref = $q->fetchrow_hashref) {
52                 push @divisions, $ref->{'divisjon'};
53         }
54
55         return @divisions;
56 }
57
58 sub get_subdivisions {
59         my ($dbh, $locale, $season, $division) = @_;
60
61         my @subdivisions = ();
62
63         my $q = $dbh->prepare('SELECT DISTINCT(avdeling) FROM fotballserier se JOIN fotballspraak sp ON se.spraak=sp.id WHERE kultur=? AND sesong=? AND divisjon=? ORDER BY avdeling');
64         $q->execute($locale, $season, $division);
65
66         while (my $ref = $q->fetchrow_hashref) {
67                 push @subdivisions, $ref->{'avdeling'};
68         }
69
70         return @subdivisions;
71 }
72
73 sub print_division_selector {
74         my ($dbh, $locale, $divisions, $subdivisions, $division, $subdivision) = @_;
75
76         print <<"EOF";
77     <form method="get" action="/$locale/">
78 EOF
79
80         my $max_division = $divisions->[(scalar @$divisions) - 1];
81
82         print <<"EOF";
83      <p>Divisjon:
84         <select name="divisjon" onchange="form.submit();">
85 EOF
86
87         for my $d (@$divisions) {
88                 if ($d == $division) {
89                         print "        <option value=\"$d\" selected=\"selected\">$d</option>\n";
90                 } else {
91                         print "        <option value=\"$d\">$d</option>\n";
92                 }
93         }
94
95         print <<"EOF";
96         </select>
97         Avdeling:
98         <select name="avdeling" onchange="form.submit();">
99 EOF
100
101         for my $sd (@$subdivisions) {
102                 if ($sd == $subdivision) {
103                         print "        <option value=\"$sd\" selected=\"selected\">$sd</option>\n";
104                 } else {
105                         print "        <option value=\"$sd\">$sd</option>\n";
106                 }
107         }
108
109         print <<"EOF";
110         </select>
111         <input type="submit" value="Vis" />
112       </p>
113     </form>
114 EOF
115 }
116
117 sub get_players_and_ratings {
118         my ($dbh, $locale, $season, $division, $subdivision) = @_;
119
120         my $q = $dbh->prepare('SELECT fotballdeltagere.id,fotballdeltagere.navn,rating,rating_stddev FROM fotballdeltagere JOIN fotballserier ON fotballdeltagere.serie=fotballserier.nr NATURAL JOIN spiller_kultur LEFT JOIN ratings ON fotballdeltagere.id=ratings.id WHERE kultur=? AND sesong=? AND divisjon=? AND avdeling=?');
121         $q->execute($locale, $season, $division, $subdivision);
122
123         while (my $ref = $q->fetchrow_hashref) {
124                 my $id = $ref->{'id'};
125                 $players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
126                 $ratings{$id} = $ref->{'rating'};
127                 $ratings_stddev{$id} = $ref->{'rating_stddev'};
128         }
129         $q->finish;
130 }
131
132 sub get_matches {
133         my ($dbh, $locale, $season, $division, $subdivision) = @_;
134
135         my @matches = ();
136         my $q = $dbh->prepare('
137         SELECT
138           d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
139         FROM ( SELECT * FROM fotballresultater UNION ALL SELECT * FROM fotballresultater_2123 ) r
140           JOIN fotballserier s ON r.serie=s.nr
141           JOIN fotballspraak sp ON s.spraak=sp.id
142           JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
143           JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
144         WHERE
145           kultur=? AND sesong=? AND divisjon=? AND avdeling=?
146           AND lagrecno > motstander
147         ');
148         $q->execute($locale, $season, $division, $subdivision);
149
150         while (my $ref = $q->fetchrow_hashref) {
151                 push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
152         }
153         $q->finish;
154
155         return @matches;
156 }
157
158 sub get_covariance_matrix {
159         my ($dbh, @players) = @_;
160
161         my $player_sql = '{' . join(',', @players ) . '}';
162         my $q = $dbh->prepare('SELECT * FROM covariance WHERE player1=ANY(?::smallint[]) AND player2=ANY(?::smallint[])', { pg_prepare_now => 0 });
163         $q->execute($player_sql, $player_sql);
164
165         my $cov = {};
166         while (my $ref = $q->fetchrow_hashref) {
167                 $cov->{$ref->{'player1'}}{$ref->{'player2'}} = $ref->{'cov'};
168         }
169
170         return $cov;
171 }
172
173 sub write_parms_to_file {
174         my ($aux_parms, $match_stddev, $used_ratings, $used_cov) = @_;
175
176         POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
177
178         my @sorted_players = sort { $players{$a} cmp $players{$b} } keys %players;
179
180         POSIX::setlocale(&POSIX::LC_ALL, 'C');
181
182         my $tmpnam = POSIX::tmpnam();
183         open MCCALC, ">", $tmpnam
184                 or die "$tmpnam: $!";
185
186         printf MCCALC "%f\n", $match_stddev;
187         printf MCCALC "%d\n", scalar keys %players;
188
189         for my $id (@sorted_players) {
190                 my $rating = $used_ratings->{$id} // 500.0;
191                 printf MCCALC "%s %f\n", $id, $rating;
192         }
193
194         # covariance matrix
195         for my $id1 (keys %players) {
196                 for my $id2 (keys %players) {
197                         if ($id1 == $id2) {
198                                 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // $aux_parms->{'rating_prior_stddev'});
199                         } else {
200                                 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // 0.0);
201                         }
202                 }
203                 printf MCCALC "\n";
204         }
205
206         for my $match (@matches) {
207                 printf MCCALC "%s %s %d %d\n", $match->[0], $match->[1], $match->[2], $match->[3];
208         }
209         close MCCALC;
210
211         POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
212
213         return $tmpnam;
214 }
215
216 my $num_tables = 0;
217
218 sub make_table {
219         my ($locale, $aux_parms, $match_stddev, $lowest_division, $used_ratings, $used_cov, $division, $subdivision) = @_;
220         ++$num_tables;
221
222         print <<"EOF";
223     <script type="text/javascript">
224     <!--
225 function showScenario(element_id, url) {
226     var obj = document.getElementById(element_id);
227     var parent = obj.parentElement;
228     parent.removeChild(obj);
229     obj = obj.cloneNode(false);
230     obj.data = url;
231     parent.appendChild(obj);
232 }
233     //-->
234     </script>
235     <table class="probmatrix">
236       <tr>
237         <th></th>
238 EOF
239
240         my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, $used_ratings, $used_cov);
241         my %prob = ();
242
243         open MCCALC, "$config::base_dir/mcwordfeud $trials < $tmpnam |"
244                 or die "mccalc: $!";
245         while (<MCCALC>) {
246                 chomp;
247                 my @x = split /\s+/;
248                 my $id = $x[0];
249                 my $player = sprintf "%s (%.0f ± %.0f)", $players{$id}, ($ratings{$id} // 500.0), ($ratings_stddev{$id} // $aux_parms->{'rating_prior_stddev'});
250                 $prob{$player} = [ @x[1..$#x] ];
251         }
252         close MCCALC;
253         unlink $tmpnam;
254
255         my $num_games = scalar keys %prob;
256         for my $i (1..$num_games) {
257                 print "        <th>$i.</th>\n";
258         }
259         print "        <th>NEDRYKK</th>\n" unless ($lowest_division);
260         print "      </tr>\n";
261
262         my $pnum = 0;
263         for my $player (sort { $a cmp $b } keys %prob) {
264                 ++$pnum;
265                 print "      <tr>\n";
266                 print "        <th>$player</th>\n";
267
268                 for my $i (1..$num_games) {
269                         my $pn = $prob{$player}->[$i - 1] / $trials;
270
271                         my $r = color(1.0 - $pn / 3);
272                         my $g = color(1.0 - $pn / 3);
273                         my $b = color(1.0);
274
275                         if ($i == 1) {
276                                 ($g, $b) = ($b, $g);
277                         } elsif ($i >= $num_games - 1 && !$lowest_division) {
278                                 ($r, $b) = ($b, $r);
279                         }
280
281                         my $num_total_games = ($num_games * ($num_games - 1)) / 2;
282                         if (scalar @matches == $num_total_games || $prob{$player}->[$i - 1] == $trials) {
283                                 printf "        <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
284                         } else {
285                                 printf "        <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\"><a class=\"unmarkedlink\" href=\"javascript:showScenario('scenario$num_tables', '/$locale/?divisjon=$division;avdeling=$subdivision;spiller=$pnum;posisjon=$i');\">%.1f%%</a></td>\n", $pn * 100.0;
286                         }
287                 }
288
289                 unless ($lowest_division) {
290                         my $pn = ($prob{$player}->[$num_games - 1] + $prob{$player}->[$num_games - 2]) / $trials;
291
292                         my $r = color(1.0);
293                         my $g = color(1.0 - $pn / 3);
294                         my $b = color(1.0 - $pn / 3);
295                         printf "        <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
296                 }
297                 print "      </tr>\n";
298         }
299
300         print << "EOF";
301     </table>
302     
303     <p class="scenario"><object id="scenario$num_tables" data="" type="text/html"></object></p>
304 EOF
305 }
306
307 sub make_cov_table {
308         my ($cov) = @_;
309         my @players = (sort { $players{$a} cmp $players{$b} } keys %players);
310
311         print <<"EOF";
312     <table class="probmatrix">
313       <tr>
314         <th></th>
315 EOF
316
317         for my $player (@players) {
318                 printf "        <th>%s</th>\n", $players{$player};
319         }
320         print "      </tr>\n";
321
322         my $pnum = 0;
323         for my $player (@players) {
324                 ++$pnum;
325                 print "      <tr>\n";
326                 printf "        <th>%s</th>\n", $players{$player};
327
328                 for my $player2 (@players) {
329                         printf "        <td class=\"num\">%.3f</td>\n", $cov->{$player}{$player2};
330                 }
331                 print "      </tr>\n";
332         }
333
334         print "    </table>\n";
335 }
336
337 sub find_avg_rating {
338         my ($ratings) = shift;
339
340         my $sum_rating = 0.0;
341         for my $r (values %$ratings) {
342                 $sum_rating += ($r // 500.0);
343         }
344         return $sum_rating / scalar keys %$ratings;
345 }
346
347 sub print_header {
348         my ($cgi, $title) = @_;
349         print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
350         print <<"EOF";
351 <?xml version="1.0" encoding="UTF-8" ?>
352 <!DOCTYPE
353   html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
354   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
355 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no">
356   <head>
357     <title>$title</title>
358     <link rel="stylesheet" href="style" type="text/css" />
359   </head>
360   <body>
361 EOF
362 }
363
364 sub print_footer {
365         print <<"EOF";
366   </body>
367 </html>
368 EOF
369 }
370
371 my $locale = wloh_common::get_locale($cgi);
372 my $aux_parms = wloh_common::get_auxillary_parameters($dbh, $locale);
373 my $match_stddev = $aux_parms->{'score_stddev'} * sqrt(2.0);
374
375 my $division = $cgi->param('divisjon') // -1;
376 my $subdivision = $cgi->param('avdeling') // -1;
377 my $match_player = $cgi->param('spiller');
378 my $match_position = $cgi->param('posisjon');
379
380 my $season = wloh_common::get_max_season($dbh, $locale);
381 die "Nonexistent locale!" if (!defined($season));
382
383 my @divisions = get_divisions($dbh, $locale, $season);
384 $division = $divisions[0] if (!grep { $_ == $division } @divisions);
385 my @subdivisions = get_subdivisions($dbh, $locale, $season, $division);
386 $subdivision = $subdivisions[0] if (!grep { $_ == $subdivision } @subdivisions);
387
388 get_players_and_ratings($dbh, $locale, $season, $division, $subdivision);
389 @matches = get_matches($dbh, $locale, $season, $division, $subdivision);
390 my $cov = get_covariance_matrix($dbh, keys %players);
391
392 print_header($cgi, 'WLoH-plasseringsannsynlighetsberegning');
393
394 if (defined($match_player) && defined($match_position)) {
395         my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, \%ratings, $cov);
396
397         --$match_player;
398         --$match_position;
399
400         my @scenario = ();
401         open MCCALC, "$config::base_dir/mcwordfeud $trials $match_player $match_position < $tmpnam |"
402                 or die "mccalc: $!";
403         while (<MCCALC>) {
404                 /(\d+) (\d+) (-?\d+)/ or next;
405                 chomp;
406                 push @scenario, [ $1, $2, $3 ];
407         }
408         close MCCALC;
409         unlink $tmpnam;
410
411         my @sorted_players = sort { $players{$a} cmp $players{$b} } keys %players;
412         my $player_name = $players{$sorted_players[$match_player]};
413
414         if (scalar @scenario == 0) {
415                 printf "    <p>Fant ingen m&aring;te <strong>%s</strong> kan ende p&aring; <strong>%d.</strong> plass p&aring;.</p>\n",
416                         $player_name, ($match_position + 1);
417         } else {
418                 printf "    <p>Scenario der <strong>%s</strong> ender p&aring; <strong>%d.</strong> plass:</p>\n",
419                         $player_name, ($match_position + 1);
420                 print "    <ul>\n";
421                 for my $m (@scenario) {
422                         printf "    <li>%s &ndash; %s: %+d</li>\n", $players{$m->[0]}, $players{$m->[1]}, $m->[2];
423                 }
424                 print "    </ul>\n";
425         }
426 } else {
427         POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
428         wloh_common::print_navbar($cgi, $dbh, $locale);
429         printf <<"EOF", $match_stddev;
430     <h1>WLoH-plasseringsannsynlighetsberegning</h1>
431
432     <p><em>Dette er et hobbyprosjekt fra tredjepart, og ikke en offisiell del av
433       <a href="http://wordfeud.aasmul.net/">Wordfeud Leage of Honour</a>.</em></p>
434
435     <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
436       den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
437       %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
438       Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.
439       For scenarioeksempel, klikk i en rute.</p>
440
441     <p>Spillerne er sortert etter nick.</p>
442 EOF
443
444         print_division_selector($dbh, $locale, \@divisions, \@subdivisions, $division, $subdivision);
445
446         my $max_division = $divisions[$#divisions];
447         my $lowest_division = ($division == $max_division);
448         make_table($locale, $aux_parms, $match_stddev, $lowest_division, {}, {}, $division, $subdivision);
449
450         print <<"EOF";
451     <p style="clear: both; padding-top: 1em;">Under er en variant som tar relativ spillestyrke med i beregningen;
452       se <a href="rating">ratingsiden</a>.</p>
453 EOF
454
455         make_table($locale, $aux_parms, $match_stddev, $lowest_division, \%ratings, $cov, $division, $subdivision);
456
457         my $avg_rating = find_avg_rating(\%ratings);
458         printf "    <p style=\"clear: both; padding-top: 1em;\">Gjennomsnittlig rating i denne avdelingen er <strong>%.1f</strong>.</p>\n", $avg_rating;
459
460         if (defined($cgi->param('showcov'))) {
461                 make_cov_table($cov);
462         }
463
464         wloh_common::output_last_sync($dbh);
465 }
466
467 print_footer();