]> git.sesse.net Git - wloh/blobdiff - www/index.pl
Factor out some HTML headers and footers.
[wloh] / www / index.pl
index 34cc7077471389d7ca4bde8343ae0ee40b773eb9..2cecec4672ad48d7a1893ef4a951989f367c556b 100755 (executable)
@@ -29,8 +29,6 @@ my %players = ();
 my %ratings = ();
 my %ratings_stddev = ();
 my @matches = ();
-my %parms = ();
-my $match_stddev;
 
 sub sanitize {
        return HTML::Entities::encode_entities(shift);
@@ -177,7 +175,7 @@ sub get_covariance_matrix {
 }
 
 sub write_parms_to_file {
-       my ($used_ratings, $used_cov) = @_;
+       my ($aux_parms, $match_stddev, $used_ratings, $used_cov) = @_;
 
        POSIX::setlocale(&POSIX::LC_ALL, 'C');
 
@@ -197,7 +195,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} // $parms{-3});
+                               printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // $aux_parms->{-3});
                        } else {
                                printf MCCALC "%f ", ($used_cov->{$id1}{$id2} // 0.0);
                        }
@@ -216,7 +214,7 @@ sub write_parms_to_file {
 }
 
 sub make_table {
-       my ($lowest_division, $used_ratings, $used_cov) = @_;
+       my ($aux_parms, $match_stddev, $lowest_division, $used_ratings, $used_cov) = @_;
 
        print <<"EOF";
 
@@ -225,7 +223,7 @@ sub make_table {
         <th></th>
 EOF
 
-       my $tmpnam = write_parms_to_file($used_ratings, $used_cov);
+       my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, $used_ratings, $used_cov);
        my %prob = ();
 
        open MCCALC, "$config::base_dir/mcwordfeud $trials < $tmpnam |"
@@ -234,7 +232,7 @@ 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} // $parms{-3});
+               my $player = sprintf "%s (%.0f ± %.0f)", $players{$id}, ($ratings{$id} // 500.0), ($ratings_stddev{$id} // $aux_parms->{-3});
                $prob{$player} = [ @x[1..$#x] ];
        }
        close MCCALC;
@@ -293,34 +291,65 @@ sub find_avg_rating {
        return $sum_rating / scalar keys %ratings;
 }
 
-# Get auxillary parameters
-my $q = $dbh->prepare('SELECT * FROM ratings WHERE id < 0');
-$q->execute;
+sub get_auxillary_parameters {
+       my $q = $dbh->prepare('SELECT * FROM ratings WHERE id < 0');
+       $q->execute;
 
-while (my $ref = $q->fetchrow_hashref) {
-       $parms{$ref->{'id'}} = $ref->{'rating'};
+       my $aux_parms = {};
+       while (my $ref = $q->fetchrow_hashref) {
+               $aux_parms->{$ref->{'id'}} = $ref->{'rating'};
+       }
+       return $aux_parms;
 }
-$match_stddev = $parms{-2} * sqrt(2.0);
-
-my $season;
-my $division = $cgi->param('divisjon') // -1;
-my $subdivision = $cgi->param('avdeling') // -1;
-my $last_division = 0;
-
-POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
 
-print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
-printf <<"EOF", $match_stddev;
+sub print_header {
+       my ($cgi, $title) = @_;
+       print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'now');
+       print <<"EOF";
 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE
   html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no">
   <head>
-    <title>WLoH-plasseringsannsynlighetsberegning</title>
+    <title>$title</title>
     <link rel="stylesheet" href="/style" type="text/css" />
   </head>
   <body>
+EOF
+}
+
+sub print_footer {
+       my $dbh = shift;
+       wloh_common::output_last_sync($dbh);
+
+       print <<"EOF";
+  </body>
+</html>
+EOF
+}
+
+my $aux_parms = get_auxillary_parameters($dbh);
+my $match_stddev = $aux_parms->{-2} * sqrt(2.0);
+
+my $season;
+my $division = $cgi->param('divisjon') // -1;
+my $subdivision = $cgi->param('avdeling') // -1;
+
+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);
+
+get_players_and_ratings($dbh, $season, $division, $subdivision);
+my @matches = get_matches($dbh, $season, $division, $subdivision);
+my $cov = get_covariance_matrix($dbh, keys %players);
+
+print_header($cgi, 'WLoH-plasseringsannsynlighetsberegning');
+
+POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
+printf <<"EOF", $match_stddev;
     <h1>WLoH-plasseringsannsynlighetsberegning</h1>
 
     <p><em>Dette er et hobbyprosjekt fra tredjepart, og ikke en offisiell del av
@@ -334,37 +363,20 @@ printf <<"EOF", $match_stddev;
     <p>Spillerne er sortert etter nick.</p>
 EOF
 
-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);
-
 print_division_selector($dbh, \@divisions, \@subdivisions, $division, $subdivision);
 
-get_players_and_ratings($dbh, $season, $division, $subdivision);
-
-my @matches = get_matches($dbh, $season, $division, $subdivision);
-
-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($aux_parms, $match_stddev, $lowest_division, {}, {});
 
 print <<"EOF";
     <p>Under er en variant som tar relativ spillestyrke med i beregningen;
       se <a href="/rating">ratingsiden</a>.</p>
 EOF
 
-make_table($lowest_division, \%ratings, $cov);
+make_table($aux_parms, $match_stddev, $lowest_division, \%ratings, $cov);
 
 my $avg_rating = find_avg_rating(\%ratings);
 printf "    <p>Gjennomsnittlig rating i denne avdelingen er <strong>%.1f</strong>.</p>\n", $avg_rating;
 
-wloh_common::output_last_sync($dbh);
-
-print <<"EOF";
-  </body>
-</html>
-EOF
+print_footer($dbh);