1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use App\Models\ContestModel as OutdatedContestModel; |
9
|
|
|
use Cache; |
10
|
|
|
|
11
|
|
|
class ContestModel extends Model |
12
|
|
|
{ |
13
|
|
|
protected $table='contest'; |
14
|
|
|
protected $primaryKey='cid'; |
15
|
|
|
const DELETED_AT=null; |
16
|
|
|
const UPDATED_AT=null; |
17
|
|
|
const CREATED_AT=null; |
18
|
|
|
|
19
|
|
|
//Repository function |
20
|
|
|
public function participants($ignore_frozen = true) |
21
|
|
|
{ |
22
|
|
|
if($this->registration){ |
23
|
|
|
$participants = ContestParticipant::where('cid',$this->cid)->get(); |
24
|
|
|
$participants->load('user'); |
25
|
|
|
$users = new EloquentCollection; |
26
|
|
|
foreach ($participants as $participant) { |
27
|
|
|
$user = $participant->user; |
28
|
|
|
$users->add($user); |
29
|
|
|
} |
30
|
|
|
return $users->unique(); |
31
|
|
|
}else{ |
32
|
|
|
$this->load('submissions.user'); |
33
|
|
|
if($ignore_frozen){ |
34
|
|
|
$frozen_time = $this->frozen_time; |
35
|
|
|
$submissions = $this->submissions()->where('submission_date','<',$frozen_time)->get(); |
36
|
|
|
}else{ |
37
|
|
|
$submissions = $this->submissions; |
38
|
|
|
} |
39
|
|
|
$users = new EloquentCollection; |
40
|
|
|
foreach ($submissions as $submission) { |
41
|
|
|
$user = $submission->user; |
42
|
|
|
$users->add($user); |
43
|
|
|
} |
44
|
|
|
return $users->unique(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Repository/Service? function |
49
|
|
|
public function rankRefresh() |
50
|
|
|
{ |
51
|
|
|
$ret = []; |
52
|
|
|
$participants = $this->participants(); |
53
|
|
|
$contest_problems = $this->problems; |
54
|
|
|
$contest_problems->load('problem'); |
55
|
|
|
if($this->rule == 1){ |
56
|
|
|
// ACM/ICPC Mode |
57
|
|
|
foreach ($participants as $participant) { |
58
|
|
|
$prob_detail=[]; |
59
|
|
|
$totPen=0; |
60
|
|
|
$totScore=0; |
61
|
|
|
foreach ($contest_problems as $contest_problem) { |
62
|
|
|
$prob_stat = $contest_problem->userStatus($participant); |
63
|
|
|
$prob_detail[]=[ |
64
|
|
|
'ncode'=>$contest_problem->ncode, |
65
|
|
|
'pid'=>$contest_problem->pid, |
66
|
|
|
'color'=>$prob_stat['color'], |
67
|
|
|
'wrong_doings'=>$prob_stat['wrong_doings'], |
68
|
|
|
'solved_time_parsed'=>$prob_stat['solved_time_parsed'] |
69
|
|
|
]; |
70
|
|
|
if ($prob_stat['solved']) { |
71
|
|
|
$totPen+=$prob_stat['wrong_doings'] * 20; |
72
|
|
|
$totPen+=$prob_stat['solved_time'] / 60; |
73
|
|
|
$totScore+=$prob_stat['solved']; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
$ret[]=[ |
77
|
|
|
"uid" => $participant->id, |
78
|
|
|
"name" => $participant->name, |
79
|
|
|
"nick_name" => DB::table("group_member")->where([ |
80
|
|
|
"uid" => $participant->id, |
81
|
|
|
"gid" => $this->group->gid |
82
|
|
|
])->where("role", ">", 0)->first()["nick_name"] ?? '', |
83
|
|
|
"score" => $totScore, |
84
|
|
|
"penalty" => $totPen, |
85
|
|
|
"problem_detail" => $prob_detail |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
usort($ret, function ($a, $b) { |
89
|
|
|
if ($a["score"]==$b["score"]) { |
90
|
|
|
if ($a["penalty"]==$b["penalty"]) { |
91
|
|
|
return 0; |
92
|
|
|
} elseif (($a["penalty"]>$b["penalty"])) { |
93
|
|
|
return 1; |
94
|
|
|
} else { |
95
|
|
|
return -1; |
96
|
|
|
} |
97
|
|
|
} elseif ($a["score"]>$b["score"]) { |
98
|
|
|
return -1; |
99
|
|
|
} else { |
100
|
|
|
return 1; |
101
|
|
|
} |
102
|
|
|
}); |
103
|
|
|
Cache::tags(['contest', 'rank'])->put($this->cid, $ret, 60); |
104
|
|
|
return $ret; |
105
|
|
|
}else{ |
106
|
|
|
// IO Mode |
107
|
|
|
$c = new OutdatedContestModel(); |
108
|
|
|
return $c->contestRankCache($this->cid); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function problems() |
113
|
|
|
{ |
114
|
|
|
return $this->hasMany('App\Models\Eloquent\ContestProblem','cid','cid'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function submissions() |
118
|
|
|
{ |
119
|
|
|
return $this->hasMany('App\Models\Eloquent\SubmissionModel','cid','cid'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function group() |
123
|
|
|
{ |
124
|
|
|
return $this->hasOne('App\Models\Eloquent\GroupModel','gid','gid'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getFrozenTimeAttribute() |
128
|
|
|
{ |
129
|
|
|
$end_time = strtotime($this->end_time); |
130
|
|
|
return $end_time - $this->froze_length; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|