Completed
Push — master ( df2731...5214c7 )
by Julien
02:35
created

Fight::belongsToFirstRound()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Xoco70\KendoTournaments\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
8
class Fight extends Model
9
{
10
    /**
11
     * Fight constructor.
12
     * @param int $userId1
13
     * @param int $userId2
14
     */
15
    public function __construct($userId1 = null, $userId2 = null)
16
    {
17
        $this->c1 = $userId1;
18
        $this->c2 = $userId2;
19
    }
20
21
    protected $table = 'fight';
22
    public $timestamps = true;
23
24
    protected $fillable = [
25
        'group_id',
26
        'c1',
27
        'c2',
28
    ];
29
30
    /**
31
     * @param $group
32
     * @param $competitor1
33
     * @param $competitor2
34
     * @param $order
35
     * @return mixed
36
     */
37
    private static function createPreliminaryFight($group, $competitor1, $competitor2, $order)
38
    {
39
        $fight = new self();
40
        $fight->fighters_group_id = $group->id;
41
        $fight->c1 = $competitor1 != null ? $competitor1->id : null;
42
        $fight->c2 = $competitor2 != null ? $competitor2->id : null;
43
        $fight->short_id = $order++;
44
        $fight->area = $group->area;
45
        $fight->save();
46
        return $order;
47
    }
48
49
    /**
50
     * Get First Fighter.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
53
     */
54
    public function group()
55
    {
56
        return $this->belongsTo(FightersGroup::class, 'fighters_group_id');
57
    }
58
59
    /**
60
     * @param Championship $championship
61
     *
62
     * @return Collection
63
     */
64
    private static function getActorsToFights(Championship $championship, FightersGroup $group = null)
65
    {
66
        if ($group == null) return null;
67
        $fighters = $group->getFighters();
68
        $fighterType = $group->getFighterType();
69
        if (sizeof($fighters) == 0) {
70
            $fighters->push(new $fighterType);
71
            $fighters->push(new $fighterType);
72
        } else if (count($fighters) % 2 != 0) {
73
            $fighters->push(new $fighterType);
74
        }
75
76
        return $fighters;
77
    }
78
79
    /**
80
     * Get First Fighter.
81
     *
82
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
83
     */
84
    public function competitor1()
85
    {
86
        return $this->belongsTo(Competitor::class, 'c1', 'id');
87
    }
88
89
    /**
90
     * Get Second Fighter.
91
     *
92
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
93
     */
94
    public function competitor2()
95
    {
96
        return $this->belongsTo(Competitor::class, 'c2', 'id');
97
    }
98
99
    /**
100
     * Get First Fighter.
101
     *
102
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
103
     */
104
    public function team1()
105
    {
106
        return $this->belongsTo(Team::class, 'c1', 'id');
107
    }
108
109
    /**
110
     * Get Second Fighter.
111
     *
112
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
113
     */
114
    public function team2()
115
    {
116
        return $this->belongsTo(Team::class, 'c2', 'id');
117
    }
118
119
    /**
120
     * Save a Fight.
121
     *
122
     * @param Collection $groups
123
     * @param int $numGroup
124
     */
125
    public static function savePreliminaryFightGroup($groups, $numGroup = 1)
126
    {
127
        $competitor1 = $competitor2 = null;
128
        $order = 1;
129
130
        foreach ($groups as $group) {
131
132
            $fighters = $group->getFighters();
133
134
            $fighter1 = $fighters->get(0);
135
            $fighter2 = $fighters->get(1);
136
            $fighter3 = $fighters->get(2);
137
138
            switch ($numGroup) {
139
                case 1:
140
                    $competitor1 = $fighter1;
141
                    $competitor2 = $fighter2;
142
                    break;
143
                case 2:
144
                    $competitor1 = $fighter2;
145
                    $competitor2 = $fighter3;
146
                    break;
147
                case 3:
148
                    $competitor1 = $fighter3;
149
                    $competitor2 = $fighter1;
150
                    break;
151
            }
152
            $order = self::createPreliminaryFight($group, $competitor1, $competitor2, $order);
153
        }
154
    }
155
156
157
    /**
158
     * @param Championship $championship
159
     */
160
    public static function saveGroupFights(Championship $championship)
161
    {
162
//        $order = 1;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
163
        $round = [];
164
        foreach ($championship->fightersGroups()->get() as $group) {
165
            $fighters = self::getActorsToFights($championship, $group);
166
            $away = $fighters->splice(count($fighters) / 2); // 2
167
            $home = $fighters; // 1
168
169
            $countHome = count($home);
170
            $countAway = count($away);
171
            for ($i = 0; $i < $countHome + $countAway - 1; $i++) {
172
                for ($j = 0; $j < $countHome; $j++) {
173
174
                    $round[$i][$j]['Home'] = $home[$j];
175
                    $round[$i][$j]['Away'] = $away[$j];
176
                    $fight = new self();
177
                    $fight->fighters_group_id = $group->id;
178
                    $fight->c1 = $round[$i][$j]['Home']->id;
179
                    $fight->c2 = $round[$i][$j]['Away']->id;
180
                    $fight->area = $group->area;
181
                    $fight->save();
182
183
                }
184
                if ($countHome + $countAway - 1 > 2) {
185
                    $away->prepend($home->splice(1, 1)->shift());
186
                    $home->push($away->pop());
187
//                    $order++;
188
                }
189
            }
190
        }
191
    }
192
193
    public function getFighterAttr($numFighter, $attr)
194
    {
195
        $isTeam = $this->group->championship->category->isTeam;
196
        if ($isTeam) {
197
            $teamToUpdate = 'team' . $numFighter;
198
            return $this->$teamToUpdate == null ? '' : $this->$teamToUpdate->$attr;
199
        }
200
        $competitorToUpdate = 'competitor' . $numFighter;
201
        if ($attr == 'name') {
202
            return $this->$competitorToUpdate == null
203
                ? 'BYE'
204
                : $this->$competitorToUpdate->user->firstname . " " . $this->$competitorToUpdate->user->lastname;
205
        } elseif ($attr == 'short_id') {
206
            return $this->$competitorToUpdate == null ? '' : $this->$competitorToUpdate->short_id;
207
        }
208
        return null;
209
    }
210
211
    /**
212
     * Update parent Fight
213
     * @param $fighterToUpdate
214
     * @param $fight
215
     */
216
    public function updateParentFight($fighterToUpdate, $fight)
217
    {
218
        if ($fight != null) {
219 View Code Duplication
            if (($fight->c1 != null || $fight->c2 == null)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
                $this->$fighterToUpdate = $fight->c1;
221
            }
222 View Code Duplication
            if ($fight->c1 == null || $fight->c2 != null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
                $this->$fighterToUpdate = $fight->c2;
224
            }
225
            if ($fight->dontHave2Fighters()) {
226
                $this->$fighterToUpdate = null;
227
            }
228
        }
229
230
    }
231
232
    /**
233
     * Returns the parent field that need to be updated
234
     * @return null|string
235
     */
236
    public function getParentFighterToUpdate()
237
    {
238
        $childrenGroup = $this->group->parent->children;
239
        foreach ($childrenGroup as $key => $children) {
240
            $childFight = $children->fights->get(0);
241
            if ($childFight->id == $this->id) {
242
                if ($key % 2 == 0) {
243
                    return "c1";
244
                }
245
                if ($key % 2 == 1) {
246
                    return "c2";
247
                }
248
            }
249
        }
250
        return null;
251
    }
252
253
    /**
254
     * In the original fight ( child ) return the field that contains data to copy to parent
255
     * @return null|string
256
     */
257
    public function getValueToUpdate()
258
    {
259
        if ($this->c1 != null && $this->c2 != null) {
260
            return null;
261
        }
262
        if ($this->c1 != null) {
263
            return "c1";
264
        }
265
        if ($this->c2 != null) {
266
            return "c2";
267
        }
268
        return null;
269
    }
270
271
    /**
272
     * Check if we are able to fill the parent fight or not
273
     * If one of the children has c1 x c2, then we must wait to fill parent
274
     *
275
     * @return bool
276
     */
277
    public function hasDeterminedParent()
278
    {
279
        if ($this->has2Fighters()) return true;
280
        foreach ($this->group->children as $child) {
281
            $fight = $child->fights->get(0);
282
            if ($fight->has2Fighters()) return false;
283
        }
284
        return true;
285
    }
286
287
    public function shouldBeInFightList()
288
    {
289
        if ($this->belongsToFirstRound() && $this->dontHave2Fighters()) return false;
290
        if ($this->has2Fighters()) return true;
291
        // We aint in the first round, and there is 1 or 0 competitor
292
        // We check children, and see :
293
        // if there is 2  - 2 fighters -> undetermine, we cannot add it to fight list
294
        // if there is 2  - 1 fighters -> undetermine, we cannot add it to fight list
295
        // if there is 2  - 0 fighters -> undetermine, we cannot add it to fight list
296
        // if there is 1  - 2 fighters -> undetermine, we cannot add it to fight list
297
        // if there is 1  - 1 fighters -> fight should have 2 fighters, undetermines
298
        // if there is 1  - 0 fighters -> determined, fight should not be in the list
299
        // if there is 0  - 1 fighters -> determined, fight should not be in the list
300
        // So anyway, we should return false
301
        return false;
302
    }
303
304
    /**
305
     * return true if fight has 2 fighters ( No BYE )
306
     * @return bool
307
     */
308
    private function has2Fighters(): bool
309
    {
310
        return $this->c1 != null && $this->c2 != null;
311
    }
312
313
    private function belongsToFirstRound()
314
    {
315
        $firstRoundFights = $this->group->championship->firstRoundFights->pluck('id')->toArray();
316
        if (in_array($this->id, $firstRoundFights)) return true;
317
        return false;
318
    }
319
320
    private function dontHave2Fighters() // 1 or 0
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
321
    {
322
        return $this->c1 == null || $this->c2 == null;
323
    }
324
325
326
    public static function generateFightsId($championship)
327
    {
328
        $order = 1;
329
        foreach ($championship->fights as $fight) {
330
            $order = $fight->updateShortId($order);
331
        }
332
    }
333
334
    /**
335
     * @param $order
336
     * @return int
337
     */
338
    public function updateShortId($order)
339
    {
340
        if ($this->shouldBeInFightList()) {
341
            $this->short_id = $order;
342
            $this->save();
343
            return ++$order;
344
        }
345
        return $order;
346
    }
347
348
}