]> git.sesse.net Git - wloh/blobdiff - www/index.pl
Train one model (with its own aux parms) per locale.
[wloh] / www / index.pl
index e80188efc0818f3297d5047141f30055525b65ed..7546d658ac8a4b5bf14bf8a30b8e46d408c4092b 100755 (executable)
@@ -7,8 +7,14 @@ use CGI::Carp qw( fatalsToBrowser );
 use DBI;
 use POSIX;
 use Devel::Peek;
+use HTML::Entities;
+use Encode;
+use utf8;
 use locale;
 require '../config.pm';
+require '../common.pm';
+
+my $cgi = CGI->new;
 
 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
        or die "connect: " . $DBI::errstr;
@@ -21,14 +27,11 @@ binmode STDOUT, ':utf8';
 
 my %players = ();
 my %ratings = ();
+my %ratings_stddev = ();
 my @matches = ();
-my %parms = ();
-my $match_stddev;
 
 sub sanitize {
-       $_ = shift;
-       y/ \t\n<>&/_/;
-       return $_;
+       return HTML::Entities::encode_entities(shift);
 }
 
 sub color {
@@ -36,16 +39,143 @@ sub color {
        return int(255.0 * ($x ** (1.80)));
 }
 
-sub make_table {
-       my $used_ratings = shift;
+sub get_divisions {
+       my ($dbh, $locale, $season) = @_;
+
+       my @divisions = ();
+
+       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');
+       $q->execute($locale, $season);
+
+       while (my $ref = $q->fetchrow_hashref) {
+               push @divisions, $ref->{'divisjon'};
+       }
+
+       return @divisions;
+}
+
+sub get_subdivisions {
+       my ($dbh, $locale, $season, $division) = @_;
+
+       my @subdivisions = ();
+
+       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');
+       $q->execute($locale, $season, $division);
+
+       while (my $ref = $q->fetchrow_hashref) {
+               push @subdivisions, $ref->{'avdeling'};
+       }
+
+       return @subdivisions;
+}
+
+sub print_division_selector {
+       my ($dbh, $locale, $divisions, $subdivisions, $division, $subdivision) = @_;
 
        print <<"EOF";
+    <form method="get" action="/$locale/">
+EOF
 
-    <table>
-      <tr>
-        <th></th>
+       my $max_division = $divisions->[(scalar @$divisions) - 1];
+
+       print <<"EOF";
+     <p>Divisjon:
+        <select name="divisjon" onchange="form.submit();">
 EOF
 
+       for my $d (@$divisions) {
+               if ($d == $division) {
+                       print "        <option value=\"$d\" selected=\"selected\">$d</option>\n";
+               } else {
+                       print "        <option value=\"$d\">$d</option>\n";
+               }
+       }
+
+       print <<"EOF";
+        </select>
+        Avdeling:
+        <select name="avdeling" onchange="form.submit();">
+EOF
+
+       for my $sd (@$subdivisions) {
+               if ($sd == $subdivision) {
+                       print "        <option value=\"$sd\" selected=\"selected\">$sd</option>\n";
+               } else {
+                       print "        <option value=\"$sd\">$sd</option>\n";
+               }
+       }
+
+       print <<"EOF";
+        </select>
+        <input type="submit" value="Vis" />
+      </p>
+    </form>
+EOF
+}
+
+sub get_players_and_ratings {
+       my ($dbh, $locale, $season, $division, $subdivision) = @_;
+
+       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=?');
+       $q->execute($locale, $season, $division, $subdivision);
+
+       while (my $ref = $q->fetchrow_hashref) {
+               my $id = $ref->{'id'};
+               $players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
+               $ratings{$id} = $ref->{'rating'};
+               $ratings_stddev{$id} = $ref->{'rating_stddev'};
+       }
+       $q->finish;
+}
+
+sub get_matches {
+       my ($dbh, $locale, $season, $division, $subdivision) = @_;
+
+       my @matches = ();
+       my $q = $dbh->prepare('
+       SELECT
+         d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
+       FROM fotballresultater r
+         JOIN fotballserier s ON r.serie=s.nr
+          JOIN fotballspraak sp ON s.spraak=sp.id
+         JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
+         JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
+       WHERE
+         kultur=? AND sesong=? AND divisjon=? AND avdeling=?
+         AND lagrecno > motstander
+       ');
+       $q->execute($locale, $season, $division, $subdivision);
+
+       while (my $ref = $q->fetchrow_hashref) {
+               push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
+       }
+       $q->finish;
+
+       return @matches;
+}
+
+sub get_covariance_matrix {
+       my ($dbh, @players) = @_;
+
+       my $player_sql = '{' . join(',', @players ) . '}';
+       my $q = $dbh->prepare('SELECT * FROM covariance WHERE player1=ANY(?::smallint[]) AND player2=ANY(?::smallint[])', { pg_prepare_now => 0 });
+       $q->execute($player_sql, $player_sql);
+
+       my $cov = {};
+       while (my $ref = $q->fetchrow_hashref) {
+               $cov->{$ref->{'player1'}}{$ref->{'player2'}} = $ref->{'cov'};
+       }
+
+       return $cov;
+}
+
+sub write_parms_to_file {
+       my ($aux_parms, $match_stddev, $used_ratings, $used_cov) = @_;
+
+       POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
+
+       my @sorted_players = sort { $players{$a} cmp $players{$b} } keys %players;
+
        POSIX::setlocale(&POSIX::LC_ALL, 'C');
 
        my $tmpnam = POSIX::tmpnam();
@@ -55,12 +185,21 @@ EOF
        printf MCCALC "%f\n", $match_stddev;
        printf MCCALC "%d\n", scalar keys %players;
 
-       for my $id (keys %players) {
-               if (exists($used_ratings->{$id})) {
-                       printf MCCALC "%s %f\n", $id, $used_ratings->{$id};
-               } else {
-                       printf MCCALC "%s %f\n", $id, 1500.0;
+       for my $id (@sorted_players) {
+               my $rating = $used_ratings->{$id} // 500.0;
+               printf MCCALC "%s %f\n", $id, $rating;
+       }
+
+       # covariance matrix
+       for my $id1 (keys %players) {
+               for my $id2 (keys %players) {
+                       if ($id1 == $id2) {
+                               printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // $aux_parms->{'rating_prior_stddev'});
+                       } else {
+                               printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // 0.0);
+                       }
                }
+               printf MCCALC "\n";
        }
 
        for my $match (@matches) {
@@ -70,6 +209,34 @@ EOF
 
        POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
 
+       return $tmpnam;
+}
+
+my $num_tables = 0;
+
+sub make_table {
+       my ($locale, $aux_parms, $match_stddev, $lowest_division, $used_ratings, $used_cov, $division, $subdivision) = @_;
+       ++$num_tables;
+
+       print <<"EOF";
+    <script type="text/javascript">
+    <!--
+function showScenario(element_id, url) {
+    var obj = document.getElementById(element_id);
+    var parent = obj.parentElement;
+    parent.removeChild(obj);
+    obj = obj.cloneNode(false);
+    obj.data = url;
+    parent.appendChild(obj);
+}
+    //-->
+    </script>
+    <table class="probmatrix">
+      <tr>
+        <th></th>
+EOF
+
+       my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, $used_ratings, $used_cov);
        my %prob = ();
 
        open MCCALC, "$config::base_dir/mcwordfeud $trials < $tmpnam |"
@@ -78,20 +245,22 @@ EOF
                chomp;
                my @x = split /\s+/;
                my $id = $x[0];
-               my $player = $players{$id};
+               my $player = sprintf "%s (%.0f ± %.0f)", $players{$id}, ($ratings{$id} // 500.0), ($ratings_stddev{$id} // $aux_parms->{'rating_prior_stddev'});
                $prob{$player} = [ @x[1..$#x] ];
        }
        close MCCALC;
-       #unlink $tmpnam;
+       unlink $tmpnam;
 
        my $num_games = scalar keys %prob;
        for my $i (1..$num_games) {
                print "        <th>$i.</th>\n";
        }
-       print "        <th>NEDRYKK</th>\n";
+       print "        <th>NEDRYKK</th>\n" unless ($lowest_division);
        print "      </tr>\n";
 
+       my $pnum = 0;
        for my $player (sort { $a cmp $b } keys %prob) {
+               ++$pnum;
                print "      <tr>\n";
                print "        <th>$player</th>\n";
 
@@ -104,122 +273,159 @@ EOF
 
                        if ($i == 1) {
                                ($g, $b) = ($b, $g);
-                       } elsif ($i >= $num_games - 1) {
+                       } elsif ($i >= $num_games - 1 && !$lowest_division) {
                                ($r, $b) = ($b, $r);
                        }
 
-                       printf "        <td style=\"background-color: rgb($r, $g, $b)\">%.1f%%</td>\n", $pn * 100.0;
+                       my $num_total_games = ($num_games * ($num_games - 1)) / 2;
+                       if (scalar @matches == $num_total_games || $prob{$player}->[$i - 1] == $trials) {
+                               printf "        <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
+                       } else {
+                               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;
+                       }
                }
 
-               {
+               unless ($lowest_division) {
                        my $pn = ($prob{$player}->[$num_games - 1] + $prob{$player}->[$num_games - 2]) / $trials;
 
                        my $r = color(1.0);
                        my $g = color(1.0 - $pn / 3);
                        my $b = color(1.0 - $pn / 3);
-                       printf "        <td style=\"background-color: rgb($r, $g, $b)\">%.1f%%</td>\n", $pn * 100.0;
+                       printf "        <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
                }
                print "      </tr>\n";
        }
 
        print << "EOF";
     </table>
+    
+    <p class="scenario"><object id="scenario$num_tables" data="" type="text/html"></object></p>
 EOF
 }
 
-# Get auxillary parameters
-my $q = $dbh->prepare('SELECT * FROM ratings WHERE id < 0');
-$q->execute;
+sub find_avg_rating {
+       my ($ratings) = shift;
 
-while (my $ref = $q->fetchrow_hashref) {
-       $parms{$ref->{'id'}} = $ref->{'rating'};
+       my $sum_rating = 0.0;
+       for my $r (values %$ratings) {
+               $sum_rating += ($r // 500.0);
+       }
+       return $sum_rating / scalar keys %$ratings;
 }
-$match_stddev = $parms{-2} * sqrt(2.0);
-
-# Get players and ratings
-my $season = 18;
-my $division = 1;
-my $subdivision = 1;
-
-$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=?');
-$q->execute($season, $division, $subdivision);
 
-while (my $ref = $q->fetchrow_hashref) {
-       my $id = $ref->{'id'};
-       $players{$id} = sanitize($ref->{'navn'});
-       $ratings{$id} = $ref->{'rating'};
+sub print_header {
+       my ($cgi, $title) = @_;
+       print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
+       print <<"EOF";
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE
+  html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
+  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no">
+  <head>
+    <title>$title</title>
+    <link rel="stylesheet" href="style" type="text/css" />
+  </head>
+  <body>
+EOF
 }
-$q->finish;
-
-$q = $dbh->prepare('
-SELECT
-  d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
-FROM fotballresultater r
-  JOIN fotballserier s ON r.serie=s.nr
-  JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
-  JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
-WHERE
-  sesong=? AND divisjon=? AND avdeling=?
-  AND lagrecno > motstander
-');
-$q->execute($season, $division, $subdivision);
-
-while (my $ref = $q->fetchrow_hashref) {
-       push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
+
+sub print_footer {
+       print <<"EOF";
+  </body>
+</html>
+EOF
 }
-$q->finish;
 
-POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
+my $locale = wloh_common::get_locale($cgi);
+my $aux_parms = wloh_common::get_auxillary_parameters($dbh, $locale);
+my $match_stddev = $aux_parms->{'score_stddev'} * sqrt(2.0);
 
-print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'Thu, 01 Dec 1994 16:00:00 GMT');
-printf <<"EOF", $match_stddev;
-<html>
-  <head>
-    <title>WLoH-plasseringsannsynlighetsberegning</title>
-    <style type="text/css">
-body {
-       color: black;
-       background: white;
-       font-family: sans-serif;
-}
-table {
-       border-collapse: collapse;
-       border: 1px solid black;
-}
-td, th {
-       border: 1px solid black;
-       padding: 5px;
-}
-td {
-       text-align: right;
-}
-    </style>
-  </head>
-  <body>
+my $division = $cgi->param('divisjon') // -1;
+my $subdivision = $cgi->param('avdeling') // -1;
+my $match_player = $cgi->param('spiller');
+my $match_position = $cgi->param('posisjon');
+
+my $season = wloh_common::get_max_season($dbh, $locale);
+die "Nonexistent locale!" if (!defined($season));
+
+my @divisions = get_divisions($dbh, $locale, $season);
+$division = $divisions[0] if (!grep { $_ == $division } @divisions);
+my @subdivisions = get_subdivisions($dbh, $locale, $season, $division);
+$subdivision = $subdivisions[0] if (!grep { $_ == $subdivision } @subdivisions);
+
+get_players_and_ratings($dbh, $locale, $season, $division, $subdivision);
+@matches = get_matches($dbh, $locale, $season, $division, $subdivision);
+my $cov = get_covariance_matrix($dbh, keys %players);
+
+print_header($cgi, 'WLoH-plasseringsannsynlighetsberegning');
+
+if (defined($match_player) && defined($match_position)) {
+       my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, \%ratings, $cov);
+
+       --$match_player;
+       --$match_position;
+
+       my @scenario = ();
+       open MCCALC, "$config::base_dir/mcwordfeud $trials $match_player $match_position < $tmpnam |"
+               or die "mccalc: $!";
+       while (<MCCALC>) {
+               /(\d+) (\d+) (-?\d+)/ or next;
+               chomp;
+               push @scenario, [ $1, $2, $3 ];
+       }
+       close MCCALC;
+       unlink $tmpnam;
+
+       my @sorted_players = sort { $players{$a} cmp $players{$b} } keys %players;
+       my $player_name = $players{$sorted_players[$match_player]};
+
+       if (scalar @scenario == 0) {
+               printf "    <p>Fant ingen m&aring;te <strong>%s</strong> kan ende p&aring; <strong>%d.</strong> plass p&aring;.</p>\n",
+                       $player_name, ($match_position + 1);
+       } else {
+               printf "    <p>Scenario der <strong>%s</strong> ender p&aring; <strong>%d.</strong> plass:</p>\n",
+                       $player_name, ($match_position + 1);
+               print "    <ul>\n";
+               for my $m (@scenario) {
+                       printf "    <li>%s &ndash; %s: %+d</li>\n", $players{$m->[0]}, $players{$m->[1]}, $m->[2];
+               }
+               print "    </ul>\n";
+       }
+} else {
+       POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
+       printf <<"EOF", $match_stddev;
     <h1>WLoH-plasseringsannsynlighetsberegning</h1>
 
+    <p><em>Dette er et hobbyprosjekt fra tredjepart, og ikke en offisiell del av
+      <a href="http://wordfeud.aasmul.net/">Wordfeud Leage of Honour</a>.</em></p>
+
     <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
       den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
       %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
-      Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.</p>
-
-    <p>Andre divisjoner enn 1. divisjon kommer etter hvert.</p>
+      Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.
+      For scenarioeksempel, klikk i en rute.</p>
 
     <p>Spillerne er sortert etter nick.</p>
 EOF
 
-make_table({});
+       print_division_selector($dbh, $locale, \@divisions, \@subdivisions, $division, $subdivision);
+
+       my $max_division = $divisions[$#divisions];
+       my $lowest_division = ($division == $max_division);
+       make_table($locale, $aux_parms, $match_stddev, $lowest_division, {}, {}, $division, $subdivision);
 
-print <<"EOF";
-    <p>Under er en variant som pr&oslash;ver &aring; ta relativ spillestyrke med i betraktningen.
-      Disse er basert p&aring; WLoH-data og oppdateres hver hele time (takk til Lobotommy for tilgang!),
-      men modellen er forel&oslash;pig ikke fullstendig tunet.</p>
+       print <<"EOF";
+    <p style="clear: both; padding-top: 1em;">Under er en variant som tar relativ spillestyrke med i beregningen;
+      se <a href="rating">ratingsiden</a>.</p>
 EOF
 
-make_table(\%ratings);
+       make_table($locale, $aux_parms, $match_stddev, $lowest_division, \%ratings, $cov, $division, $subdivision);
 
-print << "EOF";
-    </table>
-  </body>
-</html>
-EOF
+       my $avg_rating = find_avg_rating(\%ratings);
+       printf "    <p style=\"clear: both; padding-top: 1em;\">Gjennomsnittlig rating i denne avdelingen er <strong>%.1f</strong>.</p>\n", $avg_rating;
+
+       wloh_common::output_last_sync($dbh);
+}
+
+print_footer();