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 print_division_selector {
+ my ($dbh, $divisions, $subdivisions, $division, $subdivision) = @_;
+
+ print <<"EOF";
+ <form method="get" action="/">
+EOF
+
+ my $max_division = $divisions->[(scalar @$divisions) - 1];
+
+ print <<"EOF";
+ <p>Divisjon:
+ <select name="divisjon" onchange="form.submit();">
+EOF
+
+ 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";
+ }
+ }
+
+ print <<"EOF";
+ </select>
+ <input type="submit" value="Vis" />
+ </p>
+ </form>
+EOF
+}
+
+sub get_covariance_matrix {
+ my ($dbh, @players) = @_;
+
+ my $player_sql = '{' . join(',', @players ) . '}';
+ my $q = $dbh->prepare('SELECT * FROM covariance WHERE player1=ANY(?::smallint[]) AND player2=ANY(?::smallint[])', { pg_prepare_now => 0 });
+ $q->execute($player_sql, $player_sql);
+
+ my $cov = {};
+ while (my $ref = $q->fetchrow_hashref) {
+ $cov->{$ref->{'player1'}}{$ref->{'player2'}} = $ref->{'cov'};
+ }
+
+ return $cov;
+}
+
sub make_table {
my ($lowest_division, $used_ratings, $used_cov) = @_;
Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.</p>
<p>Spillerne er sortert etter nick.</p>
-
- <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'};
-}
-
-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;
+my $season = 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);
-while (my $ref = $q->fetchrow_hashref) {
- my $d = $ref->{'divisjon'};
- 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 $found_subdivision = 0;
-
-while (my $ref = $q->fetchrow_hashref) {
- my $sd = $ref->{'avdeling'};
- 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" />
- </p>
- </form>
-EOF
+print_division_selector($dbh, \@divisions, \@subdivisions, $division, $subdivision);
# 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);
$players{$id} = sanitize(Encode::decode_utf8($ref->{'navn'}));
$ratings{$id} = $ref->{'rating'};
$ratings_stddev{$id} = $ref->{'rating_stddev'};
+ $sum_rating += $ref->{'rating'};
}
$q->finish;
}
$q->finish;
-# Pick up covariance matrix
-my $player_sql = '{' . join(',', keys %players ) . '}';
-my $q = $dbh->prepare('SELECT * FROM covariance WHERE player1=ANY(?::smallint[]) AND player2=ANY(?::smallint[])', { pg_prepare_now => 0 });
-$q->execute($player_sql, $player_sql);
-
-my $cov = {};
-while (my $ref = $q->fetchrow_hashref) {
- $cov->{$ref->{'player1'}}{$ref->{'player2'}} = $ref->{'cov'};
-}
+my $cov = get_covariance_matrix($dbh, keys %players);
+my $max_division = $divisions[$#divisions];
my $lowest_division = ($division == $max_division);
make_table($lowest_division, {}, {});
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";