]> git.sesse.net Git - ccbs/blob - html/do-finish-tournament.pl
Fix UTF-8.
[ccbs] / html / do-finish-tournament.pl
1 #! /usr/bin/perl
2
3 use ccbs;
4 use strict;
5 use warnings;
6
7 ccbs::admin_only();
8
9 my $dbh = ccbs::db_connect();
10 my $cgi = new CGI;
11
12 $dbh->{AutoCommit} = 0;
13
14 my $tournament = $cgi->param('tournament');
15 my %already_ordered = ();
16 my $ranking = 1;
17 my $points = 100;
18
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);
21
22 my $last_sround;
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'};
27
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));
35                 $ranking++;
36                 $already_ordered{$s->{'player'}} = 1;
37         }
38 }
39
40 # This should never happen
41 if (!defined($last_sround)) {
42         ccbs::user_error("Forsøk på å avslutte en turnering med flere grupper aktive.");
43 }
44
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));
54         $ranking++;
55         $already_ordered{$s->{'player'}} = 1;
56 }
57
58 $dbh->commit;
59 $dbh->disconnect;
60
61 ccbs::print_see_other('show-tournament.pl?id=' . $tournament);
62
63 # gives the usual 100, 91, 83, 76. 65, 61, ... series
64 sub points_for_place {
65         my $n = shift;
66         if ($n <= 10) {
67                 return 110 - (21/2) * $n + (1/2) * $n * $n;
68         } elsif ($n <= 65) {
69                 return 65 - $n;
70         } else {
71                 return 0;
72         }
73 }