7 my $dbh = ccbs::db_connect();
10 $dbh->{AutoCommit} = 0;
12 my $tournament = $cgi->param('tournament');
13 my %already_ordered = ();
17 # Find all last rounds with only one group per round
18 my $srounds = ccbs::db_fetch_all($dbh, 'SELECT round FROM groups WHERE tournament=? GROUP BY round HAVING COUNT(*) = 1 ORDER BY round DESC', $tournament);
21 for my $sr (@$srounds) {
22 # only accept strict ordering
23 last if (defined($last_sround) && $sr->{'round'} != $last_sround - 1);
24 $last_sround = $sr->{'round'};
26 # Grab the highscore list from this round
27 my $scores = ccbs::db_fetch_all($dbh, 'SELECT player,SUM(score) AS score FROM scores WHERE tournament=? AND round=? GROUP BY parallel,player ORDER BY SUM(score) DESC',
28 $tournament, $sr->{'round'});
29 for my $s (@$scores) {
30 next if ($already_ordered{$s->{'player'}});
31 $dbh->do('INSERT INTO tournamentrankings (tournament, ranking, player, points) VALUES (?,?,?,?)',
32 undef, $tournament, $ranking, $s->{'player'}, points_for_place($ranking));
34 $already_ordered{$s->{'player'}} = 1;
38 # This should never happen
39 if (!defined($last_sround)) {
40 ccbs::user_error("Forsøk på å avslutte en turnering med flere grupper aktive.");
43 # Grab all the remaining groups; we order by the simple criteria:
44 # 1. If player A has gone to group X and player B hasn't, player A is higher.
45 # 2. If player A has higher max score than player B, player A is higher.
46 my $scores = ccbs::db_fetch_all($dbh, 'SELECT player FROM scores WHERE tournament=? AND round < ? GROUP BY round,player ORDER BY round DESC,MAX(score) DESC',
47 $tournament, $last_sround);
48 for my $s (@$scores) {
49 next if ($already_ordered{$s->{'player'}});
50 $dbh->do('INSERT INTO tournamentrankings (tournament, ranking, player, points) VALUES (?,?,?,?)',
51 undef, $tournament, $ranking, $s->{'player'}, points_for_place($ranking));
53 $already_ordered{$s->{'player'}} = 1;
59 ccbs::print_see_other('show-tournament.pl?id=' . $tournament);
61 # gives the usual 100, 91, 83, 76. 65, 61, ... series
62 sub points_for_place {
65 return 110 - (21/2) * $n + (1/2) * $n * $n;