]> git.sesse.net Git - wloh/commitdiff
Factor out fetching of aux parameters.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 25 Mar 2012 15:36:01 +0000 (17:36 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 25 Mar 2012 15:36:01 +0000 (17:36 +0200)
www/index.pl

index 34cc7077471389d7ca4bde8343ae0ee40b773eb9..34ae134437183c253420723537499059e1baebd4 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,14 +291,19 @@ 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 $aux_parms = get_auxillary_parameters($dbh);
+my $match_stddev = $aux_parms->{-2} * sqrt(2.0);
 
 my $season;
 my $division = $cgi->param('divisjon') // -1;
@@ -350,14 +353,14 @@ 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;