Passed
Pull Request — master (#610)
by John
22:48
created

Contest::isJudgingComplete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Models\Eloquent;
4
5
use Illuminate\Database\Eloquent\Model;
6
use App\Models\ProblemModel as OutdatedProblemModel;
7
use Illuminate\Support\Facades\DB;
8
use App\Models\ContestModel as OutdatedContestModel;
9
use Cache;
10
11
class Contest extends Model
12
{
13
    protected $table='contest';
14
    protected $primaryKey='cid';
15
16
    public function getParsedRuleAttribute()
17
    {
18
        $rule=["Unknown", "ICPC", "IOI", "Custom ICPC", "Custom IOI", "HASAAOSE Compulter Exam"];
19
        return $rule[$this->rule];
20
    }
21
22
    public static function boot()
23
    {
24
        parent::boot();
25
        static::saving(function($model) {
26
            if ($model->custom_icon!="" && $model->custom_icon!=null && $model->custom_icon[0]!="/") {
27
                $model->custom_icon="/$model->custom_icon";
28
            }
29
            if ($model->img!="" && $model->img!=null && $model->img[0]!="/") {
30
                $model->img="/$model->img";
31
            }
32
        });
33
    }
34
35
    //Repository function
36
    public function participants($ignore_frozen=true)
37
    {
38
        if ($this->registration) {
39
            $participants=ContestParticipant::where('cid', $this->cid)->get();
40
            $participants->load('user');
41
            $users=collect();
42
            foreach ($participants as $participant) {
43
                $user=$participant->user;
44
                $users->add($user);
45
            }
46
            return $users->unique();
47
        } else {
48
            $this->load('submissions.user');
49
            if ($ignore_frozen) {
50
                $frozen_time=$this->frozen_time;
51
                $submissions=$this->submissions()->where('submission_date', '<', $frozen_time)->get();
52
            } else {
53
                $submissions=$this->submissions;
54
            }
55
            $users=collect();
56
            foreach ($submissions as $submission) {
57
                $user=$submission->user;
58
                $users->add($user);
59
            }
60
            return $users->unique();
61
        }
62
    }
63
64
    // Repository/Service? function
65
    public function rankRefresh()
66
    {
67
        $ret=[];
68
        $participants=$this->participants();
69
        $contest_problems=$this->problems;
70
        $contest_problems->load('problem');
71
        if ($this->rule==1) {
72
            // ACM/ICPC Mode
73
            foreach ($participants as $participant) {
74
                $prob_detail=[];
75
                $totPen=0;
76
                $totScore=0;
77
                foreach ($contest_problems as $contest_problem) {
78
                    $prob_stat=$contest_problem->userStatus($participant);
79
                    $prob_detail[]=[
80
                        'ncode'=>$contest_problem->ncode,
81
                        'pid'=>$contest_problem->pid,
82
                        'color'=>$prob_stat['color'],
83
                        'wrong_doings'=>$prob_stat['wrong_doings'],
84
                        'solved_time_parsed'=>$prob_stat['solved_time_parsed']
85
                    ];
86
                    if ($prob_stat['solved']) {
87
                        $totPen+=$prob_stat['wrong_doings'] * 20;
88
                        $totPen+=$prob_stat['solved_time'] / 60;
89
                        $totScore+=$prob_stat['solved'];
90
                    }
91
                }
92
                $ret[]=[
93
                    "uid" => $participant->id,
94
                    "name" => $participant->name,
95
                    "nick_name" => DB::table("group_member")->where([
96
                        "uid" => $participant->id,
97
                        "gid" => $this->group->gid
98
                    ])->where("role", ">", 0)->first()["nick_name"] ?? '',
99
                    "score" => $totScore,
100
                    "penalty" => $totPen,
101
                    "problem_detail" => $prob_detail
102
                ];
103
            }
104
            usort($ret, function($a, $b) {
105
                if ($a["score"]==$b["score"]) {
106
                    if ($a["penalty"]==$b["penalty"]) {
107
                        return 0;
108
                    } elseif (($a["penalty"]>$b["penalty"])) {
109
                        return 1;
110
                    } else {
111
                        return -1;
112
                    }
113
                } elseif ($a["score"]>$b["score"]) {
114
                    return -1;
115
                } else {
116
                    return 1;
117
                }
118
            });
119
            Cache::tags(['contest', 'rank'])->put($this->cid, $ret, 60);
120
            return $ret;
121
        } else {
122
            // IO Mode
123
            $c=new OutdatedContestModel();
124
            return $c->contestRankCache($this->cid);
125
        }
126
    }
127
128
    public function clarifications()
129
    {
130
        return $this->hasMany('App\Models\Eloquent\ContestClarification', 'cid', 'cid');
131
    }
132
133
    public function problems()
134
    {
135
        return $this->hasMany('App\Models\Eloquent\ContestProblem', 'cid', 'cid');
136
    }
137
138
    public function submissions()
139
    {
140
        return $this->hasMany('App\Models\Eloquent\Submission', 'cid', 'cid');
141
    }
142
143
    public function group()
144
    {
145
        return $this->hasOne('App\Models\Eloquent\Group', 'gid', 'gid');
146
    }
147
148
    public function getFrozenTimeAttribute()
149
    {
150
        $end_time=strtotime($this->end_time);
151
        return $end_time-$this->froze_length;
152
    }
153
154
    public function getIsEndAttribute()
155
    {
156
        return strtotime($this->end_time)<time();
157
    }
158
159
    public static function getProblemSet($cid, $renderLatex=false)
160
    {
161
        $ret=[];
162
        $problemset=ContestProblem::where('cid', $cid)->orderBy('number', 'asc')->get();
163
        foreach ($problemset as $problem) {
164
            $problemDetail=ProblemModel::find($problem->pid);
0 ignored issues
show
Bug introduced by
The type App\Models\Eloquent\ProblemModel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
165
            $problemRet=(new OutdatedProblemModel())->detail($problemDetail->pcode);
166
            if ($renderLatex) {
167
                foreach (['description', 'input', 'output', 'note'] as $section) {
168
                    $problemRet['parsed'][$section]=latex2Image($problemRet['parsed'][$section]);
169
                }
170
            }
171
            $problemRet['index']=$problem->ncode;
172
            $problemRet['testcases']=$problemRet['samples'];
173
            unset($problemRet['samples']);
174
            $ret[]=$problemRet;
175
        }
176
        return $ret;
177
    }
178
179
    public function isJudgingComplete()
180
    {
181
        return $this->submissions->whereIn('verdict', ['Waiting', 'Pending'])->count()==0;
182
    }
183
}
184