]> git.sesse.net Git - wloh/blobdiff - www/index.pl
Make rudimentary HTML output for scenario analysis.
[wloh] / www / index.pl
index 34ae134437183c253420723537499059e1baebd4..8274574a23df52464c1c26a6a783d6d74d1be778 100755 (executable)
@@ -302,40 +302,37 @@ sub get_auxillary_parameters {
        return $aux_parms;
 }
 
-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 $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>
-    <h1>WLoH-plasseringsannsynlighetsberegning</h1>
+EOF
+}
 
-    <p><em>Dette er et hobbyprosjekt fra tredjepart, og ikke en offisiell del av
-      <a href="http://wordfeud.aasmul.net/">Wordfeud Leage of Honour</a>.</em></p>
+sub print_footer {
+       print <<"EOF";
+  </body>
+</html>
+EOF
+}
 
-    <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
-      den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
-      %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
-      Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.</p>
+my $aux_parms = get_auxillary_parameters($dbh);
+my $match_stddev = $aux_parms->{-2} * sqrt(2.0);
 
-    <p>Spillerne er sortert etter nick.</p>
-EOF
+my $division = $cgi->param('divisjon') // -1;
+my $subdivision = $cgi->param('avdeling') // -1;
+my $match_player = $cgi->param('spiller');
+my $match_position = $cgi->param('posisjon');
 
 my $season = get_max_season($dbh);
 my @divisions = get_divisions($dbh, $season);
@@ -343,31 +340,72 @@ $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);
+@matches = get_matches($dbh, $season, $division, $subdivision);
+my $cov = get_covariance_matrix($dbh, keys %players);
 
-my @matches = get_matches($dbh, $season, $division, $subdivision);
+print_header($cgi, 'WLoH-plasseringsannsynlighetsberegning');
 
-my $cov = get_covariance_matrix($dbh, keys %players);
+if (defined($match_player) && defined($match_position)) {
+       my $tmpnam = write_parms_to_file($aux_parms, $match_stddev, \%ratings, $cov);
 
-my $max_division = $divisions[$#divisions];
-my $lowest_division = ($division == $max_division);
-make_table($aux_parms, $match_stddev, $lowest_division, {}, {});
+       --$match_player;
+       --$match_position;
 
-print <<"EOF";
+       my @scenario = ();
+       open MCCALC, "$config::base_dir/mcwordfeud $trials $match_player $match_position < $tmpnam |"
+               or die "mccalc: $!";
+       while (<MCCALC>) {
+               /(\d+) (\d+) (-?\d+)/ or next;
+               chomp;
+               push @scenario, [ $1, $2, $3 ];
+       }
+       close MCCALC;
+       #unlink $tmpnam;
+
+       if (scalar @scenario == 0) {
+               # FIXME: distinguish between "all played" and "none found"
+               print "    <p>Fant ingen m&aring;te dette kunne skje p&aring.</p>\n";
+       } else {
+               print "    <ul>\n";
+               for my $m (@scenario) {
+                       printf "    <li>%s &ndash; %s: %+d</li>\n", $players{$m->[0]}, $players{$m->[1]}, $m->[2];
+               }
+               print "    </ul>\n";
+       }
+} else {
+       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
+      <a href="http://wordfeud.aasmul.net/">Wordfeud Leage of Honour</a>.</em></p>
+
+    <p>Beregningen tar ikke hensyn til ujevn spillestyrke, ting som er sagt i forumet e.l.;
+      den antar at samtlige uspilte kamper trekkes fra en normalfordeling med standardavvik
+      %.1f poeng. Sannsynlighetene kan summere til andre tall enn 100%% pga. avrunding.
+      Tallene vil variere litt fra gang til gang fordi utregningen skjer ved randomisering.</p>
+
+    <p>Spillerne er sortert etter nick.</p>
+EOF
+
+       print_division_selector($dbh, \@divisions, \@subdivisions, $division, $subdivision);
+
+       my $max_division = $divisions[$#divisions];
+       my $lowest_division = ($division == $max_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($aux_parms, $match_stddev, $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;
+       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);
+       wloh_common::output_last_sync($dbh);
+}
 
-print <<"EOF";
-  </body>
-</html>
-EOF
+print_footer();