X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FGruntmaster%2FPage%2FSt.pm;h=2d7b23831f29cb08420ae5d04f90dc1b9d07279c;hb=d3200993969efcd4d9c0ce6a5666a012815ad2d5;hp=f4d961e2dd12b7c586a1baf0a1a302669c77ef75;hpb=5bbf0128012c09c78462ee49b874f968385b0978;p=plack-app-gruntmaster.git diff --git a/lib/Gruntmaster/Page/St.pm b/lib/Gruntmaster/Page/St.pm index f4d961e..2d7b238 100644 --- a/lib/Gruntmaster/Page/St.pm +++ b/lib/Gruntmaster/Page/St.pm @@ -1,69 +1,61 @@ package Gruntmaster::Page::St; -use 5.014000; -use strict; -use warnings; -use parent qw/Exporter/; -our @EXPORT_OK = qw/generate/; -our $VERSION = '0.001'; - -use constant TITLE => 'Standings'; - -use Fcntl qw/:flock/; -use HTML::Template::Compiled; -use IO::File; -use List::Util qw/sum/; -use POSIX qw/strftime/; -use YAML::Any qw/LoadFile/; -use Gruntmaster::Page::Common qw/header footer/; - -my %templates = ( - en => <<'HTML', - - - -
UsernameTotal -
- - - -
-HTML -); - -$templates{$_} = header($_, TITLE) . $templates{$_} for keys %templates; -$templates{$_} .= footer $_ for keys %templates; +use Gruntmaster::Page::Base st => 'Standings'; + +use constant LEVEL_VALUES => { + beginner => 100, + easy => 250, + medium => 500, + hard => 1000, +}; + +sub calc_score{ + my ($mxscore, $time, $tries, $totaltime) = @_; + my $score = $mxscore; + $time = 0 if $time < 0; + $time = 300 if $time > $totaltime; + $score = ($totaltime - $time) / $totaltime * $score; + $score -= $tries / 10 * $mxscore; + $score = $mxscore * 3 / 10 if $score < $mxscore * 3 / 10; + int $score + 0.5 +} -sub generate{ - my ($path, $lang) = @_; - $path =~ s,/st\.html$,,; - my $template = $templates{$lang}; - my $htc = HTML::Template::Compiled->new(scalarref => \$template); - IO::File->new(">$path/meta.yml")->close unless -f "$path/meta.yml"; - flock my $metafh = IO::File->new("<$path/meta.yml"), LOCK_SH; - my $meta = LoadFile "$path/meta.yml"; - my @problems = sort grep { /^\w+$/ } map { s,.*/,,r } <$path/../pb/*>; - my %scores; - for (1 .. $meta->{last}) { - my $meta = LoadFile "$path/$_/meta.yml"; - if ($meta->{result_text} =~ m/^(\d+)/) { - $scores{$meta->{user}}{$meta->{problem}} = $1; - } else { - $scores{$meta->{user}}{$meta->{problem}} = $meta->{result} ? 0 : 100; +sub _generate{ + my ($self, $htc, $lang, $env, $ct) = @_; + debug $env => "language is '$lang' and contest is '$ct'"; + + $ct &&= db($env)->contest($ct); + + my @problems = map { $_->problem } db($env)->contest_problems->search({contest => $ct->id}, {qw/join problem order_by problem.level/}); + my (%scores, %tries); + for my $job (db($env)->jobs->search({contest => $ct->id})) { + + if ($ct) { + my $time = $job->date - $ct->start; + next if $time < 0; + my $value = $job->problem->value // LEVEL_VALUES->{$job->problem->level}; + $scores{$job->owner->id}{$job->problem->id} = $job->result ? 0 : calc_score ($value, $time, $tries{$job->owner}{$job->problem}, $ct->stop - $ct->start); + $tries{$job->owner->id}{$job->problem->id}++; + } else { + no warnings 'numeric'; + $scores{$job->owner->id}{$job->problem->id} = 0 + $job->result_text || ($job->result ? 0 : 100) + } } - } - my @st = sort { $b->{score} <=> $a->{score} } map { - my $user = $_; - +{ - user => $user, - score => sum (values $scores{$user}), - scores => [map { $scores{$user}{$_} // '-'} @problems], - } - } keys %scores; - $htc->param(problems => \@problems); - $htc->param(st => \@st); - $htc->output + my @st = sort { $b->{score} <=> $a->{score} or $a->{user} cmp $b->{user}} map { + my $user = $_; + +{ + user => db($env)->user($user), + score => sum (values $scores{$user}), + scores => [map { $scores{$user}{$_->id} // '-'} @problems], + problems => $ct, + } + } keys %scores; + + $st[0]->{rank} = 1; + $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st; + $htc->param(problems => \@problems) if $ct; + $htc->param(st => \@st); } 1