Submitter::submit()   F
last analyzed

Complexity

Conditions 21
Paths 182

Size

Total Lines 134
Code Lines 106

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 106
c 0
b 0
f 0
nc 182
nop 0
dl 0
loc 134
rs 2.7866

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
            'spj_compile_config'=>$data['spj_compile_config'],
46
            'spj_src'=>$data['spj_src']
47
        ]);
48
    }
49
    public function submit()
50
    {
51
        $validator=Validator::make($this->post_data, [
52
            'pid' => 'required|integer',
53
            'coid' => 'required|integer',
54
            'solution' => 'required',
55
        ]);
56
        if ($validator->fails()) {
57
            $this->sub['verdict']="System Error";
58
            return;
59
        }
60
        $judgerModel=new JudgerModel();
61
        $problemModel=new ProblemModel();
62
        $contestModel=new ContestModel();
63
        $bestServer=$judgerModel->server($this->oid);
64
        if (is_null($bestServer)) {
65
            $this->sub['verdict']="Compile Error";
66
            $this->sub['compile_info']="No Available Judger.";
67
            return;
68
        }
69
        $probBasic=$problemModel->basic($this->post_data["pid"]);
70
        $submitURL="http://".$bestServer["host"].":".$bestServer["port"];
71
        $submit_data=[
72
            "solution" => $this->post_data["solution"],
73
            "language" => $this->post_data["lang"],
74
            "max_cpu_time" => $probBasic["time_limit"] * ($this->post_data["lang"]=="java" ? 3 : 1),
75
            "max_memory" => $probBasic["memory_limit"] * 1024,
76
            "test_case_id" => $probBasic["pcode"],
77
            "token" => $bestServer["token"],
78
            "spj_version" => null,
79
            "spj_config" => null,
80
            "spj_compile_config" => null,
81
            "spj_src" => null
82
        ];
83
        if ($probBasic["spj"] && $probBasic["spj_version"]) {
84
            $submit_data["spj_version"]=$probBasic["spj_version"];
85
            $submit_data["spj_config"]=$probBasic["spj_lang"];
86
            $submit_data["spj_compile_config"]=[
87
                "src_name" => "spj-{spj_version}.c",
88
                "exe_name" => "spj-{spj_version}",
89
                "max_cpu_time" => 3000,
90
                "max_real_time" => 5000,
91
                "max_memory" => 1073741824,
92
                "compile_command" => "/usr/bin/gcc -DONLINE_JUDGE -O2 -w -fmax-errors=3 -std=c99 {src_path} -lm -o {exe_path}"
93
            ]; // fixed at C99, future linked with spj_lang
94
            $submit_data["spj_src"]=$probBasic["spj_src"];
95
        }
96
        $temp=$this->submitJudger($submitURL, $submit_data);
97
        if (isset($this->post_data["contest"])) {
98
            $this->sub['cid']=$this->post_data["contest"];
99
            if ($contestModel->rule($this->sub['cid'])==2) {
100
                // IOI Mode
101
                $this->sub['verdict']="Accepted";
102
                if (!is_null($temp["err"])) {
103
                    if (strpos($temp["data"], 'Compiler runtime error, info: ')!==false) {
104
                        $tempRes=json_decode(explode('Compiler runtime error, info: ', $temp["data"])[1], true);
105
                        $this->sub['verdict']=$this->verdictDict[$tempRes["result"]];
106
                        $this->sub['time']=$tempRes["cpu_time"];
107
                        $this->sub['memory']=round($tempRes["memory"] / 1024);
108
                    } else {
109
                        $this->sub['verdict']=$this->verdictDict["-2"];
110
                        $this->sub['time']=0;
111
                        $this->sub['memory']=0;
112
                        $this->sub['compile_info']=$temp["data"];
113
                    }
114
                    return;
115
                }
116
                $this->sub["score"]=count($temp["data"]);
117
                foreach ($temp["data"] as $record) {
118
                    if ($record["result"]) {
119
                        // well... WA or anyway
120
                        $this->sub['verdict']=$this->verdictDict[8];
121
                        $this->sub["score"]--;
122
                    }
123
                }
124
                if ($this->sub["score"]==0) {
125
                    $this->sub['verdict']=$this->verdictDict[$temp["data"][0]["result"]];
126
                    $this->sub['time']=$temp["data"][0]["cpu_time"];
127
                    $this->sub['memory']=round($temp["data"][0]["memory"] / 1024);
128
                    return;
129
                }
130
                $tempMemory=$temp["data"][0]["memory"];
131
                $tempTime=$temp["data"][0]["cpu_time"];
132
                foreach ($temp["data"] as $t) {
133
                    $tempMemory=max($tempMemory, $t["memory"]);
134
                    $tempTime=max($tempTime, $t["cpu_time"]);
135
                }
136
                $this->sub['time']=$tempTime;
137
                $this->sub['memory']=round($tempMemory / 1024);
138
                return;
139
            }
140
        } else {
141
            $this->sub['cid']=null;
142
        }
143
        if (!is_null($temp["err"])) {
144
            if (strpos($temp["data"], 'Compiler runtime error, info: ')!==false) {
145
                $tempRes=json_decode(explode('Compiler runtime error, info: ', $temp["data"])[1], true);
146
                $this->sub['verdict']=$this->verdictDict[$tempRes["result"]];
147
                $this->sub['time']=$tempRes["cpu_time"];
148
                $this->sub['memory']=round($tempRes["memory"] / 1024);
149
            } else {
150
                $this->sub['verdict']=$this->verdictDict["-2"];
151
                $this->sub['time']=0;
152
                $this->sub['memory']=0;
153
                $this->sub['compile_info']=$temp["data"];
154
            }
155
            return;
156
        }
157
        $this->sub["score"]=count($temp["data"]);
158
        foreach ($temp["data"] as $record) {
159
            if ($record["result"]) {
160
                // well... WA or anyway
161
                $this->sub["score"]--;
162
            }
163
        }
164
        foreach ($temp["data"] as $record) {
165
            if ($record["result"]) {
166
                // well... WA or anyway
167
                $this->sub['verdict']=$this->verdictDict[$record["result"]];
168
                $this->sub['time']=$record["cpu_time"];
169
                $this->sub['memory']=round($record["memory"] / 1024);
170
                return;
171
            }
172
        }
173
        $tempMemory=$temp["data"][0]["memory"];
174
        $tempTime=$temp["data"][0]["cpu_time"];
175
        foreach ($temp["data"] as $t) {
176
            $tempMemory=max($tempMemory, $t["memory"]);
177
            $tempTime=max($tempTime, $t["cpu_time"]);
178
        }
179
        $this->sub['verdict']="Accepted";
180
        // $this->sub['score']=1;
181
        $this->sub['time']=$tempTime;
182
        $this->sub['memory']=round($tempMemory / 1024);
183
    }
184
}
185