Passed
Push — master ( c8168c...bff662 )
by John
05:10
created

Submitter::submit()   F

Complexity

Conditions 21
Paths 182

Size

Total Lines 122
Code Lines 96

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 96
nc 182
nop 0
dl 0
loc 122
rs 3.4833
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace App\Babel\Extension\noj;
3
4
use App\Babel\Submit\Curl;
5
use App\Models\OJModel;
6
use App\Models\JudgerModel;
7
use App\Models\ProblemModel;
8
use App\Models\ContestModel;
9
use Illuminate\Support\Facades\Validator;
10
use Requests;
11
12
class Submitter extends Curl
13
{
14
    protected $sub;
15
    public $oid=null;
16
    public $post_data=[];
17
    public $verdictDict=[
18
        -2 => "Compile Error",
19
        -1 => "Wrong Answer",
20
        0 => "Accepted",
21
        1 => "Time Limit Exceed",
22
        2 => "Real Time Limit Exceed",
23
        3 => "Memory Limit Exceed",
24
        4 => "Runtime Error",
25
        5 => "System Error",
26
        6 => "Pending",
27
        7 => "Judging",
28
        8 => "Partially Accepted"
29
    ];
30
    public function __construct(& $sub, $all_data)
31
    {
32
        $this->sub=& $sub;
33
        $this->post_data=$all_data;
34
        $this->oid=OJModel::oid('noj');
35
    }
36
    public function submitJudger($submitURL, $data)
37
    {
38
        $judgeClient=new JudgeClient($data["token"], $submitURL);
39
        return $judgeClient->judge($data["solution"], $data["language"], $data["test_case_id"], [
40
            'output' => false,
41
            'max_cpu_time'=>$data['max_cpu_time'],
42
            'max_memory'=>$data['max_memory'],
43
            'spj_version'=>$data['spj_version'],
44
            'spj_config'=>$data['spj_config']
45
        ]);
46
    }
47
    public function submit()
48
    {
49
        $validator=Validator::make($this->post_data, [
50
            'pid' => 'required|integer',
51
            'coid' => 'required|integer',
52
            'solution' => 'required',
53
        ]);
54
        if ($validator->fails()) {
55
            $this->sub['verdict']="System Error";
56
            return;
57
        }
58
        $judgerModel=new JudgerModel();
59
        $problemModel=new ProblemModel();
60
        $contestModel=new ContestModel();
61
        $bestServer=$judgerModel->server($this->oid);
62
        if (is_null($bestServer)) {
63
            $this->sub['verdict']="Compile Error";
64
            $this->sub['compile_info']="No Available Judger.";
65
            return;
66
        }
67
        $probBasic=$problemModel->basic($this->post_data["pid"]);
68
        $submitURL="http://".$bestServer["host"].":".$bestServer["port"];
69
        $submit_data=[
70
            "solution" => $this->post_data["solution"],
71
            "language" => $this->post_data["lang"],
72
            "max_cpu_time" => $probBasic["time_limit"] * ($this->post_data["lang"]=="java" ? 3 : 1),
73
            "max_memory" => $probBasic["memory_limit"] * 1024,
74
            "test_case_id" => $probBasic["pcode"],
75
            "token" => $bestServer["token"],
76
            "spj_version" => null,
77
            "spj_config" => null
78
        ];
79
        if($probBasic["spj"] && $probBasic["spj_version"]){
80
            $submit_data["spj_version"]=$probBasic["spj_version"];
81
            $submit_data["spj_config"]=$probBasic["spj_lang"];
82
        }
83
        $temp=$this->submitJudger($submitURL, $submit_data);
84
        if (isset($this->post_data["contest"])) {
85
            $this->sub['cid']=$this->post_data["contest"];
86
            if ($contestModel->rule($this->sub['cid'])==2) {
87
                // OI Mode
88
                $this->sub['verdict']="Accepted";
89
                if (!is_null($temp["err"])) {
90
                    if (strpos($temp["data"], 'Compiler runtime error, info: ')!==false) {
91
                        $tempRes=json_decode(explode('Compiler runtime error, info: ', $temp["data"])[1], true);
92
                        $this->sub['verdict']=$this->verdictDict[$tempRes["result"]];
93
                        $this->sub['time']=$tempRes["cpu_time"];
94
                        $this->sub['memory']=round($tempRes["memory"] / 1024);
95
                    } else {
96
                        $this->sub['verdict']="Compile Error";
97
                        $this->sub['time']=0;
98
                        $this->sub['memory']=0;
99
                    }
100
                    return;
101
                }
102
                $this->sub["score"]=count($temp["data"]);
103
                foreach ($temp["data"] as $record) {
104
                    if ($record["result"]) {
105
                        // well... WA or anyway
106
                        $this->sub['verdict']=$this->verdictDict[8];
107
                        $this->sub["score"]--;
108
                    }
109
                }
110
                if ($this->sub["score"]==0) {
111
                    $this->sub['verdict']=$this->verdictDict[$temp["data"][0]["result"]];
112
                    $this->sub['time']=$temp["data"][0]["cpu_time"];
113
                    $this->sub['memory']=round($temp["data"][0]["memory"] / 1024);
114
                    return;
115
                }
116
                $tempMemory=$temp["data"][0]["memory"];
117
                $tempTime=$temp["data"][0]["cpu_time"];
118
                foreach ($temp["data"] as $t) {
119
                    $tempMemory=max($tempMemory, $t["memory"]);
120
                    $tempTime=max($tempTime, $t["cpu_time"]);
121
                }
122
                $this->sub['time']=$tempTime;
123
                $this->sub['memory']=round($tempMemory / 1024);
124
                return;
125
            }
126
        } else {
127
            $this->sub['cid']=null;
128
        }
129
        if (!is_null($temp["err"])) {
130
            if (strpos($temp["data"], 'Compiler runtime error, info: ')!==false) {
131
                $tempRes=json_decode(explode('Compiler runtime error, info: ', $temp["data"])[1], true);
132
                $this->sub['verdict']=$this->verdictDict[$tempRes["result"]];
133
                $this->sub['time']=$tempRes["cpu_time"];
134
                $this->sub['memory']=round($tempRes["memory"] / 1024);
135
            } else {
136
                $this->sub['verdict']=$this->verdictDict["-2"];
137
                $this->sub['time']=0;
138
                $this->sub['memory']=0;
139
                $this->sub['compile_info']=$temp["data"];
140
            }
141
            return;
142
        }
143
        $this->sub["score"]=count($temp["data"]);
144
        foreach ($temp["data"] as $record) {
145
            if ($record["result"]) {
146
                // well... WA or anyway
147
                $this->sub["score"]--;
148
            }
149
        }
150
        foreach ($temp["data"] as $record) {
151
            if ($record["result"]) {
152
                // well... WA or anyway
153
                $this->sub['verdict']=$this->verdictDict[$record["result"]];
154
                $this->sub['time']=$record["cpu_time"];
155
                $this->sub['memory']=round($record["memory"] / 1024);
156
                return;
157
            }
158
        }
159
        $tempMemory=$temp["data"][0]["memory"];
160
        $tempTime=$temp["data"][0]["cpu_time"];
161
        foreach ($temp["data"] as $t) {
162
            $tempMemory=max($tempMemory, $t["memory"]);
163
            $tempTime=max($tempTime, $t["cpu_time"]);
164
        }
165
        $this->sub['verdict']="Accepted";
166
        $this->sub['score']=1;
167
        $this->sub['time']=$tempTime;
168
        $this->sub['memory']=round($tempMemory / 1024);
169
    }
170
}
171