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