6 use CGI::Carp qw( fatalsToBrowser );
13 require '../config.pm';
17 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
18 or die "connect: " . $DBI::errstr;
19 $dbh->{AutoCommit} = 0;
20 $dbh->{RaiseError} = 1;
24 binmode STDOUT, ':utf8';
33 return HTML::Entities::encode_entities(shift);
38 return int(255.0 * ($x ** (1.80)));
42 my ($lowest_division, $used_ratings) = @_;
51 POSIX::setlocale(&POSIX::LC_ALL, 'C');
53 my $tmpnam = POSIX::tmpnam();
54 open MCCALC, ">", $tmpnam
57 printf MCCALC "%f\n", $match_stddev;
58 printf MCCALC "%d\n", scalar keys %players;
60 for my $id (keys %players) {
61 if (exists($used_ratings->{$id})) {
62 printf MCCALC "%s %f\n", $id, $used_ratings->{$id};
64 printf MCCALC "%s %f\n", $id, 1500.0;
68 for my $match (@matches) {
69 printf MCCALC "%s %s %d %d\n", $match->[0], $match->[1], $match->[2], $match->[3];
73 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
77 open MCCALC, "$config::base_dir/mcwordfeud $trials < $tmpnam |"
83 my $player = $players{$id};
84 $prob{$player} = [ @x[1..$#x] ];
89 my $num_games = scalar keys %prob;
90 for my $i (1..$num_games) {
91 print " <th>$i.</th>\n";
93 print " <th>NEDRYKK</th>\n" unless ($lowest_division);
96 for my $player (sort { $a cmp $b } keys %prob) {
98 print " <th>$player</th>\n";
100 for my $i (1..$num_games) {
101 my $pn = $prob{$player}->[$i - 1] / $trials;
103 my $r = color(1.0 - $pn / 3);
104 my $g = color(1.0 - $pn / 3);
109 } elsif ($i >= $num_games - 1 && !$lowest_division) {
113 printf " <td style=\"background-color: rgb($r, $g, $b)\">%.1f%%</td>\n", $pn * 100.0;
116 unless ($lowest_division) {
117 my $pn = ($prob{$player}->[$num_games - 1] + $prob{$player}->[$num_games - 2]) / $trials;
120 my $g = color(1.0 - $pn / 3);
121 my $b = color(1.0 - $pn / 3);
122 printf " <td style=\"background-color: rgb($r, $g, $b)\">%.1f%%</td>\n", $pn * 100.0;
132 # Get auxillary parameters
133 my $q = $dbh->prepare('SELECT * FROM ratings WHERE id < 0');
136 while (my $ref = $q->fetchrow_hashref) {
137 $parms{$ref->{'id'}} = $ref->{'rating'};
139 $match_stddev = $parms{-2} * sqrt(2.0);
142 my $division = $cgi->param('divisjon') // -1;
143 my $subdivision = $cgi->param('avdeling') // -1;
144 my $last_division = 0;
146 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
148 print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
149 printf <<"EOF", $match_stddev;
152 <title>WLoH-plasseringsannsynlighetsberegning</title>
153 <style type="text/css">
157 font-family: sans-serif;
160 border-collapse: collapse;
161 border: 1px solid black;
164 border: 1px solid black;
173 <h1>WLoH-plasseringsannsynlighetsberegning</h1>
175 <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
176 den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
177 %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
178 Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.</p>
180 <p>Spillerne er sortert etter nick.</p>
182 <form method="get" action="/">
185 $q = $dbh->prepare('SELECT MAX(sesong) AS max_sesong FROM fotballserier');
188 while (my $ref = $q->fetchrow_hashref) {
189 $season = $ref->{'max_sesong'};
194 <select name="divisjon" onchange="form.submit();">
197 $q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier WHERE sesong=? ORDER BY divisjon');
198 $q->execute($season);
200 my $found_division = 0;
203 while (my $ref = $q->fetchrow_hashref) {
204 my $d = $ref->{'divisjon'};
205 if ($d == $division) {
206 print " <option value=\"$d\" selected=\"selected\">$d</option>\n";
209 print " <option value=\"$d\">$d</option>\n";
214 $division = 1 if (!$found_division);
219 <select name="avdeling" onchange="form.submit();">
222 $q = $dbh->prepare('SELECT DISTINCT(avdeling) FROM fotballserier WHERE sesong=? AND divisjon=? ORDER BY avdeling');
223 $q->execute($season, $division);
225 my $found_subdivision = 0;
227 while (my $ref = $q->fetchrow_hashref) {
228 my $sd = $ref->{'avdeling'};
229 if ($sd == $subdivision) {
230 print " <option value=\"$sd\" selected=\"selected\">$sd</option>\n";
231 $found_subdivision = 1;
233 print " <option value=\"$sd\">$sd</option>\n";
237 $subdivision = 1 if (!$found_subdivision);
241 <input type="submit" value="Vis" />
246 # Get players and ratings
247 $q = $dbh->prepare('SELECT fotballdeltagere.id,fotballdeltagere.navn,rating FROM fotballdeltagere JOIN fotballserier ON fotballdeltagere.serie=fotballserier.nr JOIN ratings ON fotballdeltagere.id=ratings.id AND sesong=? AND divisjon=? AND avdeling=?');
248 $q->execute($season, $division, $subdivision);
250 while (my $ref = $q->fetchrow_hashref) {
251 my $id = $ref->{'id'};
252 $players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
253 $ratings{$id} = $ref->{'rating'};
259 d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
260 FROM fotballresultater r
261 JOIN fotballserier s ON r.serie=s.nr
262 JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
263 JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
265 sesong=? AND divisjon=? AND avdeling=?
266 AND lagrecno > motstander
268 $q->execute($season, $division, $subdivision);
270 while (my $ref = $q->fetchrow_hashref) {
271 push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
275 my $lowest_division = ($division == $max_division);
276 make_table($lowest_division, {});
279 <p>Under er en variant som prøver å ta relativ spillestyrke med i betraktningen.
280 Disse er basert på WLoH-data og oppdateres hver hele time (takk til Lobotommy for tilgang!),
281 men modellen er foreløpig ikke fullstendig tunet.</p>
284 make_table($lowest_division, \%ratings);