+ # 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(_("There's still") . $ref->{'num_incomplete'} . _("songs left in this round which don't have all data registered.");
+ }
+
+ # 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)/SUM(CASE WHEN chosen THEN 10 ELSE feetrating END) AS score FROM scores NATURAL JOIN tournaments NATURAL JOIN max_single_feetrating 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};
+ }
+ }