]> git.sesse.net Git - ccbs/blob - html/do-start-round.pl
Add state to the random song chooser, so it doesn't choose the same songs twice....
[ccbs] / html / do-start-round.pl
1 #! /usr/bin/perl
2
3 use ccbs;
4 use strict;
5 use warnings;
6
7 my $dbh = ccbs::db_connect();
8 my $cgi = new CGI;
9
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');
16
17 $dbh->{AutoCommit} = 0;
18
19 $dbh->do('INSERT INTO rounds (tournament, round, randomsongs, chosensongs, numqualifying) VALUES (?, ?, ?, ?, ?)',
20         undef, $tournament, $round, $num_random, $num_chosen, $num_qual);
21
22 for my $i (1..$num_groups) {
23         $dbh->do('INSERT INTO groups (tournament, round, parallel) VALUES (?, ?, ?)',
24                 undef, $tournament, $round, $i);
25 }
26
27 # Seed people into groups (quite preliminary for now)
28 my $people;
29 if ($round == 1) {
30         $people = ccbs::db_fetch_all($dbh, 'SELECT * FROM players WHERE player IN ( SELECT player FROM tournamentparticipation WHERE tournament=? ) ORDER BY lower(nick)',
31                 $tournament);
32 } else {
33         die "Fix qualifying later";
34 }
35
36 # Zigzag people to get the most fair groups possible
37 my $group = 1;
38 my $direction = 1;
39 my $position = 1;
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);
43
44         if ($group + $direction < 1 || $group + $direction > $num_groups) {
45                 $direction = -$direction;
46                 $position++;
47         } else {
48                 $group += $direction;
49         }
50 }
51
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',
56                         undef, $tournament);
57                 if (!defined($ref)) {
58                         ccbs::user_error('Det er ikke flere sanger igjen i sangvelgeren!');
59                 }
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'});
64
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);
67         }
68 }
69
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);
75         }
76 }
77
78 ccbs::print_see_other('show-tournament.pl?id=' . $tournament);
79
80 $dbh->commit;
81 $dbh->disconnect;