From: Marius Gavrilescu Date: Fri, 18 Apr 2014 15:03:58 +0000 (+0300) Subject: Import some changes from the mindcoding branch X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=a122bb9b5954e4384b8f1c3f52bcdda74b421f25;p=plack-app-gruntmaster.git Import some changes from the mindcoding branch --- diff --git a/lib/Gruntmaster/Page/Base.pm b/lib/Gruntmaster/Page/Base.pm index 99ca432..1a68b3c 100644 --- a/lib/Gruntmaster/Page/Base.pm +++ b/lib/Gruntmaster/Page/Base.pm @@ -88,6 +88,7 @@ sub generate{ my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML', use_perl => 1); $self->_generate($htc, $lang, @args); my $out = $htc->output; + utf8::decode($out) for 1 .. 3; utf8::encode($out); my $vary = 'Accept-Language, ' . $self->vary; [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => $vary, 'X-Forever' => 1, 'Cache-Control' => 'max-age=' . $self->max_age], [ $out ] ] diff --git a/lib/Gruntmaster/Page/Generic.pm b/lib/Gruntmaster/Page/Generic.pm index 9e07d4c..15b887e 100644 --- a/lib/Gruntmaster/Page/Generic.pm +++ b/lib/Gruntmaster/Page/Generic.pm @@ -38,6 +38,8 @@ sub list { $rs = $rs->search(undef, {order_by => 'me.id'}) unless $rs->is_ordered; if (my $page = $env->{'gruntmaster.page'}) { my $pages = int ($rs->count / PAGE_SIZE); + $params{default_page} = $page == -1; + $pages = 1 if $pages < 1; $page = $pages if $page == -1; @params{'page', 'pages'} = ($page, $pages); $rs = $rs->search(undef, {offset => ($page - 1) * PAGE_SIZE, ($page == $pages ? () : (rows => PAGE_SIZE))}); @@ -47,7 +49,7 @@ sub list { exists $thing{columns} ? (columns => $thing{columns}) : (), }); my @thing = map +{rs => $_, $_->get_columns}, $rs->all; - @thing = map { $thing{mangle}->(); $_ } @thing if exists $thing{mangle}; + @thing = map { $thing{mangle}->($env); $_ } @thing if exists $thing{mangle}; @thing = grep { $thing{choose}->() } @thing if exists $thing{choose}; @thing = sort { $thing{sortby}->() } @thing if exists $thing{sortby}; $thing{group} //= sub { $thing{id} }; @@ -64,7 +66,8 @@ sub entry { my %thing = %$thing; debug $env => "Rsname is $thing{rsname} and id is $id"; my %params = map {+ rs => $_, $_->get_columns } db($env)->resultset($thing{rsname})->find($id); - $thing{mangle}->(local $_ = \%params) if exists $thing{mangle}; + local $_ = \%params; + $thing{mangle}->($env) if exists $thing{mangle}; wantarray ? %params : \%params } @@ -132,10 +135,11 @@ thing { $_->{owner_name} = $_->{rs}->owner->name; $_->{cansubmit} = $env->{'gruntmaster.contest'} ? time < db($env)->contest($env->{'gruntmaster.contest'})->stop : 1; eval { - db($env)->open->create({ + db($env)->opens->create({ contest => $env->{'gruntmaster.contest'}, problem => $_->{id}, owner => $env->{REMOTE_USER}, + time => time, }) } if $env->{'gruntmaster.contest'} && time >= db($env)->contest($env->{'gruntmaster.contest'})->start; }; @@ -161,6 +165,7 @@ thing { }; sortby { $b->{id} <=> $a->{id}}; mangle { + my $env = shift; $_->{results} &&= decode_json $_->{results}; $_->{owner_name} = $_->{rs}->owner->name; $_->{problem_name} = $_->{rs}->problem->name; @@ -171,5 +176,6 @@ thing { putsym 'Gruntmaster::Page::Pb::Entry::vary', sub { 'Authorization' }; putsym 'Gruntmaster::Page::Pb::Entry::max_age', sub { 600 }; +putsym 'Gruntmaster::Page::Log::max_age', sub { 10 }; 1 diff --git a/lib/Gruntmaster/Page/Register.pm b/lib/Gruntmaster/Page/Register.pm index ed25660..ef2760f 100644 --- a/lib/Gruntmaster/Page/Register.pm +++ b/lib/Gruntmaster/Page/Register.pm @@ -14,7 +14,7 @@ sub generate{ return reply 'All fields are required' if grep { !length } $username, $password, $confirm_password, $name, $email, $phone, $town, $university, $level; pwset $username, $password; - db($env)->create({id => $username, name => $name, email => $email, phone => $phone, town => $town, university => $university, level => $level}); + db($env)->users->create({id => $username, name => $name, email => $email, phone => $phone, town => $town, university => $university, level => $level}); purge "/us/"; reply 'Registered successfully'; diff --git a/lib/Gruntmaster/Page/St.pm b/lib/Gruntmaster/Page/St.pm index 547eaba..9ed2a31 100644 --- a/lib/Gruntmaster/Page/St.pm +++ b/lib/Gruntmaster/Page/St.pm @@ -28,13 +28,16 @@ sub _generate{ my @problems = map { $_->problem } db($env)->contest_problems->search({contest => $ct && $ct->id}, {qw/join problem order_by problem.level/}); my (%scores, %tries); - for my $job (db($env)->jobs->search({contest => $ct && $ct->id})) { + for my $job (db($env)->jobs->search({contest => $ct && $ct->id}, {order_by => 'id'})) { if ($ct) { - my $time = $job->date - $ct->start; + my $open = db($env)->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}; - $scores{$job->owner->id}{$job->problem->id} = $job->result ? 0 : calc_score ($value, $time, $tries{$job->owner}{$job->problem}, $ct->stop - $ct->start); + 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)); $tries{$job->owner->id}{$job->problem->id}++; } else { no warnings 'numeric';