]> git.sesse.net Git - ccbs/blobdiff - html/do-start-round.pl
Set all songs to single expert per default (this will break when we go to DDREx,...
[ccbs] / html / do-start-round.pl
index 2637076503eddb76584864307c6f6555323f4a92..00d94ff55e301709ce5abe8ad755e2e0abf562b0 100755 (executable)
@@ -30,7 +30,35 @@ if ($round == 1) {
        $people = ccbs::db_fetch_all($dbh, 'SELECT * FROM players WHERE player IN ( SELECT player FROM tournamentparticipation WHERE tournament=? ) ORDER BY lower(nick)',
                $tournament);
 } else {
-       die "Fix qualifying later";
+       # First of all, check that there are no null values!
+       my $ref = $dbh->selectrow_hashref('SELECT COUNT(*) AS num_incomplete FROM scores WHERE tournament=? AND round=? AND (song IS NULL OR playmode IS NULL OR difficulty IS NULL OR chosen IS NULL or score IS NULL)', undef, $tournament, $round-1);
+       if ($ref->{'num_incomplete'} != 0) {
+               ccbs::user_error("Det er fortsatt $ref->{'num_incomplete'} sanger igjen i denne runden som ikke har alle data registrert.");
+       }
+
+       # Find out how many people will go on from the _current_ group (ie. the one
+       # before the one we just inserted)
+       my $ref = $dbh->selectrow_hashref('SELECT numqualifying FROM rounds WHERE tournament=? AND round=?',
+               undef, $tournament, $round - 1);
+       my $num_qual_prev = $ref->{'numqualifying'};
+
+       # Get the total list of scores for each player in this round, and pick
+       # out the best N
+       $people = [];
+       my $q = $dbh->prepare('SELECT parallel,player,SUM(score) AS score FROM scores WHERE tournament=? AND round=? GROUP BY parallel,player ORDER BY parallel, SUM(score) DESC');
+       $q->execute($tournament, $round - 1);
+       
+       my ($parallel,$num_from_this_parallel);
+
+       while (my $ref = $q->fetchrow_hashref()) {
+               if (!defined($parallel) || $parallel != $ref->{'parallel'}) {
+                       $parallel = $ref->{'parallel'};
+                       $num_from_this_parallel = 0;
+               }
+               if ($num_from_this_parallel++ < $num_qual_prev) {
+                       push @$people, {%$ref};
+               }
+       }
 }
 
 # Zigzag people to get the most fair groups possible
@@ -62,7 +90,7 @@ for my $g (1..$num_groups) {
                $dbh->do('INSERT INTO roundrandomsongs (tournament, round, parallel, song) VALUES (?,?,?,?)',
                        undef, $tournament, $round, $g, $ref->{'song'});
 
-               $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,?,NULL,NULL,\'f\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
+               $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,?,\'single\',\'expert\',\'f\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
                        $s, $ref->{'song'}, $tournament, $round, $g);
        }
 }
@@ -70,7 +98,7 @@ for my $g (1..$num_groups) {
 # Add empty "score" records for the chosen songs.
 for my $g (1..$num_groups) {
        for my $s (1..$num_chosen) {
-               $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,NULL,NULL,NULL,\'t\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
+               $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,NULL,\'single\',\'expert\',\'t\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
                        $s + $num_random, $tournament, $round, $g);
        }
 }