|
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
|
|
|
* @param $group |
|
32
|
|
|
* @param $competitor1 |
|
33
|
|
|
* @param $competitor2 |
|
34
|
|
|
* @param $order |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
*/ |
|
37
|
|
|
private static function createFight($group, $competitor1, $competitor2, $order) |
|
38
|
|
|
{ |
|
39
|
|
|
$fight = new self(); |
|
40
|
|
|
$fight->fighters_group_id = $group->id; |
|
41
|
|
|
$fight->c1 = $competitor1 != null ? $competitor1->id : null; |
|
42
|
|
|
$fight->c2 = $competitor2 != null ? $competitor2->id : null; |
|
43
|
|
|
$fight->short_id = $order++; |
|
44
|
|
|
$fight->area = $group->area; |
|
45
|
|
|
$fight->save(); |
|
46
|
|
|
return $order; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get First Fighter. |
|
51
|
|
|
* |
|
52
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
53
|
|
|
*/ |
|
54
|
|
|
public function group() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->belongsTo(FightersGroup::class, 'fighters_group_id'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param Championship $championship |
|
61
|
|
|
* |
|
62
|
|
|
* @return Collection |
|
63
|
|
|
*/ |
|
64
|
|
|
private static function getActorsToFights(Championship $championship, FightersGroup $group = null) |
|
65
|
|
|
{ |
|
66
|
|
|
if ($group == null) return null; |
|
67
|
|
|
$fighters = $group->getFighters(); |
|
68
|
|
|
$fighterType = $group->getFighterType(); |
|
69
|
|
|
if (sizeof($fighters) == 0) { |
|
70
|
|
|
$fighters->push(new $fighterType); |
|
71
|
|
|
$fighters->push(new $fighterType); |
|
72
|
|
|
} else if (count($fighters) % 2 != 0) { |
|
73
|
|
|
$fighters->push(new $fighterType); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $fighters; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get First Fighter. |
|
81
|
|
|
* |
|
82
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
83
|
|
|
*/ |
|
84
|
|
|
public function competitor1() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->belongsTo(Competitor::class, 'c1', 'id'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get Second Fighter. |
|
91
|
|
|
* |
|
92
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
93
|
|
|
*/ |
|
94
|
|
|
public function competitor2() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->belongsTo(Competitor::class, 'c2', 'id'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get First Fighter. |
|
101
|
|
|
* |
|
102
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
103
|
|
|
*/ |
|
104
|
|
|
public function team1() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->belongsTo(Team::class, 'c1', 'id'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get Second Fighter. |
|
111
|
|
|
* |
|
112
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
113
|
|
|
*/ |
|
114
|
|
|
public function team2() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->belongsTo(Team::class, 'c2', 'id'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Save a Fight. |
|
121
|
|
|
* |
|
122
|
|
|
* @param Collection $groups |
|
123
|
|
|
* @param int $numGroup |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function savePreliminaryFightGroup($groups, $numGroup = 1) |
|
126
|
|
|
{ |
|
127
|
|
|
$competitor1 = $competitor2 = null; |
|
128
|
|
|
$order = 1; |
|
129
|
|
|
|
|
130
|
|
|
foreach ($groups as $group) { |
|
131
|
|
|
|
|
132
|
|
|
$fighters = $group->getFighters(); |
|
133
|
|
|
[$fighter1, $fighter2, $fighter3 ] = $fighters; |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
switch ($numGroup) { |
|
136
|
|
|
case 1: |
|
137
|
|
|
$competitor1 = $fighter1; |
|
138
|
|
|
$competitor2 = $fighter2; |
|
139
|
|
|
break; |
|
140
|
|
|
case 2: |
|
141
|
|
|
$competitor1 = $fighter2; |
|
142
|
|
|
$competitor2 = $fighter3; |
|
143
|
|
|
break; |
|
144
|
|
|
case 3: |
|
145
|
|
|
$competitor1 = $fighter3; |
|
146
|
|
|
$competitor2 = $fighter1; |
|
147
|
|
|
break; |
|
148
|
|
|
} |
|
149
|
|
|
$order = self::createFight($group, $competitor1, $competitor2, $order); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param Championship $championship |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function saveGroupFights(Championship $championship) |
|
158
|
|
|
{ |
|
159
|
|
|
$order = 1; |
|
160
|
|
|
$round = []; |
|
161
|
|
|
foreach ($championship->fightersGroups()->get() as $group) { |
|
162
|
|
|
$fighters = self::getActorsToFights($championship, $group); |
|
163
|
|
|
$away = $fighters->splice(count($fighters) / 2); // 2 |
|
164
|
|
|
$home = $fighters; // 1 |
|
165
|
|
|
|
|
166
|
|
|
$countHome = count($home); |
|
167
|
|
|
$countAway = count($away); |
|
168
|
|
|
for ($i = 0; $i < $countHome + $countAway - 1; $i++) { |
|
169
|
|
|
for ($j = 0; $j < $countHome; $j++) { |
|
170
|
|
|
|
|
171
|
|
|
$round[$i][$j]['Home'] = $home[$j]; |
|
172
|
|
|
$round[$i][$j]['Away'] = $away[$j]; |
|
173
|
|
|
$fight = new self(); |
|
174
|
|
|
$fight->fighters_group_id = $group->id; |
|
175
|
|
|
$fight->c1 = $round[$i][$j]['Home']->id; |
|
176
|
|
|
$fight->c2 = $round[$i][$j]['Away']->id; |
|
177
|
|
|
$fight->short_id = $order++; |
|
178
|
|
|
$fight->area = $group->area; |
|
179
|
|
|
$fight->save(); |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
if ($countHome + $countAway - 1 > 2) { |
|
183
|
|
|
$away->prepend($home->splice(1, 1)->shift()); |
|
184
|
|
|
$home->push($away->pop()); |
|
185
|
|
|
$order++; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function getFighterAttr($numFighter, $attr) |
|
192
|
|
|
{ |
|
193
|
|
|
$isTeam = $this->group->championship->category->isTeam; |
|
194
|
|
|
if ($isTeam) { |
|
195
|
|
|
$teamToUpdate = 'team' . $numFighter; |
|
196
|
|
|
return $this->$teamToUpdate == null ? '' : $this->$teamToUpdate->$attr; |
|
197
|
|
|
} |
|
198
|
|
|
$competitorToUpdate = 'competitor' . $numFighter; |
|
199
|
|
|
if ($attr == 'name') { |
|
200
|
|
|
return $this->$competitorToUpdate == null |
|
201
|
|
|
? 'BYE' |
|
202
|
|
|
: $this->$competitorToUpdate->user->firstname . " " . $this->$competitorToUpdate->user->lastname; |
|
203
|
|
|
} elseif ($attr == 'short_id') { |
|
204
|
|
|
return $this->$competitorToUpdate == null ? '' : $this->$competitorToUpdate->short_id; |
|
205
|
|
|
} |
|
206
|
|
|
return null; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Update parent Fight |
|
211
|
|
|
* @param $fighterToUpdate |
|
212
|
|
|
* @param $fight |
|
213
|
|
|
*/ |
|
214
|
|
|
public function updateParentFight($fighterToUpdate, $fight) |
|
215
|
|
|
{ |
|
216
|
|
|
if ($fight != null) { |
|
217
|
|
|
if (($fight->c1 != null || $fight->c2 == null)) { |
|
218
|
|
|
$this->$fighterToUpdate = $fight->c1; |
|
219
|
|
|
} |
|
220
|
|
|
if ($fight->c1 == null || $fight->c2 != null) { |
|
221
|
|
|
$this->$fighterToUpdate = $fight->c2; |
|
222
|
|
|
} |
|
223
|
|
|
if ($fight->c1 == null || $fight->c2 == null) { |
|
224
|
|
|
$this->$fighterToUpdate = null; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Returns the parent field that need to be updated |
|
232
|
|
|
* @return null|string |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getParentFighterToUpdate() |
|
235
|
|
|
{ |
|
236
|
|
|
$childrenGroup = $this->group->parent->children; |
|
237
|
|
|
foreach ($childrenGroup as $key => $children) { |
|
238
|
|
|
$childFight = $children->fights->get(0); |
|
239
|
|
|
if ($childFight->id == $this->id) { |
|
240
|
|
|
if ($key % 2 == 0) { |
|
241
|
|
|
return "c1"; |
|
242
|
|
|
} |
|
243
|
|
|
if ($key % 2 == 1) { |
|
244
|
|
|
return "c2"; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
return null; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* In the original fight ( child ) return the field that contains data to copy to parent |
|
253
|
|
|
* @return null|string |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getValueToUpdate() |
|
256
|
|
|
{ |
|
257
|
|
|
if ($this->c1 != null && $this->c2 != null) { |
|
258
|
|
|
return null; |
|
259
|
|
|
} |
|
260
|
|
|
if ($this->c1 != null) { |
|
261
|
|
|
return "c1"; |
|
262
|
|
|
} |
|
263
|
|
|
if ($this->c2 != null) { |
|
264
|
|
|
return "c2"; |
|
265
|
|
|
} |
|
266
|
|
|
return null; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Check if we are able to fill the parent fight or not |
|
271
|
|
|
* If one of the children has c1 x c2, then we must wait to fill parent |
|
272
|
|
|
* |
|
273
|
|
|
* @return bool |
|
274
|
|
|
*/ |
|
275
|
|
|
function hasDeterminedParent() |
|
276
|
|
|
{ |
|
277
|
|
|
|
|
278
|
|
|
if ($this->c1 != null && $this->c2 != null) return true; |
|
279
|
|
|
foreach ($this->group->children as $child) { |
|
280
|
|
|
$fight = $child->fights->get(0); |
|
281
|
|
|
if ($fight->c1 != null && $fight->c2 != null) return false; |
|
282
|
|
|
} |
|
283
|
|
|
return true; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
} |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.