Passed
Pull Request — master (#103)
by Chenyi
04:00
created

HDU::hduSubmit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 0
dl 0
loc 19
rs 9.7998
c 0
b 0
f 0
1
<?php
2
namespace App\Http\Controllers\VirtualJudge\HDU;
3
4
use App\Http\Controllers\VirtualJudge\Curl;
5
use App\Models\CompilerModel;
6
use App\Models\JudgerModel;
7
use Illuminate\Support\Facades\Validator;
8
use Requests;
9
10
class HDU extends Curl
11
{
12
    protected $sub;
13
    public $post_data=[];
14
15
    public function __construct(& $sub, $all_data)
16
    {
17
        $this->sub=& $sub;
18
        $this->post_data=$all_data;
19
        $judger=new JudgerModel();
20
        $judger_list=$judger->list(8);
21
        $this->judgerAccount = $judger_list[array_rand($judger_list)];
0 ignored issues
show
Bug Best Practice introduced by
The property judgerAccount does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
22
    }
23
24
    private function hduLogin()
25
    {
26
        $response=$this->grab_page('http://acm.hdu.edu.cn','hdu');
27
        if (strpos($response, 'Sign In')!==false) {
28
            $params=[
29
                'username' => $this->judgerAccount["handle"],
30
                'userpass' => $this->judgerAccount["password"],
31
                'login' => 'Sign In',
32
            ];
33
            $this->login('http://acm.hdu.edu.cn/userloginex.php?action=login', http_build_query($params), 'hdu');
34
        }
35
    }
36
37
    private function hduSubmit()
38
    {
39
        $params=[
40
            'problemid' => $this->post_data['iid'],
41
            'language' => $this->post_data['lang'],
42
            'usercode' => base64_encode($this->post_data["solution"]),
43
            'submit' => 'Submit',
44
        ];
45
46
        $response=$this->post_data("http://acm.hdu.edu.cn/submit.php", http_build_query($params), "hdu", true, false);
47
48
        if (!strpos('Location: status.php', $response)) {
49
            $this->sub['verdict']='Submission Error';
50
        } else {
51
            $res=Requests::get('http://acm.hdu.edu.cn/status.php?user='.$this->judgerAccount['handle'].'&pid='.$this->post_data['iid']);
52
            if (!preg_match("/<td height=22px>([\s\S]*?)<\/td>/", $res->body, $match)) {
53
                $this->sub['verdict']='Submission Error';
54
            } else {
55
                $this->sub['remote_id']=$match[1];
56
            }
57
        }
58
    }
59
60
    public function submit()
61
    {
62
        $validator=Validator::make($this->post_data, [
63
            'pid' => 'required|integer',
64
            'coid' => 'required|integer',
65
            'iid' => 'required|integer',
66
            'solution' => 'required',
67
        ]);
68
69
        if ($validator->fails()) {
70
            $this->sub['verdict']="System Error";
71
            return;
72
        }
73
74
        $this->hduLogin();
75
        $this->hduSubmit();
76
    }
77
}