]> git.sesse.net Git - wloh/commitdiff
Support changing divisions etc.
authorSteinar H. Gunderson <Steinar H. Gunderson sesse@debian.org>
Sat, 17 Mar 2012 17:24:22 +0000 (18:24 +0100)
committerSteinar H. Gunderson <Steinar H. Gunderson sesse@debian.org>
Sat, 17 Mar 2012 17:24:22 +0000 (18:24 +0100)
www/index.pl

index e80188efc0818f3297d5047141f30055525b65ed..ecac609acdef31d01988f3e9edb7b7224bcf5b06 100755 (executable)
@@ -10,6 +10,8 @@ use Devel::Peek;
 use locale;
 require '../config.pm';
 
+my $cgi = CGI->new;
+
 my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
        or die "connect: " . $DBI::errstr;
 $dbh->{AutoCommit} = 0;
@@ -136,42 +138,13 @@ while (my $ref = $q->fetchrow_hashref) {
 }
 $match_stddev = $parms{-2} * sqrt(2.0);
 
-# Get players and ratings
-my $season = 18;
-my $division = 1;
-my $subdivision = 1;
-
-$q = $dbh->prepare('SELECT fotballdeltagere.id,fotballdeltagere.navn,rating FROM fotballdeltagere JOIN fotballserier ON fotballdeltagere.serie=fotballserier.nr JOIN ratings ON fotballdeltagere.id=ratings.id AND sesong=? AND divisjon=? AND avdeling=?');
-$q->execute($season, $division, $subdivision);
-
-while (my $ref = $q->fetchrow_hashref) {
-       my $id = $ref->{'id'};
-       $players{$id} = sanitize($ref->{'navn'});
-       $ratings{$id} = $ref->{'rating'};
-}
-$q->finish;
-
-$q = $dbh->prepare('
-SELECT
-  d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
-FROM fotballresultater r
-  JOIN fotballserier s ON r.serie=s.nr
-  JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
-  JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
-WHERE
-  sesong=? AND divisjon=? AND avdeling=?
-  AND lagrecno > motstander
-');
-$q->execute($season, $division, $subdivision);
-
-while (my $ref = $q->fetchrow_hashref) {
-       push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
-}
-$q->finish;
+my $season = $cgi->param('sesong') // -1;
+my $division = $cgi->param('divisjon') // -1;
+my $subdivision = $cgi->param('avdeling') // -1;
 
 POSIX::setlocale(&POSIX::LC_ALL, 'nb_NO.UTF-8');
 
-print CGI->header(-type=>'text/html; charset=utf-8', -expires=>'Thu, 01 Dec 1994 16:00:00 GMT');
+print $cgi->header(-type=>'text/html; charset=utf-8', -expires=>'Thu, 01 Dec 1994 16:00:00 GMT');
 printf <<"EOF", $match_stddev;
 <html>
   <head>
@@ -203,11 +176,116 @@ td {
       %.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>Andre divisjoner enn 1. divisjon kommer etter hvert.</p>
-
     <p>Spillerne er sortert etter nick.</p>
+
+    <form method="get" action="/">
+      <p>Sesong:
+        <select name="sesong">
+EOF
+
+$q = $dbh->prepare('SELECT DISTINCT(sesong) FROM fotballserier ORDER BY sesong');
+$q->execute;
+my $max_season;
+my @seasons = ();
+while (my $ref = $q->fetchrow_hashref) {
+       push @seasons, $ref->{'sesong'};
+       $max_season = $ref->{'sesong'};
+}
+
+my $found_season = 0;
+for my $s (@seasons) {
+       if ($s == $season || (!$found_season && $s == $max_season)) {
+               print "        <option value=\"$s\" selected=\"selected\">$s</option>\n";
+               $season = $s;
+               $found_season = 1;
+       } else {
+               print "        <option value=\"$s\">$s</option>\n";
+       }
+}
+
+print <<"EOF";
+        </select>
+        Divisjon:
+        <select name="divisjon">
+EOF
+
+$q = $dbh->prepare('SELECT DISTINCT(divisjon) FROM fotballserier WHERE sesong=? ORDER BY divisjon');
+$q->execute($season);
+
+my $found_division = 0;
+
+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";
+       }
+}
+
+$division = 1 if (!$found_division);
+
+print <<"EOF";
+        </select>
+        Avdeling:
+        <select name="avdeling">
 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
+
+# Get players and ratings
+$q = $dbh->prepare('SELECT fotballdeltagere.id,fotballdeltagere.navn,rating FROM fotballdeltagere JOIN fotballserier ON fotballdeltagere.serie=fotballserier.nr JOIN ratings ON fotballdeltagere.id=ratings.id AND sesong=? AND divisjon=? AND avdeling=?');
+$q->execute($season, $division, $subdivision);
+
+while (my $ref = $q->fetchrow_hashref) {
+       my $id = $ref->{'id'};
+       $players{$id} = sanitize($ref->{'navn'});
+       $ratings{$id} = $ref->{'rating'};
+}
+$q->finish;
+
+$q = $dbh->prepare('
+SELECT
+  d1.id AS p1, d2.id AS p2, maalfor AS score1, maalmot AS score2
+FROM fotballresultater r
+  JOIN fotballserier s ON r.serie=s.nr
+  JOIN fotballdeltagere d1 ON r.lagrecno=d1.nr AND r.serie=d1.serie
+  JOIN fotballdeltagere d2 ON r.motstander=d2.nr AND r.serie=d2.serie
+WHERE
+  sesong=? AND divisjon=? AND avdeling=?
+  AND lagrecno > motstander
+');
+$q->execute($season, $division, $subdivision);
+
+while (my $ref = $q->fetchrow_hashref) {
+       push @matches, [ $ref->{'p1'}, $ref->{'p2'}, $ref->{'score1'}, $ref->{'score2'} ];
+}
+$q->finish;
+
+
 make_table({});
 
 print <<"EOF";