Skip to content

Commit

Permalink
optimize submit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
as6325400 committed Oct 1, 2024
1 parent ebca835 commit afbe477
Showing 1 changed file with 71 additions and 94 deletions.
165 changes: 71 additions & 94 deletions webapp/src/Controller/Team/SubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,111 +73,88 @@ public function createAction(Request $request, ?Problem $problem = null): Respon

$formUpload->handleRequest($request);
$formPaste->handleRequest($request);
if ($formUpload->isSubmitted() && $formUpload->isValid()) {
if ($formUpload->isSubmitted() || $formPaste->isSubmitted()) {
if ($contest === null) {
$this->addFlash('danger', 'No active contest');
} elseif (!$this->dj->checkrole('jury') && !$contest->getFreezeData()->started()) {
$this->addFlash('danger', 'Contest has not yet started');
} else {
/** @var Problem $problem */
$problem = $formUpload->get('problem')->getData();
/** @var Language $language */
$language = $formUpload->get('language')->getData();
/** @var UploadedFile[] $files */
$files = $formUpload->get('code')->getData();
if (!is_array($files)) {
$files = [$files];
}
$entryPoint = $formUpload->get('entry_point')->getData() ?: null;
$submission = $this->submissionService->submitSolution(
$team,
$this->dj->getUser(),
$problem->getProbid(),
$contest,
$language,
$files,
'team page',
null,
null,
$entryPoint,
null,
null,
$message
);

if ($submission) {
$this->addFlash(
'success',
'Submission done! Watch for the verdict in the list below.'
$problem = null;
$language = null;
$files = [];
$entryPoint = null;
$message = '';

if ($formUpload->isSubmitted() && $formUpload->isValid()) {
$problem = $formUpload->get('problem')->getData();
$language = $formUpload->get('language')->getData();
$files = $formUpload->get('code')->getData();
if (!is_array($files)) {
$files = [$files];
}
$entryPoint = $formUpload->get('entry_point')->getData() ?: null;
} elseif ($formPaste->isSubmitted() && $formPaste->isValid()) {
$problem = $formPaste->get('problem')->getData();
$language = $formPaste->get('language')->getData();
$codeContent = $formPaste->get('code_content')->getData();

if ($codeContent == null || empty(trim($codeContent))) {
$this->addFlash('danger', 'No code content provided.');
return $this->redirectToRoute('team_index');
}

$tempDir = sys_get_temp_dir();
$tempFileName = sprintf(
'submission_%s_%s_%s.%s',
$user->getUsername(),
$problem->getName(),
date('Y-m-d_H-i-s'),
$language->getExtensions()[0]
);
} else {
$this->addFlash('danger', $message);
}
return $this->redirectToRoute('team_index');
}
} elseif ($formPaste->isSubmitted() && $formPaste->isValid()) {
if ($contest === null) {
$this->addFlash('danger', 'No active contest');
} elseif (!$this->dj->checkrole('jury') && !$contest->getFreezeData()->started()) {
$this->addFlash('danger', 'Contest has not yet started');
} else {
$problem = $formPaste->get('problem')->getData();
$language = $formPaste->get('language')->getData();
$codeContent = $formPaste->get('code_content')->getData();
if($codeContent == null || empty(trim($codeContent))) {
$this->addFlash('danger','No code content provided.');
return $this->redirectToRoute('team_index');
$tempFileName = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $tempFileName);
$tempFilePath = $tempDir . DIRECTORY_SEPARATOR . $tempFileName;
file_put_contents($tempFilePath, $codeContent);

$uploadedFile = new UploadedFile(
$tempFilePath,
$tempFileName,
'application/octet-stream',
null,
true
);

$files = [$uploadedFile];
$entryPoint = $tempFileName;
}
$tempDir = sys_get_temp_dir();
$tempFileName = sprintf(
'submission_%s_%s_%s.%s',
$user->getUsername(),
$problem->getName(),
date('Y-m-d_H-i-s'),
$language->getExtensions()[0]
);
$tempFileName = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $tempFileName);
$tempFilePath = $tempDir . DIRECTORY_SEPARATOR . $tempFileName;
file_put_contents($tempFilePath, $codeContent);

$uploadedFile = new UploadedFile(
$tempFilePath,
$tempFileName,
'application/octet-stream',
null,
true
);

$files = [$uploadedFile];
$entryPoint = $tempFileName;
$submission = $this->submissionService->submitSolution(
$team,
$this->dj->getUser(),
$problem,
$contest,
$language,
$files,
'team page',
null,
null,
$entryPoint,
null,
null,
$message
);
if ($submission) {
$this->addFlash(
'success',
'Submission done! Watch for the verdict in the list below.'

if ($problem && $language && !empty($files)) {
$submission = $this->submissionService->submitSolution(
$team,
$this->dj->getUser(),
$problem->getProbid(),
$contest,
$language,
$files,
'team page',
null,
null,
$entryPoint,
null,
null,
$message
);
} else {
$this->addFlash('danger', $message);

if ($submission) {
$this->addFlash('success', 'Submission done! Watch for the verdict in the list below.');
} else {
$this->addFlash('danger', $message);
}

return $this->redirectToRoute('team_index');
}

return $this->redirectToRoute('team_index');
}
}

$data = [
'formupload' => $formUpload->createView(),
'formpaste' => $formPaste->createView(),
Expand Down

0 comments on commit afbe477

Please sign in to comment.