Passed
Push — master ( 14ed1b...ecb8e9 )
by Julien
31:40
created

FightersGroup::teams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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\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
     * @return Collection
140
     */
141
    public function competitorsWithNull(): Collection
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(): Collection
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(): Collection
171
    {
172
        if ($this->championship->category->isTeam()) {
173
            $fighters = $this->teamsWithNull();
174
        } else {
175
            $fighters = $this->competitorsWithNull();
176
        }
177
178
        if (sizeof($fighters) == 0) {
179
            $treeGen = new TreeGen($this->championship, null, null);
0 ignored issues
show
Unused Code introduced by
The call to TreeGen::__construct() has too many arguments starting with null.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
180
            $fighters = $treeGen->createByeGroup(2);
181
        }
182
        return $fighters;
183
    }
184
185
    public static function getBaseNumGroups($initialGroupId, $numGroups, $numRound): int
186
    {
187
        $parentId = $initialGroupId;
188
189
        for ($i = 1; $i <= $numRound; $i++) {
190
            $parentId += $numGroups / $numRound;
191
        }
192
193
        return $parentId;
194
    }
195
196
    /**
197
     * Problem is I can't make the difference between Bye and Empty :(
198
     * @param Championship $championship
199
     */
200
    public static function generateNextRoundsFights(Championship $championship)
201
    {
202
        $maxRounds = 4;
203
        for ($numRound = 1; $numRound < $maxRounds; $numRound++) {
204
            $fightsByRound = $championship->fightsByRound($numRound)->with('group.parent')->get();
205
206
            foreach ($fightsByRound as $numFight => $fight) {
207
208
                $parentGroup = $fight->group->parent;
209
                if ($parentGroup == null ) break;
210
                $parentFight = $parentGroup->fights->get(0);
211
212
                // IN this $fight, if it is the first child or the second child --> c1 or c2 in parent
213
214
                // IN this $fight, is c1 or c2 has the info?
215
                if ($championship->isDirectEliminationType()) {
216
                    // determine wether c1 or c2 must be updated
217
                    $fighterToUpdate = $fight->getParentFighterToUpdate();
218
                    $valueToUpdate = $fight->getValueToUpdate();
219
                    // First Fight
220
                    if ($valueToUpdate != null) {
221
                        $parentFight->$fighterToUpdate = $fight->$valueToUpdate;
222
                        $parentFight->save();
223
                    }
224
                }
225
            }
226
        }
227
    }
228
}
229