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