Passed
Push — master ( c0817c...061d54 )
by John
03:45
created

SubmissionModel::getRecord()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 57
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 44
nc 16
nop 1
dl 0
loc 57
rs 8.9048
c 0
b 0
f 0

How to fix   Long Method   

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
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\DB;
7
use App\Models\Tool\PastebinModel;
8
9
class SubmissionModel extends Model
10
{
11
    protected $tableName='submission';
12
    protected $table='submission';
13
    protected $primaryKey='sid';
14
    const DELETED_AT=null;
15
    const UPDATED_AT=null;
16
    const CREATED_AT=null;
17
18
    public $colorScheme=[
19
        "Waiting"                => "wemd-blue-text",
20
        "Judge Error"            => "wemd-black-text",
21
        "System Error"           => "wemd-black-text",
22
        "Compile Error"          => "wemd-orange-text",
23
        "Runtime Error"          => "wemd-red-text",
24
        "Wrong Answer"           => "wemd-red-text",
25
        "Time Limit Exceed"      => "wemd-deep-purple-text",
26
        "Real Time Limit Exceed" => "wemd-deep-purple-text",
27
        "Accepted"               => "wemd-green-text",
28
        "Memory Limit Exceed"    => "wemd-deep-purple-text",
29
        "Presentation Error"     => "wemd-red-text",
30
        "Submitted"              => "wemd-blue-text",
31
        "Pending"                => "wemd-blue-text",
32
        "Judging"                => "wemd-blue-text",
33
        "Partially Accepted"     => "wemd-cyan-text",
34
        'Submission Error'       => 'wemd-black-text',
35
        'Output Limit Exceeded'  => 'wemd-deep-purple-text',
36
        "Idleness Limit Exceed"  => 'wemd-deep-purple-text'
37
    ];
38
    public $langConfig=[];
39
40
    public function __construct()
41
    {
42
        $tempLangConfig=[[
43
            "id" => "plaintext",
44
            "extensions" => [".txt", ".gitignore"],
45
            "aliases" => ["Plain Text", "text"],
46
            "mimetypes" => ["text/plain"]
47
        ], [
48
            "id" => "json",
49
            "extensions" => [".json", ".bowerrc", ".jshintrc", ".jscsrc", ".eslintrc", ".babelrc"],
50
            "aliases" => ["JSON", "json"],
51
            "mimetypes" => ["application/json"]
52
        ], [
53
            "id" => "bat",
54
            "extensions" => [".bat", ".cmd"],
55
            "aliases" => ["Batch", "bat"]
56
        ], [
57
            "id" => "coffeescript",
58
            "extensions" => [".coffee"],
59
            "aliases" => ["CoffeeScript", "coffeescript", "coffee"],
60
            "mimetypes" => ["text/x-coffeescript", "text/coffeescript"]
61
        ], [
62
            "id" => "c",
63
            "extensions" => [".c", ".h"],
64
            "aliases" => ["C", "c"]
65
        ], [
66
            "id" => "cpp",
67
            "extensions" => [".cpp", ".cc", ".cxx", ".hpp", ".hh", ".hxx"],
68
            "aliases" => ["C++", "Cpp", "cpp"]
69
        ], [
70
            "id" => "csharp",
71
            "extensions" => [".cs", ".csx", ".cake"],
72
            "aliases" => ["C#", "csharp"]
73
        ], [
74
            "id" => "csp",
75
            "extensions" => [],
76
            "aliases" => ["CSP", "csp"]
77
        ], [
78
            "id" => "css",
79
            "extensions" => [".css"],
80
            "aliases" => ["CSS", "css"],
81
            "mimetypes" => ["text/css"]
82
        ], [
83
            "id" => "dockerfile",
84
            "extensions" => [".dockerfile"],
85
            "filenames" => ["Dockerfile"],
86
            "aliases" => ["Dockerfile"]
87
        ], [
88
            "id" => "fsharp",
89
            "extensions" => [".fs", ".fsi", ".ml", ".mli", ".fsx", ".fsscript"],
90
            "aliases" => ["F#", "FSharp", "fsharp"]
91
        ], [
92
            "id" => "go",
93
            "extensions" => [".go"],
94
            "aliases" => ["Go"]
95
        ], [
96
            "id" => "handlebars",
97
            "extensions" => [".handlebars", ".hbs"],
98
            "aliases" => ["Handlebars", "handlebars"],
99
            "mimetypes" => ["text/x-handlebars-template"]
100
        ], [
101
            "id" => "html",
102
            "extensions" => [".html", ".htm", ".shtml", ".xhtml", ".mdoc", ".jsp", ".asp", ".aspx", ".jshtm"],
103
            "aliases" => ["HTML", "htm", "html", "xhtml"],
104
            "mimetypes" => ["text/html", "text/x-jshtm", "text/template", "text/ng-template"]
105
        ], [
106
            "id" => "ini",
107
            "extensions" => [".ini", ".properties", ".gitconfig"],
108
            "filenames" => ["config", ".gitattributes", ".gitconfig", ".editorconfig"],
109
            "aliases" => ["Ini", "ini"]
110
        ], [
111
            "id" => "java",
112
            "extensions" => [".java", ".jav"],
113
            "aliases" => ["Java", "java"],
114
            "mimetypes" => ["text/x-java-source", "text/x-java"]
115
        ], [
116
            "id" => "javascript",
117
            "extensions" => [".js", ".es6", ".jsx"],
118
            "firstLine" => "^#!.*\\bnode",
119
            "filenames" => ["jakefile"],
120
            "aliases" => ["JavaScript", "javascript", "js"],
121
            "mimetypes" => ["text/javascript"]
122
        ], [
123
            "id" => "less",
124
            "extensions" => [".less"],
125
            "aliases" => ["Less", "less"],
126
            "mimetypes" => ["text/x-less", "text/less"]
127
        ], [
128
            "id" => "lua",
129
            "extensions" => [".lua"],
130
            "aliases" => ["Lua", "lua"]
131
        ], [
132
            "id" => "markdown",
133
            "extensions" => [".md", ".markdown", ".mdown", ".mkdn", ".mkd", ".mdwn", ".mdtxt", ".mdtext"],
134
            "aliases" => ["Markdown", "markdown"]
135
        ], [
136
            "id" => "msdax",
137
            "extensions" => [".dax", ".msdax"],
138
            "aliases" => ["DAX", "MSDAX"]
139
        ], [
140
            "id" => "mysql",
141
            "extensions" => [],
142
            "aliases" => ["MySQL", "mysql"]
143
        ], [
144
            "id" => "objective-c",
145
            "extensions" => [".m"],
146
            "aliases" => ["Objective-C"]
147
        ], [
148
            "id" => "pgsql",
149
            "extensions" => [],
150
            "aliases" => ["PostgreSQL", "postgres", "pg", "postgre"]
151
        ], [
152
            "id" => "php",
153
            "extensions" => [".php", ".php4", ".php5", ".phtml", ".ctp"],
154
            "aliases" => ["PHP", "php"],
155
            "mimetypes" => ["application/x-php"]
156
        ], [
157
            "id" => "postiats",
158
            "extensions" => [".dats", ".sats", ".hats"],
159
            "aliases" => ["ATS", "ATS/Postiats"]
160
        ], [
161
            "id" => "powerquery",
162
            "extensions" => [".pq", ".pqm"],
163
            "aliases" => ["PQ", "M", "Power Query", "Power Query M"]
164
        ], [
165
            "id" => "powershell",
166
            "extensions" => [".ps1", ".psm1", ".psd1"],
167
            "aliases" => ["PowerShell", "powershell", "ps", "ps1"]
168
        ], [
169
            "id" => "pug",
170
            "extensions" => [".jade", ".pug"],
171
            "aliases" => ["Pug", "Jade", "jade"]
172
        ], [
173
            "id" => "python",
174
            "extensions" => [".py", ".rpy", ".pyw", ".cpy", ".gyp", ".gypi"],
175
            "aliases" => ["Python", "py"],
176
            "firstLine" => "^#!/.*\\bpython[0-9.-]*\\b"
177
        ], [
178
            "id" => "r",
179
            "extensions" => [".r", ".rhistory", ".rprofile", ".rt"],
180
            "aliases" => ["R", "r"]
181
        ], [
182
            "id" => "razor",
183
            "extensions" => [".cshtml"],
184
            "aliases" => ["Razor", "razor"],
185
            "mimetypes" => ["text/x-cshtml"]
186
        ], [
187
            "id" => "redis",
188
            "extensions" => [".redis"],
189
            "aliases" => ["redis"]
190
        ], [
191
            "id" => "redshift",
192
            "extensions" => [],
193
            "aliases" => ["Redshift", "redshift"]
194
        ], [
195
            "id" => "ruby",
196
            "extensions" => [".rb", ".rbx", ".rjs", ".gemspec", ".pp"],
197
            "filenames" => ["rakefile"],
198
            "aliases" => ["Ruby", "rb"]
199
        ], [
200
            "id" => "rust",
201
            "extensions" => [".rs", ".rlib"],
202
            "aliases" => ["Rust", "rust"]
203
        ], [
204
            "id" => "sb",
205
            "extensions" => [".sb"],
206
            "aliases" => ["Small Basic", "sb"]
207
        ], [
208
            "id" => "scss",
209
            "extensions" => [".scss"],
210
            "aliases" => ["Sass", "sass", "scss"],
211
            "mimetypes" => ["text/x-scss", "text/scss"]
212
        ], [
213
            "id" => "sol",
214
            "extensions" => [".sol"],
215
            "aliases" => ["sol", "solidity", "Solidity"]
216
        ], [
217
            "id" => "sql",
218
            "extensions" => [".sql"],
219
            "aliases" => ["SQL"]
220
        ], [
221
            "id" => "st",
222
            "extensions" => [".st", ".iecst", ".iecplc", ".lc3lib"],
223
            "aliases" => ["StructuredText", "scl", "stl"]
224
        ], [
225
            "id" => "swift",
226
            "aliases" => ["Swift", "swift"],
227
            "extensions" => [".swift"],
228
            "mimetypes" => ["text/swift"]
229
        ], [
230
            "id" => "typescript",
231
            "extensions" => [".ts", ".tsx"],
232
            "aliases" => ["TypeScript", "ts", "typescript"],
233
            "mimetypes" => ["text/typescript"]
234
        ], [
235
            "id" => "vb",
236
            "extensions" => [".vb"],
237
            "aliases" => ["Visual Basic", "vb"]
238
        ], [
239
            "id" => "xml",
240
            "extensions" => [".xml", ".dtd", ".ascx", ".csproj", ".config", ".wxi", ".wxl", ".wxs", ".xaml", ".svg", ".svgz"],
241
            "firstLine" => "(\\<\\?xml.*)|(\\<svg)|(\\<\\!doctype\\s+svg)",
242
            "aliases" => ["XML", "xml"],
243
            "mimetypes" => ["text/xml", "application/xml", "application/xaml+xml", "application/xml-dtd"]
244
        ], [
245
            "id" => "yaml",
246
            "extensions" => [".yaml", ".yml"],
247
            "aliases" => ["YAML", "yaml", "YML", "yml"],
248
            "mimetypes" => ["application/x-yaml"]
249
        ], [
250
            "id" => "scheme",
251
            "extensions" => [".scm", ".ss", ".sch", ".rkt"],
252
            "aliases" => ["scheme", "Scheme"]
253
        ], [
254
            "id" => "clojure",
255
            "extensions" => [".clj", ".clojure"],
256
            "aliases" => ["clojure", "Clojure"]
257
        ], [
258
            "id" => "shell",
259
            "extensions" => [".sh", ".bash"],
260
            "aliases" => ["Shell", "sh"]
261
        ], [
262
            "id" => "perl",
263
            "extensions" => [".pl"],
264
            "aliases" => ["Perl", "pl"]
265
        ], [
266
            "id" => "azcli",
267
            "extensions" => [".azcli"],
268
            "aliases" => ["Azure CLI", "azcli"]
269
        ], [
270
            "id" => "apex",
271
            "extensions" => [".cls"],
272
            "aliases" => ["Apex", "apex"],
273
            "mimetypes" => ["text/x-apex-source", "text/x-apex"]
274
        ]];
275
        foreach ($tempLangConfig as $t) {
276
            $this->langConfig[$t["id"]]=$t;
277
        }
278
    }
279
280
    public function insert($sub)
281
    {
282
        if (strlen($sub['verdict'])==0) {
283
            $sub['verdict']="Judge Error";
284
        }
285
286
        $sid=DB::table($this->tableName)->insertGetId([
287
            'time' => $sub['time'],
288
            'verdict' => $sub['verdict'],
289
            'solution' => $sub['solution'],
290
            'language' => $sub['language'],
291
            'submission_date' => $sub['submission_date'],
292
            'memory' => $sub['memory'],
293
            'uid' => $sub['uid'],
294
            'pid' => $sub['pid'],
295
            'cid' => $sub['cid'],
296
            'color' => $this->colorScheme[$sub['verdict']],
297
            'remote_id'=>$sub['remote_id'],
298
            'compile_info'=>"",
299
            'coid'=>$sub['coid'],
300
            'score'=>$sub['score']
301
        ]);
302
303
        return $sid;
304
    }
305
306
    public function getJudgeStatus($sid, $uid)
307
    {
308
        $status=DB::table($this->tableName)->where(['sid'=>$sid])->first();
309
        if (empty($status)) {
310
            return [];
311
        }
312
        if ($status["share"]==1 && $status["cid"]) {
313
            $end_time=strtotime(DB::table("contest")->where(["cid"=>$status["cid"]])->select("end_time")->first()["end_time"]);
314
            if (time()<$end_time) {
315
                $status["solution"]=null;
316
            }
317
        }
318
        if ($status["share"]==0 && $status["uid"]!=$uid) {
319
            $status["solution"]=null;
320
        }
321
        $compilerModel=new CompilerModel();
322
        $status["lang"]=$compilerModel->detail($status["coid"])["lang"];
323
        $status["owner"]=$uid==$status["uid"];
324
        return $status;
325
    }
326
327
    public function downloadCode($sid, $uid)
328
    {
329
        $status=DB::table($this->tableName)->where(['sid'=>$sid])->first();
330
        if (empty($status) || ($status["share"]==0 && $status["uid"]!=$uid)) {
331
            return [];
332
        }
333
        $lang=DB::table("compiler")->where(['coid'=>$status["coid"]])->first()["lang"];
334
        $curLang=isset($this->langConfig[$lang]) ? $this->langConfig[$lang] : $this->langConfig["plaintext"];
335
        return [
336
            "content"=>$status["solution"],
337
            "name"=>$status["submission_date"].$curLang["extensions"][0],
338
        ];
339
    }
340
341
    public function getProblemStatus($pid, $uid, $cid=null)
342
    {
343
        if ($cid) {
344
            $end_time=strtotime(DB::table("contest")->where(["cid"=>$cid])->select("end_time")->first()["end_time"]);
345
            // Get the very first AC record
346
            $ac=DB::table($this->tableName)->where([
347
                'pid'=>$pid,
348
                'uid'=>$uid,
349
                'cid'=>$cid,
350
                'verdict'=>'Accepted'
351
            ])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first();
352
            if (empty($ac)) {
353
                $pac=DB::table($this->tableName)->where([
354
                    'pid'=>$pid,
355
                    'uid'=>$uid,
356
                    'cid'=>$cid,
357
                    'verdict'=>'Partially Accepted'
358
                ])->where("submission_date", "<", $end_time)->orderBy('submission_date', 'desc')->first();
359
                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;
360
            } else {
361
                return $ac;
362
            }
363
        } else {
364
            $ac=DB::table($this->tableName)->where([
365
                'pid'=>$pid,
366
                'uid'=>$uid,
367
                'cid'=>$cid,
368
                'verdict'=>'Accepted'
369
            ])->orderBy('submission_date', 'desc')->first();
370
            return empty($ac) ? DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->orderBy('submission_date', 'desc')->first() : $ac;
371
        }
372
    }
373
374
    public function getProblemSubmission($pid, $uid, $cid=null)
375
    {
376
        $statusList=DB::table($this->tableName)->where(['pid'=>$pid, 'uid'=>$uid, 'cid'=>$cid])->orderBy('submission_date', 'desc')->limit(10)->get()->all();
377
        return $statusList;
378
    }
379
380
    public function countSolution($s)
381
    {
382
        return DB::table($this->tableName)->where(['solution'=>$s])->count();
383
    }
384
385
    public function getEarliestSubmission($oid)
386
    {
387
        return DB::table($this->tableName)  ->join('problem', 'problem.pid', '=', 'submission.pid')
388
                                            ->select("sid", "OJ as oid", "remote_id", "cid", "jid")
389
                                            ->where(['verdict'=>'Waiting', 'OJ'=>$oid])
390
                                            ->orderBy("sid", "asc")
391
                                            ->first();
392
    }
393
394
    public function countEarliestWaitingSubmission($oid)
395
    {
396
        $early_sid=$this->getEarliestSubmission($oid);
397
        if ($early_sid==null) {
398
            return 0;
399
        }
400
        $early_sid=$early_sid["sid"];
401
        return DB::table($this->tableName)  ->join('problem', 'problem.pid', '=', 'submission.pid')
402
                                            ->where(['OJ'=>$oid])
403
                                            ->where("sid", ">=", $early_sid)
404
                                            ->count();
405
    }
406
407
408
    public function getWaitingSubmission()
409
    {
410
        return DB::table($this->tableName)  ->join('problem', 'problem.pid', '=', 'submission.pid')
411
                                            ->select("sid", "OJ as oid", "remote_id", "cid", "jid")
412
                                            ->where(['verdict'=>'Waiting'])
413
                                            ->get();
414
    }
415
416
    public function countWaitingSubmission($oid)
417
    {
418
        return DB::table($this->tableName)  ->join('problem', 'problem.pid', '=', 'submission.pid')
419
                                            ->where(['verdict'=>'Waiting', 'OJ'=>$oid])
420
                                            ->count();
421
    }
422
423
    public function updateSubmission($sid, $sub)
424
    {
425
        if (isset($sub['verdict'])) {
426
            $sub["color"]=$this->colorScheme[$sub['verdict']];
427
        }
428
        return DB::table($this->tableName)->where(['sid'=>$sid])->update($sub);
429
    }
430
431
    public function formatSubmitTime($date)
432
    {
433
        $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"];
434
        $lengths=["60", "60", "24", "7", "4.35", "12", "10"];
435
436
        $now=time();
437
        $unix_date=strtotime($date);
438
439
        if (empty($unix_date)) {
440
            return "Bad date";
441
        }
442
443
        if ($now>$unix_date) {
444
            $difference=$now-$unix_date;
445
            $tense="ago";
446
        } else {
447
            $difference=$unix_date-$now;
448
            $tense="from now";
449
        }
450
451
        for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) {
452
            $difference/=$lengths[$j];
453
        }
454
455
        $difference=round($difference);
456
457
        if ($difference!=1) {
458
            $periods[$j].="s";
459
        }
460
461
        return "$difference $periods[$j] {$tense}";
462
    }
463
464
    public function getRecord($filter)
465
    {
466
        $paginator=DB::table("submission")->where([
467
            'cid'=>null
468
        ])->join(
469
            "users",
470
            "users.id",
471
            "=",
472
            "submission.uid"
473
        )->join(
474
            "problem",
475
            "problem.pid",
476
            "=",
477
            "submission.pid"
478
        )->select(
479
            "sid",
480
            "uid",
481
            "problem.pid as pid",
482
            "pcode",
483
            "name",
484
            "color",
485
            "verdict",
486
            "time",
487
            "memory",
488
            "language",
489
            "score",
490
            "submission_date",
491
            "share"
492
        )->orderBy(
493
            'submission_date',
494
            'desc'
495
        );
496
497
        if($filter["pcode"]){
498
            $paginator=$paginator->where(["pcode"=>$filter["pcode"]]);
499
        }
500
501
        if($filter["result"]){
502
            $paginator=$paginator->where(["verdict"=>$filter["result"]]);
503
        }
504
505
        if($filter["account"]){
506
            $paginator=$paginator->where(["name"=>$filter["account"]]);
507
        }
508
509
        $paginator=$paginator->paginate(50);
510
511
512
        $records=$paginator->all();
513
        foreach ($records as &$r) {
514
            $r["submission_date_parsed"]=$this->formatSubmitTime(date('Y-m-d H:i:s', $r["submission_date"]));
515
            $r["submission_date"]=date('Y-m-d H:i:s', $r["submission_date"]);
516
            $r["nick_name"]="";
517
        }
518
        return [
519
            "paginator"=>$paginator,
520
            "records"=>$records
521
        ];
522
    }
523
524
    public function share($sid, $uid)
525
    {
526
        $basic=DB::table($this->tableName)->where(['sid'=>$sid, 'uid'=>$uid])->first();
527
        if (empty($basic)) {
528
            return [];
529
        }
530
        DB::table($this->tableName)->where(['sid'=>$sid])->update([
531
            "share"=>$basic["share"] ? 0 : 1
532
        ]);
533
        return [
534
            "share"=>$basic["share"] ? 0 : 1
535
        ];
536
    }
537
538
    public function sharePB($sid, $uid)
539
    {
540
        $basic=DB::table($this->tableName)->where(['sid'=>$sid, 'uid'=>$uid])->first();
541
        $problem=DB::table("problem")->where(['pid'=>$basic["pid"]])->first();
542
        $compiler=DB::table("compiler")->where(['coid'=>$basic["coid"]])->first();
543
        if (empty($basic)) {
544
            return [];
545
        }
546
        $pastebinModel=new PastebinModel();
547
        $ret=$pastebinModel->generate([
548
            "syntax"=>$compiler["lang"],
549
            "expiration"=>0,
550
            "content"=>$basic["solution"],
551
            "title"=>$problem["pcode"]." - ".$basic["verdict"],
552
            "uid"=>$uid
553
        ]);
554
555
        if (is_null($ret)) {
556
            return [];
557
        } else {
558
            return [
559
                "code" => $ret
560
            ];
561
        }
562
    }
563
}
564