-
Notifications
You must be signed in to change notification settings - Fork 0
/
Submitter.php
102 lines (91 loc) · 3.03 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
<?php
namespace App\Babel\Extension\poj;
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('poj');
$judger_list=$judger->list($this->oid);
$this->selectedJudger=$judger_list[array_rand($judger_list)];
}
private function _login()
{
$response=$this->grab_page([
"site"=>'http://poj.org',
"oj"=>'poj',
"handle"=>$this->selectedJudger["handle"]
]);
if (strpos($response, 'Log Out')===false) {
$params=[
'user_id1' => $this->selectedJudger["handle"],
'password1' => $this->selectedJudger["password"],
'B1' => 'login',
];
$this->login([
"url"=>'http://poj.org/login',
"data"=>http_build_query($params),
"oj"=>'poj',
"ret"=>true,
"handle"=>$this->selectedJudger["handle"]
]);
}
}
private function _submit()
{
$this->sub["jid"]=$this->selectedJudger["jid"];
$params=[
'problem_id' => $this->post_data['iid'],
'language' => $this->post_data['lang'],
'source' => base64_encode($this->post_data["solution"]),
'encoded' => 1, // Optional, but sometimes base64 seems smaller than url encode
];
$response=$this->post_data([
"site"=>"http://poj.org/submit",
"data"=>http_build_query($params),
"oj"=>"poj",
"ret"=>true,
"follow"=>false,
"returnHeader"=>true,
"postJson"=>false,
"extraHeaders"=>[],
"handle"=>$this->selectedJudger["handle"]
]);
if (!preg_match('/Location: .*\/status/', $response, $match)) {
$this->sub['verdict']='Submission Error';
} else {
$res=Requests::get('http://poj.org/status?problem_id='.$this->post_data['iid'].'&user_id='.urlencode($this->selectedJudger["handle"]));
if (!preg_match('/<tr align=center><td>(\d+)<\/td>/', $res->body, $match)) {
$this->sub['verdict']='Submission Error';
} else {
$this->sub['remote_id']=$match[1];
}
}
}
public function submit()
{
$validator=Validator::make($this->post_data, [
'pid' => 'required|integer',
'coid' => 'required|integer',
'iid' => 'required|integer',
'solution' => 'required',
]);
if ($validator->fails()) {
$this->sub['verdict']="System Error";
return;
}
$this->_login();
$this->_submit();
}
}