From Redis to Postgres - Part 3 (Bugfixes, first try)
[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})) {
32
33 if ($ct) {
34 my $time = $job->date - $ct->start;
35 next if $time < 0;
36 my $value = $job->problem->value // LEVEL_VALUES->{$job->problem->level};
37 $scores{$job->owner->id}{$job->problem->id} = $job->result ? 0 : calc_score ($value, $time, $tries{$job->owner}{$job->problem}, $ct->stop - $ct->start);
38 $tries{$job->owner->id}{$job->problem->id}++;
39 } else {
40 no warnings 'numeric';
41 $scores{$job->owner->id}{$job->problem->id} = 0 + $job->result_text || ($job->result ? 0 : 100)
42 }
43 }
44
45 my @st = sort { $b->{score} <=> $a->{score} or $a->{user}->id cmp $b->{user}->id} map {
46 my $user = $_;
47 +{
48 user => db($env)->user($user),
49 score => sum (values $scores{$user}),
50 scores => [map { $scores{$user}{$_->id} // '-'} @problems],
51 problems => $ct,
52 }
53 } keys %scores;
54
55 $st[0]->{rank} = 1;
56 $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st;
57 $htc->param(problems => \@problems) if $ct;
58 $htc->param(st => \@st);
59 }
60
61 1
This page took 0.025537 seconds and 4 git commands to generate.