]>
Commit | Line | Data |
---|---|---|
5bbf0128 MG |
1 | package Gruntmaster::Page::St; |
2 | ||
bb95f538 | 3 | use Gruntmaster::Page::Base st => 'Standings'; |
5bbf0128 | 4 | |
d17951d1 MG |
5 | use constant LEVEL_VALUES => { |
6 | beginner => 100, | |
7 | easy => 250, | |
8 | medium => 500, | |
9 | hard => 1000, | |
10 | }; | |
11 | ||
12 | sub calc_score{ | |
d3200993 | 13 | my ($mxscore, $time, $tries, $totaltime) = @_; |
d17951d1 | 14 | my $score = $mxscore; |
d3200993 MG |
15 | $time = 0 if $time < 0; |
16 | $time = 300 if $time > $totaltime; | |
17 | $score = ($totaltime - $time) / $totaltime * $score; | |
d17951d1 MG |
18 | $score -= $tries / 10 * $mxscore; |
19 | $score = $mxscore * 3 / 10 if $score < $mxscore * 3 / 10; | |
c9e7fdde | 20 | int $score + 0.5 |
d17951d1 MG |
21 | } |
22 | ||
bb95f538 | 23 | sub _generate{ |
dfc00182 MG |
24 | my ($self, $htc, $lang, $env) = @_; |
25 | #debug $env => "language is '$lang'"; | |
5bbf0128 | 26 | |
dfc00182 | 27 | my $ct = $env->{'gruntmaster.contest'} && db($env)->contest($env->{'gruntmaster.contest'}); |
cd9af27e | 28 | |
dfc00182 | 29 | my @problems = map { $_->problem } db($env)->contest_problems->search({contest => $ct && $ct->id}, {qw/join problem order_by problem.level/}); |
d17951d1 | 30 | my (%scores, %tries); |
d0d5bac6 | 31 | for my $job (db($env)->jobs->search({contest => $ct && $ct->id}, {order_by => 'id'})) { |
d3200993 MG |
32 | |
33 | if ($ct) { | |
e8cc4fa4 MG |
34 | my $open = db($env)->open($ct->id, $job->problem->id, $job->owner->id); |
35 | my $time = $job->date - ($open || $ct->start); | |
d3200993 MG |
36 | next if $time < 0; |
37 | my $value = $job->problem->value // LEVEL_VALUES->{$job->problem->level}; | |
38 | $scores{$job->owner->id}{$job->problem->id} = $job->result ? 0 : calc_score ($value, $time, $tries{$job->owner}{$job->problem}, $ct->stop - $ct->start); | |
39 | $tries{$job->owner->id}{$job->problem->id}++; | |
1c0a5167 MG |
40 | } else { |
41 | no warnings 'numeric'; | |
d3200993 | 42 | $scores{$job->owner->id}{$job->problem->id} = 0 + $job->result_text || ($job->result ? 0 : 100) |
cd9af27e | 43 | } |
5bbf0128 | 44 | } |
5bbf0128 | 45 | |
dfc00182 | 46 | my @st = sort { $b->{score} <=> $a->{score} or $a->{user}->id cmp $b->{user}->id} map { |
cd9af27e MG |
47 | my $user = $_; |
48 | +{ | |
d3200993 | 49 | user => db($env)->user($user), |
cd9af27e | 50 | score => sum (values $scores{$user}), |
d3200993 MG |
51 | scores => [map { $scores{$user}{$_->id} // '-'} @problems], |
52 | problems => $ct, | |
cd9af27e MG |
53 | } |
54 | } keys %scores; | |
cb98abf4 MG |
55 | |
56 | $st[0]->{rank} = 1; | |
57 | $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st; | |
d3200993 | 58 | $htc->param(problems => \@problems) if $ct; |
cd9af27e | 59 | $htc->param(st => \@st); |
5bbf0128 MG |
60 | } |
61 | ||
62 | 1 |