-
Notifications
You must be signed in to change notification settings - Fork 53
/
Championship.php
298 lines (260 loc) · 7.72 KB
/
Championship.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
namespace Xoco70\LaravelTournaments\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Xoco70\LaravelTournaments\Contracts\TreeGenerable;
use Xoco70\LaravelTournaments\TreeGen\PlayOffCompetitorTreeGen;
use Xoco70\LaravelTournaments\TreeGen\PlayOffTeamTreeGen;
use Xoco70\LaravelTournaments\TreeGen\SingleEliminationCompetitorTreeGen;
use Xoco70\LaravelTournaments\TreeGen\SingleEliminationTeamTreeGen;
class Championship extends Model
{
use SoftDeletes;
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
protected $table = 'championship';
public $timestamps = true;
protected $fillable = [
'tournament_id',
'category_id',
];
protected static function boot()
{
parent::boot();
static::deleting(function ($championship) {
$championship->competitors()->delete();
$championship->settings()->delete();
});
static::restoring(function ($championship) {
$championship->competitors()->restore();
$championship->settings()->restore();
});
}
/**
* A championship has many Competitors.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function competitors()
{
return $this->hasMany(Competitor::class);
}
public function fighters()
{
if ($this->category->isTeam) {
return $this->hasMany(Team::class);
}
return $this->hasMany(Competitor::class);
}
/**
* A championship belongs to a Category.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function category()
{
return $this->belongsTo(Category::class);
}
/**
* A championship belongs to a Tournament.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function tournament()
{
return $this->belongsTo(Tournament::class);
}
/**
* Get All competitors from a Championships.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users()
{
return $this->belongsToMany(config('laravel-tournaments.user.model'), 'competitor', 'championship_id')
->withPivot('confirmed')
->withTimestamps();
}
/**
* A championship only has 1 Settings.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function settings()
{
return $this->hasOne(ChampionshipSettings::class);
}
/**
* A championship has Many Teams.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function teams()
{
return $this->hasMany(Team::class);
}
/**
* Check if Championship has Preliminary Round Configured.
*
* @return bool
*/
public function hasPreliminary()
{
return $this->settings == null || $this->settings->hasPreliminary;
}
/**
* Check if 2nd Round of Championship is Round Robin.
*
* @return bool
*/
public function isPlayOffType()
{
return $this->settings != null && $this->settings->treeType == ChampionshipSettings::PLAY_OFF;
}
/**
* Check if 2nd Round of Championship is Single Elimination.
*
* @return bool
*/
public function isSingleEliminationType()
{
return $this->getSettings() != null
&& $this->getSettings()->treeType == ChampionshipSettings::SINGLE_ELIMINATION;
}
/**
* A championship has Many Groups of Fighters.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function fightersGroups()
{
return $this->hasMany(FightersGroup::class);
}
/**
* A championship has Many fights.
*
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function fights()
{
return $this->hasManyThrough(Fight::class, FightersGroup::class)->orderBy('id', 'asc');
}
/**
* Get the fights that happen to the first round.
*
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function firstRoundFights()
{
return $this->hasManyThrough(Fight::class, FightersGroup::class)
->where('fighters_groups.round', 1);
}
private function hasNoCustomSettings()
{
return $this->settings == null;
}
public function buildName()
{
if ($this->settings != null && $this->settings->alias != null) {
return $this->settings->alias;
}
if ($this->hasNoCustomSettings()) {
return $this->category->name;
}
$genders = [
'M' => trans('categories.male'),
'F' => trans('categories.female'),
'X' => trans('categories.mixt'),
];
$teamText = $this->category->isTeam == 1 ? trans_choice('core.team', 1) : trans('categories.single');
$ageCategoryText = $this->category->getAgeString();
$gradeText = $this->category->getGradeString();
return $teamText.' '.$genders[$this->category->gender].' '.$ageCategoryText.' '.$gradeText;
}
public function getSettings()
{
return $this->settings ?? new ChampionshipSettings(ChampionshipSettings::DEFAULT_SETTINGS);
}
/**
* Return Groups that belongs to a round.
*
* @param int $round
*
* @return HasMany
*/
public function groupsByRound($round)
{
return $this->fightersGroups()->where('round', $round);
}
/**
* Return Groups that belongs to a round.
*
* @param int $round
*
* @return HasMany
*/
public function groupsFromRound($round)
{
return $this->fightersGroups()->where('round', '>=', $round);
}
/**
* Return Fights that belongs to a round.
*
* @param int $round
*
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
*/
public function fightsByRound($round)
{
return $this->hasManyThrough(Fight::class, FightersGroup::class)->where('round', $round);
}
public function isPlayoffCompetitor()
{
return !$this->category->isTeam() && $this->isPlayOffType();
}
public function isPlayoffTeam()
{
return $this->category->isTeam() && $this->isPlayOffType();
}
public function isSingleEliminationCompetitor()
{
return !$this->category->isTeam() && $this->isSingleEliminationType();
}
public function isSingleEliminationTeam()
{
return $this->category->isTeam() && $this->isSingleEliminationType();
}
/**
* @return TreeGenerable
*/
public function chooseGenerationStrategy()
{
switch (true) {
case $this->isSingleEliminationCompetitor():
$generation = new SingleEliminationCompetitorTreeGen($this, null);
break;
case $this->isSingleEliminationTeam():
$generation = new SingleEliminationTeamTreeGen($this, null);
break;
case $this->isPlayoffCompetitor():
$generation = new PlayOffCompetitorTreeGen($this, null);
break;
case $this->isPlayoffTeam():
$generation = new PlayOffTeamTreeGen($this, null);
break;
default:
$generation = new PlayOffCompetitorTreeGen($this, null);
}
return $generation;
}
/**
* @return int
*/
public function getGroupSize()
{
if ($this->hasPreliminary()) {
return $this->getSettings()->preliminaryGroupSize;
}
return 2;
}
}