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 = ();
36 return int(255.0 * ($x ** (1.80)));
40 my ($dbh, $locale, $season) = @_;
44 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');
45 $q->execute($locale, $season);
47 while (my $ref = $q->fetchrow_hashref) {
48 push @divisions, $ref->{'divisjon'};
54 sub get_subdivisions {
55 my ($dbh, $locale, $season, $division) = @_;
57 my @subdivisions = ();
59 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');
60 $q->execute($locale, $season, $division);
62 while (my $ref = $q->fetchrow_hashref) {
63 push @subdivisions, $ref->{'avdeling'};
69 sub get_division_selector {
70 my ($divisions, $division) = @_;
73 for my $d (@$divisions) {
78 $parms->{'option/selected'} = 'selected' if ($d == $division);
84 sub get_players_and_ratings {
85 my ($dbh, $locale, $season, $division, $subdivision) = @_;
87 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=?');
88 $q->execute($locale, $season, $division, $subdivision);
90 while (my $ref = $q->fetchrow_hashref) {
91 my $id = $ref->{'id'};
92 $players{$id} = Encode::decode_utf8($ref->{'navn'});
93 $ratings{$id} = $ref->{'rating'};
94 $ratings_stddev{$id} = $ref->{'rating_stddev'};
100 my ($dbh, $locale, $season, $division, $subdivision) = @_;
103 my $q = $dbh->prepare('
105 d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
106 FROM ( SELECT * FROM fotballresultater UNION ALL SELECT * FROM fotballresultater_2123 ) r
107 JOIN fotballserier s ON r.serie=s.nr
108 JOIN fotballspraak sp ON s.spraak=sp.id
109 JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
110 JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
112 kultur=? AND sesong=? AND divisjon=? AND avdeling=?
113 AND lagrecno > motstander
115 $q->execute($locale, $season, $division, $subdivision);
117 while (my $ref = $q->fetchrow_hashref) {
118 push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
125 sub get_covariance_matrix {
126 my ($dbh, @players) = @_;
128 my $player_sql = '{' . join(',', @players ) . '}';
129 my $q = $dbh->prepare('SELECT * FROM covariance WHERE player1=ANY(?::smallint[]) AND player2=ANY(?::smallint[])', { pg_prepare_now => 0 });
130 $q->execute($player_sql, $player_sql);
133 while (my $ref = $q->fetchrow_hashref) {
134 $cov->{$ref->{'player1'}}{$ref->{'player2'}} = $ref->{'cov'};
140 sub write_parms_to_file {
141 my ($locale, $aux_parms, $match_stddev, $used_ratings, $used_cov) = @_;
143 wloh_common::set_locale($locale);
145 my @sorted_players = sort { $players{$a} cmp $players{$b} } keys %players;
147 POSIX::setlocale(&POSIX::LC_ALL, 'C');
149 my $tmpnam = POSIX::tmpnam();
150 open MCCALC, ">", $tmpnam
151 or die "$tmpnam: $!";
153 printf MCCALC "%f\n", $match_stddev;
154 printf MCCALC "%d\n", scalar keys %players;
156 for my $id (@sorted_players) {
157 my $rating = $used_ratings->{$id} // 500.0;
158 printf MCCALC "%s %f\n", $id, $rating;
162 for my $id1 (keys %players) {
163 for my $id2 (keys %players) {
165 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // $aux_parms->{'rating_prior_stddev'});
167 printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // 0.0);
173 for my $match (@matches) {
174 printf MCCALC "%s %s %d %d\n", $match->[0], $match->[1], $match->[2], $match->[3];
178 wloh_common::set_locale($locale);
184 my ($locale, $aux_parms, $match_stddev, $lowest_division, $used_ratings, $used_cov, $division, $subdivision, $table_id) = @_;
186 my $tmpnam = write_parms_to_file($locale, $aux_parms, $match_stddev, $used_ratings, $used_cov);
189 open MCCALC, "$config::base_dir/mcwordfeud $trials < $tmpnam |"
195 my $player = sprintf "%s (%.0f ± %.0f)", $players{$id}, ($ratings{$id} // 500.0), ($ratings_stddev{$id} // $aux_parms->{'rating_prior_stddev'});
196 $prob{$player} = [ @x[1..$#x] ];
201 my $num_games = scalar keys %prob;
203 # Make list of ranks. (Relegation is handled specially.)
205 for my $i (1..$num_games) {
206 push @ranks, { 'th' => wloh_common::get_ordinal($i, $locale) };
212 for my $player (sort { $a cmp $b } keys %prob) {
215 my @player_ranks = ();
217 for my $i (1..$num_games) {
218 my $pn = $prob{$player}->[$i - 1] / $trials;
220 my $r = color(1.0 - $pn / 3);
221 my $g = color(1.0 - $pn / 3);
226 } elsif ($i >= $num_games - 1 && !$lowest_division) {
230 my $num_total_games = ($num_games * ($num_games - 1)) / 2;
231 if (scalar @matches == $num_total_games || $prob{$player}->[$i - 1] == $trials) {
232 push @player_ranks, {
233 'td/style' => "background-color: rgb($r, $g, $b)",
234 'td' => sprintf("%.1f%%", $pn * 100.0),
238 push @player_ranks, {
239 'td/style' => "background-color: rgb($r, $g, $b)",
240 'a' => sprintf("%.1f%%", $pn * 100.0),
241 'a/href' => "javascript:showScenario('$table_id', '/$locale/?divisjon=$division;avdeling=$subdivision;spiller=$pnum;posisjon=$i')"
246 unless ($lowest_division) {
247 my $pn = ($prob{$player}->[$num_games - 1] + $prob{$player}->[$num_games - 2]) / $trials;
250 my $g = color(1.0 - $pn / 3);
251 my $b = color(1.0 - $pn / 3);
252 push @player_ranks, {
253 'td/style' => "background-color: rgb($r, $g, $b)",
254 'td' => sprintf("%.1f%%", $pn * 100.0),
260 'player-ranks' => \@player_ranks
266 'tbody' => \@players,
268 $parms->{'relegation'} = '' if ($lowest_division);
274 my @players = (sort { $players{$a} cmp $players{$b} } keys %players);
276 my @player_list = ();
277 for my $player (@players) {
278 push @player_list, { 'th' => $players{$player} };
281 my @player_rows = ();
282 for my $player (@players) {
284 for my $player2 (@players) {
285 my $c = sprintf("%.2f", $cov->{$player}{$player2});
286 push @elements, { 'td' => $c };
289 '#player' => $players{$player},
290 'elements' => \@elements
295 'player-list' => \@player_list,
296 'tbody' => \@player_rows
300 sub find_avg_rating {
301 my ($ratings) = shift;
303 my $sum_rating = 0.0;
304 for my $r (values %$ratings) {
305 $sum_rating += ($r // 500.0);
307 return $sum_rating / scalar keys %$ratings;
311 my ($cgi, $title) = @_;
312 print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
314 <?xml version="1.0" encoding="UTF-8" ?>
316 html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
317 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
318 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no">
320 <title>$title</title>
321 <link rel="stylesheet" href="style" type="text/css" />
334 my $locale = wloh_common::get_locale($cgi);
335 my $aux_parms = wloh_common::get_auxillary_parameters($dbh, $locale);
336 my $match_stddev = $aux_parms->{'score_stddev'} * sqrt(2.0);
338 my $division = $cgi->param('divisjon') // -1;
339 my $subdivision = $cgi->param('avdeling') // -1;
340 my $match_player = $cgi->param('spiller');
341 my $match_position = $cgi->param('posisjon');
343 my $season = wloh_common::get_max_season($dbh, $locale);
344 die "Nonexistent locale!" if (!defined($season));
346 my @divisions = get_divisions($dbh, $locale, $season);
347 $division = $divisions[0] if (!grep { $_ == $division } @divisions);
348 my @subdivisions = get_subdivisions($dbh, $locale, $season, $division);
349 $subdivision = $subdivisions[0] if (!grep { $_ == $subdivision } @subdivisions);
351 get_players_and_ratings($dbh, $locale, $season, $division, $subdivision);
352 @matches = get_matches($dbh, $locale, $season, $division, $subdivision);
353 my $cov = get_covariance_matrix($dbh, keys %players);
355 if (defined($match_player) && defined($match_position)) {
356 my $tmpnam = write_parms_to_file($locale, $aux_parms, $match_stddev, \%ratings, $cov);
362 open MCCALC, "$config::base_dir/mcwordfeud $trials $match_player $match_position < $tmpnam |"
365 /(\d+) (\d+) (-?\d+)/ or next;
368 'player-1' => $players{$1},
369 'player-2' => $players{$2},
370 'result' => sprintf("%+d", $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 print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+5m');
381 wloh_common::process_template('scenario-not-found', $locale, {
382 '#nick' => $player_name,
383 '#rank' => wloh_common::get_ordinal($match_position + 1, $locale)
386 print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+5m');
387 wloh_common::process_template('scenario', $locale, {
388 '#nick' => $player_name,
389 '#rank' => wloh_common::get_ordinal($match_position + 1, $locale),
390 '#results' => \@scenario
394 wloh_common::set_locale($locale);
396 my $max_division = $divisions[$#divisions];
397 my $lowest_division = ($division == $max_division);
398 my $basic_table = make_table($locale, $aux_parms, $match_stddev, $lowest_division, {}, {}, $division, $subdivision, 'scenario1');
399 my $adjusted_table = make_table($locale, $aux_parms, $match_stddev, $lowest_division, \%ratings, $cov, $division, $subdivision, 'scenario2');
400 my $cov_table = (defined($cgi->param('showcov'))) ? make_cov_table($cov) : '';
402 my $avg_rating = find_avg_rating(\%ratings);
404 print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+5m');
405 wloh_common::process_template('index', $locale, {
406 '#navbar' => wloh_common::get_navbar($cgi, $dbh, $locale),
407 '#division-selector/action' => "/$locale/",
408 '#division' => get_division_selector(\@divisions, $division),
409 '#subdivision' => get_division_selector(\@subdivisions, $subdivision),
410 '#basic-probabilities' => $basic_table,
411 '#adjusted-probabilities' => $adjusted_table,
412 'cov-table' => $cov_table,
413 'match-stddev' => sprintf("%.1f", $match_stddev),
414 '#average-rating' => sprintf("%.1f", $avg_rating),
415 'last-sync' => wloh_common::get_last_sync($dbh),