X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FGruntmaster%2FData.pm;h=023966f71e263315316fe5d1dd76a6d06d47032a;hb=86a97f649d2f98d8a42254f9b33b9f7b5979105f;hp=a12e476d8036064cb1c77c708b9a25388e1ba8c4;hpb=91203bd53e7e96ab65976297ea6e95041d5feaac;p=gruntmaster-data.git diff --git a/lib/Gruntmaster/Data.pm b/lib/Gruntmaster/Data.pm index a12e476..023966f 100644 --- a/lib/Gruntmaster/Data.pm +++ b/lib/Gruntmaster/Data.pm @@ -15,7 +15,7 @@ __PACKAGE__->load_namespaces; # Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-03-05 13:11:39 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:dAEmtAexvUaNXLgYz2rNEg -our $VERSION = '5999.000_009'; +our $VERSION = '5999.000_011'; use Lingua::EN::Inflect qw/PL_N/; use JSON::MaybeXS qw/decode_json/; @@ -29,26 +29,19 @@ use constant JOBS_PER_PAGE => 10; sub dynsub{ our ($name, $sub) = @_; - no strict 'refs'; + no strict 'refs'; ## no critic (Strict) *$name = subname $name => $sub } BEGIN { - for my $rs (qw/contest contest_problem job open problem user/) { + for my $rs (qw/contest contest_problem job open problem user problem_status contest_status/) { my $rsname = ucfirst $rs; - $rsname =~ s/_([a-z])/\u$1/g; + $rsname =~ s/_([a-z])/\u$1/gs; dynsub PL_N($rs) => sub { $_[0]->resultset($rsname) }; dynsub $rs => sub { $_[0]->resultset($rsname)->find($_[1]) }; } } -use constant LEVEL_VALUES => { - beginner => 100, - easy => 250, - medium => 500, - hard => 1000, -}; - sub calc_score{ my ($mxscore, $time, $tries, $totaltime) = @_; my $score = $mxscore; @@ -62,32 +55,30 @@ sub calc_score{ sub standings { my ($self, $ct) = @_; - $ct &&= $self->contest($ct); - - my @problems = map { $_->problem } $self->contest_problems->search({contest => $ct && $ct->id}, {qw/join problem order_by problem.level/}); - my (%scores, %tries); - for my $job ($self->jobs->search({contest => $ct && $ct->id}, {order_by => 'id'})) { - if ($ct) { - my $open = $self->opens->find($ct->id, $job->problem->id, $job->owner->id); - my $time = $job->date - ($open ? $open->time : $ct->start); - next if $time < 0; - my $value = $job->problem->value // LEVEL_VALUES->{$job->problem->level}; - my $factor = $job->result ? 0 : 1; - $factor = $1 / 100 if $job->result_text =~ /^(\d+ )/; - $scores{$job->owner->id}{$job->problem->id} = int ($factor * calc_score ($value, $time, $tries{$job->owner->id}{$job->problem->id}++, $ct->stop - $ct->start)); - } else { - no warnings 'numeric'; - $scores{$job->owner->id}{$job->problem->id} = 0 + $job->result_text || ($job->result ? 0 : 100) - } + my $ctobj = $self->contest($ct); + + my @problems = map { $_->rawproblem } $self->contest_problems->search({contest => $ct}, {qw/join problem order_by problem.level/}); + my (%scores, %tries, %opens); + $opens{$_->rawproblem, $_->rawowner} = $_ for $self->opens->search({contest => $ct}); + for my $job ($self->jobs->search({contest => $ct}, {qw/order_by me.id prefetch/ => [qw/problem/]})) { + my $open = $opens{$job->rawproblem, $job->rawowner}; + my $time = $job->date - ($open ? $open->time : $ctobj->start); + next if $time < 0; + my $value = $job->problem->value; + my $factor = $job->result ? 0 : 1; + $factor = $1 / 100 if $job->result_text =~ /^(\d+ )/s; + $scores{$job->rawowner}{$job->rawproblem} = int ($factor * calc_score ($value, $time, $tries{$job->rawowner}{$job->rawproblem}++, $ctobj->stop - $ctobj->start)); } - my @st = sort { $b->{score} <=> $a->{score} or $a->{user}->id cmp $b->{user}->id} map { + my %user_to_name = map { $_ => $_->name } $self->users->all; + + my @st = sort { $b->{score} <=> $a->{score} or $a->{user} cmp $b->{user} } map { ## no critic (ProhibitReverseSortBlock) my $user = $_; +{ - user => $self->user($user), + user => $user, + user_name => $user_to_name{$user}, score => sum (values %{$scores{$user}}), - scores => [map { $scores{$user}{$_->id} // '-'} @problems], - problems => $ct, + scores => [map { $scores{$user}{$_} // '-'} @problems], } } keys %scores; @@ -98,12 +89,15 @@ sub standings { sub user_list { my $rs = $_[0]->users->search(undef, {order_by => 'name', columns => USER_PUBLIC_COLUMNS}); - [ map +{ $_->get_columns }, $rs->all ] + [ map { { $_->get_columns } } $rs->all ] } sub user_entry { my ($self, $id) = @_; - +{ $self->users->find($id, {columns => USER_PUBLIC_COLUMNS})->get_columns } + my $user = $self->users->find($id, {columns => USER_PUBLIC_COLUMNS, prefetch => [qw/problem_statuses contest_statuses/]}); + my @problems = map { {problem => $_->get_column('problem'), solved => $_->solved} } $user->problem_statuses; + my @contests = map { {contest => $_->contest->id, contest_name => $_->contest->name, rank => $_->rank, score => $_->score} } $user->contest_statuses->search(undef, {prefetch => 'contest'}); + +{ $user->get_columns, problems => \@problems, contests => \@contests } } sub problem_list { @@ -123,9 +117,11 @@ sub problem_list { sub problem_entry { my ($self, $id, $contest, $user) = @_; - my $pb = $self->problems->find($id, {columns => PROBLEM_PUBLIC_COLUMNS, prefetch => 'owner'}); my $running = $contest && $self->contest($contest)->is_running; - eval { + my $columns = PROBLEM_PUBLIC_COLUMNS; + push @$columns, 'solution' unless $running; + my $pb = $self->problems->find($id, {columns => $columns, prefetch => 'owner'}); + eval { ## no critic (RequireCheckingReturnValueOfEval) $self->opens->create({ contest => $contest, problem => $id, @@ -133,7 +129,8 @@ sub problem_entry { time => time, }) } if $running; - +{ $pb->get_columns, owner_name => $pb->owner->name, cansubmit => $contest ? $running : 1 } + $contest &&= $self->contest($contest); + +{ $pb->get_columns, owner_name => $pb->owner->name, cansubmit => $contest ? $running : 1, $running ? (contest_start => $contest->start, contest_stop => $contest->stop) : () } } sub contest_list { @@ -191,6 +188,30 @@ sub job_entry { \%params } +sub update_status { + my ($self) = @_; + my @jobs = $self->jobs->search(undef, {cache => 1})->all; + + my %hash; + $hash{$_->get_column('problem'), $_->get_column('owner')} = [$_->id, $_->result ? 0 : 1] for @jobs; + my @problem_statuses = map { [split ($;), @{$hash{$_}} ] } keys %hash; + + my @contest_statuses = map { + my $contest = $_->id; + my @standings = $self->standings($contest); + map { [$contest, $_->{user}, $_->{score}, $_->{rank}] } @standings; + } $self->contests->all; + + my $txn = sub { + $self->problem_statuses->delete; + $self->problem_statuses->populate([[qw/problem owner job solved/], @problem_statuses]); + $self->contest_statuses->delete; + $self->contest_statuses->populate([[qw/contest owner score rank/], @contest_statuses]); + }; + + $self->txn_do($txn); +} + 1; __END__