1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xoco70\LaravelTournaments\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
|
8
|
|
|
class Fight extends Model |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Fight constructor. |
12
|
|
|
* |
13
|
|
|
* @param int $userId1 |
14
|
|
|
* @param int $userId2 |
15
|
|
|
*/ |
16
|
|
|
public function __construct($userId1 = null, $userId2 = null) |
17
|
|
|
{ |
18
|
|
|
parent::__construct(); |
19
|
|
|
$this->c1 = $userId1; |
20
|
|
|
$this->c2 = $userId2; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected $table = 'fight'; |
24
|
|
|
public $timestamps = true; |
25
|
|
|
|
26
|
|
|
protected $fillable = [ |
27
|
|
|
'group_id', |
28
|
|
|
'fight_id', |
29
|
|
|
'c1', |
30
|
|
|
'c2', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get First Fighter. |
35
|
|
|
* |
36
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
37
|
|
|
*/ |
38
|
|
|
public function group() |
39
|
|
|
{ |
40
|
|
|
return $this->belongsTo(FightersGroup::class, 'fighters_group_id'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param FightersGroup|null $group |
45
|
|
|
* |
46
|
|
|
* @return Collection |
47
|
|
|
*/ |
48
|
|
|
protected static function getFightersWithByes(FightersGroup $group) |
49
|
|
|
{ |
50
|
|
|
if ($group == null) { |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
$fighters = $group->getFightersWithBye(); |
54
|
|
|
$fighterType = $group->getFighterType(); |
55
|
|
|
if (count($fighters) == 0) { |
56
|
|
|
$fighters->push(new $fighterType()); |
57
|
|
|
$fighters->push(new $fighterType()); |
58
|
|
|
} elseif (count($fighters) % 2 != 0) { |
59
|
|
|
$fighters->push(new $fighterType()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $fighters; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get First Fighter. |
67
|
|
|
* |
68
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
69
|
|
|
*/ |
70
|
|
|
public function competitor1() |
71
|
|
|
{ |
72
|
|
|
return $this->belongsTo(Competitor::class, 'c1', 'id'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get Second Fighter. |
77
|
|
|
* |
78
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
79
|
|
|
*/ |
80
|
|
|
public function competitor2() |
81
|
|
|
{ |
82
|
|
|
return $this->belongsTo(Competitor::class, 'c2', 'id'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get First Fighter. |
87
|
|
|
* |
88
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
89
|
|
|
*/ |
90
|
|
|
public function team1() |
91
|
|
|
{ |
92
|
|
|
return $this->belongsTo(Team::class, 'c1', 'id'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get Second Fighter. |
98
|
|
|
* |
99
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
100
|
|
|
*/ |
101
|
|
|
public function team2() |
102
|
|
|
{ |
103
|
|
|
return $this->belongsTo(Team::class, 'c2', 'id'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get First Fighter. |
108
|
|
|
* |
109
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
110
|
|
|
*/ |
111
|
|
|
public function fighter1() |
112
|
|
|
{ |
113
|
|
|
return $this->group->championship->category->isTeam |
114
|
|
|
? $this->team1() |
115
|
|
|
: $this->competitor1(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get First Fighter. |
120
|
|
|
* |
121
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
122
|
|
|
*/ |
123
|
|
|
public function fighter2() |
124
|
|
|
{ |
125
|
|
|
return $this->group->championship->category->isTeam |
126
|
|
|
? $this->team2() |
127
|
|
|
: $this->competitor2(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param $numFighter |
132
|
|
|
* @param $attr |
133
|
|
|
* |
134
|
|
|
* @return null|string |
135
|
|
|
*/ |
136
|
|
|
public function getFighterAttr($numFighter, $attr) |
137
|
|
|
{ |
138
|
|
|
$isTeam = $this->group->championship->category->isTeam; |
139
|
|
|
if ($isTeam) { |
140
|
|
|
$teamToUpdate = 'team' . $numFighter; |
141
|
|
|
|
142
|
|
|
return optional($this->$teamToUpdate)->$attr; |
143
|
|
|
} |
144
|
|
|
$competitorToUpdate = 'competitor' . $numFighter; |
145
|
|
|
if ($attr == 'name') { |
146
|
|
|
return $this->$competitorToUpdate == null |
147
|
|
|
? '' |
148
|
|
|
: $this->$competitorToUpdate->user->firstname . ' ' . $this->$competitorToUpdate->user->lastname; |
149
|
|
|
} elseif ($attr == 'short_id') { |
150
|
|
|
return optional($this->$competitorToUpdate)->short_id; |
151
|
|
|
} |
152
|
|
|
return null; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function shouldBeInFightList($default) |
159
|
|
|
{ |
160
|
|
|
if ($this->belongsToFirstRound() && $this->dontHave2Fighters()) { |
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
if (!$this->belongsToFirstRound() && $this->dontHave0Fighters()) { |
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
if ($this->has2Fighters()) { |
167
|
|
|
return true; |
168
|
|
|
} |
169
|
|
|
// We aint in the first round, and there is 1 or 0 competitor |
170
|
|
|
// We check children, and see : |
171
|
|
|
// if there is 2 - 2 fighters -> undetermine, we cannot add it to fight list |
172
|
|
|
// if there is 2 - 1 fighters -> undetermine, we cannot add it to fight list |
173
|
|
|
// if there is 2 - 0 fighters -> undetermine, we cannot add it to fight list |
174
|
|
|
// if there is 1 - 2 fighters -> undetermine, we cannot add it to fight list |
175
|
|
|
// if there is 1 - 1 fighters -> fight should have 2 fighters, undetermines |
176
|
|
|
// if there is 1 - 0 fighters -> determined, fight should not be in the list |
177
|
|
|
// if there is 0 - 1 fighters -> determined, fight should not be in the list |
178
|
|
|
return $default; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* return true if fight has 2 fighters ( No BYE ). |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function has2Fighters(): bool |
187
|
|
|
{ |
188
|
|
|
return $this->c1 != null && $this->c2 != null; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
|
|
private function belongsToFirstRound(): bool |
195
|
|
|
{ |
196
|
|
|
$firstRoundFights = $this->group->championship->firstRoundFights->pluck('id')->toArray(); |
197
|
|
|
if (in_array($this->id, $firstRoundFights)) { |
198
|
|
|
return true; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
private function dontHave2Fighters() |
208
|
|
|
{ |
209
|
|
|
return $this->c1 == null || $this->c2 == null; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
|
|
private function dontHave0Fighters() |
216
|
|
|
{ |
217
|
|
|
return $this->c1 != null || $this->c2 != null; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param Championship $championship |
222
|
|
|
*/ |
223
|
|
|
public static function generateFightsId(Championship $championship) |
224
|
|
|
{ |
225
|
|
|
$order = 1; |
226
|
|
|
foreach ($championship->fights as $fight) { |
227
|
|
|
$order = $fight->updateShortId($order); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param $order |
233
|
|
|
* |
234
|
|
|
* @return int |
235
|
|
|
*/ |
236
|
|
|
public function updateShortId($order) |
237
|
|
|
{ |
238
|
|
|
if ($this->shouldBeInFightList(false)) { |
239
|
|
|
$this->short_id = $order; |
240
|
|
|
$this->save(); |
241
|
|
|
|
242
|
|
|
return ++$order; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $order; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|