6 use CGI::Carp qw( fatalsToBrowser );
14 require '../config.pm';
18 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
19 or die "connect: " . $DBI::errstr;
20 $dbh->{AutoCommit} = 0;
21 $dbh->{RaiseError} = 1;
25 binmode STDOUT, ':utf8';
29 my %ratings_stddev = ();
35 return HTML::Entities::encode_entities(shift);
40 return int(255.0 * ($x ** (1.80)));
44 my ($lowest_division, $used_ratings, $used_cov) = @_;
53 POSIX::setlocale(&POSIX::LC_ALL, 'C');
55 my $tmpnam = POSIX::tmpnam();
56 open MCCALC, ">", $tmpnam
59 printf MCCALC "%f\n", $match_stddev;
60 printf MCCALC "%d\n", scalar keys %players;
62 for my $id (keys %players) {
63 my $rating = $used_ratings->{$id} // 1500.0;
64 printf MCCALC "%s %f\n", $id, $rating;
68 for my $id1 (keys %players) {
69 for my $id2 (keys %players) {
71 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // $parms{-3});
73 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // 0.0);
79 for my $match (@matches) {
80 printf MCCALC "%s %s %d %d\n", $match->[0], $match->[1], $match->[2], $match->[3];
84 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
88 open MCCALC, "$config::base_dir/mcwordfeud $trials < $tmpnam |"
94 my $player = sprintf "%s (%.0f ± %.0f)", $players{$id}, ($ratings{$id} // 1500.0), ($ratings_stddev{$id} // $parms{-3});
95 $prob{$player} = [ @x[1..$#x] ];
100 my $num_games = scalar keys %prob;
101 for my $i (1..$num_games) {
102 print " <th>$i.</th>\n";
104 print " <th>NEDRYKK</th>\n" unless ($lowest_division);
107 for my $player (sort { $a cmp $b } keys %prob) {
109 print " <th>$player</th>\n";
111 for my $i (1..$num_games) {
112 my $pn = $prob{$player}->[$i - 1] / $trials;
114 my $r = color(1.0 - $pn / 3);
115 my $g = color(1.0 - $pn / 3);
120 } elsif ($i >= $num_games - 1 && !$lowest_division) {
124 printf " <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
127 unless ($lowest_division) {
128 my $pn = ($prob{$player}->[$num_games - 1] + $prob{$player}->[$num_games - 2]) / $trials;
131 my $g = color(1.0 - $pn / 3);
132 my $b = color(1.0 - $pn / 3);
133 printf " <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
143 # Get auxillary parameters
144 my $q = $dbh->prepare('SELECT * FROM ratings WHERE id < 0');
147 while (my $ref = $q->fetchrow_hashref) {
148 $parms{$ref->{'id'}} = $ref->{'rating'};
150 $match_stddev = $parms{-2} * sqrt(2.0);
153 my $division = $cgi->param('divisjon') // -1;
154 my $subdivision = $cgi->param('avdeling') // -1;
155 my $last_division = 0;
157 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
159 print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
160 printf <<"EOF", $match_stddev;
161 <?xml version="1.0" encoding="UTF-8" ?>
163 html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
164 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
165 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no">
167 <title>WLoH-plasseringsannsynlighetsberegning</title>
168 <link rel="stylesheet" href="/style" type="text/css" />
171 <h1>WLoH-plasseringsannsynlighetsberegning</h1>
173 <p><em>Dette er et hobbyprosjekt fra tredjepart, og ikke en offisiell del av
174 <a href="http://wordfeud.aasmul.net/">Wordfeud Leage of Honour</a>.</em></p>
176 <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
177 den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
178 %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
179 Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.</p>
181 <p>Spillerne er sortert etter nick.</p>
183 <form method="get" action="/">
186 $q = $dbh->prepare('SELECT MAX(sesong) AS max_sesong FROM fotballserier');
189 while (my $ref = $q->fetchrow_hashref) {
190 $season = $ref->{'max_sesong'};
195 <select name="divisjon" onchange="form.submit();">
198 $q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier WHERE sesong=? ORDER BY divisjon');
199 $q->execute($season);
201 my $found_division = 0;
204 while (my $ref = $q->fetchrow_hashref) {
205 my $d = $ref->{'divisjon'};
206 if ($d == $division) {
207 print " <option value=\"$d\" selected=\"selected\">$d</option>\n";
210 print " <option value=\"$d\">$d</option>\n";
215 $division = 1 if (!$found_division);
220 <select name="avdeling" onchange="form.submit();">
223 $q = $dbh->prepare('SELECT DISTINCT(avdeling) FROM fotballserier WHERE sesong=? AND divisjon=? ORDER BY avdeling');
224 $q->execute($season, $division);
226 my $found_subdivision = 0;
228 while (my $ref = $q->fetchrow_hashref) {
229 my $sd = $ref->{'avdeling'};
230 if ($sd == $subdivision) {
231 print " <option value=\"$sd\" selected=\"selected\">$sd</option>\n";
232 $found_subdivision = 1;
234 print " <option value=\"$sd\">$sd</option>\n";
238 $subdivision = 1 if (!$found_subdivision);
242 <input type="submit" value="Vis" />
247 # Get players and ratings
248 $q = $dbh->prepare('SELECT fotballdeltagere.id,fotballdeltagere.navn,rating,rating_stddev FROM fotballdeltagere JOIN fotballserier ON fotballdeltagere.serie=fotballserier.nr LEFT JOIN ratings ON fotballdeltagere.id=ratings.id WHERE sesong=? AND divisjon=? AND avdeling=?');
249 $q->execute($season, $division, $subdivision);
251 while (my $ref = $q->fetchrow_hashref) {
252 my $id = $ref->{'id'};
253 $players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
254 $ratings{$id} = $ref->{'rating'};
255 $ratings_stddev{$id} = $ref->{'rating_stddev'};
261 d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
262 FROM fotballresultater r
263 JOIN fotballserier s ON r.serie=s.nr
264 JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
265 JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
267 sesong=? AND divisjon=? AND avdeling=?
268 AND lagrecno > motstander
270 $q->execute($season, $division, $subdivision);
272 while (my $ref = $q->fetchrow_hashref) {
273 push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
277 # Pick up covariance matrix
278 my $player_sql = '{' . join(',', keys %players ) . '}';
279 my $q = $dbh->prepare('SELECT * FROM covariance WHERE player1=ANY(?::smallint[]) AND player2=ANY(?::smallint[])', { pg_prepare_now => 0 });
280 $q->execute($player_sql, $player_sql);
283 while (my $ref = $q->fetchrow_hashref) {
284 $cov->{$ref->{'player1'}}{$ref->{'player2'}} = $ref->{'cov'};
287 my $lowest_division = ($division == $max_division);
288 make_table($lowest_division, {}, {});
291 <p>Under er en variant som tar relativ spillestyrke med i beregningen;
292 se <a href="/rating">ratingsiden</a>.</p>
295 make_table($lowest_division, \%ratings, $cov);