generated from NJUPTAAA/NOJ_Extension_Babel_Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubmitter.php
101 lines (90 loc) · 2.91 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
<?php
namespace App\Babel\Extension\noiopen;
use App\Babel\Submit\Curl;
use App\Models\OJModel;
use App\Models\JudgerModel;
use Illuminate\Support\Facades\Validator;
use Requests;
class Submitter extends Curl
{
public $oid=null;
protected $sub;
public $post_data=[];
protected $selectedJudger;
public function __construct(& $sub, $all_data)
{
$this->sub=& $sub;
$this->post_data=$all_data;
$judger=new JudgerModel();
$this->oid=OJModel::oid('noiopen');
$judger_list=$judger->list($this->oid);
$this->selectedJudger=$judger_list[array_rand($judger_list)];
}
private function _login()
{
$response=$this->grab_page([
"site"=>'http://noi.openjudge.cn',
"oj"=>'noiopen',
"handle"=>$this->selectedJudger["handle"]
]);
if (mb_strpos($response, '登出')===false) {
$params=[
'email' => $this->selectedJudger["handle"],
'password' => $this->selectedJudger["password"],
'redirectUrl' => '',
];
$this->login([
"url"=>'http://noi.openjudge.cn/api/auth/login',
"data"=>http_build_query($params),
"oj"=>'noiopen',
"ret"=>true,
"handle"=>$this->selectedJudger["handle"]
]);
}
}
private function _submit()
{
$this->sub["jid"]=$this->selectedJudger["jid"];
$params=[
'contestId' => $this->post_data['cid'],
'problemNumber' => $this->post_data['iid'],
'language' => $this->post_data['lang'],
'source' => base64_encode($this->post_data["solution"]),
'sourceEncode' => 'base64',
];
$response=$this->post_data([
"site"=>"http://noi.openjudge.cn/api/solution/submit",
"data"=>http_build_query($params),
"oj"=>"noiopen",
"ret"=>true,
"follow"=>false,
"returnHeader"=>false,
"postJson"=>false,
"extraHeaders"=>[],
"handle"=>$this->selectedJudger["handle"]
]);
$response=json_decode($response, true);
if ($response["result"]=="ERROR") {
$this->sub['verdict']='Submission Error';
} else {
$submissionURL=$response["redirect"];
$this->sub['remote_id']=explode('/',explode("solution/",$submissionURL)[1])[0];
}
}
public function submit()
{
$validator=Validator::make($this->post_data, [
'pid' => 'required|integer',
'coid' => 'required|integer',
'iid' => 'required',
'cid' => 'required',
'solution' => 'required',
]);
if ($validator->fails()) {
$this->sub['verdict']="System Error";
return;
}
$this->_login();
$this->_submit();
}
}