-
Notifications
You must be signed in to change notification settings - Fork 53
/
Fight.php
245 lines (219 loc) · 5.99 KB
/
Fight.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
<?php
namespace Xoco70\LaravelTournaments\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
class Fight extends Model
{
/**
* Fight constructor.
*
* @param int $userId1
* @param int $userId2
*/
public function __construct($userId1 = null, $userId2 = null)
{
parent::__construct();
$this->c1 = $userId1;
$this->c2 = $userId2;
}
protected $table = 'fight';
public $timestamps = true;
protected $fillable = [
'group_id',
'fight_id',
'c1',
'c2',
];
/**
* Get First Fighter.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function group()
{
return $this->belongsTo(FightersGroup::class, 'fighters_group_id');
}
/**
* @param FightersGroup|null $group
*
* @return Collection
*/
protected static function getFightersWithByes(FightersGroup $group)
{
if ($group == null) {
return;
}
$fighters = $group->getFightersWithBye();
$fighterType = $group->getFighterType();
if (count($fighters) == 0) {
$fighters->push(new $fighterType());
$fighters->push(new $fighterType());
} elseif (count($fighters) % 2 != 0) {
$fighters->push(new $fighterType());
}
return $fighters;
}
/**
* Get First Fighter.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function competitor1()
{
return $this->belongsTo(Competitor::class, 'c1', 'id');
}
/**
* Get Second Fighter.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function competitor2()
{
return $this->belongsTo(Competitor::class, 'c2', 'id');
}
/**
* Get First Fighter.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function team1()
{
return $this->belongsTo(Team::class, 'c1', 'id');
}
/**
* Get Second Fighter.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function team2()
{
return $this->belongsTo(Team::class, 'c2', 'id');
}
/**
* Get First Fighter.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function fighter1()
{
return $this->group->championship->category->isTeam
? $this->team1()
: $this->competitor1();
}
/**
* Get First Fighter.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function fighter2()
{
return $this->group->championship->category->isTeam
? $this->team2()
: $this->competitor2();
}
/**
* @param $numFighter
* @param $attr
*
* @return null|string
*/
public function getFighterAttr($numFighter, $attr)
{
$isTeam = $this->group->championship->category->isTeam;
if ($isTeam) {
$teamToUpdate = 'team'.$numFighter;
return optional($this->$teamToUpdate)->$attr;
}
$competitorToUpdate = 'competitor'.$numFighter;
if ($attr == 'name') {
return $this->$competitorToUpdate == null
? ''
: $this->$competitorToUpdate->user->firstname.' '.$this->$competitorToUpdate->user->lastname;
} elseif ($attr == 'short_id') {
return optional($this->$competitorToUpdate)->short_id;
}
}
/**
* @return bool
*/
public function shouldBeInFightList($default)
{
if ($this->belongsToFirstRound() && $this->dontHave2Fighters()) {
return false;
}
if (!$this->belongsToFirstRound() && $this->dontHave0Fighters()) {
return true;
}
if ($this->has2Fighters()) {
return true;
}
// We aint in the first round, and there is 1 or 0 competitor
// We check children, and see :
// if there is 2 - 2 fighters -> undetermine, we cannot add it to fight list
// if there is 2 - 1 fighters -> undetermine, we cannot add it to fight list
// if there is 2 - 0 fighters -> undetermine, we cannot add it to fight list
// if there is 1 - 2 fighters -> undetermine, we cannot add it to fight list
// if there is 1 - 1 fighters -> fight should have 2 fighters, undetermines
// if there is 1 - 0 fighters -> determined, fight should not be in the list
// if there is 0 - 1 fighters -> determined, fight should not be in the list
return $default;
}
/**
* return true if fight has 2 fighters ( No BYE ).
*
* @return bool
*/
private function has2Fighters(): bool
{
return $this->c1 != null && $this->c2 != null;
}
/**
* @return bool
*/
private function belongsToFirstRound(): bool
{
$firstRoundFights = $this->group->championship->firstRoundFights->pluck('id')->toArray();
if (in_array($this->id, $firstRoundFights)) {
return true;
}
return false;
}
/**
* @return bool
*/
private function dontHave2Fighters()
{
return $this->c1 == null || $this->c2 == null;
}
/**
* @return bool
*/
private function dontHave0Fighters()
{
return $this->c1 != null || $this->c2 != null;
}
/**
* @param Championship $championship
*/
public static function generateFightsId(Championship $championship)
{
$order = 1;
foreach ($championship->fights as $fight) {
$order = $fight->updateShortId($order);
}
}
/**
* @param $order
*
* @return int
*/
private function updateShortId($order)
{
if ($this->shouldBeInFightList(false)) {
$this->short_id = $order;
$this->save();
return ++$order;
}
return $order;
}
}