|
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\TreeGen; |
|
11
|
|
|
|
|
12
|
|
|
class FightersGroup extends Model |
|
13
|
|
|
{ |
|
14
|
|
|
protected $table = 'fighters_groups'; |
|
15
|
|
|
public $timestamps = true; |
|
16
|
|
|
protected $guarded = ['id']; |
|
17
|
|
|
|
|
18
|
|
|
use NodeTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Check if Request contains tournamentSlug / Should Move to TreeRequest When Built. |
|
22
|
|
|
* |
|
23
|
|
|
* @param $request |
|
24
|
|
|
* |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function hasTournamentInRequest($request) |
|
28
|
|
|
{ |
|
29
|
|
|
return $request->tournament != null; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Check if Request contains championshipId / Should Move to TreeRequest When Built. |
|
34
|
|
|
* |
|
35
|
|
|
* @param $request |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function hasChampionshipInRequest($request) |
|
40
|
|
|
{ |
|
41
|
|
|
return $request->championshipId != null; // has return false, don't know why |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
46
|
|
|
*/ |
|
47
|
|
|
public function championship() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->belongsTo(Championship::class); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
54
|
|
|
*/ |
|
55
|
|
|
public function fights() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->hasMany(Fight::class); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function teams() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->belongsToMany(Team::class, 'fighters_group_team')->withTimestamps(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function competitors() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->belongsToMany(Competitor::class, 'fighters_group_competitor')->withTimestamps(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Generate First Round Fights |
|
72
|
|
|
* @param Championship $championship |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function generateFights(Championship $championship) |
|
75
|
|
|
{ |
|
76
|
|
|
$settings = $championship->getSettings(); |
|
77
|
|
|
// Delete previous fight for this championship |
|
78
|
|
|
|
|
79
|
|
|
$arrGroupsId = $championship->fightersGroups()->get()->pluck('id'); |
|
80
|
|
|
|
|
81
|
|
|
Fight::destroy($arrGroupsId); |
|
82
|
|
|
|
|
83
|
|
|
if ($settings->hasPreliminary && $settings->preliminaryGroupSize == 3) { |
|
84
|
|
|
for ($numGroup = 1; $numGroup <= $settings->preliminaryGroupSize; $numGroup++) { |
|
85
|
|
|
Fight::savePreliminaryFightGroup($championship->fightersGroups()->get(), $numGroup); |
|
86
|
|
|
} |
|
87
|
|
|
} else { |
|
88
|
|
|
Fight::saveGroupFights($championship); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Supercharge of sync Many2Many function. |
|
94
|
|
|
* Original sync doesn't insert NULL ids. |
|
95
|
|
|
* |
|
96
|
|
|
* @param $fighters |
|
97
|
|
|
*/ |
|
98
|
|
|
public function syncTeams($fighters) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->teams()->detach(); |
|
101
|
|
|
foreach ($fighters as $fighter) { |
|
102
|
|
|
if ($fighter != null) { |
|
103
|
|
|
$this->teams()->attach($fighter); |
|
104
|
|
|
} else { |
|
105
|
|
|
// Insert row manually |
|
106
|
|
|
DB::table('fighters_group_team')->insertGetId( |
|
107
|
|
|
['team_id' => null, 'fighters_group_id' => $this->id] |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Supercharge of sync Many2Many function. |
|
115
|
|
|
* Original sync doesn't insert NULL ids. |
|
116
|
|
|
* |
|
117
|
|
|
* @param $fighters |
|
118
|
|
|
*/ |
|
119
|
|
|
public function syncCompetitors($fighters) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->competitors()->detach(); |
|
122
|
|
|
foreach ($fighters as $fighter) { |
|
123
|
|
|
if ($fighter != null) { |
|
124
|
|
|
$this->competitors()->attach($fighter); |
|
125
|
|
|
} else { |
|
126
|
|
|
DB::table('fighters_group_competitor')->insertGetId( |
|
127
|
|
|
['competitor_id' => null, 'fighters_group_id' => $this->id, |
|
128
|
|
|
"created_at" => Carbon::now(), |
|
129
|
|
|
"updated_at" => Carbon::now(), |
|
130
|
|
|
] |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Get the many 2 many relationship with |
|
139
|
|
|
* |
|
140
|
|
|
* @return Collection |
|
141
|
|
|
*/ |
|
142
|
|
|
public function competitorsWithNull(): Collection |
|
143
|
|
|
{ |
|
144
|
|
|
$competitors = new Collection(); |
|
145
|
|
|
$fgcs = FighterGroupCompetitor::where('fighters_group_id', $this->id) |
|
146
|
|
|
->with('competitor') |
|
147
|
|
|
->get(); |
|
148
|
|
|
foreach ($fgcs as $fgc) { |
|
149
|
|
|
$competitors->push($fgc->competitor ?? new Competitor()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $competitors; |
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
public function teamsWithNull(): Collection |
|
158
|
|
|
{ |
|
159
|
|
|
$teams = new Collection(); |
|
160
|
|
|
$fgcs = FighterGroupTeam::where('fighters_group_id', $this->id) |
|
161
|
|
|
->with('team') |
|
162
|
|
|
->get(); |
|
163
|
|
|
foreach ($fgcs as $fgc) { |
|
164
|
|
|
$teams->push($fgc->team ?? new Team()); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $teams; |
|
168
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function getFighters(): Collection |
|
172
|
|
|
{ |
|
173
|
|
|
if ($this->championship->category->isTeam()) { |
|
174
|
|
|
$fighters = $this->teamsWithNull(); |
|
175
|
|
|
} else { |
|
176
|
|
|
$fighters = $this->competitorsWithNull(); |
|
177
|
|
|
} |
|
178
|
|
|
return $fighters; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public static function getBaseNumGroups($initialGroupId, $numGroups, $numRound): int |
|
182
|
|
|
{ |
|
183
|
|
|
$parentId = $initialGroupId; |
|
184
|
|
|
|
|
185
|
|
|
for ($i = 1; $i <= $numRound; $i++) { |
|
186
|
|
|
$parentId += $numGroups / $numRound; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $parentId; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* |
|
194
|
|
|
* @param Championship $championship |
|
195
|
|
|
*/ |
|
196
|
|
|
public static function generateNextRoundsFights(Championship $championship) |
|
197
|
|
|
{ |
|
198
|
|
|
$championship = $championship->withCount('teams', 'competitors')->first(); |
|
199
|
|
|
$fightersCount = $championship->competitors_count + $championship->teams_count; |
|
200
|
|
|
$maxRounds = intval(ceil(log($fightersCount, 2))); |
|
201
|
|
|
for ($numRound = 1; $numRound < $maxRounds; $numRound++) { |
|
202
|
|
|
$fightsByRound = $championship->fightsByRound($numRound)->with('group.parent', 'group.children')->get(); |
|
203
|
|
|
foreach ($fightsByRound as $fight) { |
|
204
|
|
|
|
|
205
|
|
|
$parentGroup = $fight->group->parent; |
|
206
|
|
|
if ($parentGroup == null) break; |
|
207
|
|
|
$parentFight = $parentGroup->fights->get(0); //TODO This Might change when extending to Preliminary |
|
208
|
|
|
|
|
209
|
|
|
// IN this $fight, is c1 or c2 has the info? |
|
210
|
|
|
if ($championship->isDirectEliminationType()) { |
|
211
|
|
|
|
|
212
|
|
|
// determine whether c1 or c2 must be updated |
|
213
|
|
|
$fighterToUpdate = $fight->getParentFighterToUpdate(); |
|
214
|
|
|
$valueToUpdate = $fight->getValueToUpdate(); |
|
215
|
|
|
// we need to know if the child has empty fighters, is this BYE or undetermined |
|
216
|
|
|
if ($fight->hasDeterminedParent() && $valueToUpdate != null) { |
|
217
|
|
|
$parentFight->$fighterToUpdate = $fight->$valueToUpdate; |
|
218
|
|
|
$parentFight->save(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|