X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FGruntmaster%2FData.pm;h=023966f71e263315316fe5d1dd76a6d06d47032a;hb=86a97f649d2f98d8a42254f9b33b9f7b5979105f;hp=ce87c254c9b85027fa1d1c7dbeceaa6bd030e8e7;hpb=347ea1e4cea0a3bb98bbcb978b6c8d3296e7707f;p=gruntmaster-data.git diff --git a/lib/Gruntmaster/Data.pm b/lib/Gruntmaster/Data.pm index ce87c25..023966f 100644 --- a/lib/Gruntmaster/Data.pm +++ b/lib/Gruntmaster/Data.pm @@ -34,7 +34,7 @@ sub dynsub{ } 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/gs; dynsub PL_N($rs) => sub { $_[0]->resultset($rsname) }; @@ -55,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; - my $factor = $job->result ? 0 : 1; - $factor = $1 / 100 if $job->result_text =~ /^(\d+ )/s; - $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'; ## no critic (ProhibitNoWarnings) - $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 { ## no critic (ProhibitReverseSortBlock) + 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; @@ -96,7 +94,10 @@ sub user_list { 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 { @@ -116,8 +117,10 @@ 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; + 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, @@ -185,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__