]> git.sesse.net Git - wloh/commitdiff
Make the display frontend filter out only the data relevant for the current locale.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 30 May 2012 18:52:54 +0000 (20:52 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 30 May 2012 18:52:54 +0000 (20:52 +0200)
common.pm
www/index.pl
www/rating.pl

index 7ec01a06e4eee50bb8975329ff0e47f9779bc2b2..832a61173f0f85932e164d0f828742ac78862df6 100644 (file)
--- 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;
index c26f12892168ef5702e47404d1e0dde34df32667..916af5c55349cb1b5baa583d1aa584c158c64e27 100755 (executable)
@@ -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";
-    <form method="get" action="/">
+    <form method="get" action="/$locale/">
 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 "        <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\">%.1f%%</td>\n", $pn * 100.0;
                        } else {
-                               printf "        <td style=\"background-color: rgb($r, $g, $b)\" class=\"num\"><a class=\"unmarkedlink\" href=\"javascript:showScenario('scenario$num_tables', '/?divisjon=$division;avdeling=$subdivision;spiller=$pnum;posisjon=$i');\">%.1f%%</a></td>\n", $pn * 100.0;
+                               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;
                        }
                }
 
@@ -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";
   </body>
@@ -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)) {
     <p>Spillerne er sortert etter nick.</p>
 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";
     <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($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 "    <p style=\"clear: both; padding-top: 1em;\">Gjennomsnittlig rating i denne avdelingen er <strong>%.1f</strong>.</p>\n", $avg_rating;
index aae154d29534433f9369c89de285c86a1a39216d..40e91656cced3c3b8d6c57ca92aae3bf44e1be4e 100755 (executable)
@@ -84,7 +84,9 @@ printf <<"EOF", $params{-3}, $match_stddev;
       </tr>
 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) {