]> iEval git - gruntmaster-data.git/blobdiff - lib/Gruntmaster/Data.pm
Set @EXPORT_OK to the value of @EXPORT
[gruntmaster-data.git] / lib / Gruntmaster / Data.pm
index 4bd7513bd3e23e7c6206b007931ca3277f0610b8..9613584bf892e2f347f51165841254c291f852db 100644 (file)
@@ -4,7 +4,8 @@ use warnings;
 
 use parent qw/Exporter/;
 our $VERSION = '5999.000_013';
-our @EXPORT = qw/purge db user_list user_entry problem_list problem_entry problem_full contest_list contest_entry contest_full contest_has_problem job_list job_entry job_full create_job standings update_status rerun_job take_job finish_job/; ## no critic (ProhibitAutomaticExportation)
+our @EXPORT = qw/dbinit purge db user_list user_entry problem_list problem_entry contest_list contest_entry contest_has_problem job_list job_entry create_job standings update_status rerun_job take_job finish_job/; ## no critic (ProhibitAutomaticExportation)
+our @EXPORT_OK = @EXPORT;
 
 use JSON::MaybeXS qw/decode_json/;
 use HTTP::Tiny;
@@ -27,18 +28,13 @@ my %statements = (
 
        contest_list_sth => 'SELECT * FROM contest_entry',
        contest_entry_sth => 'SELECT * FROM contest_entry WHERE id = ?',
-       contest_full_sth => 'SELECT * FROM contests WHERE id = ?',
-       contest_problems_sth => 'SELECT problem FROM contest_problems JOIN problems pb ON problem=pb.id WHERE contest = ? ORDER BY pb.value',
        contest_has_problem_sth => 'SELECT EXISTS(SELECT 1 FROM contest_problems WHERE contest = ? AND problem = ?)',
        opens_sth => 'SELECT problem,owner,time FROM opens WHERE contest = ?',
 
        problem_entry_sth => 'SELECT ' . (join ',', @{PROBLEM_PUBLIC_COLUMNS()}, 'statement', 'solution') . ' FROM problems WHERE id = ?',
-       problem_full_sth => 'SELECT * FROM problems WHERE id = ?',
        limits_sth => 'SELECT format,timeout FROM limits WHERE problem = ?',
-       problem_values_sth => 'SELECT id,value FROM problems',
 
        job_entry_sth => 'SELECT * FROM job_entry WHERE id = ?',
-       job_full_sth => 'SELECT * FROM jobs WHERE id = ?',
 
        rerun_job_sth => 'UPDATE jobs SET daemon=NULL,result=-2,result_text=NULL,results=NULL,errors=NULL WHERE id = ?',
        take_job_sth => 'UPDATE jobs SET daemon=? WHERE id = (SELECT id FROM jobs WHERE daemon IS NULL LIMIT 1 FOR UPDATE) RETURNING id',
@@ -47,7 +43,7 @@ my %statements = (
 our $db;
 sub db () { $db }
 
-sub init {
+sub dbinit {
        $db = DBIx::Simple->new(@_);
        $db->keep_statements = 100;
 };
@@ -130,14 +126,10 @@ sub problem_entry {
        $ret
 }
 
-sub problem_full { scalar query(problem_full_sth => $_[0])->hash }
-
 sub contest_list { add_names query('contest_list_sth')->hashes }
 
 sub contest_entry { add_names query(contest_entry_sth => $_[0])->hash }
 
-sub contest_full { scalar query(contest_full_sth => $_[0])->hash }
-
 sub contest_has_problem { query('contest_has_problem_sth', @_[0, 1])->flat }
 
 sub job_list {
@@ -170,8 +162,6 @@ sub job_entry {
        $ret
 }
 
-sub job_full { scalar query(job_full_sth => $_[0])->hash }
-
 sub create_job {
        my (%args) = @_;
        $db->update('users', {lastjob => time}, {id => $args{owner}});
@@ -179,7 +169,7 @@ sub create_job {
        scalar $db->insert('jobs', \%args, {returning => 'id'})->list
 }
 
-sub calc_score {
+sub _calc_score {
        my ($mxscore, $time, $tries, $totaltime) = @_;
        my $score = $mxscore;
        $time = 300 if $time > $totaltime; # uncoverable branch true does not happen anymore (only possible if opens are broken)
@@ -191,12 +181,10 @@ sub calc_score {
 
 sub standings {
        my ($ct) = @_;
+       my @problems = sort { $a->{value} <=> $b->{value} } @{problem_list contest => $ct};
+       my %values = map { $_->{id} => $_->{value} } @problems;
        $ct = contest_entry $ct;
 
-       my @problems = query(contest_problems_sth => $ct->{id})->flat;
-       my $pblist = problem_list;
-       my %values = query('problem_values_sth')->map;
-
        my (%scores, %tries, %opens);
        my $opens = query(opens_sth => $ct->{id});
        while ($opens->into(my ($problem, $owner, $time))) {
@@ -212,7 +200,7 @@ sub standings {
                my $value = $values{$job->{problem}};
                my $factor = $job->{result} ? 0 : 1;
                $factor = $1 / 100 if $job->{result_text} =~ /^(\d+ )/s;
-               $scores{$job->{owner}}{$job->{problem}} = int ($factor * calc_score ($value, $time, $tries{$job->{owner}}{$job->{problem}}++, $ct->{stop} - $ct->{start}));
+               $scores{$job->{owner}}{$job->{problem}} = int ($factor * _calc_score ($value, $time, $tries{$job->{owner}}{$job->{problem}}++, $ct->{stop} - $ct->{start}));
        }
 
        my @st = sort { $b->{score} <=> $a->{score} or $a->{user} cmp $b->{user} } map { ## no critic (ProhibitReverseSortBlock)
@@ -221,16 +209,14 @@ sub standings {
                        user => $user,
                        user_name => object_name(users => $user),
                        score => sum (values %{$scores{$user}}),
-                       scores => [map { $scores{$user}{$_} // '-'} @problems],
+                       scores => [map { $scores{$user}{$_->{id}} // '-'} @problems],
                }
        } keys %scores;
 
        $st[0]->{rank} = 1 if @st;
        $st[$_]->{rank} = $st[$_ - 1]->{rank} + ($st[$_]->{score} < $st[$_ - 1]->{score}) for 1 .. $#st;
-       +{
-               st => \@st,
-               problems => [map { [ $_, object_name(problems => $_)] } @problems],
-       }
+
+       \@st
 }
 
 sub update_status {
@@ -245,7 +231,7 @@ sub update_status {
 
        my @contest_statuses = map {
                my $ct = $_;
-               map { [$ct, $_->{user}, $_->{score}, $_->{rank}] } @{standings($ct)->{st}}
+               map { [$ct, $_->{user}, $_->{score}, $_->{rank}] } @{standings $ct}
        } $db->select('contests', 'id')->flat;
 
        $db->begin;
@@ -266,7 +252,7 @@ sub rerun_job {
 sub take_job {
        my ($daemon) = @_;
        my $id = query(take_job_sth => $daemon)->list;
-       return $id ? job_full $id : undef;
+       return $id ? db->select(jobs => '*', {id => $id})->hash : undef;
 }
 
 sub finish_job {
This page took 0.027321 seconds and 4 git commands to generate.