X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=www%2Findex.pl;h=0bf35594f8e385c7878badb0b6678c4b012e1124;hb=79ca58ab629b09e8fe59415578d2ee1ea5cb2079;hp=2cecec4672ad48d7a1893ef4a951989f367c556b;hpb=dec0ff92103e212921be03744323ed3a3074e996;p=wloh diff --git a/www/index.pl b/www/index.pl index 2cecec4..0bf3559 100755 --- a/www/index.pl +++ b/www/index.pl @@ -11,8 +11,9 @@ use HTML::Entities; 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; @@ -39,19 +40,13 @@ sub color { return int(255.0 * ($x ** (1.80))); } -sub get_max_season { - my $dbh = shift; - my $ref = $dbh->selectrow_hashref('SELECT MAX(sesong) AS max_sesong FROM fotballserier'); - return $ref->{'max_sesong'}; -} - sub get_divisions { - my ($dbh, $season) = @_; + my ($dbh, $locale, $season) = @_; my @divisions = (); - my $q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier WHERE sesong=? ORDER BY divisjon'); - $q->execute($season); + 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'); + $q->execute($locale, $season); while (my $ref = $q->fetchrow_hashref) { push @divisions, $ref->{'divisjon'}; @@ -61,12 +56,12 @@ sub get_divisions { } sub get_subdivisions { - my ($dbh, $season, $division) = @_; + my ($dbh, $locale, $season, $division) = @_; my @subdivisions = (); - my $q = $dbh->prepare('SELECT DISTINCT(avdeling) FROM fotballserier WHERE sesong=? AND divisjon=? ORDER BY avdeling'); - $q->execute($season, $division); + 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'); + $q->execute($locale, $season, $division); while (my $ref = $q->fetchrow_hashref) { push @subdivisions, $ref->{'avdeling'}; @@ -75,55 +70,26 @@ sub get_subdivisions { return @subdivisions; } -sub print_division_selector { - my ($dbh, $divisions, $subdivisions, $division, $subdivision) = @_; - - print <<"EOF"; -
-EOF + return \@ret; } sub get_players_and_ratings { - my ($dbh, $season, $division, $subdivision) = @_; + my ($dbh, $locale, $season, $division, $subdivision) = @_; - my $q = $dbh->prepare('SELECT fotballdeltagere.id,fotballdeltagere.navn,rating,rating_stddev FROM fotballdeltagere JOIN fotballserier ON fotballdeltagere.serie=fotballserier.nr LEFT JOIN ratings ON fotballdeltagere.id=ratings.id WHERE sesong=? AND divisjon=? AND avdeling=?'); - $q->execute($season, $division, $subdivision); + 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=?'); + $q->execute($locale, $season, $division, $subdivision); while (my $ref = $q->fetchrow_hashref) { my $id = $ref->{'id'}; @@ -135,21 +101,22 @@ sub get_players_and_ratings { } sub get_matches { - my ($dbh, $season, $division, $subdivision) = @_; + my ($dbh, $locale, $season, $division, $subdivision) = @_; my @matches = (); 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 JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie WHERE - sesong=? AND divisjon=? AND avdeling=? + kultur=? AND sesong=? AND divisjon=? AND avdeling=? AND lagrecno > motstander '); - $q->execute($season, $division, $subdivision); + $q->execute($locale, $season, $division, $subdivision); while (my $ref = $q->fetchrow_hashref) { push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ]; @@ -177,6 +144,10 @@ sub get_covariance_matrix { sub write_parms_to_file { my ($aux_parms, $match_stddev, $used_ratings, $used_cov) = @_; + POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8'); + + my @sorted_players = sort { $players{$a} cmp $players{$b} } keys %players; + POSIX::setlocale(&POSIX::LC_ALL, 'C'); my $tmpnam = POSIX::tmpnam(); @@ -186,7 +157,7 @@ sub write_parms_to_file { printf MCCALC "%f\n", $match_stddev; printf MCCALC "%d\n", scalar keys %players; - for my $id (keys %players) { + for my $id (@sorted_players) { my $rating = $used_ratings->{$id} // 500.0; printf MCCALC "%s %f\n", $id, $rating; } @@ -195,7 +166,7 @@ sub write_parms_to_file { for my $id1 (keys %players) { for my $id2 (keys %players) { if ($id1 == $id2) { - printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // $aux_parms->{-3}); + printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // $aux_parms->{'rating_prior_stddev'}); } else { printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // 0.0); } @@ -214,14 +185,7 @@ sub write_parms_to_file { } sub make_table { - my ($aux_parms, $match_stddev, $lowest_division, $used_ratings, $used_cov) = @_; - - print <<"EOF"; - --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 = (); @@ -232,22 +196,28 @@ EOF chomp; my @x = split /\s+/; my $id = $x[0]; - my $player = sprintf "%s (%.0f ± %.0f)", $players{$id}, ($ratings{$id} // 500.0), ($ratings_stddev{$id} // $aux_parms->{-3}); + my $player = sprintf "%s (%.0f ± %.0f)", $players{$id}, ($ratings{$id} // 500.0), ($ratings_stddev{$id} // $aux_parms->{'rating_prior_stddev'}); $prob{$player} = [ @x[1..$#x] ]; } close MCCALC; - #unlink $tmpnam; + unlink $tmpnam; my $num_games = scalar keys %prob; + + # Make list of ranks. + my @ranks = (); for my $i (1..$num_games) { - print " | $i. | \n"; + push @ranks, { 'th' => "$i." }; } - print "NEDRYKK | \n" unless ($lowest_division); - print "
---|---|---|
$player | \n"; + ++$pnum; + + my @player_ranks = (); for my $i (1..$num_games) { my $pn = $prob{$player}->[$i - 1] / $trials; @@ -262,7 +232,20 @@ EOF ($r, $b) = ($b, $r); } - printf "%.1f%% | \n", $pn * 100.0; + my $num_total_games = ($num_games * ($num_games - 1)) / 2; + if (scalar @matches == $num_total_games || $prob{$player}->[$i - 1] == $trials) { + push @player_ranks, { + 'td/style' => "background-color: rgb($r, $g, $b)", + 'td' => sprintf("%.1f%%", $pn * 100.0), + 'link' => '' + }; + } else { + 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" + }; + } } unless ($lowest_division) { @@ -271,35 +254,60 @@ EOF my $r = color(1.0); my $g = color(1.0 - $pn / 3); my $b = color(1.0 - $pn / 3); - printf "%.1f%% | \n", $pn * 100.0; + push @player_ranks, { + 'td/style' => "background-color: rgb($r, $g, $b)", + 'td' => sprintf("%.1f%%", $pn * 100.0), + 'link' => '' + }; } - print "
Dette er et hobbyprosjekt fra tredjepart, og ikke en offisiell del av - Wordfeud Leage of Honour.
- -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.
+my $season = wloh_common::get_max_season($dbh, $locale); +die "Nonexistent locale!" if (!defined($season)); -Spillerne er sortert etter nick.
-EOF +my @divisions = get_divisions($dbh, $locale, $season); +$division = $divisions[0] if (!grep { $_ == $division } @divisions); +my @subdivisions = get_subdivisions($dbh, $locale, $season, $division); +$subdivision = $subdivisions[0] if (!grep { $_ == $subdivision } @subdivisions); -print_division_selector($dbh, \@divisions, \@subdivisions, $division, $subdivision); +get_players_and_ratings($dbh, $locale, $season, $division, $subdivision); +@matches = get_matches($dbh, $locale, $season, $division, $subdivision); +my $cov = get_covariance_matrix($dbh, keys %players); -my $max_division = $divisions[$#divisions]; -my $lowest_division = ($division == $max_division); -make_table($aux_parms, $match_stddev, $lowest_division, {}, {}); +if (defined($match_player) && defined($match_position)) { + print_header($cgi, 'WLoH-plasseringsannsynlighetsberegning'); + my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, \%ratings, $cov); -print <<"EOF"; -Under er en variant som tar relativ spillestyrke med i beregningen; - se ratingsiden.
-EOF + --$match_player; + --$match_position; -make_table($aux_parms, $match_stddev, $lowest_division, \%ratings, $cov); + my @scenario = (); + open MCCALC, "$config::base_dir/mcwordfeud $trials $match_player $match_position < $tmpnam |" + or die "mccalc: $!"; + while (Fant ingen måte %s kan ende på %d. plass på.
\n", + $player_name, ($match_position + 1); + } else { + printf "Scenario der %s ender på %d. plass:
\n", + $player_name, ($match_position + 1); + print "Gjennomsnittlig rating i denne avdelingen er %.1f.
\n", $avg_rating; + my $max_division = $divisions[$#divisions]; + my $lowest_division = ($division == $max_division); + 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); + + 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($dbh);