public function testCoderOfTheMonthCalc()
 {
     $user = UserFactory::createUser();
     $contest = ContestsFactory::createContest();
     $problem = ProblemsFactory::createProblem();
     ContestsFactory::addProblemToContest($problem, $contest);
     ContestsFactory::addUser($contest, $user);
     // Creating 10 AC runs for our user in the last month
     $n = 10;
     $lastMonth = intval(date('m')) - 1;
     $runCreationDate = null;
     if ($lastMonth == 0) {
         $runCreationDate = date(intval(date('Y') - 1) . '-12-01');
     } else {
         $runCreationDate = date('Y-' . $lastMonth . '-01');
     }
     for ($i = 0; $i < $n; $i++) {
         $runData = RunsFactory::createRun($problem, $contest, $user);
         RunsFactory::gradeRun($runData);
         // Force the run to be in last month
         $run = RunsDAO::getByAlias($runData['response']['guid']);
         $run->setTime($runCreationDate);
         RunsDAO::save($run);
     }
     $response = UserController::apiCoderOfTheMonth(new Request());
     $this->assertEquals($user->getUsername(), $response['userinfo']['username']);
 }
Пример #2
0
 public function testRunTotals()
 {
     // Get a problem
     $problemData = ProblemsFactory::createProblem();
     // Get a contest
     $contestData = ContestsFactory::createContest();
     // Add the problem to the contest
     ContestsFactory::addProblemToContest($problemData, $contestData);
     // Create our contestant
     $contestant = UserFactory::createUser();
     // Create a run
     $runData = RunsFactory::createRun($problemData, $contestData, $contestant);
     $runDataOld = RunsFactory::createRun($problemData, $contestData, $contestant);
     $run = RunsDAO::getByAlias($runDataOld['response']['guid']);
     $run->setTime(date('Y-m-d H:i:s', strtotime('-72 hours')));
     RunsDAO::save($run);
     $response = RunController::apiCounts(new Request());
     $this->assertGreaterThan(1, count($response));
 }
Пример #3
0
 /**
  * Given a run id, set a score to a given run
  *
  * @param type $runData
  * @param int $points
  * @param string $verdict
  */
 public static function gradeRun($runData, $points = 1, $verdict = 'AC', $submitDelay = null)
 {
     $run = RunsDAO::getByAlias($runData['response']['guid']);
     $run->setVerdict($verdict);
     $run->setScore($points);
     $run->setContestScore($points * 100);
     $run->setStatus('ready');
     $run->judged_by = 'J1';
     if (!is_null($submitDelay)) {
         $run->submit_delay = $submitDelay;
         $run->penalty = $submitDelay;
     }
     RunsDAO::save($run);
 }
Пример #4
0
 /**
  * Create a new run
  *
  * @param Request $r
  * @return array
  * @throws Exception
  * @throws InvalidDatabaseOperationException
  * @throws InvalidFilesystemOperationException
  */
 public static function apiCreate(Request $r)
 {
     // Init
     self::initializeGrader();
     // Authenticate user
     self::authenticateRequest($r);
     // Validate request
     self::validateCreateRequest($r);
     self::$log->info('New run being submitted!!');
     $response = array();
     if (self::$practice) {
         if (OMEGAUP_LOCKDOWN) {
             throw new ForbiddenAccessException('lockdown');
         }
         $submit_delay = 0;
         $contest_id = null;
         $test = 0;
     } else {
         //check the kind of penalty_type for this contest
         $penalty_type = $r['contest']->penalty_type;
         switch ($penalty_type) {
             case 'contest_start':
                 // submit_delay is calculated from the start
                 // of the contest
                 $start = $r['contest']->getStartTime();
                 break;
             case 'problem_open':
                 // submit delay is calculated from the
                 // time the user opened the problem
                 $opened = ContestProblemOpenedDAO::getByPK($r['contest']->getContestId(), $r['problem']->getProblemId(), $r['current_user_id']);
                 if (is_null($opened)) {
                     //holy moly, he is submitting a run
                     //and he hasnt even opened the problem
                     //what should be done here?
                     throw new NotAllowedToSubmitException('runEvenOpened');
                 }
                 $start = $opened->getOpenTime();
                 break;
             case 'none':
             case 'runtime':
                 //we dont care
                 $start = null;
                 break;
             default:
                 self::$log->error('penalty_type for this contests is not a valid option, asuming `none`.');
                 $start = null;
         }
         if (!is_null($start)) {
             //ok, what time is it now?
             $c_time = time();
             $start = strtotime($start);
             //asuming submit_delay is in minutes
             $submit_delay = (int) (($c_time - $start) / 60);
         } else {
             $submit_delay = 0;
         }
         $contest_id = $r['contest']->getContestId();
         $test = Authorization::IsContestAdmin($r['current_user_id'], $r['contest']) ? 1 : 0;
     }
     // Populate new run object
     $run = new Runs(array('user_id' => $r['current_user_id'], 'problem_id' => $r['problem']->getProblemId(), 'contest_id' => $contest_id, 'language' => $r['language'], 'source' => $r['source'], 'status' => 'new', 'runtime' => 0, 'penalty' => $submit_delay, 'memory' => 0, 'score' => 0, 'contest_score' => $contest_id != null ? 0 : null, 'submit_delay' => $submit_delay, 'guid' => md5(uniqid(rand(), true)), 'verdict' => 'JE', 'test' => $test));
     try {
         // Push run into DB
         RunsDAO::save($run);
         SubmissionLogDAO::save(new SubmissionLog(array('user_id' => $run->user_id, 'run_id' => $run->run_id, 'contest_id' => $run->contest_id, 'ip' => ip2long($_SERVER['REMOTE_ADDR']))));
         // Update submissions counter++
         $r['problem']->setSubmissions($r['problem']->getSubmissions() + 1);
         ProblemsDAO::save($r['problem']);
     } catch (Exception $e) {
         // Operation failed in the data layer
         throw new InvalidDatabaseOperationException($e);
     }
     try {
         // Create file for the run
         $filepath = RunController::getSubmissionPath($run);
         FileHandler::CreateFile($filepath, $r['source']);
     } catch (Exception $e) {
         throw new InvalidFilesystemOperationException($e);
     }
     // Call Grader
     try {
         self::$grader->Grade([$run->guid], false, false);
     } catch (Exception $e) {
         self::$log->error('Call to Grader::grade() failed:');
         self::$log->error($e);
     }
     if (self::$practice) {
         $response['submission_deadline'] = 0;
     } else {
         // Add remaining time to the response
         try {
             $contest_user = ContestsUsersDAO::getByPK($r['current_user_id'], $r['contest']->getContestId());
             if ($r['contest']->getWindowLength() === null) {
                 $response['submission_deadline'] = strtotime($r['contest']->getFinishTime());
             } else {
                 $response['submission_deadline'] = min(strtotime($r['contest']->getFinishTime()), strtotime($contest_user->getAccessTime()) + $r['contest']->getWindowLength() * 60);
             }
         } catch (Exception $e) {
             // Operation failed in the data layer
             throw new InvalidDatabaseOperationException($e);
         }
     }
     // Happy ending
     $response['guid'] = $run->getGuid();
     $response['status'] = 'ok';
     // Expire rank cache
     UserController::deleteProblemsSolvedRankCacheList();
     return $response;
 }
 /**
  * Rejudge problem
  *
  * @param Request $r
  * @throws ApiException
  * @throws InvalidDatabaseOperationException
  */
 public static function apiRejudge(Request $r)
 {
     self::authenticateRequest($r);
     self::validateRejudge($r);
     // We need to rejudge runs after an update, let's initialize the grader
     self::initializeGrader();
     // Call Grader
     $runs = array();
     try {
         $runs = RunsDAO::search(new Runs(array('problem_id' => $r['problem']->getProblemId())));
         $guids = array();
         foreach ($runs as $run) {
             $guids[] = $run->guid;
             $run->setStatus('new');
             $run->setVerdict('JE');
             $run->setScore(0);
             $run->setContestScore(0);
             RunsDAO::save($run);
             // Expire details of the run
             RunController::invalidateCacheOnRejudge($run);
         }
         self::$grader->Grade($guids, true, false);
     } catch (Exception $e) {
         self::$log->error('Failed to rejudge runs after problem update');
         self::$log->error($e);
         throw new InvalidDatabaseOperationException($e);
     }
     $response = array();
     // All clear
     $response['status'] = 'ok';
     return $response;
 }
Пример #6
0
 /**
  * Given a run id, set a score to a given run
  * 
  * @param type $runData
  * @param int $points
  * @param string $verdict
  */
 public static function gradeRun($runData, $points = 1, $verdict = "AC", $submitDelay = null)
 {
     $run = RunsDAO::getByAlias($runData["response"]["guid"]);
     $run->setVerdict($verdict);
     $run->setScore($points);
     $run->setContestScore($points * 100);
     $run->setStatus("ready");
     $run->judged_by = "J1";
     if (!is_null($submitDelay)) {
         $run->submit_delay = $submitDelay;
         $run->penalty = $submitDelay;
     }
     RunsDAO::save($run);
 }