|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xoco70\KendoTournaments\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Facades\DB; |
|
9
|
|
|
use Kalnoy\Nestedset\NodeTrait; |
|
10
|
|
|
use Xoco70\KendoTournaments\TreeGen\DirectEliminationTreeGen; |
|
11
|
|
|
use Xoco70\KendoTournaments\TreeGen\TreeGen; |
|
12
|
|
|
|
|
13
|
|
|
class FightersGroup extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
protected $table = 'fighters_groups'; |
|
16
|
|
|
public $timestamps = true; |
|
17
|
|
|
protected $guarded = ['id']; |
|
18
|
|
|
|
|
19
|
|
|
use NodeTrait; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
// /** |
|
|
|
|
|
|
23
|
|
|
// * @param Championship $championship |
|
24
|
|
|
// * @param $fightsByRound |
|
25
|
|
|
// */ |
|
26
|
|
|
// private static function updateParentFight(Championship $championship, $fightsByRound) |
|
27
|
|
|
// { |
|
28
|
|
|
// foreach ($fightsByRound as $fight) { |
|
29
|
|
|
// $parentGroup = $fight->group->parent; |
|
30
|
|
|
// if ($parentGroup == null) break; |
|
31
|
|
|
// $parentFight = $parentGroup->fights->get(0); //TODO This Might change when extending to Preliminary |
|
32
|
|
|
// |
|
33
|
|
|
// // IN this $fight, is c1 or c2 has the info? |
|
34
|
|
|
// if ($championship->isDirectEliminationType()) { |
|
35
|
|
|
// // determine whether c1 or c2 must be updated |
|
36
|
|
|
// self::chooseAndUpdateParentFight($fight, $parentFight); |
|
37
|
|
|
// } |
|
38
|
|
|
// } |
|
39
|
|
|
// } |
|
40
|
|
|
|
|
41
|
|
|
// /** |
|
|
|
|
|
|
42
|
|
|
// * @param $fight |
|
43
|
|
|
// * @param $parentFight |
|
44
|
|
|
// */ |
|
45
|
|
|
// private static function chooseAndUpdateParentFight($fight, $parentFight) |
|
46
|
|
|
// { |
|
47
|
|
|
// $fighterToUpdate = $fight->getParentFighterToUpdate(); |
|
48
|
|
|
// $valueToUpdate = $fight->getValueToUpdate(); |
|
49
|
|
|
// // we need to know if the child has empty fighters, is this BYE or undetermined |
|
50
|
|
|
// if ($fight->hasDeterminedParent() && $valueToUpdate != null) { |
|
51
|
|
|
// $parentFight->$fighterToUpdate = $fight->$valueToUpdate; |
|
52
|
|
|
// $parentFight->save(); |
|
53
|
|
|
// } |
|
54
|
|
|
// } |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
58
|
|
|
*/ |
|
59
|
|
|
public function championship() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->belongsTo(Championship::class); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
66
|
|
|
*/ |
|
67
|
|
|
public function fights() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->hasMany(Fight::class); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function fighters() |
|
73
|
|
|
{ |
|
74
|
|
|
if ($this->championship->category->isTeam()) { |
|
75
|
|
|
return $this->teamsWithNull(); |
|
76
|
|
|
} |
|
77
|
|
|
return $this->competitorsWithNull(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function teams() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->belongsToMany(Team::class, 'fighters_group_team')->withTimestamps(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function competitors() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->belongsToMany(Competitor::class, 'fighters_group_competitor')->withTimestamps(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// /** |
|
|
|
|
|
|
91
|
|
|
// * Generate First Round Fights |
|
92
|
|
|
// * @param Championship $championship |
|
93
|
|
|
// */ |
|
94
|
|
|
// public static function generateFights(Championship $championship) |
|
95
|
|
|
// { |
|
96
|
|
|
// $settings = $championship->getSettings(); |
|
97
|
|
|
// // Delete previous fight for this championship |
|
98
|
|
|
// |
|
99
|
|
|
// $arrGroupsId = $championship->fightersGroups()->get()->pluck('id'); |
|
100
|
|
|
// |
|
101
|
|
|
// Fight::destroy($arrGroupsId); |
|
102
|
|
|
// // Very specific case to common case : Preliminary with 3 fighters |
|
103
|
|
|
// if ($settings->hasPreliminary && $settings->preliminaryGroupSize == 3) { |
|
104
|
|
|
// for ($numGroup = 1; $numGroup <= $settings->preliminaryGroupSize; $numGroup++) { |
|
105
|
|
|
// PreliminaryFight::saveFights($championship->fightersGroups()->get(), $numGroup); |
|
106
|
|
|
// } |
|
107
|
|
|
// return; |
|
108
|
|
|
// } |
|
109
|
|
|
// DirectEliminationFight::saveFights($championship); |
|
110
|
|
|
// } |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Supercharge of sync Many2Many function. |
|
114
|
|
|
* Original sync doesn't insert NULL ids. |
|
115
|
|
|
* |
|
116
|
|
|
* @param $fighters |
|
117
|
|
|
*/ |
|
118
|
|
|
public function syncTeams($fighters) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->teams()->detach(); |
|
121
|
|
|
foreach ($fighters as $fighter) { |
|
122
|
|
|
if ($fighter != null) { |
|
123
|
|
|
$this->teams()->attach($fighter); |
|
124
|
|
|
} else { |
|
125
|
|
|
// Insert row manually |
|
126
|
|
|
DB::table('fighters_group_team')->insertGetId( |
|
127
|
|
|
['team_id' => null, 'fighters_group_id' => $this->id] |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Supercharge of sync Many2Many function. |
|
135
|
|
|
* Original sync doesn't insert NULL ids. |
|
136
|
|
|
* |
|
137
|
|
|
* @param $fighters |
|
138
|
|
|
*/ |
|
139
|
|
|
public function syncCompetitors($fighters) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->competitors()->detach(); |
|
142
|
|
|
foreach ($fighters as $fighter) { |
|
143
|
|
|
if ($fighter != null) { |
|
144
|
|
|
$this->competitors()->attach($fighter); |
|
145
|
|
|
} else { |
|
146
|
|
|
DB::table('fighters_group_competitor')->insertGetId( |
|
147
|
|
|
['competitor_id' => null, 'fighters_group_id' => $this->id, |
|
148
|
|
|
"created_at" => Carbon::now(), |
|
149
|
|
|
"updated_at" => Carbon::now(), |
|
150
|
|
|
] |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Get the many 2 many relationship with |
|
159
|
|
|
* |
|
160
|
|
|
* @return Collection |
|
161
|
|
|
*/ |
|
162
|
|
|
public function competitorsWithNull(): Collection |
|
163
|
|
|
{ |
|
164
|
|
|
$competitors = new Collection(); |
|
165
|
|
|
$fgcs = FighterGroupCompetitor::where('fighters_group_id', $this->id) |
|
166
|
|
|
->with('competitor') |
|
167
|
|
|
->get(); |
|
168
|
|
|
foreach ($fgcs as $fgc) { |
|
169
|
|
|
$competitors->push($fgc->competitor ?? new Competitor()); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $competitors; |
|
173
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
public function teamsWithNull(): Collection |
|
178
|
|
|
{ |
|
179
|
|
|
$teams = new Collection(); |
|
180
|
|
|
$fgcs = FighterGroupTeam::where('fighters_group_id', $this->id) |
|
181
|
|
|
->with('team') |
|
182
|
|
|
->get(); |
|
183
|
|
|
foreach ($fgcs as $fgc) { |
|
184
|
|
|
$teams->push($fgc->team ?? new Team()); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $teams; |
|
188
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function getFighters(): Collection |
|
192
|
|
|
{ |
|
193
|
|
|
if ($this->championship->category->isTeam()) { |
|
194
|
|
|
return $this->teamsWithNull(); |
|
195
|
|
|
} |
|
196
|
|
|
return $this->competitorsWithNull(); |
|
197
|
|
|
|
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public static function getBaseNumGroups($initialGroupId, $numGroups, $numRound): int |
|
201
|
|
|
{ |
|
202
|
|
|
$parentId = $initialGroupId; |
|
203
|
|
|
|
|
204
|
|
|
for ($i = 1; $i <= $numRound; $i++) { |
|
205
|
|
|
$parentId += $numGroups / $numRound; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $parentId; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
// /** |
|
|
|
|
|
|
212
|
|
|
// * |
|
213
|
|
|
// * @param Championship $championship |
|
214
|
|
|
// */ |
|
215
|
|
|
// public static function generateNextRoundsFights(Championship $championship) |
|
216
|
|
|
// { |
|
217
|
|
|
// $championship = $championship->withCount('teams', 'competitors')->first(); |
|
218
|
|
|
// $fightersCount = $championship->competitors_count + $championship->teams_count; |
|
219
|
|
|
// $maxRounds = intval(ceil(log($fightersCount, 2))); |
|
220
|
|
|
// for ($numRound = 1; $numRound < $maxRounds; $numRound++) { |
|
221
|
|
|
// $fightsByRound = $championship->fightsByRound($numRound)->with('group.parent', 'group.children')->get(); |
|
222
|
|
|
// self::updateParentFight($championship, $fightsByRound); |
|
223
|
|
|
// } |
|
224
|
|
|
// |
|
225
|
|
|
// } |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @return string |
|
229
|
|
|
*/ |
|
230
|
|
|
public function getFighterType() |
|
231
|
|
|
{ |
|
232
|
|
|
if ($this->championship->category->isTeam()) { |
|
233
|
|
|
return Team::class; |
|
234
|
|
|
} |
|
235
|
|
|
return Competitor::class; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.