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