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 ( SELECT * FROM fotballresultater UNION ALL SELECT * FROM fotballresultater_2123 ) 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>
308 my @players = (sort { $players{$a} cmp $players{$b} } keys %players);
311 <table class="probmatrix">
316 for my $player (@players) {
317 printf " <th>%s</th>\n", $players{$player};
322 for my $player (@players) {
325 printf " <th>%s</th>\n", $players{$player};
327 for my $player2 (@players) {
328 printf " <td class=\"num\">%.3f</td>\n", $cov->{$player}{$player2};
336 sub find_avg_rating {
337 my ($ratings) = shift;
339 my $sum_rating = 0.0;
340 for my $r (values %$ratings) {
341 $sum_rating += ($r // 500.0);
343 return $sum_rating / scalar keys %$ratings;
347 my ($cgi, $title) = @_;
348 print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
350 <?xml version="1.0" encoding="UTF-8" ?>
352 html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
353 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
354 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no">
356 <title>$title</title>
357 <link rel="stylesheet" href="style" type="text/css" />
370 my $locale = wloh_common::get_locale($cgi);
371 my $aux_parms = wloh_common::get_auxillary_parameters($dbh, $locale);
372 my $match_stddev = $aux_parms->{'score_stddev'} * sqrt(2.0);
374 my $division = $cgi->param('divisjon') // -1;
375 my $subdivision = $cgi->param('avdeling') // -1;
376 my $match_player = $cgi->param('spiller');
377 my $match_position = $cgi->param('posisjon');
379 my $season = wloh_common::get_max_season($dbh, $locale);
380 die "Nonexistent locale!" if (!defined($season));
382 my @divisions = get_divisions($dbh, $locale, $season);
383 $division = $divisions[0] if (!grep { $_ == $division } @divisions);
384 my @subdivisions = get_subdivisions($dbh, $locale, $season, $division);
385 $subdivision = $subdivisions[0] if (!grep { $_ == $subdivision } @subdivisions);
387 get_players_and_ratings($dbh, $locale, $season, $division, $subdivision);
388 @matches = get_matches($dbh, $locale, $season, $division, $subdivision);
389 my $cov = get_covariance_matrix($dbh, keys %players);
391 print_header($cgi, 'WLoH-plasseringsannsynlighetsberegning');
393 if (defined($match_player) && defined($match_position)) {
394 my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, \%ratings, $cov);
400 open MCCALC, "$config::base_dir/mcwordfeud $trials $match_player $match_position < $tmpnam |"
403 /(\d+) (\d+) (-?\d+)/ or next;
405 push @scenario, [ $1, $2, $3 ];
410 my @sorted_players = sort { $players{$a} cmp $players{$b} } keys %players;
411 my $player_name = $players{$sorted_players[$match_player]};
413 if (scalar @scenario == 0) {
414 printf " <p>Fant ingen måte <strong>%s</strong> kan ende på <strong>%d.</strong> plass på.</p>\n",
415 $player_name, ($match_position + 1);
417 printf " <p>Scenario der <strong>%s</strong> ender på <strong>%d.</strong> plass:</p>\n",
418 $player_name, ($match_position + 1);
420 for my $m (@scenario) {
421 printf " <li>%s – %s: %+d</li>\n", $players{$m->[0]}, $players{$m->[1]}, $m->[2];
426 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
427 wloh_common::print_navbar($cgi, $dbh, $locale);
428 printf <<"EOF", $match_stddev;
429 <h1>WLoH-plasseringsannsynlighetsberegning</h1>
431 <p><em>Dette er et hobbyprosjekt fra tredjepart, og ikke en offisiell del av
432 <a href="http://wordfeud.aasmul.net/">Wordfeud Leage of Honour</a>.</em></p>
434 <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
435 den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
436 %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
437 Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.
438 For scenarioeksempel, klikk i en rute.</p>
440 <p>Spillerne er sortert etter nick.</p>
443 print_division_selector($dbh, $locale, \@divisions, \@subdivisions, $division, $subdivision);
445 my $max_division = $divisions[$#divisions];
446 my $lowest_division = ($division == $max_division);
447 make_table($locale, $aux_parms, $match_stddev, $lowest_division, {}, {}, $division, $subdivision);
450 <p style="clear: both; padding-top: 1em;">Under er en variant som tar relativ spillestyrke med i beregningen;
451 se <a href="rating">ratingsiden</a>.</p>
454 make_table($locale, $aux_parms, $match_stddev, $lowest_division, \%ratings, $cov, $division, $subdivision);
456 my $avg_rating = find_avg_rating(\%ratings);
457 printf " <p style=\"clear: both; padding-top: 1em;\">Gjennomsnittlig rating i denne avdelingen er <strong>%.1f</strong>.</p>\n", $avg_rating;
459 if (defined($cgi->param('showcov'))) {
460 make_cov_table($cov);
463 wloh_common::output_last_sync($dbh);