|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xoco70\KendoTournaments\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
|
7
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
8
|
|
|
use Illuminate\Foundation\Auth\User; |
|
9
|
|
|
use Xoco70\KendoTournaments\Contracts\TreeGenerable; |
|
10
|
|
|
use Xoco70\KendoTournaments\TreeGen\DirectEliminationCompetitorTreeGen; |
|
11
|
|
|
use Xoco70\KendoTournaments\TreeGen\DirectEliminationTeamTreeGen; |
|
12
|
|
|
use Xoco70\KendoTournaments\TreeGen\PlayOffCompetitorTreeGen; |
|
13
|
|
|
use Xoco70\KendoTournaments\TreeGen\PlayOffTeamTreeGen; |
|
14
|
|
|
use Xoco70\KendoTournaments\TreeGen\TreeGen; |
|
15
|
|
|
|
|
16
|
|
|
class Championship extends Model |
|
17
|
|
|
{ |
|
18
|
|
|
use SoftDeletes; |
|
19
|
|
|
protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
20
|
|
|
protected $table = 'championship'; |
|
21
|
|
|
|
|
22
|
|
|
public $timestamps = true; |
|
23
|
|
|
protected $fillable = [ |
|
24
|
|
|
'tournament_id', |
|
25
|
|
|
'category_id', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
protected static function boot() |
|
29
|
|
|
{ |
|
30
|
|
|
parent::boot(); |
|
31
|
|
|
|
|
32
|
|
|
static::deleting(function ($championship) { |
|
33
|
|
|
$championship->competitors()->delete(); |
|
34
|
|
|
$championship->settings()->delete(); |
|
35
|
|
|
}); |
|
36
|
|
|
static::restoring(function ($championship) { |
|
37
|
|
|
$championship->competitors()->restore(); |
|
38
|
|
|
$championship->settings()->restore(); |
|
39
|
|
|
}); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* A championship has many Competitors. |
|
44
|
|
|
* |
|
45
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
46
|
|
|
*/ |
|
47
|
|
|
public function competitors() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->hasMany(Competitor::class); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* A championship belongs to a Category. |
|
54
|
|
|
* |
|
55
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
56
|
|
|
*/ |
|
57
|
|
|
public function category() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->belongsTo(Category::class); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* A championship belongs to a Tournament. |
|
64
|
|
|
* |
|
65
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
66
|
|
|
*/ |
|
67
|
|
|
public function tournament() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->belongsTo(Tournament::class); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get All competitors from a Championships. |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
76
|
|
|
*/ |
|
77
|
|
|
public function users() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->belongsToMany(User::class, 'competitor', 'championship_id') |
|
80
|
|
|
->withPivot('confirmed') |
|
81
|
|
|
->withTimestamps(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* A championship only has 1 Settings. |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
|
88
|
|
|
*/ |
|
89
|
|
|
public function settings() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->hasOne(ChampionshipSettings::class); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* A championship has Many Teams. |
|
96
|
|
|
* |
|
97
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
98
|
|
|
*/ |
|
99
|
|
|
public function teams() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->hasMany(Team::class); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Check if Championship has Preliminary Round Configured. |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
|
|
public function hasPreliminary() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->settings == null || $this->settings->hasPreliminary; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Check if 2nd Round of Championship is Round Robin. |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
public function isPlayOffType() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->settings != null && $this->settings->treeType == ChampionshipSettings::PLAY_OFF; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Check if 2nd Round of Championship is Direct Elimination. |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
|
|
public function isDirectEliminationType() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->settings != null && $this->settings->treeType == ChampionshipSettings::DIRECT_ELIMINATION; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* A championship has Many Groups of Fighters. |
|
136
|
|
|
* |
|
137
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
138
|
|
|
*/ |
|
139
|
|
|
public function fightersGroups() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->hasMany(FightersGroup::class); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* A championship has Many fights. |
|
146
|
|
|
* |
|
147
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
|
148
|
|
|
*/ |
|
149
|
|
|
public function fights() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->hasManyThrough(Fight::class, FightersGroup::class); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Get the fights that happen to the first round |
|
156
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
|
157
|
|
|
*/ |
|
158
|
|
|
public function firstRoundFights() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->hasManyThrough(Fight::class, FightersGroup::class) |
|
161
|
|
|
->where('fighters_groups.round', 1) |
|
162
|
|
|
->orderby('fighters_groups.id', 'desc'); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
private function hasNoCustomSettings() |
|
166
|
|
|
{ |
|
167
|
|
|
return |
|
168
|
|
|
$this->settings == null || |
|
169
|
|
|
($this->settings->ageCategory == null || $this->settings->ageCategory == 0) && |
|
170
|
|
|
$this->settings->ageMin == null && |
|
171
|
|
|
$this->settings->ageMax == null && |
|
172
|
|
|
$this->settings->gradeMin == null && |
|
173
|
|
|
$this->settings->gradeMax == null && |
|
174
|
|
|
($this->settings->gradeCategory == null || $this->settings->gradeCategory == 0); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function buildName() |
|
178
|
|
|
{ |
|
179
|
|
|
if ($this->settings != null && $this->settings->alias != null && $this->settings->alias != '') |
|
180
|
|
|
return $this->settings->alias; |
|
181
|
|
|
|
|
182
|
|
|
if ($this->hasNoCustomSettings()) { |
|
183
|
|
|
return $this->category->name; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$genders = [ |
|
187
|
|
|
'M' => trans('categories.male'), |
|
188
|
|
|
'F' => trans('categories.female'), |
|
189
|
|
|
'X' => trans('categories.mixt') |
|
190
|
|
|
]; |
|
191
|
|
|
|
|
192
|
|
|
$teamText = $this->category->isTeam == 1 ? trans_choice('core.team', 1) : trans('categories.single'); |
|
193
|
|
|
$ageCategoryText = $this->category->getAgeString(); |
|
194
|
|
|
$gradeText = $this->category->getGradeString(); |
|
195
|
|
|
|
|
196
|
|
|
return $teamText . ' ' . $genders[$this->category->gender] . ' ' . $ageCategoryText . ' ' . $gradeText; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function getSettings() |
|
200
|
|
|
{ |
|
201
|
|
|
return $setting = $this->settings ?? new ChampionshipSettings(ChampionshipSettings::DEFAULT_SETTINGS); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Return Groups that belongs to a round |
|
206
|
|
|
* @param integer $round |
|
207
|
|
|
* @return HasMany |
|
208
|
|
|
*/ |
|
209
|
|
|
public function groupsByRound($round) |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->fightersGroups()->where('round', $round); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Return Fights that belongs to a round |
|
216
|
|
|
* |
|
217
|
|
|
* @param integer $round |
|
218
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
|
219
|
|
|
*/ |
|
220
|
|
|
public function fightsByRound($round) |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->hasManyThrough(Fight::class, FightersGroup::class)->where('round', $round); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function isPlayoffCompetitor() |
|
226
|
|
|
{ |
|
227
|
|
|
return !$this->category->isTeam() && |
|
228
|
|
|
($this->isPlayOffType() || $this->hasPreliminary()); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function isPlayoffTeam() |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->category->isTeam() && |
|
234
|
|
|
($this->isPlayOffType() || $this->hasPreliminary()); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function isDirectEliminationCompetitor() |
|
238
|
|
|
{ |
|
239
|
|
|
return !$this->category->isTeam() && $this->isDirectEliminationType() && !$this->hasPreliminary(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function isDirectEliminationTeam() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->category->isTeam() && $this->isDirectEliminationType() && !$this->hasPreliminary(); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @return TreeGenerable |
|
249
|
|
|
*/ |
|
250
|
|
|
public function chooseGenerationStrategy() |
|
251
|
|
|
{ |
|
252
|
|
|
$generation = new TreeGen($this, null); |
|
253
|
|
|
switch (true) { |
|
254
|
|
|
case $this->isDirectEliminationCompetitor(): |
|
|
|
|
|
|
255
|
|
|
$generation = new DirectEliminationCompetitorTreeGen($this, null); |
|
256
|
|
|
break; |
|
257
|
|
|
case $this->isDirectEliminationTeam(): |
|
|
|
|
|
|
258
|
|
|
$generation = new DirectEliminationTeamTreeGen($this, null); |
|
259
|
|
|
break; |
|
260
|
|
|
case $this->isPlayoffCompetitor(): |
|
261
|
|
|
$generation = new PlayOffCompetitorTreeGen($this, null); |
|
262
|
|
|
break; |
|
263
|
|
|
case $this->isPlayoffTeam(): |
|
264
|
|
|
$generation = new PlayOffTeamTreeGen($this, null); |
|
265
|
|
|
break; |
|
266
|
|
|
default: |
|
267
|
|
|
dd("bad choice"); |
|
268
|
|
|
} |
|
269
|
|
|
return $generation; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
} |
|
273
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.