Completed
Push — master ( 12bd23...4aeb2b )
by Julien
02:03
created

FightersGroup::fightersWithBye()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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