]> iEval git - gruntmaster-data.git/blobdiff - lib/Gruntmaster/Data.pm
Write tests
[gruntmaster-data.git] / lib / Gruntmaster / Data.pm
index 846d7721f21748735910c86d7bc7de3e930d29bd..ab0fe36de464f11906686b1134a8bd872db5b8e7 100644 (file)
@@ -1,5 +1,5 @@
 package Gruntmaster::Data;
-use v5.14;
+use 5.014;
 use warnings;
 
 use parent qw/Exporter/;
@@ -15,9 +15,7 @@ use DBIx::Simple;
 use List::Util qw/sum/;
 use SQL::Abstract;
 
-use constant CONTEST_PUBLIC_COLUMNS => [qw/id name description start stop owner/];
 use constant PROBLEM_PUBLIC_COLUMNS => [qw/id author writer level name owner private timeout olimit value/];
-use constant USER_PUBLIC_COLUMNS => [qw/id admin name town university country level/];
 use constant JOBS_PER_PAGE => 50;
 
 my %statements = (
@@ -42,7 +40,7 @@ my %statements = (
        job_full_sth => 'SELECT * FROM jobs WHERE id = ?',
 );
 
-my $db;
+our $db;
 
 sub init {
        $db = DBIx::Simple->new(@_);
@@ -53,7 +51,7 @@ sub purge;
 
 sub query {
        my ($stat, @extra) = @_;
-       $db->query($statements{$stat} // $stat, @extra)
+       $db->query($statements{$stat}, @extra)
 }
 
 my (%name_cache, %name_cache_time);
@@ -72,10 +70,10 @@ sub object_name {
 }
 
 
-sub add_names ($) {
+sub add_names ($) { ## no critic (ProhibitSubroutinePrototypes)
        my ($el) = @_;
        if (ref $el eq 'ARRAY') {
-               &add_names ($_) for @$el
+               &add_names ($_) for @$el ## no critic (ProhibitAmpersandSigils)
        } else {
                for my $object (qw/contest owner problem/) {
                        my $table = $object eq 'owner' ? 'users' : "${object}s";
@@ -86,7 +84,7 @@ sub add_names ($) {
        $el
 }
 
-sub user_list { scalar query('user_list_sth')->hashes }
+sub user_list { +{us => scalar query('user_list_sth')->hashes} }
 
 sub user_entry {
        my ($id) = @_;
@@ -118,8 +116,8 @@ sub problem_list {
 }
 
 sub problem_entry {
-       my ($id, $contest, $user) = @_;
-       $contest &&= contest_entry $contest;
+       my ($id, $contest) = @_;
+       $contest = contest_entry ($contest) if $contest;
        my $ret = add_names query(problem_entry_sth => $id)->hash;
        my $limits = query(limits_sth => $id)->hashes;
        $ret->{limits} = $limits if @$limits;
@@ -127,6 +125,7 @@ sub problem_entry {
        if ($contest) {
                $ret->{contest_start} = $contest->{start};
                $ret->{contest_stop}  = $contest->{stop};
+               delete $ret->{solution}
        }
 
        $ret
@@ -189,7 +188,7 @@ sub job_list {
 sub job_entry {
        my ($id) = @_;
        my $ret = add_names query(job_entry_sth => $id)->hash;
-       $ret->{results} &&= decode_json $ret->{results};
+       $ret->{results} = decode_json $ret->{results} if $ret->{results};
        $ret
 }
 
@@ -208,8 +207,7 @@ sub create_job {
 sub calc_score {
        my ($mxscore, $time, $tries, $totaltime) = @_;
        my $score = $mxscore;
-       $time = 0 if $time < 0;
-       $time = 300 if $time > $totaltime;
+       $time = 300 if $time > $totaltime; # uncoverable branch true does not happen anymore (only possible if opens are broken)
        $score = ($totaltime - $time) / $totaltime * $score;
        $score -= $tries / 10 * $mxscore;
        $score = $mxscore * 3 / 10 if $score < $mxscore * 3 / 10;
@@ -223,7 +221,6 @@ sub standings {
        my @problems = query(contest_problems_sth => $ct->{id})->flat;
        my $pblist = problem_list;
        my %values = query('problem_values_sth')->map;
-#      $values{$_} = $values{$_}->{value} for keys %values;
 
        my (%scores, %tries, %opens);
        my $opens = query(opens_sth => $ct->{id});
@@ -236,7 +233,7 @@ sub standings {
        while (my $job = $jobs->hash) {
                my $open = $opens{$job->{problem}, $job->{owner}} // $ct->{start};
                my $time = $job->{date} - $open;
-               next if $time < 0;
+               next if $time < 0; # uncoverable branch true job sent before contest is deprecated
                my $value = $values{$job->{problem}};
                my $factor = $job->{result} ? 0 : 1;
                $factor = $1 / 100 if $job->{result_text} =~ /^(\d+ )/s;
@@ -262,7 +259,7 @@ sub standings {
 }
 
 sub update_status {
-       my $jobs = $db->select('jobs', 'id,owner,problem,result', {}, 'id');
+       my $jobs = $db->select('jobs', 'id,owner,problem,result', {-not_bool => 'private'}, 'id');
 
        my %hash;
        while ($jobs->into(my ($id, $owner, $problem, $result))) {
This page took 0.029711 seconds and 4 git commands to generate.