Completed
Push — master ( a4261d...fc5e52 )
by John
14s
created

Submitter   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 115
dl 0
loc 149
rs 10
c 0
b 0
f 0
wmc 21

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A submitJudger() 0 7 1
D submit() 0 116 19
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
        ]);
44
    }
45
    public function submit()
46
    {
47
        $validator=Validator::make($this->post_data, [
48
            'pid' => 'required|integer',
49
            'coid' => 'required|integer',
50
            'solution' => 'required',
51
        ]);
52
        if ($validator->fails()) {
53
            $this->sub['verdict']="System Error";
54
            return;
55
        }
56
        $judgerModel=new JudgerModel();
57
        $problemModel=new ProblemModel();
58
        $contestModel=new ContestModel();
59
        $bestServer=$judgerModel->server(1);
60
        if (is_null($bestServer)) {
61
            $this->sub['verdict']="Compile Error";
62
            $this->sub['compile_info']="No Available Judger.";
63
            return;
64
        }
65
        $probBasic=$problemModel->basic($this->post_data["pid"]);
66
        $submitURL="http://".$bestServer["host"].":".$bestServer["port"];
67
        $submit_data=[
68
            "solution" => $this->post_data["solution"],
69
            "language" => $this->post_data["lang"],
70
            "max_cpu_time" => $probBasic["time_limit"] * ($this->post_data["lang"]=="java" ? 3 : 1),
71
            "max_memory" => $probBasic["memory_limit"] * 1024,
72
            "test_case_id" => $probBasic["pcode"],
73
            "token" => $bestServer["token"]
74
        ];
75
        $temp=$this->submitJudger($submitURL, $submit_data);
76
        if (isset($this->post_data["contest"])) {
77
            $this->sub['cid']=$this->post_data["contest"];
78
            if ($contestModel->rule($this->sub['cid'])==2) {
79
                // OI Mode
80
                $this->sub['verdict']="Accepted";
81
                if (!is_null($temp["err"])) {
82
                    if (strpos($temp["data"], 'Compiler runtime error, info: ')!==false) {
83
                        $tempRes=json_decode(explode('Compiler runtime error, info: ', $temp["data"])[1], true);
84
                        $this->sub['verdict']=$this->verdictDict[$tempRes["result"]];
85
                        $this->sub['time']=$tempRes["cpu_time"];
86
                        $this->sub['memory']=round($tempRes["memory"] / 1024);
87
                    } else {
88
                        $this->sub['verdict']="Compile Error";
89
                        $this->sub['time']=0;
90
                        $this->sub['memory']=0;
91
                    }
92
                    return;
93
                }
94
                $this->sub["score"]=count($temp["data"]);
95
                foreach ($temp["data"] as $record) {
96
                    if ($record["result"]) {
97
                        // well... WA or anyway
98
                        $this->sub['verdict']=$this->verdictDict[8];
99
                        $this->sub["score"]--;
100
                    }
101
                }
102
                if ($this->sub["score"]==0) {
103
                    $this->sub['verdict']=$this->verdictDict[$temp["data"][0]["result"]];
104
                    $this->sub['time']=$temp["data"][0]["cpu_time"];
105
                    $this->sub['memory']=round($temp["data"][0]["memory"] / 1024);
106
                    return;
107
                }
108
                $tempMemory=$temp["data"][0]["memory"];
109
                $tempTime=$temp["data"][0]["cpu_time"];
110
                foreach ($temp["data"] as $t) {
111
                    $tempMemory=max($tempMemory, $t["memory"]);
112
                    $tempTime=max($tempTime, $t["cpu_time"]);
113
                }
114
                $this->sub['time']=$tempTime;
115
                $this->sub['memory']=round($tempMemory / 1024);
116
                return;
117
            }
118
        } else {
119
            $this->sub['cid']=null;
120
        }
121
        if (!is_null($temp["err"])) {
122
            if (strpos($temp["data"], 'Compiler runtime error, info: ')!==false) {
123
                $tempRes=json_decode(explode('Compiler runtime error, info: ', $temp["data"])[1], true);
124
                $this->sub['verdict']=$this->verdictDict[$tempRes["result"]];
125
                $this->sub['time']=$tempRes["cpu_time"];
126
                $this->sub['memory']=round($tempRes["memory"] / 1024);
127
            } else {
128
                $this->sub['verdict']=$this->verdictDict["-2"];
129
                $this->sub['time']=0;
130
                $this->sub['memory']=0;
131
                $this->sub['compile_info']=$temp["data"];
132
            }
133
            return;
134
        }
135
        $this->sub["score"]=count($temp["data"]);
136
        foreach ($temp["data"] as $record) {
137
            if ($record["result"]) {
138
                // well... WA or anyway
139
                $this->sub["score"]--;
140
            }
141
        }
142
        foreach ($temp["data"] as $record) {
143
            if ($record["result"]) {
144
                // well... WA or anyway
145
                $this->sub['verdict']=$this->verdictDict[$record["result"]];
146
                $this->sub['time']=$record["cpu_time"];
147
                $this->sub['memory']=round($record["memory"] / 1024);
148
                return;
149
            }
150
        }
151
        $tempMemory=$temp["data"][0]["memory"];
152
        $tempTime=$temp["data"][0]["cpu_time"];
153
        foreach ($temp["data"] as $t) {
154
            $tempMemory=max($tempMemory, $t["memory"]);
155
            $tempTime=max($tempTime, $t["cpu_time"]);
156
        }
157
        $this->sub['verdict']="Accepted";
158
        $this->sub['score']=1;
159
        $this->sub['time']=$tempTime;
160
        $this->sub['memory']=round($tempMemory / 1024);
161
    }
162
}
163