Completed
Push — master ( 1f6d44...f65102 )
by Julien
02:28
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
dl 0
loc 14
rs 9.2
c 2
b 1
f 0
cc 4
eloc 10
nc 4
nop 1
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
    /**
32
     * Get First Fighter.
33
     *
34
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
35
     */
36
    public function group()
37
    {
38
        return $this->belongsTo(FightersGroup::class, 'fighters_group_id');
39
    }
40
41
    /**
42
     * @param FightersGroup|null $group
43
     * @return Collection
44
     * @internal param Championship $championship
45
     *
46
     */
47
    protected static function getFightersWithNull(FightersGroup $group)
48
    {
49
        if ($group == null) return null;
50
        $fighters = $group->getFighters();
51
        $fighterType = $group->getFighterType();
52
        if (sizeof($fighters) == 0) {
53
            $fighters->push(new $fighterType);
54
            $fighters->push(new $fighterType);
55
        } else if (count($fighters) % 2 != 0) {
56
            $fighters->push(new $fighterType);
57
        }
58
59
        return $fighters;
60
    }
61
62
    /**
63
     * Get First Fighter.
64
     *
65
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
66
     */
67
    public function competitor1()
68
    {
69
        return $this->belongsTo(Competitor::class, 'c1', 'id');
70
    }
71
72
    /**
73
     * Get Second Fighter.
74
     *
75
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
76
     */
77
    public function competitor2()
78
    {
79
        return $this->belongsTo(Competitor::class, 'c2', 'id');
80
    }
81
82
    /**
83
     * Get First Fighter.
84
     *
85
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
86
     */
87
    public function team1()
88
    {
89
        return $this->belongsTo(Team::class, 'c1', 'id');
90
    }
91
92
    /**
93
     * Get Second Fighter.
94
     *
95
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
96
     */
97
    public function team2()
98
    {
99
        return $this->belongsTo(Team::class, 'c2', 'id');
100
    }
101
102
103
    public function getFighterAttr($numFighter, $attr)
104
    {
105
        $isTeam = $this->group->championship->category->isTeam;
106
        if ($isTeam) {
107
            $teamToUpdate = 'team' . $numFighter;
108
            return $this->$teamToUpdate == null ? '' : $this->$teamToUpdate->$attr;
109
        }
110
        $competitorToUpdate = 'competitor' . $numFighter;
111
        if ($attr == 'name') {
112
            return $this->$competitorToUpdate == null
113
                ? 'BYE'
114
                : $this->$competitorToUpdate->user->firstname . " " . $this->$competitorToUpdate->user->lastname;
115
        } elseif ($attr == 'short_id') {
116
            return $this->$competitorToUpdate == null ? '' : $this->$competitorToUpdate->short_id;
117
        }
118
        return null;
119
    }
120
121
    /**
122
     * Update parent Fight
123
     * @param $fighterToUpdate
124
     * @param $fight
125
     */
126
    public function updateParentFight($fighterToUpdate, $fight)
127
    {
128
        if ($fight != null) {
129 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...
130
                $this->$fighterToUpdate = $fight->c1;
131
            }
132 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...
133
                $this->$fighterToUpdate = $fight->c2;
134
            }
135
            if ($fight->dontHave2Fighters()) {
136
                $this->$fighterToUpdate = null;
137
            }
138
        }
139
140
    }
141
142
    /**
143
     * Returns the parent field that need to be updated
144
     * @return null|string
145
     */
146
    public function getParentFighterToUpdate()
147
    {
148
        $childrenGroup = $this->group->parent->children;
149
        foreach ($childrenGroup as $key => $children) {
150
            $childFight = $children->fights->get(0);
151
            if ($childFight->id == $this->id) {
152
                if ($key % 2 == 0) {
153
                    return "c1";
154
                }
155
                if ($key % 2 == 1) {
156
                    return "c2";
157
                }
158
            }
159
        }
160
        return null;
161
    }
162
163
    /**
164
     * In the original fight ( child ) return the field that contains data to copy to parent
165
     * @return null|string
166
     */
167
    public function getValueToUpdate()
168
    {
169
        if ($this->c1 != null && $this->c2 != null) {
170
            return null;
171
        }
172
        if ($this->c1 != null) {
173
            return "c1";
174
        }
175
        if ($this->c2 != null) {
176
            return "c2";
177
        }
178
        return null;
179
    }
180
181
    /**
182
     * Check if we are able to fill the parent fight or not
183
     * If one of the children has c1 x c2, then we must wait to fill parent
184
     *
185
     * @return bool
186
     */
187
    public function hasDeterminedParent()
188
    {
189
        if ($this->has2Fighters()) return true;
190
        foreach ($this->group->children as $child) {
191
            $fight = $child->fights->get(0);
192
            if ($fight->has2Fighters()) return false;
193
        }
194
        return true;
195
    }
196
197
    public function shouldBeInFightList()
198
    {
199
        if ($this->belongsToFirstRound() && $this->dontHave2Fighters()) return false;
200
        if ($this->has2Fighters()) return true;
201
        // We aint in the first round, and there is 1 or 0 competitor
202
        // We check children, and see :
203
        // if there is 2  - 2 fighters -> undetermine, we cannot add it to fight list
204
        // if there is 2  - 1 fighters -> undetermine, we cannot add it to fight list
205
        // if there is 2  - 0 fighters -> undetermine, we cannot add it to fight list
206
        // if there is 1  - 2 fighters -> undetermine, we cannot add it to fight list
207
        // if there is 1  - 1 fighters -> fight should have 2 fighters, undetermines
208
        // if there is 1  - 0 fighters -> determined, fight should not be in the list
209
        // if there is 0  - 1 fighters -> determined, fight should not be in the list
210
        // So anyway, we should return false
211
        return false;
212
    }
213
214
    /**
215
     * return true if fight has 2 fighters ( No BYE )
216
     * @return bool
217
     */
218
    private function has2Fighters(): bool
219
    {
220
        return $this->c1 != null && $this->c2 != null;
221
    }
222
223
    private function belongsToFirstRound()
224
    {
225
        $firstRoundFights = $this->group->championship->firstRoundFights->pluck('id')->toArray();
226
        if (in_array($this->id, $firstRoundFights)) return true;
227
        return false;
228
    }
229
230
    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...
231
    {
232
        return $this->c1 == null || $this->c2 == null;
233
    }
234
235
236
    public static function generateFightsId($championship)
237
    {
238
        $order = 1;
239
        foreach ($championship->fights as $fight) {
240
            $order = $fight->updateShortId($order);
241
        }
242
    }
243
244
    /**
245
     * @param $order
246
     * @return int
247
     */
248
    public function updateShortId($order)
249
    {
250
        if ($this->shouldBeInFightList()) {
251
            $this->short_id = $order;
252
            $this->save();
253
            return ++$order;
254
        }
255
        return $order;
256
    }
257
258
}