+# Seed people into groups (quite preliminary for now)
+my $people;
+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";
+}
+
+# Zigzag people to get the most fair groups possible
+my $group = 1;
+my $direction = 1;
+my $position = 1;
+for my $p (@$people) {
+ $dbh->do('INSERT INTO roundparticipation (tournament, round, parallel, player, position) VALUES (?, ?, ?, ?, ?)', undef,
+ $tournament, $round, $group, $p->{'player'}, $position);
+
+ if ($group + $direction < 1 || $group + $direction > $num_groups) {
+ $direction = -$direction;
+ $position++;
+ } else {
+ $group += $direction;
+ }
+}
+
+# Pick random songs for the groups (FIXME: add state to the random generator later)
+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');
+ $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,
+ $s, $ref->{'song'}, $tournament, $round, $g);
+ }
+}
+
+# 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,
+ $s + $num_random, $tournament, $round, $g);
+ }
+}
+
+ccbs::print_see_other('show-tournament.pl?id=' . $tournament);