]> git.sesse.net Git - wloh/blobdiff - www/index.pl
Factor out subdivision-fetching code.
[wloh] / www / index.pl
index 529163266df023dc7e173e5a6dfec0c4f7a454e3..ef2e15eeacad5f042a1c3ba6515c302f4ba9ecb4 100755 (executable)
@@ -41,6 +41,42 @@ 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 @divisions = ();
+
+       my $q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier WHERE sesong=? ORDER BY divisjon');
+       $q->execute($season);
+
+       while (my $ref = $q->fetchrow_hashref) {
+               push @divisions, $ref->{'divisjon'};
+       }
+
+       return @divisions;
+}
+
+sub get_subdivisions {
+       my ($dbh, $season, $division) = @_;
+
+       my @subdivisions = ();
+
+       my $q = $dbh->prepare('SELECT DISTINCT(avdeling) FROM fotballserier WHERE sesong=? AND divisjon=? ORDER BY avdeling');
+       $q->execute($season, $division);
+
+       while (my $ref = $q->fetchrow_hashref) {
+               push @subdivisions, $ref->{'avdeling'};
+       }
+
+       return @subdivisions;
+}
+
 sub make_table {
        my ($lowest_division, $used_ratings, $used_cov) = @_;
 
@@ -184,60 +220,41 @@ printf <<"EOF", $match_stddev;
     <form method="get" action="/">
 EOF
 
-$q = $dbh->prepare('SELECT MAX(sesong) AS max_sesong FROM fotballserier');
-$q->execute;
-my $season;
-while (my $ref = $q->fetchrow_hashref) {
-       $season = $ref->{'max_sesong'};
-}
+my $season = get_max_season($dbh);
+my @divisions = get_divisions($dbh, $season);
+$division = 1 if (!grep { $_ == $division } @divisions);
+my $max_division = $divisions[$#divisions];
 
 print <<"EOF";
      <p>Divisjon:
         <select name="divisjon" onchange="form.submit();">
 EOF
 
-$q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier WHERE sesong=? ORDER BY divisjon');
-$q->execute($season);
-
-my $found_division = 0;
-my $max_division;
-
-while (my $ref = $q->fetchrow_hashref) {
-       my $d = $ref->{'divisjon'};
+for my $d (@divisions) {
        if ($d == $division) {
                print "        <option value=\"$d\" selected=\"selected\">$d</option>\n";
-               $found_division = 1;
        } else {
                print "        <option value=\"$d\">$d</option>\n";
        }
-       $max_division = $d;
 }
 
-$division = 1 if (!$found_division);
-
 print <<"EOF";
         </select>
         Avdeling:
         <select name="avdeling" onchange="form.submit();">
 EOF
 
-$q = $dbh->prepare('SELECT DISTINCT(avdeling) FROM fotballserier WHERE sesong=? AND divisjon=? ORDER BY avdeling');
-$q->execute($season, $division);
+my @subdivisions = get_subdivisions($dbh, $season, $division);
+$subdivision = 1 if (!grep { $_ == $subdivision } @subdivisions);
 
-my $found_subdivision = 0;
-
-while (my $ref = $q->fetchrow_hashref) {
-       my $sd = $ref->{'avdeling'};
+for my $sd (@subdivisions) {
        if ($sd == $subdivision) {
                print "        <option value=\"$sd\" selected=\"selected\">$sd</option>\n";
-               $found_subdivision = 1;
        } else {
                print "        <option value=\"$sd\">$sd</option>\n";
        }
 }
 
-$subdivision = 1 if (!$found_subdivision);
-
 print <<"EOF";
         </select>
         <input type="submit" value="Vis" />
@@ -246,6 +263,8 @@ print <<"EOF";
 EOF
 
 # Get players and ratings
+my $sum_rating = 0.0;
+
 $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);
 
@@ -254,6 +273,7 @@ while (my $ref = $q->fetchrow_hashref) {
        $players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
        $ratings{$id} = $ref->{'rating'};
        $ratings_stddev{$id} = $ref->{'rating_stddev'};
+       $sum_rating += $ref->{'rating'};
 }
 $q->finish;
 
@@ -295,6 +315,9 @@ EOF
 
 make_table($lowest_division, \%ratings, $cov);
 
+my $avg_rating = $sum_rating / scalar keys %players;
+printf "    <p>Gjennomsnittlig rating i denne avdelingen er <strong>%.1f</strong>.</p>\n", $avg_rating;
+
 wloh_common::output_last_sync($dbh);
 
 print <<"EOF";