]> git.sesse.net Git - ccbs/blob - html/do-finish-tournament.pl
Add default constructor for widestring.
[ccbs] / html / do-finish-tournament.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 $dbh->{AutoCommit} = 0;
11
12 my $tournament = $cgi->param('tournament');
13 my %already_ordered = ();
14 my $ranking = 1;
15 my $points = 100;
16
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);
19
20 my $last_sround;
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'};
25
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));
33                 $ranking++;
34                 $already_ordered{$s->{'player'}} = 1;
35         }
36 }
37
38 # This should never happen
39 if (!defined($last_sround)) {
40         ccbs::user_error("Forsøk på å avslutte en turnering med flere grupper aktive.");
41 }
42
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));
52         $ranking++;
53         $already_ordered{$s->{'player'}} = 1;
54 }
55
56 $dbh->commit;
57 $dbh->disconnect;
58
59 ccbs::print_see_other('show-tournament.pl?id=' . $tournament);
60
61 # gives the usual 100, 91, 83, 76. 65, 61, ... series
62 sub points_for_place {
63         my $n = shift;
64         if ($n <= 10) {
65                 return 110 - (21/2) * $n + (1/2) * $n * $n;
66         } elsif ($n <= 65) {
67                 return 65 - $n;
68         } else {
69                 return 0;
70         }
71 }