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 = ();
34 return HTML::Entities::encode_entities(shift);
39 return int(255.0 * ($x ** (1.80)));
43 my ($dbh, $locale, $season) = @_;
47 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');
48 $q->execute($locale, $season);
50 while (my $ref = $q->fetchrow_hashref) {
51 push @divisions, $ref->{'divisjon'};
57 sub get_subdivisions {
58 my ($dbh, $locale, $season, $division) = @_;
60 my @subdivisions = ();
62 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');
63 $q->execute($locale, $season, $division);
65 while (my $ref = $q->fetchrow_hashref) {
66 push @subdivisions, $ref->{'avdeling'};
72 sub print_division_selector {
73 my ($dbh, $locale, $divisions, $subdivisions, $division, $subdivision) = @_;
76 <form method="get" action="/$locale/">
79 my $max_division = $divisions->[(scalar @$divisions) - 1];
83 <select name="divisjon" onchange="form.submit();">
86 for my $d (@$divisions) {
87 if ($d == $division) {
88 print " <option value=\"$d\" selected=\"selected\">$d</option>\n";
90 print " <option value=\"$d\">$d</option>\n";
97 <select name="avdeling" onchange="form.submit();">
100 for my $sd (@$subdivisions) {
101 if ($sd == $subdivision) {
102 print " <option value=\"$sd\" selected=\"selected\">$sd</option>\n";
104 print " <option value=\"$sd\">$sd</option>\n";
110 <input type="submit" value="Vis" />
116 sub get_players_and_ratings {
117 my ($dbh, $locale, $season, $division, $subdivision) = @_;
119 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=?');
120 $q->execute($locale, $season, $division, $subdivision);
122 while (my $ref = $q->fetchrow_hashref) {
123 my $id = $ref->{'id'};
124 $players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
125 $ratings{$id} = $ref->{'rating'};
126 $ratings_stddev{$id} = $ref->{'rating_stddev'};
132 my ($dbh, $locale, $season, $division, $subdivision) = @_;
135 my $q = $dbh->prepare('
137 d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
138 FROM fotballresultater r
139 JOIN fotballserier s ON r.serie=s.nr
140 JOIN fotballspraak sp ON s.spraak=sp.id
141 JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
142 JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
144 kultur=? AND sesong=? AND divisjon=? AND avdeling=?
145 AND lagrecno > motstander
147 $q->execute($locale, $season, $division, $subdivision);
149 while (my $ref = $q->fetchrow_hashref) {
150 push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
157 sub get_covariance_matrix {
158 my ($dbh, @players) = @_;
160 my $player_sql = '{' . join(',', @players ) . '}';
161 my $q = $dbh->prepare('SELECT * FROM covariance WHERE player1=ANY(?::smallint[]) AND player2=ANY(?::smallint[])', { pg_prepare_now => 0 });
162 $q->execute($player_sql, $player_sql);
165 while (my $ref = $q->fetchrow_hashref) {
166 $cov->{$ref->{'player1'}}{$ref->{'player2'}} = $ref->{'cov'};
172 sub write_parms_to_file {
173 my ($aux_parms, $match_stddev, $used_ratings, $used_cov) = @_;
175 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
177 my @sorted_players = sort { $players{$a} cmp $players{$b} } keys %players;
179 POSIX::setlocale(&POSIX::LC_ALL, 'C');
181 my $tmpnam = POSIX::tmpnam();
182 open MCCALC, ">", $tmpnam
183 or die "$tmpnam: $!";
185 printf MCCALC "%f\n", $match_stddev;
186 printf MCCALC "%d\n", scalar keys %players;
188 for my $id (@sorted_players) {
189 my $rating = $used_ratings->{$id} // 500.0;
190 printf MCCALC "%s %f\n", $id, $rating;
194 for my $id1 (keys %players) {
195 for my $id2 (keys %players) {
197 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // $aux_parms->{'rating_prior_stddev'});
199 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // 0.0);
205 for my $match (@matches) {
206 printf MCCALC "%s %s %d %d\n", $match->[0], $match->[1], $match->[2], $match->[3];
210 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
218 my ($locale, $aux_parms, $match_stddev, $lowest_division, $used_ratings, $used_cov, $division, $subdivision) = @_;
222 <script type="text/javascript">
224 function showScenario(element_id, url) {
225 var obj = document.getElementById(element_id);
226 var parent = obj.parentElement;
227 parent.removeChild(obj);
228 obj = obj.cloneNode(false);
230 parent.appendChild(obj);
234 <table class="probmatrix">
239 my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, $used_ratings, $used_cov);
242 open MCCALC, "$config::base_dir/mcwordfeud $trials < $tmpnam |"
248 my $player = sprintf "%s (%.0f ± %.0f)", $players{$id}, ($ratings{$id} // 500.0), ($ratings_stddev{$id} // $aux_parms->{'rating_prior_stddev'});
249 $prob{$player} = [ @x[1..$#x] ];
254 my $num_games = scalar keys %prob;
255 for my $i (1..$num_games) {
256 print " <th>$i.</th>\n";
258 print " <th>NEDRYKK</th>\n" unless ($lowest_division);
262 for my $player (sort { $a cmp $b } keys %prob) {
265 print " <th>$player</th>\n";
267 for my $i (1..$num_games) {
268 my $pn = $prob{$player}->[$i - 1] / $trials;
270 my $r = color(1.0 - $pn / 3);
271 my $g = color(1.0 - $pn / 3);
276 } elsif ($i >= $num_games - 1 && !$lowest_division) {
280 my $num_total_games = ($num_games * ($num_games - 1)) / 2;
281 if (scalar @matches == $num_total_games || $prob{$player}->[$i - 1] == $trials) {
282 printf " <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
284 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;
288 unless ($lowest_division) {
289 my $pn = ($prob{$player}->[$num_games - 1] + $prob{$player}->[$num_games - 2]) / $trials;
292 my $g = color(1.0 - $pn / 3);
293 my $b = color(1.0 - $pn / 3);
294 printf " <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
302 <p class="scenario"><object id="scenario$num_tables" data="" type="text/html"></object></p>
306 sub find_avg_rating {
307 my ($ratings) = shift;
309 my $sum_rating = 0.0;
310 for my $r (values %$ratings) {
311 $sum_rating += ($r // 500.0);
313 return $sum_rating / scalar keys %$ratings;
317 my ($cgi, $title) = @_;
318 print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
320 <?xml version="1.0" encoding="UTF-8" ?>
322 html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
323 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
324 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no">
326 <title>$title</title>
327 <link rel="stylesheet" href="style" type="text/css" />
340 my $locale = wloh_common::get_locale($cgi);
341 my $aux_parms = wloh_common::get_auxillary_parameters($dbh, $locale);
342 my $match_stddev = $aux_parms->{'score_stddev'} * sqrt(2.0);
344 my $division = $cgi->param('divisjon') // -1;
345 my $subdivision = $cgi->param('avdeling') // -1;
346 my $match_player = $cgi->param('spiller');
347 my $match_position = $cgi->param('posisjon');
349 my $season = wloh_common::get_max_season($dbh, $locale);
350 die "Nonexistent locale!" if (!defined($season));
352 my @divisions = get_divisions($dbh, $locale, $season);
353 $division = $divisions[0] if (!grep { $_ == $division } @divisions);
354 my @subdivisions = get_subdivisions($dbh, $locale, $season, $division);
355 $subdivision = $subdivisions[0] if (!grep { $_ == $subdivision } @subdivisions);
357 get_players_and_ratings($dbh, $locale, $season, $division, $subdivision);
358 @matches = get_matches($dbh, $locale, $season, $division, $subdivision);
359 my $cov = get_covariance_matrix($dbh, keys %players);
361 print_header($cgi, 'WLoH-plasseringsannsynlighetsberegning');
363 if (defined($match_player) && defined($match_position)) {
364 my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, \%ratings, $cov);
370 open MCCALC, "$config::base_dir/mcwordfeud $trials $match_player $match_position < $tmpnam |"
373 /(\d+) (\d+) (-?\d+)/ or next;
375 push @scenario, [ $1, $2, $3 ];
380 my @sorted_players = sort { $players{$a} cmp $players{$b} } keys %players;
381 my $player_name = $players{$sorted_players[$match_player]};
383 if (scalar @scenario == 0) {
384 printf " <p>Fant ingen måte <strong>%s</strong> kan ende på <strong>%d.</strong> plass på.</p>\n",
385 $player_name, ($match_position + 1);
387 printf " <p>Scenario der <strong>%s</strong> ender på <strong>%d.</strong> plass:</p>\n",
388 $player_name, ($match_position + 1);
390 for my $m (@scenario) {
391 printf " <li>%s – %s: %+d</li>\n", $players{$m->[0]}, $players{$m->[1]}, $m->[2];
396 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
397 wloh_common::print_navbar($cgi, $dbh, $locale);
398 printf <<"EOF", $match_stddev;
399 <h1>WLoH-plasseringsannsynlighetsberegning</h1>
401 <p><em>Dette er et hobbyprosjekt fra tredjepart, og ikke en offisiell del av
402 <a href="http://wordfeud.aasmul.net/">Wordfeud Leage of Honour</a>.</em></p>
404 <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
405 den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
406 %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
407 Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.
408 For scenarioeksempel, klikk i en rute.</p>
410 <p>Spillerne er sortert etter nick.</p>
413 print_division_selector($dbh, $locale, \@divisions, \@subdivisions, $division, $subdivision);
415 my $max_division = $divisions[$#divisions];
416 my $lowest_division = ($division == $max_division);
417 make_table($locale, $aux_parms, $match_stddev, $lowest_division, {}, {}, $division, $subdivision);
420 <p style="clear: both; padding-top: 1em;">Under er en variant som tar relativ spillestyrke med i beregningen;
421 se <a href="rating">ratingsiden</a>.</p>
424 make_table($locale, $aux_parms, $match_stddev, $lowest_division, \%ratings, $cov, $division, $subdivision);
426 my $avg_rating = find_avg_rating(\%ratings);
427 printf " <p style=\"clear: both; padding-top: 1em;\">Gjennomsnittlig rating i denne avdelingen er <strong>%.1f</strong>.</p>\n", $avg_rating;
429 wloh_common::output_last_sync($dbh);