|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Facades\DB; |
|
7
|
|
|
|
|
8
|
|
|
class SubmissionModel extends Model |
|
9
|
|
|
{ |
|
10
|
|
|
protected $tableName='submission'; |
|
11
|
|
|
public $colorScheme=[ |
|
12
|
|
|
"Waiting" => "wemd-blue-text", |
|
13
|
|
|
"Judge Error" => "wemd-black-text", |
|
14
|
|
|
"System Error" => "wemd-black-text", |
|
15
|
|
|
"Compile Error" => "wemd-orange-text", |
|
16
|
|
|
"Runtime Error" => "wemd-red-text", |
|
17
|
|
|
"Wrong Answer" => "wemd-red-text", |
|
18
|
|
|
"Time Limit Exceed" => "wemd-deep-purple-text", |
|
19
|
|
|
"Real Time Limit Exceed" => "wemd-deep-purple-text", |
|
20
|
|
|
"Accepted" => "wemd-green-text", |
|
21
|
|
|
"Memory Limit Exceed" => "wemd-deep-purple-text", |
|
22
|
|
|
"Presentation Error" => "wemd-red-text", |
|
23
|
|
|
"Submitted" => "wemd-blue-text", |
|
24
|
|
|
"Pending" => "wemd-blue-text", |
|
25
|
|
|
"Judging" => "wemd-blue-text", |
|
26
|
|
|
"Partially Accepted" => "wemd-cyan-text", |
|
27
|
|
|
'Submission Error' => 'wemd-black-text', |
|
28
|
|
|
'Output Limit Exceeded' => 'wemd-deep-purple-text', |
|
29
|
|
|
"Idleness Limit Exceed" => 'wemd-deep-purple-text' |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
public function insert($sub) |
|
33
|
|
|
{ |
|
34
|
|
|
if (strlen($sub['verdict'])==0) { |
|
35
|
|
|
$sub['verdict']="Judge Error"; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$sid=DB::table($this->tableName)->insertGetId([ |
|
39
|
|
|
'time' => $sub['time'], |
|
40
|
|
|
'verdict' => $sub['verdict'], |
|
41
|
|
|
'solution' => $sub['solution'], |
|
42
|
|
|
'language' => $sub['language'], |
|
43
|
|
|
'submission_date' => $sub['submission_date'], |
|
44
|
|
|
'memory' => $sub['memory'], |
|
45
|
|
|
'uid' => $sub['uid'], |
|
46
|
|
|
'pid' => $sub['pid'], |
|
47
|
|
|
'cid' => $sub['cid'], |
|
48
|
|
|
'color' => $this->colorScheme[$sub['verdict']], |
|
49
|
|
|
'remote_id'=>$sub['remote_id'], |
|
50
|
|
|
'compile_info'=>"", |
|
51
|
|
|
'coid'=>$sub['coid'], |
|
52
|
|
|
'score'=>$sub['score'] |
|
53
|
|
|
]); |
|
54
|
|
|
|
|
55
|
|
|
return $sid; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getJudgeStatus($sid, $uid) |
|
59
|
|
|
{ |
|
60
|
|
|
$status=DB::table($this->tableName)->where(['sid'=>$sid])->first(); |
|
61
|
|
|
if($uid!=$status["uid"]){ |
|
62
|
|
|
$status["solution"]=null; |
|
63
|
|
|
} |
|
64
|
|
|
$compilerModel=new CompilerModel(); |
|
65
|
|
|
$status["lang"]=$compilerModel->detail($status["coid"])["lang"]; |
|
66
|
|
|
return $status; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function downloadCode($sid, $uid) |
|
70
|
|
|
{ |
|
71
|
|
|
$status=DB::table($this->tableName)->where(['sid'=>$sid,'uid'=>$uid])->first(); |
|
72
|
|
|
if($status){ |
|
73
|
|
|
return []; |
|
74
|
|
|
} |
|
75
|
|
|
return [ |
|
76
|
|
|
"content"=>$status["solution"], |
|
77
|
|
|
"name"=>$status["submission_date"].".code" |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getProblemStatus($pid, $uid, $cid=null) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($cid) { |
|
84
|
|
|
$end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]); |
|
85
|
|
|
// Get the very first AC record |
|
86
|
|
|
$ac=DB::table($this->tableName)->where([ |
|
87
|
|
|
'pid'=>$pid, |
|
88
|
|
|
'uid'=>$uid, |
|
89
|
|
|
'cid'=>$cid, |
|
90
|
|
|
'verdict'=>'Accepted' |
|
91
|
|
|
])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first(); |
|
92
|
|
|
if (empty($ac)) { |
|
93
|
|
|
$pac=DB::table($this->tableName)->where([ |
|
94
|
|
|
'pid'=>$pid, |
|
95
|
|
|
'uid'=>$uid, |
|
96
|
|
|
'cid'=>$cid, |
|
97
|
|
|
'verdict'=>'Partially Accepted' |
|
98
|
|
|
])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first(); |
|
99
|
|
|
return empty($pac) ? DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first() : $pac; |
|
100
|
|
|
} else { |
|
101
|
|
|
return $ac; |
|
102
|
|
|
} |
|
103
|
|
|
} else { |
|
104
|
|
|
$ac=DB::table($this->tableName)->where([ |
|
105
|
|
|
'pid'=>$pid, |
|
106
|
|
|
'uid'=>$uid, |
|
107
|
|
|
'cid'=>$cid, |
|
108
|
|
|
'verdict'=>'Accepted' |
|
109
|
|
|
])->orderBy('submission_date', 'desc')->first(); |
|
110
|
|
|
return empty($ac) ? DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->orderBy('submission_date', 'desc')->first() : $ac; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getProblemSubmission($pid, $uid, $cid=null) |
|
115
|
|
|
{ |
|
116
|
|
|
$statusList=DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->orderBy('submission_date', 'desc')->limit(10)->get()->all(); |
|
117
|
|
|
return $statusList; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function countSolution($s) |
|
121
|
|
|
{ |
|
122
|
|
|
return DB::table($this->tableName)->where(['solution'=>$s])->count(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getEarliestSubmission($oid) |
|
126
|
|
|
{ |
|
127
|
|
|
return DB::table($this->tableName) ->join('problem', 'problem.pid', '=', 'submission.pid') |
|
128
|
|
|
->select("sid", "OJ as oid", "remote_id", "cid") |
|
129
|
|
|
->where(['verdict'=>'Waiting','OJ'=>$oid]) |
|
130
|
|
|
->orderBy("sid","asc") |
|
131
|
|
|
->first(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function countEarliestWaitingSubmission($oid) |
|
135
|
|
|
{ |
|
136
|
|
|
$early_sid=$this->getEarliestSubmission($oid); |
|
137
|
|
|
if($early_sid==null) return 0; |
|
138
|
|
|
$early_sid=$early_sid["sid"]; |
|
139
|
|
|
return DB::table($this->tableName) ->join('problem', 'problem.pid', '=', 'submission.pid') |
|
140
|
|
|
->where(['OJ'=>$oid]) |
|
141
|
|
|
->where("sid",">=",$early_sid) |
|
142
|
|
|
->count(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
public function getWaitingSubmission() |
|
147
|
|
|
{ |
|
148
|
|
|
return DB::table($this->tableName) ->join('problem', 'problem.pid', '=', 'submission.pid') |
|
149
|
|
|
->select("sid", "OJ as oid", "remote_id", "cid") |
|
150
|
|
|
->where(['verdict'=>'Waiting']) |
|
151
|
|
|
->get(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function countWaitingSubmission($oid) |
|
155
|
|
|
{ |
|
156
|
|
|
return DB::table($this->tableName) ->join('problem', 'problem.pid', '=', 'submission.pid') |
|
157
|
|
|
->where(['verdict'=>'Waiting', 'OJ'=>$oid]) |
|
158
|
|
|
->count(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function updateSubmission($sid, $sub) |
|
162
|
|
|
{ |
|
163
|
|
|
if (isset($sub['verdict'])) $sub["color"]=$this->colorScheme[$sub['verdict']]; |
|
164
|
|
|
return DB::table($this->tableName)->where(['sid'=>$sid])->update($sub); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function formatSubmitTime($date) |
|
168
|
|
|
{ |
|
169
|
|
|
$periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"]; |
|
170
|
|
|
$lengths=["60", "60", "24", "7", "4.35", "12", "10"]; |
|
171
|
|
|
|
|
172
|
|
|
$now=time(); |
|
173
|
|
|
$unix_date=strtotime($date); |
|
174
|
|
|
|
|
175
|
|
|
if (empty($unix_date)) { |
|
176
|
|
|
return "Bad date"; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if ($now>$unix_date) { |
|
180
|
|
|
$difference=$now-$unix_date; |
|
181
|
|
|
$tense="ago"; |
|
182
|
|
|
} else { |
|
183
|
|
|
$difference=$unix_date-$now; |
|
184
|
|
|
$tense="from now"; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) { |
|
188
|
|
|
$difference/=$lengths[$j]; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$difference=round($difference); |
|
192
|
|
|
|
|
193
|
|
|
if ($difference!=1) { |
|
194
|
|
|
$periods[$j].="s"; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return "$difference $periods[$j] {$tense}"; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function getRecord() |
|
201
|
|
|
{ |
|
202
|
|
|
$paginator=DB::table("submission")->where([ |
|
203
|
|
|
'cid'=>null |
|
204
|
|
|
])->join( |
|
205
|
|
|
"users", |
|
206
|
|
|
"users.id", |
|
207
|
|
|
"=", |
|
208
|
|
|
"submission.uid" |
|
209
|
|
|
)->select( |
|
210
|
|
|
"sid", |
|
211
|
|
|
"uid", |
|
212
|
|
|
"pid", |
|
213
|
|
|
"name", |
|
214
|
|
|
"color", |
|
215
|
|
|
"verdict", |
|
216
|
|
|
"time", |
|
217
|
|
|
"memory", |
|
218
|
|
|
"language", |
|
219
|
|
|
"score", |
|
220
|
|
|
"submission_date" |
|
221
|
|
|
)->orderBy( |
|
222
|
|
|
'submission_date', |
|
223
|
|
|
'desc' |
|
224
|
|
|
)->paginate(50); |
|
225
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
$records= $paginator->all(); |
|
228
|
|
|
foreach ($records as &$r) { |
|
229
|
|
|
$r["submission_date_parsed"]=$this->formatSubmitTime(date('Y-m-d H:i:s', $r["submission_date"])); |
|
230
|
|
|
$r["submission_date"]=date('Y-m-d H:i:s', $r["submission_date"]); |
|
231
|
|
|
$r["nick_name"]=""; |
|
232
|
|
|
} |
|
233
|
|
|
return [ |
|
234
|
|
|
"paginator"=>$paginator, |
|
235
|
|
|
"records"=>$records |
|
236
|
|
|
]; |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|