+ sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) },
+ sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}), 60 },
+ sub (/log/:job) {
+ forbid job->private;
+ response log_entry => "Job $_{job}", db->job_entry($_{job}), 10
+ },
+ sub (/pb/:problem + ?contest~) {
+ my (undef, undef, $contest) = @_;
+ $_{contest} = $contest;
+ return NOT_FOUND if contest && !db->contest_problems->find($_{contest}, $_{problem});
+ forbid problem->private && !contest;
+ if (contest) {
+ return redirect "/pb/$_{problem}" unless contest->is_running;
+ forbid !remote_user;
+ $privacy = 'private';
+ }
+ response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id), $_{contest} ? 10 : ();
+ },
+ sub (/sol/:problem) {
+ forbid problem->private;
+ response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
+ },
+
+ sub (/) { redispatch_to '/index' },
+ sub (/favicon.ico) { redirect '/static/favicon.ico' },
+ sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1, Vary => 'X-Static'], [render_article $_{article}, 'en', maybe static => $env->{HTTP_X_STATIC}]] }
+ },
+
+ sub (POST) {
+ sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
+ my (undef, undef, $prog) = @_;
+ forbid !remote_user;
+ my $private = (problem->private && !contest) ? 1 : 0;
+ return reply 'This contest has finished' if contest && contest->is_finished;
+ return reply 'This contest has not yet started' if contest && contest->is_pending;
+ return reply 'This problem is private' if !admin && $private;
+ return reply 'This problem does not belong to this contest' if contest && !db->contest_problems->find($_{contest}, $_{problem});
+ return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
+ return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
+ remote_user->update({lastjob => time});
+
+ my $source = $prog ? read_file $prog->path : $_{source_code};
+ unlink $prog->path if $prog;
+ my $newjob = db->jobs->create({
+ maybe contest => $_{contest},
+ private => $private,
+ date => time,
+ extension => FORMAT_EXTENSION->{$_{prog_format}},
+ format => $_{prog_format},
+ problem => $_{problem},
+ source => $source,
+ owner => remote_user->id,
+ });
+
+ [303, [Location => '/log/' . $newjob->id], []]
+ },
+ }