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 if ($num_groups == 1) {
23 $dbh->do('INSERT INTO groups (tournament, round, parallel) VALUES (?, ?, ?)',
24 undef, $tournament, $round, 0);
26 for my $i (1..$num_groups) {
27 $dbh->do('INSERT INTO groups (tournament, round, parallel) VALUES (?, ?, ?)',
28 undef, $tournament, $round, $i);
32 # Seed people into groups (quite preliminary for now)
35 $people = ccbs::db_fetch_all($dbh, 'SELECT * FROM players WHERE player IN ( SELECT player FROM tournamentparticipation WHERE tournament=? ) ORDER BY lower(nick)',
38 # First of all, check that there are no null values!
39 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);
40 if ($ref->{'num_incomplete'} != 0) {
41 ccbs::user_error("Det er fortsatt $ref->{'num_incomplete'} sanger igjen i denne runden som ikke har alle data registrert.");
44 # Find out how many people will go on from the _current_ group (ie. the one
45 # before the one we just inserted)
46 my $ref = $dbh->selectrow_hashref('SELECT numqualifying FROM rounds WHERE tournament=? AND round=?',
47 undef, $tournament, $round - 1);
48 my $num_qual_prev = $ref->{'numqualifying'};
50 # Get the total list of scores for each player in this round, and pick
53 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');
54 $q->execute($tournament, $round - 1);
56 my ($parallel,$num_from_this_parallel);
58 while (my $ref = $q->fetchrow_hashref()) {
59 if (!defined($parallel) || $parallel != $ref->{'parallel'}) {
60 $parallel = $ref->{'parallel'};
61 $num_from_this_parallel = 0;
63 if ($num_from_this_parallel++ < $num_qual_prev) {
64 push @$people, {%$ref};
69 if ($num_groups == 1) {
70 # Everybody's in the same group
72 for my $p (@$people) {
73 $dbh->do('INSERT INTO roundparticipation (tournament, round, parallel, player, position) VALUES (?, ?, ?, ?, ?)', undef,
74 $tournament, $round, 0, $p->{'player'}, $position);
78 # Zigzag people to get the most fair groups possible
82 for my $p (@$people) {
83 $dbh->do('INSERT INTO roundparticipation (tournament, round, parallel, player, position) VALUES (?, ?, ?, ?, ?)', undef,
84 $tournament, $round, $group, $p->{'player'}, $position);
86 if ($group + $direction < 1 || $group + $direction > $num_groups) {
87 $direction = -$direction;
95 # Pick random songs for the groups
96 for my $g (1..$num_groups) {
97 my $gg = ($num_groups == 1) ? 0 : $g;
99 for my $s (1..$num_random) {
100 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',
102 if (!defined($ref)) {
103 ccbs::user_error('Det er ikke flere sanger igjen i sangvelgeren!');
105 $dbh->do('INSERT INTO randomsongsused (song) VALUES (?)',
106 undef, $ref->{'song'});
107 $dbh->do('INSERT INTO roundrandomsongs (tournament, round, parallel, song) VALUES (?,?,?,?)',
108 undef, $tournament, $round, $gg, $ref->{'song'});
110 $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,?,\'single\',\'expert\',\'f\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
111 $s, $ref->{'song'}, $tournament, $round, $gg);
115 # Add empty "score" records for the chosen songs.
116 for my $g (1..$num_groups) {
117 my $gg = ($num_groups == 1) ? 0 : $g;
119 for my $s (1..$num_chosen) {
120 $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,NULL,\'single\',\'expert\',\'t\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
121 $s + $num_random, $tournament, $round, $gg);
125 ccbs::print_see_other('show-tournament.pl?id=' . $tournament);