6 use CGI::Carp qw( fatalsToBrowser );
14 require '../config.pm';
15 require '../common.pm';
19 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
20 or die "connect: " . $DBI::errstr;
21 $dbh->{AutoCommit} = 0;
22 $dbh->{RaiseError} = 1;
26 binmode STDOUT, ':utf8';
30 my %ratings_stddev = ();
36 return HTML::Entities::encode_entities(shift);
41 return int(255.0 * ($x ** (1.80)));
45 my ($lowest_division, $used_ratings, $used_cov) = @_;
54 POSIX::setlocale(&POSIX::LC_ALL, 'C');
56 my $tmpnam = POSIX::tmpnam();
57 open MCCALC, ">", $tmpnam
60 printf MCCALC "%f\n", $match_stddev;
61 printf MCCALC "%d\n", scalar keys %players;
63 for my $id (keys %players) {
64 my $rating = $used_ratings->{$id} // 500.0;
65 printf MCCALC "%s %f\n", $id, $rating;
69 for my $id1 (keys %players) {
70 for my $id2 (keys %players) {
72 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // $parms{-3});
74 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // 0.0);
80 for my $match (@matches) {
81 printf MCCALC "%s %s %d %d\n", $match->[0], $match->[1], $match->[2], $match->[3];
85 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
89 open MCCALC, "$config::base_dir/mcwordfeud $trials < $tmpnam |"
95 my $player = sprintf "%s (%.0f ± %.0f)", $players{$id}, ($ratings{$id} // 500.0), ($ratings_stddev{$id} // $parms{-3});
96 $prob{$player} = [ @x[1..$#x] ];
101 my $num_games = scalar keys %prob;
102 for my $i (1..$num_games) {
103 print " <th>$i.</th>\n";
105 print " <th>NEDRYKK</th>\n" unless ($lowest_division);
108 for my $player (sort { $a cmp $b } keys %prob) {
110 print " <th>$player</th>\n";
112 for my $i (1..$num_games) {
113 my $pn = $prob{$player}->[$i - 1] / $trials;
115 my $r = color(1.0 - $pn / 3);
116 my $g = color(1.0 - $pn / 3);
121 } elsif ($i >= $num_games - 1 && !$lowest_division) {
125 printf " <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
128 unless ($lowest_division) {
129 my $pn = ($prob{$player}->[$num_games - 1] + $prob{$player}->[$num_games - 2]) / $trials;
132 my $g = color(1.0 - $pn / 3);
133 my $b = color(1.0 - $pn / 3);
134 printf " <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
144 # Get auxillary parameters
145 my $q = $dbh->prepare('SELECT * FROM ratings WHERE id < 0');
148 while (my $ref = $q->fetchrow_hashref) {
149 $parms{$ref->{'id'}} = $ref->{'rating'};
151 $match_stddev = $parms{-2} * sqrt(2.0);
154 my $division = $cgi->param('divisjon') // -1;
155 my $subdivision = $cgi->param('avdeling') // -1;
156 my $last_division = 0;
158 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
160 print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
161 printf <<"EOF", $match_stddev;
162 <?xml version="1.0" encoding="UTF-8" ?>
164 html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
165 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
166 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no">
168 <title>WLoH-plasseringsannsynlighetsberegning</title>
169 <link rel="stylesheet" href="/style" type="text/css" />
172 <h1>WLoH-plasseringsannsynlighetsberegning</h1>
174 <p><em>Dette er et hobbyprosjekt fra tredjepart, og ikke en offisiell del av
175 <a href="http://wordfeud.aasmul.net/">Wordfeud Leage of Honour</a>.</em></p>
177 <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
178 den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
179 %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
180 Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.</p>
182 <p>Spillerne er sortert etter nick.</p>
184 <form method="get" action="/">
187 $q = $dbh->prepare('SELECT MAX(sesong) AS max_sesong FROM fotballserier');
190 while (my $ref = $q->fetchrow_hashref) {
191 $season = $ref->{'max_sesong'};
196 <select name="divisjon" onchange="form.submit();">
199 $q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier WHERE sesong=? ORDER BY divisjon');
200 $q->execute($season);
202 my $found_division = 0;
205 while (my $ref = $q->fetchrow_hashref) {
206 my $d = $ref->{'divisjon'};
207 if ($d == $division) {
208 print " <option value=\"$d\" selected=\"selected\">$d</option>\n";
211 print " <option value=\"$d\">$d</option>\n";
216 $division = 1 if (!$found_division);
221 <select name="avdeling" onchange="form.submit();">
224 $q = $dbh->prepare('SELECT DISTINCT(avdeling) FROM fotballserier WHERE sesong=? AND divisjon=? ORDER BY avdeling');
225 $q->execute($season, $division);
227 my $found_subdivision = 0;
229 while (my $ref = $q->fetchrow_hashref) {
230 my $sd = $ref->{'avdeling'};
231 if ($sd == $subdivision) {
232 print " <option value=\"$sd\" selected=\"selected\">$sd</option>\n";
233 $found_subdivision = 1;
235 print " <option value=\"$sd\">$sd</option>\n";
239 $subdivision = 1 if (!$found_subdivision);
243 <input type="submit" value="Vis" />
248 # Get players and ratings
249 $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=?');
250 $q->execute($season, $division, $subdivision);
252 while (my $ref = $q->fetchrow_hashref) {
253 my $id = $ref->{'id'};
254 $players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
255 $ratings{$id} = $ref->{'rating'};
256 $ratings_stddev{$id} = $ref->{'rating_stddev'};
262 d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
263 FROM fotballresultater r
264 JOIN fotballserier s ON r.serie=s.nr
265 JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
266 JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
268 sesong=? AND divisjon=? AND avdeling=?
269 AND lagrecno > motstander
271 $q->execute($season, $division, $subdivision);
273 while (my $ref = $q->fetchrow_hashref) {
274 push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
278 # Pick up covariance matrix
279 my $player_sql = '{' . join(',', keys %players ) . '}';
280 my $q = $dbh->prepare('SELECT * FROM covariance WHERE player1=ANY(?::smallint[]) AND player2=ANY(?::smallint[])', { pg_prepare_now => 0 });
281 $q->execute($player_sql, $player_sql);
284 while (my $ref = $q->fetchrow_hashref) {
285 $cov->{$ref->{'player1'}}{$ref->{'player2'}} = $ref->{'cov'};
288 my $lowest_division = ($division == $max_division);
289 make_table($lowest_division, {}, {});
292 <p>Under er en variant som tar relativ spillestyrke med i beregningen;
293 se <a href="/rating">ratingsiden</a>.</p>
296 make_table($lowest_division, \%ratings, $cov);
298 wloh_common::output_last_sync($dbh);