X-Git-Url: https://git.sesse.net/?p=ccbs;a=blobdiff_plain;f=html%2Fdo-start-round.pl;h=ea6894e7126e88f5fcf66fa614baf75331cb3a77;hp=ff0a4178068b46e1273794bb2d1d5981d9984e78;hb=a6500d8a7d1a7fc0bd172373f1a670b8ee7ce92e;hpb=81edbdf60931cc5343964a86e95a43a4dfd6d103 diff --git a/html/do-start-round.pl b/html/do-start-round.pl index ff0a417..ea6894e 100755 --- a/html/do-start-round.pl +++ b/html/do-start-round.pl @@ -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 @@ -49,10 +77,16 @@ for my $p (@$people) { } } -# Pick random songs for the groups (FIXME: add state to the random generator later) +# Pick random songs for the groups for my $g (1..$num_groups) { for my $s (1..$num_random) { - my $ref = $dbh->selectrow_hashref('SELECT * FROM songs ORDER BY random() LIMIT 1'); + my $ref = $dbh->selectrow_hashref('SELECT * FROM machinesongs WHERE song NOT IN ( SELECT song FROM randomsongsused ) AND machine=( SELECT machine FROM tournaments WHERE tournament=? ) ORDER BY random() LIMIT 1', + undef, $tournament); + if (!defined($ref)) { + ccbs::user_error('Det er ikke flere sanger igjen i sangvelgeren!'); + } + $dbh->do('INSERT INTO randomsongsused (song) VALUES (?)', + undef, $ref->{'song'}); $dbh->do('INSERT INTO roundrandomsongs (tournament, round, parallel, song) VALUES (?,?,?,?)', undef, $tournament, $round, $g, $ref->{'song'});