Passed
Push — master ( 1565da...84ef8e )
by Julien
71:23 queued 12:22
created

Fight::getActorsToFights()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 10
nc 4
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) 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::createFight($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;
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->short_id = $order++;
181
                    $fight->area = $group->area;
182
                    $fight->save();
183
184
                }
185
                if ($countHome + $countAway - 1 > 2) {
186
                    $away->prepend($home->splice(1, 1)->shift());
187
                    $home->push($away->pop());
188
                    $order++;
189
                }
190
            }
191
        }
192
    }
193
194
    public function getFighterAttr($numFighter, $attr)
195
    {
196
        $isTeam = $this->group->championship->category->isTeam;
197
        if ($isTeam) {
198
            $teamToUpdate = 'team' . $numFighter;
199
            return $this->$teamToUpdate == null ? '' : $this->$teamToUpdate->$attr;
200
        }
201
        $competitorToUpdate = 'competitor' . $numFighter;
202
        if ($attr == 'name') {
203
            return $this->$competitorToUpdate == null
204
                ? 'BYE'
205
                : $this->$competitorToUpdate->user->firstname . " " . $this->$competitorToUpdate->user->lastname;
206
        } elseif ($attr == 'short_id') {
207
            return $this->$competitorToUpdate == null ? '' : $this->$competitorToUpdate->short_id;
208
        }
209
        return null;
210
    }
211
212
    /**
213
     * Update parent Fight
214
     */
215
    public function updateParentFight($fighterToUpdate, $fight)
216
    {
217
218 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...
219
            $this->$fighterToUpdate = $fight->c1;
220
        }
221 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...
222
            $this->$fighterToUpdate = $fight->c2;
223
        }
224
        if ($fight->c1 == null || $fight->c2 == null) {
225
            $this->$fighterToUpdate = null;
226
        }
227
    }
228
229
    /**
230
     * Returns the parent field that need to be updated
231
     * @return null|string
232
     */
233
    public function getParentFighterToUpdate()
234
    {
235
        $childrenGroup = $this->group->parent->children;
236
        foreach ($childrenGroup as $key => $children) {
237
            $childFight = $children->fights->get(0);
238
            if ($childFight->id == $this->id) {
239
                if ($key % 2 == 0) {
240
                    return "c1";
241
                }
242
                if ($key % 2 == 1) {
243
                    return "c2";
244
                }
245
            }
246
        }
247
        return null;
248
    }
249
250
    /**
251
     * In the original fight ( child ) return the field that contains data to copy to parent
252
     * @return null|string
253
     */
254
    public function getValueToUpdate()
255
    {
256
        if ($this->c1 != null && $this->c2 != null) {
257
            return null;
258
        }
259
        if ($this->c1 != null) {
260
            return "c1";
261
        }
262
        if ($this->c2 != null) {
263
            return "c2";
264
        }
265
        return null;
266
    }
267
268
    /**
269
     * Check if we are able to fill the parent fight or not
270
     * If one of the children has c1 x c2, then we must wait to fill parent
271
     *
272
     * @return bool
273
     */
274
    function hasDeterminedParent()
275
    {
276
277
        if ($this->c1 != null && $this->c2 != null) return true;
278
        foreach ($this->group->children as $child) {
279
            $fight = $child->fights->get(0);
280
            if ($fight->c1 != null && $fight->c2 != null) return false;
281
        }
282
        return true;
283
    }
284
285
}