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
|
|
|
|
10
|
|
|
class FightersGroup extends Model |
11
|
|
|
{ |
12
|
|
|
protected $table = 'fighters_groups'; |
13
|
|
|
public $timestamps = true; |
14
|
|
|
protected $guarded = ['id']; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Check if Request contains tournamentSlug / Should Move to TreeRequest When Built. |
18
|
|
|
* |
19
|
|
|
* @param $request |
20
|
|
|
* |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
|
|
public static function hasTournamentInRequest($request) |
24
|
|
|
{ |
25
|
|
|
return $request->tournament != null; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Check if Request contains championshipId / Should Move to TreeRequest When Built. |
30
|
|
|
* |
31
|
|
|
* @param $request |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
public static function hasChampionshipInRequest($request) |
36
|
|
|
{ |
37
|
|
|
return $request->championshipId != null; // has return false, don't know why |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
42
|
|
|
*/ |
43
|
|
|
public function championship() |
44
|
|
|
{ |
45
|
|
|
return $this->belongsTo(Championship::class); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
50
|
|
|
*/ |
51
|
|
|
public function fights() |
52
|
|
|
{ |
53
|
|
|
return $this->hasMany(Fight::class); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function teams() |
57
|
|
|
{ |
58
|
|
|
return $this->belongsToMany(Team::class, 'fighters_group_team')->withTimestamps(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function competitors() |
62
|
|
|
{ |
63
|
|
|
return $this->belongsToMany(Competitor::class, 'fighters_group_competitor')->withTimestamps(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Collection $fightersGroup |
68
|
|
|
* @param $settings |
69
|
|
|
*/ |
70
|
|
|
public static function generateFights(Collection $fightersGroup, $settings, Championship $championship = null) |
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
// Delete previous fight for this championship |
74
|
|
|
|
75
|
|
|
$arrGroupsId = $fightersGroup->map(function ($value, $key) { |
|
|
|
|
76
|
|
|
return $value->id; |
77
|
|
|
})->toArray(); |
78
|
|
|
Fight::destroy($arrGroupsId); |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
if ($settings->hasPreliminary && $settings->preliminaryGroupSize == 3) { |
82
|
|
|
for ($numGroup = 1; $numGroup <= $settings->preliminaryGroupSize; $numGroup++) { |
83
|
|
|
|
84
|
|
|
Fight::savePreliminaryFightGroup($fightersGroup, $numGroup); |
85
|
|
|
} |
86
|
|
|
} else { |
87
|
|
|
Fight::saveGroupFights($championship, $fightersGroup); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Supercharge of sync Many2Many function. |
93
|
|
|
* Original sync doesn't insert NULL ids. |
94
|
|
|
* |
95
|
|
|
* @param $fighters |
96
|
|
|
*/ |
97
|
|
|
public function syncTeams($fighters) |
98
|
|
|
{ |
99
|
|
|
$this->teams()->detach(); |
100
|
|
|
foreach ($fighters as $fighter) { |
101
|
|
|
if ($fighter != null) { |
102
|
|
|
$this->teams()->attach($fighter); |
103
|
|
|
} else { |
104
|
|
|
// Insert row manually |
105
|
|
|
DB::table('fighters_group_team')->insertGetId( |
106
|
|
|
['team_id' => null, 'fighters_group_id' => $this->id] |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
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 syncCompetitors($fighters) |
119
|
|
|
{ |
120
|
|
|
$this->competitors()->detach(); |
121
|
|
|
foreach ($fighters as $fighter) { |
122
|
|
|
if ($fighter != null) { |
123
|
|
|
$this->competitors()->attach($fighter); |
124
|
|
|
} else { |
125
|
|
|
DB::table('fighters_group_competitor')->insertGetId( |
126
|
|
|
['competitor_id' => null, 'fighters_group_id' => $this->id, |
127
|
|
|
"created_at" => Carbon::now(), |
128
|
|
|
"updated_at" => Carbon::now(), |
129
|
|
|
] |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the many 2 many relationship with |
138
|
|
|
* @return Collection |
139
|
|
|
*/ |
140
|
|
|
public function competitorsWithNull() |
141
|
|
|
{ |
142
|
|
|
$competitors = new Collection(); |
143
|
|
|
$fgcs = FighterGroupCompetitor::where('fighters_group_id', $this->id) |
144
|
|
|
->with('competitor') |
145
|
|
|
->get(); |
146
|
|
|
foreach ($fgcs as $fgc) { |
147
|
|
|
$competitors->push($fgc->competitor ?? new Competitor()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $competitors; |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
public function teamsWithNull() |
156
|
|
|
{ |
157
|
|
|
$teams = new Collection(); |
158
|
|
|
$fgcs = FighterGroupTeam::where('fighters_group_id', $this->id) |
159
|
|
|
->with('team') |
160
|
|
|
->get(); |
161
|
|
|
foreach ($fgcs as $fgc) { |
162
|
|
|
$teams->push($fgc->team ?? new Team()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $teams; |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.