generated from NJUPTAAA/NOJ_Extension_Babel_Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Submitter.php
148 lines (134 loc) · 5.16 KB
/
Submitter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace App\Babel\Extension\codeforces;
use App\Babel\Submit\Curl;
use App\Models\CompilerModel;
use App\Models\JudgerModel;
use App\Models\OJModel;
use App\Models\Submission\SubmissionModel;
use Illuminate\Support\Facades\Validator;
use Requests;
class Submitter extends Curl
{
protected $sub;
public $post_data=[];
protected $oid;
protected $selectedJudger;
public function __construct(& $sub, $all_data)
{
$this->sub=& $sub;
$this->post_data=$all_data;
$judger=new JudgerModel();
$this->oid=OJModel::oid('codeforces');
if(is_null($this->oid)) {
throw new Exception("Online Judge Not Found");
}
$judger_list=$judger->list($this->oid);
$this->selectedJudger=$judger_list[array_rand($judger_list)];
}
private function _login()
{
$response=$this->grab_page([
'site' => 'https://codeforces.com',
'oj' => 'codeforces',
'handle' => $this->selectedJudger["handle"],
]);
if (!(strpos($response, 'Logout')!==false)) {
$response=$this->grab_page([
'site' => 'https://codeforces.com/enter',
'oj' => 'codeforces',
'handle' => $this->selectedJudger["handle"],
]);
$exploded=explode("name='csrf_token' value='", $response);
$token=explode("'/>", $exploded[2])[0];
$params=[
'csrf_token' => $token,
'action' => 'enter',
'ftaa' => '',
'bfaa' => '',
'handleOrEmail' => $this->selectedJudger["handle"], //I wanna kill for handleOrEmail
'password' => $this->selectedJudger["password"],
'remember' => true,
];
$this->login([
'url' => 'https://codeforces.com/enter',
'data' => http_build_query($params),
'oj' => 'codeforces',
'handle' => $this->selectedJudger["handle"],
]);
}
}
private function _submit()
{
$submissionModel=new SubmissionModel();
$s_num=$submissionModel->countSolution($this->post_data["solution"]);
$space='';
for ($i=0; $i<$s_num; $i++) {
$space.=' ';
}
$contestId=$this->post_data["cid"];
$submittedProblemIndex=$this->post_data["iid"];
$programTypeId=$this->post_data["lang"];
$source=($this->post_data["solution"].$space.chr(10));
$response=$this->grab_page([
'site' => "https://codeforces.com/contest/{$this->post_data['cid']}/submit",
'oj' => "codeforces",
'handle' => $this->selectedJudger["handle"],
]);
$exploded=explode("name='csrf_token' value='", $response);
$token=explode("'/>", $exploded[2])[0];
$params=[
'csrf_token' => $token,
'action' => 'submitSolutionFormSubmitted',
'ftaa' => '',
'bfaa' => '',
'submittedProblemIndex' => $submittedProblemIndex,
'programTypeId' => $programTypeId,
'source' => $source,
'tabSize' => 4,
'sourceFile' => '',
];
$response=$this->post_data([
'site' => "https://codeforces.com/contest/{$this->post_data['cid']}/submit?csrf_token=".$token,
'data' => http_build_query($params),
'oj' => "codeforces",
'ret' => true,
'returnHeader' => true,
'handle' => $this->selectedJudger["handle"],
]);
$this->sub["jid"]=$this->selectedJudger["jid"];
if (strpos($response, 'alert("Source code hasn\'t submitted because of warning, please read it.");')!==false) {
$this->sub['verdict']='Compile Error';
preg_match('/<div class="roundbox " style="font-size:1.2rem;margin:0.5em 0;padding:0.5em;text-align:left;background-color:#eca;">[\s\S]*?<div class="roundbox-rb"> <\/div>([\s\S]*?)<div/', $response, $match);
$warning=str_replace('Press button to submit the solution.', '', $match[1]);
$this->sub['compile_info']=trim($warning);
} elseif (substr_count($response, 'My Submissions')!=2) {
// Forbidden?
$exploded=explode('<span class="error for__source">', $response);
if (!isset($exploded[1])) {
$this->sub['verdict']="Submission Error";
} else {
$this->sub['compile_info']=explode("</span>", $exploded[1])[0];
$this->sub['verdict']="Compile Error";
}
} else {
preg_match('/submissionId="(\d+)"/', $response, $match);
$this->sub['remote_id']=$match[1];
}
}
public function submit()
{
$validator=Validator::make($this->post_data, [
'pid' => 'required|integer',
'cid' => 'required|integer',
'coid' => 'required|integer',
'iid' => 'required',
'solution' => 'required',
]);
if ($validator->fails()) {
$this->sub['verdict']="System Error";
return;
}
$this->_login();
$this->_submit();
}
}