1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Judge; |
4
|
|
|
|
5
|
|
|
use App\Entities\Solution; |
6
|
|
|
use App\Exceptions\Judger\JudgerCodeInvalid; |
7
|
|
|
use App\Http\Controllers\Controller; |
8
|
|
|
use App\Http\Requests\Judger\JudgerRequest; |
9
|
|
|
use App\Http\Requests\Judger\ReportRequest; |
10
|
|
|
use App\Http\Requests\Request; |
11
|
|
|
use App\Repositories\ProblemRepository; |
12
|
|
|
use App\Services\DataProvider; |
13
|
|
|
use App\Services\JudgerService; |
14
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
15
|
|
|
|
16
|
|
|
class ApiController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** @var \App\Entities\Judger */ |
19
|
|
|
public $judger; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* ApiController constructor. |
23
|
|
|
* |
24
|
|
|
* @param \App\Http\Requests\Judger\JudgerRequest $request |
25
|
|
|
* |
26
|
|
|
* @throws \App\Exceptions\Judger\JudgerCodeInvalid |
27
|
|
|
*/ |
28
|
|
|
public function __construct(JudgerRequest $request) |
29
|
|
|
{ |
30
|
|
|
$request->validate(); |
31
|
|
|
$this->judger = $request->getJudger(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function data(Request $request) |
35
|
|
|
{ |
36
|
|
|
$pid = $request->input('pid'); |
37
|
|
|
app(ProblemRepository::class)->findOrFail($pid); |
38
|
|
|
|
39
|
|
|
try { |
40
|
|
|
/** @var DataProvider $dp */ |
41
|
|
|
$dp = app(DataProvider::class); |
42
|
|
|
|
43
|
|
|
return $dp->getData($pid); |
44
|
|
|
} catch (\LogicException $e) { |
45
|
|
|
return [ |
46
|
|
|
'message' => $e->getMessage(), |
47
|
|
|
]; |
48
|
|
|
} catch (FileNotFoundException $e) { |
49
|
|
|
return [ |
50
|
|
|
'message' => $e->getMessage(), |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function report(ReportRequest $request) |
56
|
|
|
{ |
57
|
|
|
/** @var Solution $solution */ |
58
|
|
|
$solution = Solution::query()->find($request->getSolutionId()); |
59
|
|
|
if ($solution) { |
|
|
|
|
60
|
|
|
$solution->result = $request->input('status'); |
61
|
|
|
if ($request->input('memory_cost')) { |
62
|
|
|
$solution->memory_cost = $request->input('memory_cost'); |
63
|
|
|
} |
64
|
|
|
if ($request->input('time_cost')) { |
65
|
|
|
$solution->time_cost = $request->input('time_cost'); |
66
|
|
|
} |
67
|
|
|
if ($solution->isRuntimeError() && $request->input('runtime_info')) { |
68
|
|
|
if ($info = $solution->runtimeInfo) { |
69
|
|
|
$info->delete(); |
70
|
|
|
} |
71
|
|
|
$solution->runtimeInfo()->create(['content' => $request->input('runtime_info')]); |
72
|
|
|
} |
73
|
|
|
if ($solution->isCompileError() && $request->input('compile_info')) { |
74
|
|
|
if ($info = $solution->compileInfo) { |
75
|
|
|
$info->delete(); |
76
|
|
|
} |
77
|
|
|
$solution->compileInfo()->create(['content' => $request->input('compile_info')]); |
78
|
|
|
} |
79
|
|
|
$solution->save(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return [ |
83
|
|
|
'code' => 0, |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function heartbeat() |
88
|
|
|
{ |
89
|
|
|
return ''; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function beat() |
93
|
|
|
{ |
94
|
|
|
$redis = app('redis'); |
95
|
|
|
if ($redis && $this->judger) { |
96
|
|
|
$key = implode(':', ['judger', 'beat']); |
97
|
|
|
$redis->hset($key, $this->judger->id, time()); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|