-
Notifications
You must be signed in to change notification settings - Fork 53
/
FightersGroup.php
222 lines (193 loc) · 5.34 KB
/
FightersGroup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace Xoco70\LaravelTournaments\Models;
use Carbon\Carbon;
use ErrorException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Kalnoy\Nestedset\NodeTrait;
class FightersGroup extends Model
{
protected $table = 'fighters_groups';
public $timestamps = true;
protected $guarded = ['id'];
use NodeTrait;
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function championship()
{
return $this->belongsTo(Championship::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function fights()
{
return $this->hasMany(Fight::class);
}
public function fighters()
{
if ($this->championship->category->isTeam()) {
return $this->teams;
}
return $this->competitors;
}
public function teams()
{
return $this->belongsToMany(Team::class, 'fighters_group_team')->withTimestamps();
}
public function competitors()
{
return $this->belongsToMany(Competitor::class, 'fighters_group_competitor')->withTimestamps();
}
/**
* Supercharge of sync Many2Many function.
* Original sync doesn't insert NULL ids.
*
* @param $fighters
*/
public function syncTeams($fighters)
{
$this->teams()->detach();
foreach ($fighters as $fighter) {
if ($fighter != null) {
$this->teams()->attach($fighter);
} else {
// Insert row manually
DB::table('fighters_group_team')->insertGetId(
['team_id' => null, 'fighters_group_id' => $this->id]
);
}
}
}
/**
* Supercharge of sync Many2Many function.
* Original sync doesn't insert NULL ids.
*
* @param $fighters
*/
public function syncCompetitors($fighters)
{
$this->competitors()->detach();
foreach ($fighters as $fighter) {
if ($fighter != null) {
$this->competitors()->attach($fighter);
} else {
DB::table('fighters_group_competitor')->insertGetId(
['competitor_id' => null, 'fighters_group_id' => $this->id,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]
);
}
}
}
/**
* Get the many 2 many relationship with the Null Rows.
*
* @return Collection
*/
public function competitorsWithBye(): Collection
{
$competitors = new Collection();
$fgcs = FighterGroupCompetitor::where('fighters_group_id', $this->id)
->with('competitor')
->get();
foreach ($fgcs as $fgc) {
$competitors->push($fgc->competitor ?? new Competitor());
}
return $competitors;
}
/**
* @return Collection
*/
public function teamsWithBye(): Collection
{
$teams = new Collection();
$fgcs = FighterGroupTeam::where('fighters_group_id', $this->id)
->with('team')
->get();
foreach ($fgcs as $fgc) {
$teams->push($fgc->team ?? new Team());
}
return $teams;
}
/**
* @return Collection
*/
public function getFightersWithBye(): Collection
{
if ($this->championship->category->isTeam()) {
return $this->teamsWithBye();
}
return $this->competitorsWithBye();
}
public static function getBaseNumGroups($initialGroupId, $numGroups, $numRound): int
{
$parentId = $initialGroupId;
for ($i = 1; $i <= $numRound; $i++) {
$parentId += $numGroups / $numRound;
}
return $parentId;
}
/**
* @return string
*/
public function getFighterType()
{
if ($this->championship->category->isTeam()) {
return Team::class;
}
return Competitor::class;
}
/**
* Check if we are able to fill the parent fight or not
* If one of the children has c1 x c2, then we must wait to fill parent.
*
* @return bool
*/
public function hasDeterminedParent()
{
// There is more than 1 fight, should be Preliminary
if (count($this->fighters()) > 1) {
return false;
}
foreach ($this->children as $child) {
if (count($child->fighters()) > 1) {
return false;
}
}
return true;
}
/**
* In the original fight ( child ) return the field that contains data to copy to parent.
*
* @return int
*/
public function getValueToUpdate()
{
try {
if ($this->championship->category->isTeam()) {
return $this->teams->map->id[0];
}
return $this->competitors->map->id[0];
} catch (ErrorException $e) {
return;
}
}
/**
* Returns the parent field that need to be updated.
*
* @return null|string
*/
public function getParentFighterToUpdate($keyGroup)
{
if (intval($keyGroup % 2) == 0) {
return 'c1';
}
if (intval($keyGroup % 2) == 1) {
return 'c2';
}
}
}