-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJudger.php
81 lines (69 loc) · 2.65 KB
/
Judger.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
<?php
namespace App\Babel\Extension\template;//The 'template' should be replaced by the real oj code.
use App\Babel\Submit\Curl;
use App\Models\Submission\SubmissionModel;
use App\Models\JudgerModel;
use Requests;
use Exception;
use Log;
class Judger extends Curl
{
public $verdict=[
'Accepted'=>"Accepted",
"Presentation Error"=>"Presentation Error",
'Time Limit Exceeded'=>"Time Limit Exceed",
"Memory Limit Exceeded"=>"Memory Limit Exceed",
'Wrong Answer'=>"Wrong Answer",
'Runtime Error'=>"Runtime Error",
'Output Limit Exceeded'=>"Output Limit Exceeded",
'Compile Error'=>"Compile Error",
];
private $model=[];
private $template=[];
public function __construct()
{
$this->model["submissionModel"]=new SubmissionModel();
$this->model["judgerModel"]=new JudgerModel();
}
public function judge($row)
{
$sub=[];
if (!isset($this->poj[$row['remote_id']])) {
$judgerDetail=$this->model["judgerModel"]->detail($row['jid']);
$this->appendPOJStatus($judgerDetail['handle'], $row['remote_id']);
if (!isset($this->poj[$row['remote_id']])) {
return;
}
}
$status=$this->poj[$row['remote_id']];
$sub['verdict']=$this->verdict[$status['verdict']];
if ($sub['verdict']=='Compile Error') {
try {
$res=Requests::get('http://poj.org/showcompileinfo?solution_id='.$row['remote_id']);
preg_match('/<pre>([\s\S]*)<\/pre>/', $res->body, $match);
$sub['compile_info']=html_entity_decode($match[1], ENT_QUOTES);
} catch (Exception $e) {
}
}
$sub["score"]=$sub['verdict']=="Accepted" ? 1 : 0;
$sub['time']=$status['time'];
$sub['memory']=$status['memory'];
$sub['remote_id']=$row['remote_id'];
$this->model["submissionModel"]->updateSubmission($row['sid'], $sub);
}
private function appendPOJStatus($judger, $first=null)
{
if ($first!==null) {
$first++;
}
$res=Requests::get("http://poj.org/status?user_id={$judger}&top={$first}");
$rows=preg_match_all('/<tr align=center><td>(\d+)<\/td><td>.*?<\/td><td>.*?<\/td><td>.*?<font color=.*?>(.*?)<\/font>.*?<\/td><td>(\d*)K?<\/td><td>(\d*)(?:MS)?<\/td>/', $res->body, $matches);
for ($i=0; $i<$rows; $i++) {
$this->poj[$matches[1][$i]]=[
'verdict'=>$matches[2][$i],
'memory'=>$matches[3][$i] ? $matches[3][$i] : 0,
'time'=>$matches[4][$i] ? $matches[4][$i] : 0,
];
}
}
}