]> git.sesse.net Git - ccbs/blob - html/do-start-round.pl
Fix DDRFreak parser to output some UTF-8 glyphs instead of HTML entities. Update...
[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         # 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.");
37         }
38
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'};
44
45         # Get the total list of scores for each player in this round, and pick
46         # out the best N
47         $people = [];
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);
50         
51         my ($parallel,$num_from_this_parallel);
52
53         while (my $ref = $q->fetchrow_hashref()) {
54                 if (!defined($parallel) || $parallel != $ref->{'parallel'}) {
55                         $parallel = $ref->{'parallel'};
56                         $num_from_this_parallel = 0;
57                 }
58                 if ($num_from_this_parallel++ < $num_qual_prev) {
59                         push @$people, {%$ref};
60                 }
61         }
62 }
63
64 # Zigzag people to get the most fair groups possible
65 my $group = 1;
66 my $direction = 1;
67 my $position = 1;
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);
71
72         if ($group + $direction < 1 || $group + $direction > $num_groups) {
73                 $direction = -$direction;
74                 $position++;
75         } else {
76                 $group += $direction;
77         }
78 }
79
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',
84                         undef, $tournament);
85                 if (!defined($ref)) {
86                         ccbs::user_error('Det er ikke flere sanger igjen i sangvelgeren!');
87                 }
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'});
92
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);
95         }
96 }
97
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);
103         }
104 }
105
106 ccbs::print_see_other('show-tournament.pl?id=' . $tournament);
107
108 $dbh->commit;
109 $dbh->disconnect;