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 die "Fix qualifying later";
36 # Zigzag people to get the most fair groups possible
40 for my $p (@$people) {
41 $dbh->do('INSERT INTO roundparticipation (tournament, round, parallel, player, position) VALUES (?, ?, ?, ?, ?)', undef,
42 $tournament, $round, $group, $p->{'player'}, $position);
44 if ($group + $direction < 1 || $group + $direction > $num_groups) {
45 $direction = -$direction;
52 # Pick random songs for the groups
53 for my $g (1..$num_groups) {
54 for my $s (1..$num_random) {
55 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',
58 ccbs::user_error('Det er ikke flere sanger igjen i sangvelgeren!');
60 $dbh->do('INSERT INTO randomsongsused (song) VALUES (?)',
61 undef, $ref->{'song'});
62 $dbh->do('INSERT INTO roundrandomsongs (tournament, round, parallel, song) VALUES (?,?,?,?)',
63 undef, $tournament, $round, $g, $ref->{'song'});
65 $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,?,NULL,NULL,\'f\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
66 $s, $ref->{'song'}, $tournament, $round, $g);
70 # Add empty "score" records for the chosen songs.
71 for my $g (1..$num_groups) {
72 for my $s (1..$num_chosen) {
73 $dbh->do('INSERT INTO scores SELECT tournament,round,parallel,player,?,NULL,NULL,NULL,\'t\',NULL FROM roundparticipation WHERE tournament=? AND round=? AND parallel=?', undef,
74 $s + $num_random, $tournament, $round, $g);
78 ccbs::print_see_other('show-tournament.pl?id=' . $tournament);