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 Xoco70\KendoTournaments\TreeGen\TreeGen; |
10
|
|
|
|
11
|
|
|
class FightersGroup extends Model |
12
|
|
|
{ |
13
|
|
|
protected $table = 'fighters_groups'; |
14
|
|
|
public $timestamps = true; |
15
|
|
|
protected $guarded = ['id']; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Check if Request contains tournamentSlug / Should Move to TreeRequest When Built. |
19
|
|
|
* |
20
|
|
|
* @param $request |
21
|
|
|
* |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public static function hasTournamentInRequest($request) |
25
|
|
|
{ |
26
|
|
|
return $request->tournament != null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Check if Request contains championshipId / Should Move to TreeRequest When Built. |
31
|
|
|
* |
32
|
|
|
* @param $request |
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
public static function hasChampionshipInRequest($request) |
37
|
|
|
{ |
38
|
|
|
return $request->championshipId != null; // has return false, don't know why |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
43
|
|
|
*/ |
44
|
|
|
public function championship() |
45
|
|
|
{ |
46
|
|
|
return $this->belongsTo(Championship::class); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
51
|
|
|
*/ |
52
|
|
|
public function fights() |
53
|
|
|
{ |
54
|
|
|
return $this->hasMany(Fight::class); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function teams() |
58
|
|
|
{ |
59
|
|
|
return $this->belongsToMany(Team::class, 'fighters_group_team')->withTimestamps(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function competitors() |
63
|
|
|
{ |
64
|
|
|
return $this->belongsToMany(Competitor::class, 'fighters_group_competitor')->withTimestamps(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Collection $fightersGroup |
69
|
|
|
* @param $settings |
70
|
|
|
*/ |
71
|
|
|
public static function generateFights(Collection $fightersGroup, $settings, Championship $championship = null) |
72
|
|
|
{ |
73
|
|
|
|
74
|
|
|
// Delete previous fight for this championship |
75
|
|
|
|
76
|
|
|
$arrGroupsId = $fightersGroup->map(function ($value, $key) { |
|
|
|
|
77
|
|
|
return $value->id; |
78
|
|
|
})->toArray(); |
79
|
|
|
Fight::destroy($arrGroupsId); |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
if ($settings->hasPreliminary && $settings->preliminaryGroupSize == 3) { |
83
|
|
|
for ($numGroup = 1; $numGroup <= $settings->preliminaryGroupSize; $numGroup++) { |
84
|
|
|
|
85
|
|
|
Fight::savePreliminaryFightGroup($fightersGroup, $numGroup); |
86
|
|
|
} |
87
|
|
|
} else { |
88
|
|
|
Fight::saveGroupFights($championship, $fightersGroup); |
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
|
|
|
* @return Collection |
140
|
|
|
*/ |
141
|
|
|
public function competitorsWithNull() |
142
|
|
|
{ |
143
|
|
|
$competitors = new Collection(); |
144
|
|
|
$fgcs = FighterGroupCompetitor::where('fighters_group_id', $this->id) |
145
|
|
|
->with('competitor') |
146
|
|
|
->get(); |
147
|
|
|
foreach ($fgcs as $fgc) { |
148
|
|
|
$competitors->push($fgc->competitor ?? new Competitor()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $competitors; |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
public function teamsWithNull() |
157
|
|
|
{ |
158
|
|
|
$teams = new Collection(); |
159
|
|
|
$fgcs = FighterGroupTeam::where('fighters_group_id', $this->id) |
160
|
|
|
->with('team') |
161
|
|
|
->get(); |
162
|
|
|
foreach ($fgcs as $fgc) { |
163
|
|
|
$teams->push($fgc->team ?? new Team()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $teams; |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getFighters() |
171
|
|
|
{ |
172
|
|
|
|
173
|
|
|
if ($this->championship->category->isTeam()) { |
174
|
|
|
$fighters = $this->teamsWithNull(); |
175
|
|
|
} else { |
176
|
|
|
$fighters = $this->competitorsWithNull(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if (sizeof($fighters) == 0) { |
180
|
|
|
$treeGen = new TreeGen($this->championship, null, null); |
|
|
|
|
181
|
|
|
$fighters = $treeGen->createByeGroup(2); |
182
|
|
|
} |
183
|
|
|
return $fighters; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.