Skip to content

Commit afbe477

Browse files
committed
optimize submit logic
1 parent ebca835 commit afbe477

File tree

1 file changed

+71
-94
lines changed

1 file changed

+71
-94
lines changed

webapp/src/Controller/Team/SubmissionController.php

Lines changed: 71 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -73,111 +73,88 @@ public function createAction(Request $request, ?Problem $problem = null): Respon
7373

7474
$formUpload->handleRequest($request);
7575
$formPaste->handleRequest($request);
76-
if ($formUpload->isSubmitted() && $formUpload->isValid()) {
76+
if ($formUpload->isSubmitted() || $formPaste->isSubmitted()) {
7777
if ($contest === null) {
7878
$this->addFlash('danger', 'No active contest');
7979
} elseif (!$this->dj->checkrole('jury') && !$contest->getFreezeData()->started()) {
8080
$this->addFlash('danger', 'Contest has not yet started');
8181
} else {
82-
/** @var Problem $problem */
83-
$problem = $formUpload->get('problem')->getData();
84-
/** @var Language $language */
85-
$language = $formUpload->get('language')->getData();
86-
/** @var UploadedFile[] $files */
87-
$files = $formUpload->get('code')->getData();
88-
if (!is_array($files)) {
89-
$files = [$files];
90-
}
91-
$entryPoint = $formUpload->get('entry_point')->getData() ?: null;
92-
$submission = $this->submissionService->submitSolution(
93-
$team,
94-
$this->dj->getUser(),
95-
$problem->getProbid(),
96-
$contest,
97-
$language,
98-
$files,
99-
'team page',
100-
null,
101-
null,
102-
$entryPoint,
103-
null,
104-
null,
105-
$message
106-
);
107-
108-
if ($submission) {
109-
$this->addFlash(
110-
'success',
111-
'Submission done! Watch for the verdict in the list below.'
82+
$problem = null;
83+
$language = null;
84+
$files = [];
85+
$entryPoint = null;
86+
$message = '';
87+
88+
if ($formUpload->isSubmitted() && $formUpload->isValid()) {
89+
$problem = $formUpload->get('problem')->getData();
90+
$language = $formUpload->get('language')->getData();
91+
$files = $formUpload->get('code')->getData();
92+
if (!is_array($files)) {
93+
$files = [$files];
94+
}
95+
$entryPoint = $formUpload->get('entry_point')->getData() ?: null;
96+
} elseif ($formPaste->isSubmitted() && $formPaste->isValid()) {
97+
$problem = $formPaste->get('problem')->getData();
98+
$language = $formPaste->get('language')->getData();
99+
$codeContent = $formPaste->get('code_content')->getData();
100+
101+
if ($codeContent == null || empty(trim($codeContent))) {
102+
$this->addFlash('danger', 'No code content provided.');
103+
return $this->redirectToRoute('team_index');
104+
}
105+
106+
$tempDir = sys_get_temp_dir();
107+
$tempFileName = sprintf(
108+
'submission_%s_%s_%s.%s',
109+
$user->getUsername(),
110+
$problem->getName(),
111+
date('Y-m-d_H-i-s'),
112+
$language->getExtensions()[0]
112113
);
113-
} else {
114-
$this->addFlash('danger', $message);
115-
}
116-
return $this->redirectToRoute('team_index');
117-
}
118-
} elseif ($formPaste->isSubmitted() && $formPaste->isValid()) {
119-
if ($contest === null) {
120-
$this->addFlash('danger', 'No active contest');
121-
} elseif (!$this->dj->checkrole('jury') && !$contest->getFreezeData()->started()) {
122-
$this->addFlash('danger', 'Contest has not yet started');
123-
} else {
124-
$problem = $formPaste->get('problem')->getData();
125-
$language = $formPaste->get('language')->getData();
126-
$codeContent = $formPaste->get('code_content')->getData();
127-
if($codeContent == null || empty(trim($codeContent))) {
128-
$this->addFlash('danger','No code content provided.');
129-
return $this->redirectToRoute('team_index');
114+
$tempFileName = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $tempFileName);
115+
$tempFilePath = $tempDir . DIRECTORY_SEPARATOR . $tempFileName;
116+
file_put_contents($tempFilePath, $codeContent);
117+
118+
$uploadedFile = new UploadedFile(
119+
$tempFilePath,
120+
$tempFileName,
121+
'application/octet-stream',
122+
null,
123+
true
124+
);
125+
126+
$files = [$uploadedFile];
127+
$entryPoint = $tempFileName;
130128
}
131-
$tempDir = sys_get_temp_dir();
132-
$tempFileName = sprintf(
133-
'submission_%s_%s_%s.%s',
134-
$user->getUsername(),
135-
$problem->getName(),
136-
date('Y-m-d_H-i-s'),
137-
$language->getExtensions()[0]
138-
);
139-
$tempFileName = preg_replace('/[^a-zA-Z0-9_.-]/', '_', $tempFileName);
140-
$tempFilePath = $tempDir . DIRECTORY_SEPARATOR . $tempFileName;
141-
file_put_contents($tempFilePath, $codeContent);
142-
143-
$uploadedFile = new UploadedFile(
144-
$tempFilePath,
145-
$tempFileName,
146-
'application/octet-stream',
147-
null,
148-
true
149-
);
150-
151-
$files = [$uploadedFile];
152-
$entryPoint = $tempFileName;
153-
$submission = $this->submissionService->submitSolution(
154-
$team,
155-
$this->dj->getUser(),
156-
$problem,
157-
$contest,
158-
$language,
159-
$files,
160-
'team page',
161-
null,
162-
null,
163-
$entryPoint,
164-
null,
165-
null,
166-
$message
167-
);
168-
if ($submission) {
169-
$this->addFlash(
170-
'success',
171-
'Submission done! Watch for the verdict in the list below.'
129+
130+
if ($problem && $language && !empty($files)) {
131+
$submission = $this->submissionService->submitSolution(
132+
$team,
133+
$this->dj->getUser(),
134+
$problem->getProbid(),
135+
$contest,
136+
$language,
137+
$files,
138+
'team page',
139+
null,
140+
null,
141+
$entryPoint,
142+
null,
143+
null,
144+
$message
172145
);
173-
} else {
174-
$this->addFlash('danger', $message);
146+
147+
if ($submission) {
148+
$this->addFlash('success', 'Submission done! Watch for the verdict in the list below.');
149+
} else {
150+
$this->addFlash('danger', $message);
151+
}
152+
153+
return $this->redirectToRoute('team_index');
175154
}
176-
177-
return $this->redirectToRoute('team_index');
178155
}
179156
}
180-
157+
181158
$data = [
182159
'formupload' => $formUpload->createView(),
183160
'formpaste' => $formPaste->createView(),

0 commit comments

Comments
 (0)