9 my $dbh = ccbs::db_connect();
12 my $tournament = $cgi->param('tournament');
13 my $round = $cgi->param('round');
14 my $num_random = $cgi->param('numrandom');
15 my $num_chosen = $cgi->param('numchosen');
16 my $num_groups = $cgi->param('numgroups');
17 my $num_qual = $cgi->param('numqual');
19 $dbh->{AutoCommit} = 0;
21 $dbh->do('INSERT INTO rounds (tournament, round, randomsongs, chosensongs, numqualifying) VALUES (?, ?, ?, ?, ?)',
22 undef, $tournament, $round, $num_random, $num_chosen, $num_qual);
24 if ($num_groups == 1) {
25 $dbh->do('INSERT INTO groups (tournament, round, parallel) VALUES (?, ?, ?)',
26 undef, $tournament, $round, 0);
28 for my $i (1..$num_groups) {
29 $dbh->do('INSERT INTO groups (tournament, round, parallel) VALUES (?, ?, ?)',
30 undef, $tournament, $round, $i);
34 # Seed people into groups (quite preliminary for now)
37 $people = ccbs::db_fetch_all($dbh, 'SELECT * FROM players WHERE player IN ( SELECT player FROM tournamentparticipation WHERE tournament=? ) ORDER BY random()',
40 # First of all, check that there are no null values!
41 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);
42 if ($ref->{'num_incomplete'} != 0) {
43 ccbs::user_error(_("There's still") . $ref->{'num_incomplete'} . _("songs left in this round which don't have all data registered."));
46 # Find out how many people will go on from the _current_ group (ie. the one
47 # before the one we just inserted)
48 my $ref = $dbh->selectrow_hashref('SELECT numqualifying FROM rounds WHERE tournament=? AND round=?',
49 undef, $tournament, $round - 1);
50 my $num_qual_prev = $ref->{'numqualifying'};
52 # Get the total list of scores for each player in this round, and pick
55 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');
56 $q->execute($tournament, $round - 1);
58 my ($parallel,$num_from_this_parallel);
60 while (my $ref = $q->fetchrow_hashref()) {
61 if (!defined($parallel) || $parallel != $ref->{'parallel'}) {
62 $parallel = $ref->{'parallel'};
63 $num_from_this_parallel = 0;
65 if ($num_from_this_parallel++ < $num_qual_prev) {
66 push @$people, {%$ref};
71 if ($num_groups == 1) {
72 # Everybody's in the same group
73 my @speople = sort { $a->{'score'} <=> $b->{'score'} } @$people;
76 for my $p (@speople) {
77 $dbh->do('INSERT INTO roundparticipation (tournament, round, parallel, player, position) VALUES (?, ?, ?, ?, ?)', undef,
78 $tournament, $round, 0, $p->{'player'}, $position);
82 # Zigzag people to get the most fair groups possible
83 my @speople = sort { $b->{'score'} <=> $a->{'score'} } @$people;
86 for my $g (1..$num_groups) {
92 for my $p (@speople) {
93 push @{$ngroups[$group]}, $p->{'player'};
94 if ($group + $direction < 0 || $group + $direction >= $num_groups) {
95 $direction = -$direction;
102 for my $g (@ngroups) {
103 my @gpeople = reverse @$g;
104 for my $position (0..$#gpeople) {
105 $dbh->do('INSERT INTO roundparticipation (tournament, round, parallel, player, position) VALUES (?, ?, ?, ?, ?)', undef,
106 $tournament, $round, $group, $gpeople[$position], $position + 1);
112 # Pick random songs for the groups (don't pick challenge-only songs; slightly
113 # clumsy, should we have used an IN subquery instead?)
114 for my $g (1..$num_groups) {
115 my $gg = ($num_groups == 1) ? 0 : $g;
117 for my $s (1..$num_random) {
118 my $ref = $dbh->selectrow_hashref('SELECT * FROM machinesongs NATURAL JOIN songratings WHERE song NOT IN ( SELECT song FROM randomsongsused ) AND machine=( SELECT machine FROM tournaments WHERE tournament=? ) AND playmode=\'single\' AND difficulty=\'expert\' ORDER BY random() LIMIT 1',
120 if (!defined($ref)) {
121 ccbs::user_error(_("There aren't any songs left in the song selector!"));
123 $dbh->do('INSERT INTO randomsongsused (song) VALUES (?)',
124 undef, $ref->{'song'});
125 $dbh->do('INSERT INTO roundrandomsongs (tournament, round, parallel, song) VALUES (?,?,?,?)',
126 undef, $tournament, $round, $gg, $ref->{'song'});
128 $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,?,\'single\',\'expert\',\'f\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
129 $s, $ref->{'song'}, $tournament, $round, $gg);
133 # Add empty "score" records for the chosen songs.
134 for my $g (1..$num_groups) {
135 my $gg = ($num_groups == 1) ? 0 : $g;
137 for my $s (1..$num_chosen) {
138 $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,NULL,\'single\',\'expert\',\'t\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
139 $s + $num_random, $tournament, $round, $gg);
143 ccbs::print_see_other('show-tournament.pl?id=' . $tournament);