]> git.sesse.net Git - ccbs/blob - html/do-finish-tournament.pl
7c7b14a0fe74efe5f07b94192ed907f79af4cc82
[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
25         # Grab the highscore list from this round
26         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',
27                 $tournament, $sr->{'round'});
28         for my $s (@$scores) {
29                 next if ($already_ordered{$s->{'player'}});
30                 $dbh->do('INSERT INTO tournamentrankings (tournament, ranking, player, points) VALUES (?,?,?,?)',
31                         undef, $tournament, $ranking, $s->{'player'}, points_for_place($ranking));
32                 $ranking++;
33                 $already_ordered{$s->{'player'}} = 1;
34         }
35 }
36
37 # This should never happen
38 if (!defined($last_sround)) {
39         ccbs::user_error("Forsøk på å avslutte en turnering med flere grupper aktive.");
40 }
41
42 $dbh->commit;
43 $dbh->disconnect;
44
45 ccbs::print_see_other('show-tournament.pl?id=' . $tournament);
46
47 # gives the usual 100, 91, 83, 76. 65, 61, ... series
48 sub points_for_place {
49         my $n = shift;
50         if ($n <= 10) {
51                 return 110 - (21/2) * $n + (1/2) * $n * $n;
52         } elsif ($n <= 65) {
53                 return 65 - $n;
54         } else {
55                 return 0;
56         }
57 }