From: Steinar H. Gunderson Date: Wed, 30 May 2012 18:52:54 +0000 (+0200) Subject: Make the display frontend filter out only the data relevant for the current locale. X-Git-Url: https://git.sesse.net/?p=wloh;a=commitdiff_plain;h=0ae9e34c70e35503e41d28af052c4957ca7b4f35 Make the display frontend filter out only the data relevant for the current locale. --- diff --git a/common.pm b/common.pm index 7ec01a0..832a611 100644 --- a/common.pm +++ b/common.pm @@ -5,8 +5,9 @@ use POSIX; package wloh_common; sub get_max_season { - my $dbh = shift; - my $ref = $dbh->selectrow_hashref('SELECT MAX(sesong) AS max_sesong FROM fotballserier'); + my ($dbh, $locale) = @_; + my $ref = $dbh->selectrow_hashref('SELECT MAX(sesong) AS max_sesong FROM fotballserier se JOIN fotballspraak sp ON se.spraak=sp.id WHERE kultur=?', + undef, $locale); return $ref->{'max_sesong'}; } @@ -21,4 +22,11 @@ sub output_last_sync { } } +sub get_locale { + my $cgi = shift; + my $url = $cgi->url(-absolute => 1); + $url =~ m#^/([a-z][a-z]-[A-Z][A-Z])/# or die "Invalid locale!"; + return $1; +} + 1; diff --git a/www/index.pl b/www/index.pl index c26f128..916af5c 100755 --- a/www/index.pl +++ b/www/index.pl @@ -40,12 +40,12 @@ sub color { } 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'}; @@ -55,12 +55,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'}; @@ -70,10 +70,10 @@ sub get_subdivisions { } sub print_division_selector { - my ($dbh, $divisions, $subdivisions, $division, $subdivision) = @_; + my ($dbh, $locale, $divisions, $subdivisions, $division, $subdivision) = @_; print <<"EOF"; -
+ EOF my $max_division = $divisions->[(scalar @$divisions) - 1]; @@ -114,10 +114,10 @@ EOF } 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'}; @@ -129,7 +129,7 @@ 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(' @@ -137,13 +137,14 @@ sub get_matches { d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2 FROM fotballresultater 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'} ]; @@ -214,7 +215,7 @@ sub write_parms_to_file { my $num_tables = 0; sub make_table { - my ($aux_parms, $match_stddev, $lowest_division, $used_ratings, $used_cov, $division, $subdivision) = @_; + my ($locale, $aux_parms, $match_stddev, $lowest_division, $used_ratings, $used_cov, $division, $subdivision) = @_; ++$num_tables; print <<"EOF"; @@ -280,7 +281,7 @@ EOF if (scalar @matches == $num_total_games || $prob{$player}->[$i - 1] == $trials) { printf " %.1f%%\n", $pn * 100.0; } else { - printf " %.1f%%\n", $pn * 100.0; + printf " %.1f%%\n", $pn * 100.0; } } @@ -340,13 +341,6 @@ sub print_header { EOF } -sub get_locale { - my $cgi = shift; - my $url = $cgi->url(-absolute => 1); - $url =~ m#^/([a-z][a-z]-[A-Z][A-Z])/# or die "Invalid locale!"; - return $1; -} - sub print_footer { print <<"EOF"; @@ -362,14 +356,17 @@ my $subdivision = $cgi->param('avdeling') // -1; my $match_player = $cgi->param('spiller'); my $match_position = $cgi->param('posisjon'); -my $season = wloh_common::get_max_season($dbh); -my @divisions = get_divisions($dbh, $season); -$division = 1 if (!grep { $_ == $division } @divisions); -my @subdivisions = get_subdivisions($dbh, $season, $division); -$subdivision = 1 if (!grep { $_ == $subdivision } @subdivisions); +my $locale = wloh_common::get_locale($cgi); +my $season = wloh_common::get_max_season($dbh, $locale); +die "Nonexistent locale!" if (!defined($season)); + +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); -get_players_and_ratings($dbh, $season, $division, $subdivision); -@matches = get_matches($dbh, $season, $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); print_header($cgi, 'WLoH-plasseringsannsynlighetsberegning'); @@ -423,18 +420,18 @@ if (defined($match_player) && defined($match_position)) {

Spillerne er sortert etter nick.

EOF - print_division_selector($dbh, \@divisions, \@subdivisions, $division, $subdivision); + print_division_selector($dbh, $locale, \@divisions, \@subdivisions, $division, $subdivision); my $max_division = $divisions[$#divisions]; my $lowest_division = ($division == $max_division); - make_table($aux_parms, $match_stddev, $lowest_division, {}, {}, $division, $subdivision); + make_table($locale, $aux_parms, $match_stddev, $lowest_division, {}, {}, $division, $subdivision); print <<"EOF";

Under er en variant som tar relativ spillestyrke med i beregningen; se ratingsiden.

EOF - make_table($aux_parms, $match_stddev, $lowest_division, \%ratings, $cov, $division, $subdivision); + make_table($locale, $aux_parms, $match_stddev, $lowest_division, \%ratings, $cov, $division, $subdivision); my $avg_rating = find_avg_rating(\%ratings); printf "

Gjennomsnittlig rating i denne avdelingen er %.1f.

\n", $avg_rating; diff --git a/www/rating.pl b/www/rating.pl index aae154d..40e9165 100755 --- a/www/rating.pl +++ b/www/rating.pl @@ -84,7 +84,9 @@ printf <<"EOF", $params{-3}, $match_stddev; EOF -my $season = wloh_common::get_max_season($dbh); +my $cgi = CGI->new; +my $locale = wloh_common::get_locale($cgi); +my $season = wloh_common::get_max_season($dbh, $locale); # Pick up all the subdivisions' ratings. my %subdivision_ratings = (); @@ -140,8 +142,10 @@ SELECT * FROM ratings NATURAL JOIN kanonisk_navn NATURAL JOIN siste_divisjon + NATURAL JOIN spiller_kultur +WHERE kultur=? ORDER BY rating DESC'); -$q->execute; +$q->execute($locale); $i = 0; while (my $ref = $q->fetchrow_hashref) {