7 my $dbh = ccbs::db_connect();
10 my $tournament = $cgi->param('tournament');
11 my $round = $cgi->param('round');
12 my $num_random = $cgi->param('numrandom');
13 my $num_chosen = $cgi->param('numchosen');
14 my $num_groups = $cgi->param('numgroups');
15 my $num_qual = $cgi->param('numqual');
17 $dbh->{AutoCommit} = 0;
19 $dbh->do('INSERT INTO rounds (tournament, round, randomsongs, chosensongs, numqualifying) VALUES (?, ?, ?, ?, ?)',
20 undef, $tournament, $round, $num_random, $num_chosen, $num_qual);
22 for my $i (1..$num_groups) {
23 $dbh->do('INSERT INTO groups (tournament, round, parallel) VALUES (?, ?, ?)',
24 undef, $tournament, $round, $i);
27 # Seed people into groups (quite preliminary for now)
30 $people = ccbs::db_fetch_all($dbh, 'SELECT * FROM players WHERE player IN ( SELECT player FROM tournamentparticipation WHERE tournament=? ) ORDER BY lower(nick)',
33 # First of all, check that there are no null values!
34 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);
35 if ($ref->{'num_incomplete'} != 0) {
36 ccbs::user_error("Det er fortsatt $ref->{'num_incomplete'} sanger igjen i denne runden som ikke har alle data registrert.");
39 # Find out how many people will go on from the _current_ group (ie. the one
40 # before the one we just inserted)
41 my $ref = $dbh->selectrow_hashref('SELECT numqualifying FROM rounds WHERE tournament=? AND round=?',
42 undef, $tournament, $round - 1);
43 my $num_qual_prev = $ref->{'numqualifying'};
45 # Get the total list of scores for each player in this round, and pick
48 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');
49 $q->execute($tournament, $round - 1);
51 my ($parallel,$num_from_this_parallel);
53 while (my $ref = $q->fetchrow_hashref()) {
54 if (!defined($parallel) || $parallel != $ref->{'parallel'}) {
55 $parallel = $ref->{'parallel'};
56 $num_from_this_parallel = 0;
58 if ($num_from_this_parallel++ < $num_qual_prev) {
59 push @$people, {%$ref};
64 # Zigzag people to get the most fair groups possible
68 for my $p (@$people) {
69 $dbh->do('INSERT INTO roundparticipation (tournament, round, parallel, player, position) VALUES (?, ?, ?, ?, ?)', undef,
70 $tournament, $round, $group, $p->{'player'}, $position);
72 if ($group + $direction < 1 || $group + $direction > $num_groups) {
73 $direction = -$direction;
80 # Pick random songs for the groups
81 for my $g (1..$num_groups) {
82 for my $s (1..$num_random) {
83 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',
86 ccbs::user_error('Det er ikke flere sanger igjen i sangvelgeren!');
88 $dbh->do('INSERT INTO randomsongsused (song) VALUES (?)',
89 undef, $ref->{'song'});
90 $dbh->do('INSERT INTO roundrandomsongs (tournament, round, parallel, song) VALUES (?,?,?,?)',
91 undef, $tournament, $round, $g, $ref->{'song'});
93 $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,?,NULL,NULL,\'f\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
94 $s, $ref->{'song'}, $tournament, $round, $g);
98 # Add empty "score" records for the chosen songs.
99 for my $g (1..$num_groups) {
100 for my $s (1..$num_chosen) {
101 $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,NULL,NULL,NULL,\'t\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
102 $s + $num_random, $tournament, $round, $g);
106 ccbs::print_see_other('show-tournament.pl?id=' . $tournament);