Passed
Push — master ( e07b4a...1565da )
by Julien
47:10
created

Fight::savePreliminaryFightGroup()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 30
rs 8.439
cc 5
eloc 22
nc 5
nop 2
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 createFight($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) {
67
            if ($championship->category->isTeam) {
68
                $fighters = $group->teams()->get();
69 View Code Duplication
                if (sizeof($fighters) == 0) {
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...
70
                    $fighters->push(new Team());
71
                    $fighters->push(new Team());
72
                } else if (count($fighters) % 2 != 0) {
73
                    $fighters->push(new Team(['name' => 'BYE']));
74
                }
75
                return $fighters;
76
            } else {
77
                $fighters = $group->competitors()->get();
78 View Code Duplication
                if (sizeof($fighters) == 0) { // If
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...
79
                    $fighters->push(new Competitor());
80
                    $fighters->push(new Competitor());
81
                } else if (count($fighters) % 2 != 0) { // If fighter is not pair, add a BYE
82
                    $fighters->push(new Competitor());
83
                }
84
            }
85
            return $fighters;
86
        }
87
        return null;
88
    }
89
90
    /**
91
     * Get First Fighter.
92
     *
93
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
94
     */
95
    public function competitor1()
96
    {
97
        return $this->belongsTo(Competitor::class, 'c1', 'id');
98
    }
99
100
    /**
101
     * Get Second Fighter.
102
     *
103
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
104
     */
105
    public function competitor2()
106
    {
107
        return $this->belongsTo(Competitor::class, 'c2', 'id');
108
    }
109
110
    /**
111
     * Get First Fighter.
112
     *
113
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
114
     */
115
    public function team1()
116
    {
117
        return $this->belongsTo(Team::class, 'c1', 'id');
118
    }
119
120
    /**
121
     * Get Second Fighter.
122
     *
123
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
124
     */
125
    public function team2()
126
    {
127
        return $this->belongsTo(Team::class, 'c2', 'id');
128
    }
129
130
    /**
131
     * Save a Fight.
132
     *
133
     * @param Collection $groups
134
     * @param int $numGroup
135
     */
136
    public static function savePreliminaryFightGroup($groups, $numGroup = 1)
137
    {
138
        $competitor1 = $competitor2 = null;
139
        $order = 1;
140
141
        foreach ($groups as $group) {
142
143
            $fighters = $group->getFighters();
144
145
            $fighter1 = $fighters->get(0);
146
            $fighter2 = $fighters->get(1);
147
            $fighter3 = $fighters->get(2);
148
149
            switch ($numGroup) {
150
                case 1:
151
                    $competitor1 = $fighter1;
152
                    $competitor2 = $fighter2;
153
                    break;
154
                case 2:
155
                    $competitor1 = $fighter2;
156
                    $competitor2 = $fighter3;
157
                    break;
158
                case 3:
159
                    $competitor1 = $fighter3;
160
                    $competitor2 = $fighter1;
161
                    break;
162
            }
163
            $order = self::createFight($group, $competitor1, $competitor2, $order);
164
        }
165
    }
166
167
168
    /**
169
     * @param Championship $championship
170
     */
171
    public static function saveGroupFights(Championship $championship)
172
    {
173
        $order = 1;
174
        $round = [];
175
        foreach ($championship->fightersGroups()->get() as $group) {
176
            $fighters = self::getActorsToFights($championship, $group);
177
            $away = $fighters->splice(count($fighters) / 2); // 2
178
            $home = $fighters; // 1
179
180
            $countHome = count($home);
181
            $countAway = count($away);
182
            for ($i = 0; $i < $countHome + $countAway - 1; $i++) {
183
                for ($j = 0; $j < $countHome; $j++) {
184
185
                    $round[$i][$j]['Home'] = $home[$j];
186
                    $round[$i][$j]['Away'] = $away[$j];
187
                    $fight = new self();
188
                    $fight->fighters_group_id = $group->id;
189
                    $fight->c1 = $round[$i][$j]['Home']->id;
190
                    $fight->c2 = $round[$i][$j]['Away']->id;
191
                    $fight->short_id = $order++;
192
                    $fight->area = $group->area;
193
                    $fight->save();
194
195
                }
196
                if ($countHome + $countAway - 1 > 2) {
197
                    $away->prepend($home->splice(1, 1)->shift());
198
                    $home->push($away->pop());
199
                    $order++;
200
                }
201
            }
202
        }
203
    }
204
205
    public function getFighterAttr($numFighter, $attr)
206
    {
207
        $isTeam = $this->group->championship->category->isTeam;
208
        if ($isTeam) {
209
            $teamToUpdate = 'team' . $numFighter;
210
            return $this->$teamToUpdate == null ? '' : $this->$teamToUpdate->$attr;
211
        }
212
        $competitorToUpdate = 'competitor' . $numFighter;
213
        if ($attr == 'name') {
214
            return $this->$competitorToUpdate == null
215
                ? 'BYE'
216
                : $this->$competitorToUpdate->user->firstname . " " . $this->$competitorToUpdate->user->lastname;
217
        } elseif ($attr == 'short_id') {
218
            return $this->$competitorToUpdate == null ? '' : $this->$competitorToUpdate->short_id;
219
        }
220
        return null;
221
    }
222
223
    /**
224
     * Update parent Fight
225
     */
226
    public function updateParentFight($fighterToUpdate, $fight)
227
    {
228
229 View Code Duplication
        if ($fight != null && ($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...
230
            $this->$fighterToUpdate = $fight->c1;
231
        }
232 View Code Duplication
        if ($fight != null && $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...
233
            $this->$fighterToUpdate = $fight->c2;
234
        }
235
        if ($fight->c1 == null || $fight->c2 == null) {
236
            $this->$fighterToUpdate = null;
237
        }
238
    }
239
240
    /**
241
     * Returns the parent field that need to be updated
242
     * @return null|string
243
     */
244
    public function getParentFighterToUpdate()
245
    {
246
        $childrenGroup = $this->group->parent->children;
247
        foreach ($childrenGroup as $key => $children) {
248
            $childFight = $children->fights->get(0);
249
            if ($childFight->id == $this->id) {
250
                if ($key % 2 == 0) {
251
                    return "c1";
252
                }
253
                if ($key % 2 == 1) {
254
                    return "c2";
255
                }
256
            }
257
        }
258
        return null;
259
    }
260
261
    /**
262
     * In the original fight ( child ) return the field that contains data to copy to parent
263
     * @return null|string
264
     */
265
    public function getValueToUpdate()
266
    {
267
        if ($this->c1 != null && $this->c2 != null) {
268
            return null;
269
        }
270
        if ($this->c1 != null) {
271
            return "c1";
272
        }
273
        if ($this->c2 != null) {
274
            return "c2";
275
        }
276
        return null;
277
    }
278
279
    /**
280
     * Check if we are able to fill the parent fight or not
281
     * If one of the children has c1 x c2, then we must wait to fill parent
282
     *
283
     * @return bool
284
     */
285
    function hasDeterminedParent()
286
    {
287
288
        if ($this->c1 != null && $this->c2 != null) return true;
289
        foreach ($this->group->children as $child) {
290
            $fight = $child->fights->get(0);
291
            if ($fight->c1 != null && $fight->c2 != null) return false;
292
        }
293
        return true;
294
    }
295
296
}