use Encode;
use utf8;
use locale;
-require '../config.pm';
-require '../common.pm';
+use lib qw(../include);
+require 'config.pm';
+require 'common.pm';
my $cgi = CGI->new;
my %ratings_stddev = ();
my @matches = ();
-sub sanitize {
- return HTML::Entities::encode_entities(shift);
-}
-
sub color {
my $x = shift;
return int(255.0 * ($x ** (1.80)));
return @subdivisions;
}
-sub print_division_selector {
- my ($dbh, $locale, $divisions, $subdivisions, $division, $subdivision) = @_;
-
- print <<"EOF";
- <form method="get" action="/$locale/">
-EOF
-
- my $max_division = $divisions->[(scalar @$divisions) - 1];
-
- print <<"EOF";
- <p>Divisjon:
- <select name="divisjon" onchange="form.submit();">
-EOF
+sub get_division_selector {
+ my ($divisions, $division) = @_;
+ my @ret = ();
for my $d (@$divisions) {
- if ($d == $division) {
- print " <option value=\"$d\" selected=\"selected\">$d</option>\n";
- } else {
- print " <option value=\"$d\">$d</option>\n";
- }
- }
-
- print <<"EOF";
- </select>
- Avdeling:
- <select name="avdeling" onchange="form.submit();">
-EOF
-
- for my $sd (@$subdivisions) {
- if ($sd == $subdivision) {
- print " <option value=\"$sd\" selected=\"selected\">$sd</option>\n";
- } else {
- print " <option value=\"$sd\">$sd</option>\n";
- }
+ my $parms = {
+ 'option' => $d,
+ 'option/value' => $d,
+ };
+ $parms->{'option/selected'} = 'selected' if ($d == $division);
+ push @ret, $parms;
}
-
- print <<"EOF";
- </select>
- <input type="submit" value="Vis" />
- </p>
- </form>
-EOF
+ return \@ret;
}
sub get_players_and_ratings {
while (my $ref = $q->fetchrow_hashref) {
my $id = $ref->{'id'};
- $players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
+ $players{$id} = Encode::decode_utf8($ref->{'navn'});
$ratings{$id} = $ref->{'rating'};
$ratings_stddev{$id} = $ref->{'rating_stddev'};
}
my $q = $dbh->prepare('
SELECT
d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
- FROM fotballresultater r
+ FROM ( SELECT * FROM fotballresultater UNION ALL SELECT * FROM fotballresultater_2123 ) r
JOIN fotballserier s ON r.serie=s.nr
JOIN fotballspraak sp ON s.spraak=sp.id
JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
return $tmpnam;
}
-my $num_tables = 0;
-
sub make_table {
- my ($locale, $aux_parms, $match_stddev, $lowest_division, $used_ratings, $used_cov, $division, $subdivision) = @_;
- ++$num_tables;
-
- print <<"EOF";
- <script type="text/javascript">
- <!--
-function showScenario(element_id, url) {
- var obj = document.getElementById(element_id);
- var parent = obj.parentElement;
- parent.removeChild(obj);
- obj = obj.cloneNode(false);
- obj.data = url;
- parent.appendChild(obj);
-}
- //-->
- </script>
- <table class="probmatrix">
- <tr>
- <th></th>
-EOF
+ my ($locale, $aux_parms, $match_stddev, $lowest_division, $used_ratings, $used_cov, $division, $subdivision, $table_id) = @_;
my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, $used_ratings, $used_cov);
my %prob = ();
unlink $tmpnam;
my $num_games = scalar keys %prob;
+
+ # Make list of ranks.
+ my @ranks = ();
for my $i (1..$num_games) {
- print " <th>$i.</th>\n";
+ push @ranks, { 'th' => "$i." };
}
- print " <th>NEDRYKK</th>\n" unless ($lowest_division);
- print " </tr>\n";
+ push @ranks, { 'th' => 'NEDRYKK' } unless ($lowest_division);
+
+ my @players = ();
my $pnum = 0;
for my $player (sort { $a cmp $b } keys %prob) {
++$pnum;
- print " <tr>\n";
- print " <th>$player</th>\n";
+
+ my @player_ranks = ();
for my $i (1..$num_games) {
my $pn = $prob{$player}->[$i - 1] / $trials;
my $num_total_games = ($num_games * ($num_games - 1)) / 2;
if (scalar @matches == $num_total_games || $prob{$player}->[$i - 1] == $trials) {
- printf " <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
+ push @player_ranks, {
+ 'td/style' => "background-color: rgb($r, $g, $b)",
+ 'td' => sprintf("%.1f%%", $pn * 100.0),
+ 'link' => ''
+ };
} else {
- 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;
+ push @player_ranks, {
+ 'td/style' => "background-color: rgb($r, $g, $b)",
+ 'a' => sprintf("%.1f%%", $pn * 100.0),
+ 'a/href' => "javascript:showScenario('$table_id', '/$locale/?divisjon=$division;avdeling=$subdivision;spiller=$pnum;posisjon=$i')"
+ };
}
}
my $r = color(1.0);
my $g = color(1.0 - $pn / 3);
my $b = color(1.0 - $pn / 3);
- printf " <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
+ push @player_ranks, {
+ 'td/style' => "background-color: rgb($r, $g, $b)",
+ 'td' => sprintf("%.1f%%", $pn * 100.0),
+ 'link' => ''
+ };
}
- print " </tr>\n";
+ push @players, {
+ 'player' => $player,
+ 'player-ranks' => \@player_ranks
+ };
}
- print << "EOF";
- </table>
-
- <p class="scenario"><object id="scenario$num_tables" data="" type="text/html"></object></p>
-EOF
+ return {
+ 'ranks' => \@ranks,
+ 'tbody' => \@players,
+ };
}
sub make_cov_table {
my ($cov) = @_;
my @players = (sort { $players{$a} cmp $players{$b} } keys %players);
- print <<"EOF";
- <table class="probmatrix">
- <tr>
- <th></th>
-EOF
-
+ my @player_list = ();
for my $player (@players) {
- printf " <th>%s</th>\n", $players{$player};
+ push @player_list, { 'th' => $players{$player} };
}
- print " </tr>\n";
- my $pnum = 0;
+ my @player_rows = ();
for my $player (@players) {
- ++$pnum;
- print " <tr>\n";
- printf " <th>%s</th>\n", $players{$player};
-
+ my @elements = ();
for my $player2 (@players) {
- printf " <td class=\"num\">%.3f</td>\n", $cov->{$player}{$player2};
+ my $c = sprintf("%.2f", $cov->{$player}{$player2});
+ push @elements, { 'td' => $c };
}
- print " </tr>\n";
+ push @player_rows, {
+ '#player' => $players{$player},
+ 'elements' => \@elements
+ };
}
- print " </table>\n";
+ return {
+ 'player-list' => \@player_list,
+ 'tbody' => \@player_rows
+ };
}
sub find_avg_rating {
@matches = get_matches($dbh, $locale, $season, $division, $subdivision);
my $cov = get_covariance_matrix($dbh, keys %players);
-print_header($cgi, 'WLoH-plasseringsannsynlighetsberegning');
-
if (defined($match_player) && defined($match_position)) {
my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, \%ratings, $cov);
while (<MCCALC>) {
/(\d+) (\d+) (-?\d+)/ or next;
chomp;
- push @scenario, [ $1, $2, $3 ];
+ push @scenario, {
+ 'player-1' => $players{$1},
+ 'player-2' => $players{$2},
+ 'result' => sprintf("%+d", $3),
+ };
}
close MCCALC;
unlink $tmpnam;
my $player_name = $players{$sorted_players[$match_player]};
if (scalar @scenario == 0) {
- printf " <p>Fant ingen måte <strong>%s</strong> kan ende på <strong>%d.</strong> plass på.</p>\n",
- $player_name, ($match_position + 1);
+ print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+5m');
+ wloh_common::process_template('scenario-not-found.xml', {
+ '#nick' => $player_name,
+ '#rank' => sprintf("%d.", $match_position + 1)
+ });
} else {
- printf " <p>Scenario der <strong>%s</strong> ender på <strong>%d.</strong> plass:</p>\n",
- $player_name, ($match_position + 1);
- print " <ul>\n";
- for my $m (@scenario) {
- printf " <li>%s – %s: %+d</li>\n", $players{$m->[0]}, $players{$m->[1]}, $m->[2];
- }
- print " </ul>\n";
+ print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+5m');
+ wloh_common::process_template('scenario.xml', {
+ '#nick' => $player_name,
+ '#rank' => sprintf("%d.", $match_position + 1),
+ '#results' => \@scenario
+ });
}
} else {
POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
- wloh_common::print_navbar($cgi, $dbh, $locale);
- printf <<"EOF", $match_stddev;
- <h1>WLoH-plasseringsannsynlighetsberegning</h1>
-
- <p><em>Dette er et hobbyprosjekt fra tredjepart, og ikke en offisiell del av
- <a href="http://wordfeud.aasmul.net/">Wordfeud Leage of Honour</a>.</em></p>
-
- <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
- den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
- %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
- Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.
- For scenarioeksempel, klikk i en rute.</p>
-
- <p>Spillerne er sortert etter nick.</p>
-EOF
-
- print_division_selector($dbh, $locale, \@divisions, \@subdivisions, $division, $subdivision);
my $max_division = $divisions[$#divisions];
my $lowest_division = ($division == $max_division);
- make_table($locale, $aux_parms, $match_stddev, $lowest_division, {}, {}, $division, $subdivision);
-
- print <<"EOF";
- <p style="clear: both; padding-top: 1em;">Under er en variant som tar relativ spillestyrke med i beregningen;
- se <a href="rating">ratingsiden</a>.</p>
-EOF
-
- make_table($locale, $aux_parms, $match_stddev, $lowest_division, \%ratings, $cov, $division, $subdivision);
+ my $basic_table = make_table($locale, $aux_parms, $match_stddev, $lowest_division, {}, {}, $division, $subdivision, 'scenario1');
+ my $adjusted_table = make_table($locale, $aux_parms, $match_stddev, $lowest_division, \%ratings, $cov, $division, $subdivision, 'scenario2');
+ my $cov_table = (defined($cgi->param('showcov'))) ? make_cov_table($cov) : '';
my $avg_rating = find_avg_rating(\%ratings);
- printf " <p style=\"clear: both; padding-top: 1em;\">Gjennomsnittlig rating i denne avdelingen er <strong>%.1f</strong>.</p>\n", $avg_rating;
-
- if (defined($cgi->param('showcov'))) {
- make_cov_table($cov);
- }
- wloh_common::output_last_sync($dbh);
+ print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'+5m');
+ wloh_common::process_template('index.xml', {
+ '#navbar' => wloh_common::get_navbar($cgi, $dbh, $locale),
+ '#division-selector/action' => "/$locale/",
+ '#division' => get_division_selector(\@divisions, $division),
+ '#subdivision' => get_division_selector(\@subdivisions, $subdivision),
+ '#basic-probabilities' => $basic_table,
+ '#adjusted-probabilities' => $adjusted_table,
+ 'cov-table' => $cov_table,
+ 'match-stddev' => sprintf("%.1f", $match_stddev),
+ '#average-rating' => sprintf("%.1f", $avg_rating),
+ 'last-sync' => wloh_common::get_last_sync($dbh),
+ });
}
-print_footer();