+ }
+} else {
+ # Zigzag people to get the most fair groups possible
+ my @speople = sort { $b->{'score'} <=> $a->{'score'} } @$people;
+
+ my @ngroups = ();
+ for my $g (1..$num_groups) {
+ push @ngroups, [];
+ }
+
+ my $group = 0;
+ my $direction = 1;
+ for my $p (@speople) {
+ push @{$ngroups[$group]}, $p->{'player'};
+ if ($group + $direction < 0 || $group + $direction >= $num_groups) {
+ $direction = -$direction;
+ } else {
+ $group += $direction;
+ }
+ }
+
+ $group = 1;
+ for my $g (@ngroups) {
+ my @gpeople = reverse @$g;
+ for my $position (0..$#gpeople) {
+ $dbh->do('INSERT INTO roundparticipation (tournament, round, parallel, player, position) VALUES (?, ?, ?, ?, ?)', undef,
+ $tournament, $round, $group, $gpeople[$position], $position + 1);
+ }
+ $group++;