6 use CGI::Carp qw( fatalsToBrowser );
14 use lib qw(../include);
20 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
21 or die "connect: " . $DBI::errstr;
22 $dbh->{AutoCommit} = 0;
23 $dbh->{RaiseError} = 1;
27 binmode STDOUT, ':utf8';
31 my %ratings_stddev = ();
35 return HTML::Entities::encode_entities(shift);
40 return int(255.0 * ($x ** (1.80)));
44 my ($dbh, $locale, $season) = @_;
48 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');
49 $q->execute($locale, $season);
51 while (my $ref = $q->fetchrow_hashref) {
52 push @divisions, $ref->{'divisjon'};
58 sub get_subdivisions {
59 my ($dbh, $locale, $season, $division) = @_;
61 my @subdivisions = ();
63 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');
64 $q->execute($locale, $season, $division);
66 while (my $ref = $q->fetchrow_hashref) {
67 push @subdivisions, $ref->{'avdeling'};
73 sub get_division_selector {
74 my ($divisions, $division) = @_;
77 for my $d (@$divisions) {
82 $parms->{'option/selected'} = 'selected' if ($d == $division);
88 sub get_players_and_ratings {
89 my ($dbh, $locale, $season, $division, $subdivision) = @_;
91 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=?');
92 $q->execute($locale, $season, $division, $subdivision);
94 while (my $ref = $q->fetchrow_hashref) {
95 my $id = $ref->{'id'};
96 $players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
97 $ratings{$id} = $ref->{'rating'};
98 $ratings_stddev{$id} = $ref->{'rating_stddev'};
104 my ($dbh, $locale, $season, $division, $subdivision) = @_;
107 my $q = $dbh->prepare('
109 d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
110 FROM ( SELECT * FROM fotballresultater UNION ALL SELECT * FROM fotballresultater_2123 ) r
111 JOIN fotballserier s ON r.serie=s.nr
112 JOIN fotballspraak sp ON s.spraak=sp.id
113 JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
114 JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
116 kultur=? AND sesong=? AND divisjon=? AND avdeling=?
117 AND lagrecno > motstander
119 $q->execute($locale, $season, $division, $subdivision);
121 while (my $ref = $q->fetchrow_hashref) {
122 push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
129 sub get_covariance_matrix {
130 my ($dbh, @players) = @_;
132 my $player_sql = '{' . join(',', @players ) . '}';
133 my $q = $dbh->prepare('SELECT * FROM covariance WHERE player1=ANY(?::smallint[]) AND player2=ANY(?::smallint[])', { pg_prepare_now => 0 });
134 $q->execute($player_sql, $player_sql);
137 while (my $ref = $q->fetchrow_hashref) {
138 $cov->{$ref->{'player1'}}{$ref->{'player2'}} = $ref->{'cov'};
144 sub write_parms_to_file {
145 my ($aux_parms, $match_stddev, $used_ratings, $used_cov) = @_;
147 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
149 my @sorted_players = sort { $players{$a} cmp $players{$b} } keys %players;
151 POSIX::setlocale(&POSIX::LC_ALL, 'C');
153 my $tmpnam = POSIX::tmpnam();
154 open MCCALC, ">", $tmpnam
155 or die "$tmpnam: $!";
157 printf MCCALC "%f\n", $match_stddev;
158 printf MCCALC "%d\n", scalar keys %players;
160 for my $id (@sorted_players) {
161 my $rating = $used_ratings->{$id} // 500.0;
162 printf MCCALC "%s %f\n", $id, $rating;
166 for my $id1 (keys %players) {
167 for my $id2 (keys %players) {
169 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // $aux_parms->{'rating_prior_stddev'});
171 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // 0.0);
177 for my $match (@matches) {
178 printf MCCALC "%s %s %d %d\n", $match->[0], $match->[1], $match->[2], $match->[3];
182 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
188 my ($locale, $aux_parms, $match_stddev, $lowest_division, $used_ratings, $used_cov, $division, $subdivision, $table_id) = @_;
190 my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, $used_ratings, $used_cov);
193 open MCCALC, "$config::base_dir/mcwordfeud $trials < $tmpnam |"
199 my $player = sprintf "%s (%.0f ± %.0f)", $players{$id}, ($ratings{$id} // 500.0), ($ratings_stddev{$id} // $aux_parms->{'rating_prior_stddev'});
200 $prob{$player} = [ @x[1..$#x] ];
205 my $num_games = scalar keys %prob;
207 # Make list of ranks.
209 for my $i (1..$num_games) {
210 push @ranks, { 'th' => "$i." };
212 push @ranks, { 'th' => 'NEDRYKK' } unless ($lowest_division);
217 for my $player (sort { $a cmp $b } keys %prob) {
220 my @player_ranks = ();
222 for my $i (1..$num_games) {
223 my $pn = $prob{$player}->[$i - 1] / $trials;
225 my $r = color(1.0 - $pn / 3);
226 my $g = color(1.0 - $pn / 3);
231 } elsif ($i >= $num_games - 1 && !$lowest_division) {
235 my $num_total_games = ($num_games * ($num_games - 1)) / 2;
236 if (scalar @matches == $num_total_games || $prob{$player}->[$i - 1] == $trials) {
237 push @player_ranks, {
238 'td/style' => "background-color: rgb($r, $g, $b)",
239 'td' => sprintf("%.1f%%", $pn * 100.0),
243 push @player_ranks, {
244 'td/style' => "background-color: rgb($r, $g, $b)",
245 'a' => sprintf("%.1f%%", $pn * 100.0),
246 'a/href' => "javascript:showScenario('$table_id', '/$locale/?divisjon=$division;avdeling=$subdivision;spiller=$pnum;posisjon=$i"
251 unless ($lowest_division) {
252 my $pn = ($prob{$player}->[$num_games - 1] + $prob{$player}->[$num_games - 2]) / $trials;
255 my $g = color(1.0 - $pn / 3);
256 my $b = color(1.0 - $pn / 3);
257 push @player_ranks, {
258 'td/style' => "background-color: rgb($r, $g, $b)",
259 'td' => sprintf("%.1f%%", $pn * 100.0),
265 'player-ranks' => \@player_ranks
271 'tbody' => \@players,
277 my @players = (sort { $players{$a} cmp $players{$b} } keys %players);
279 my @player_list = ();
280 for my $player (@players) {
281 push @player_list, { 'th' => $players{$player} };
284 my @player_rows = ();
285 for my $player (@players) {
287 for my $player2 (@players) {
288 my $c = sprintf("%.2f", $cov->{$player}{$player2});
289 push @elements, { 'td' => $c };
292 '#player' => $players{$player},
293 'elements' => \@elements
298 'player-list' => \@player_list,
299 'tbody' => \@player_rows
303 sub find_avg_rating {
304 my ($ratings) = shift;
306 my $sum_rating = 0.0;
307 for my $r (values %$ratings) {
308 $sum_rating += ($r // 500.0);
310 return $sum_rating / scalar keys %$ratings;
314 my ($cgi, $title) = @_;
315 print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
317 <?xml version="1.0" encoding="UTF-8" ?>
319 html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
320 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
321 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no">
323 <title>$title</title>
324 <link rel="stylesheet" href="style" type="text/css" />
337 my $locale = wloh_common::get_locale($cgi);
338 my $aux_parms = wloh_common::get_auxillary_parameters($dbh, $locale);
339 my $match_stddev = $aux_parms->{'score_stddev'} * sqrt(2.0);
341 my $division = $cgi->param('divisjon') // -1;
342 my $subdivision = $cgi->param('avdeling') // -1;
343 my $match_player = $cgi->param('spiller');
344 my $match_position = $cgi->param('posisjon');
346 my $season = wloh_common::get_max_season($dbh, $locale);
347 die "Nonexistent locale!" if (!defined($season));
349 my @divisions = get_divisions($dbh, $locale, $season);
350 $division = $divisions[0] if (!grep { $_ == $division } @divisions);
351 my @subdivisions = get_subdivisions($dbh, $locale, $season, $division);
352 $subdivision = $subdivisions[0] if (!grep { $_ == $subdivision } @subdivisions);
354 get_players_and_ratings($dbh, $locale, $season, $division, $subdivision);
355 @matches = get_matches($dbh, $locale, $season, $division, $subdivision);
356 my $cov = get_covariance_matrix($dbh, keys %players);
358 if (defined($match_player) && defined($match_position)) {
359 print_header($cgi, 'WLoH-plasseringsannsynlighetsberegning');
360 my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, \%ratings, $cov);
366 open MCCALC, "$config::base_dir/mcwordfeud $trials $match_player $match_position < $tmpnam |"
369 /(\d+) (\d+) (-?\d+)/ or next;
371 push @scenario, [ $1, $2, $3 ];
376 my @sorted_players = sort { $players{$a} cmp $players{$b} } keys %players;
377 my $player_name = $players{$sorted_players[$match_player]};
379 if (scalar @scenario == 0) {
380 printf " <p>Fant ingen måte <strong>%s</strong> kan ende på <strong>%d.</strong> plass på.</p>\n",
381 $player_name, ($match_position + 1);
383 printf " <p>Scenario der <strong>%s</strong> ender på <strong>%d.</strong> plass:</p>\n",
384 $player_name, ($match_position + 1);
386 for my $m (@scenario) {
387 printf " <li>%s – %s: %+d</li>\n", $players{$m->[0]}, $players{$m->[1]}, $m->[2];
393 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
395 my $max_division = $divisions[$#divisions];
396 my $lowest_division = ($division == $max_division);
397 my $basic_table = make_table($locale, $aux_parms, $match_stddev, $lowest_division, {}, {}, $division, $subdivision, 'scenario1');
398 my $adjusted_table = make_table($locale, $aux_parms, $match_stddev, $lowest_division, \%ratings, $cov, $division, $subdivision, 'scenario2');
399 my $cov_table = (defined($cgi->param('showcov'))) ? make_cov_table($cov) : '';
401 my $avg_rating = find_avg_rating(\%ratings);
403 print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+5m');
404 wloh_common::process_template('index.xml', {
405 '#navbar' => wloh_common::get_navbar($cgi, $dbh, $locale),
406 '#division-selector/action' => "/$locale/",
407 '#division' => get_division_selector(\@divisions, $division),
408 '#subdivision' => get_division_selector(\@subdivisions, $subdivision),
409 '#basic-probabilities' => $basic_table,
410 '#adjusted-probabilities' => $adjusted_table,
411 'cov-table' => $cov_table,
412 'match-stddev' => sprintf("%.1f", $match_stddev),
413 '#average-rating' => sprintf("%.1f", $avg_rating),
414 'last-sync' => wloh_common::get_last_sync($dbh),