Test Failed
Push — master ( b6687a...d5e75a )
by Julien
06:32
created

Fight::dontHave0Fighters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace Xoco70\LaravelTournaments\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
        parent::__construct();
18
        $this->c1 = $userId1;
19
        $this->c2 = $userId2;
20
    }
21
22
    protected $table = 'fight';
23
    public $timestamps = true;
24
25
    protected $fillable = [
26
        'group_id',
27
        'c1',
28
        'c2',
29
    ];
30
31
32
    /**
33
     * Get First Fighter.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
36
     */
37
    public function group()
38
    {
39
        return $this->belongsTo(FightersGroup::class, 'fighters_group_id');
40
    }
41
42
    /**
43
     * @param FightersGroup|null $group
44
     * @return Collection
45
     */
46
    protected static function getFightersWithByes(FightersGroup $group)
47
    {
48
        if ($group == null) return null;
49
        $fighters = $group->getFightersWithBye();
50
        $fighterType = $group->getFighterType();
51
        if (sizeof($fighters) == 0) {
52
            $fighters->push(new $fighterType);
53
            $fighters->push(new $fighterType);
54
        } else if (count($fighters) % 2 != 0) {
55
            $fighters->push(new $fighterType);
56
        }
57
58
        return $fighters;
59
    }
60
61
    /**
62
     * Get First Fighter.
63
     *
64
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
65
     */
66
    public function competitor1()
67
    {
68
        return $this->belongsTo(Competitor::class, 'c1', 'id');
69
    }
70
71
    /**
72
     * Get Second Fighter.
73
     *
74
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
75
     */
76
    public function competitor2()
77
    {
78
        return $this->belongsTo(Competitor::class, 'c2', 'id');
79
    }
80
81
    /**
82
     * Get First Fighter.
83
     *
84
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
85
     */
86
    public function team1()
87
    {
88
        return $this->belongsTo(Team::class, 'c1', 'id');
89
    }
90
91
    /**
92
     * Get Second Fighter.
93
     *
94
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
95
     */
96
    public function team2()
97
    {
98
        return $this->belongsTo(Team::class, 'c2', 'id');
99
    }
100
101
102
    /**
103
     * @param $numFighter
104
     * @param $attr
105
     * @return null|string
106
     */
107
    public function getFighterAttr($numFighter, $attr)
108
    {
109
        $isTeam = $this->group->championship->category->isTeam;
110
        if ($isTeam) {
111
            $teamToUpdate = 'team' . $numFighter;
112
            return $this->$teamToUpdate == null ? '' : $this->$teamToUpdate->$attr;
113
        }
114
        $competitorToUpdate = 'competitor' . $numFighter;
115
        if ($attr == 'name') {
116
            return $this->$competitorToUpdate == null
117
                ? 'BYE'
118
                : $this->$competitorToUpdate->user->firstname . " " . $this->$competitorToUpdate->user->lastname;
119
        } elseif ($attr == 'short_id') {
120
            return $this->$competitorToUpdate == null ? '' : $this->$competitorToUpdate->short_id;
121
        }
122
        return null;
123
    }
124
125
126
    /**
127
     * @return bool
128
     */
129
    public function shouldBeInFightList()
130
    {
131
        if ($this->belongsToFirstRound() && $this->dontHave2Fighters()) return false;
132
        if (!$this->belongsToFirstRound() && $this->dontHave0Fighters()) return true;
133
        if ($this->has2Fighters()) return true;
134
        // We aint in the first round, and there is 1 or 0 competitor
135
        // We check children, and see :
136
        // if there is 2  - 2 fighters -> undetermine, we cannot add it to fight list
137
        // if there is 2  - 1 fighters -> undetermine, we cannot add it to fight list
138
        // if there is 2  - 0 fighters -> undetermine, we cannot add it to fight list
139
        // if there is 1  - 2 fighters -> undetermine, we cannot add it to fight list
140
        // if there is 1  - 1 fighters -> fight should have 2 fighters, undetermines
141
        // if there is 1  - 0 fighters -> determined, fight should not be in the list
142
        // if there is 0  - 1 fighters -> determined, fight should not be in the list
143
        // So anyway, we should return false
144
        return false;
145
    }
146
147
    /**
148
     * return true if fight has 2 fighters ( No BYE )
149
     * @return bool
150
     */
151
    public function has2Fighters(): bool
152
    {
153
        return $this->c1 != null && $this->c2 != null;
154
    }
155
156
    /**
157
     * @return bool
158
     */
159
    private function belongsToFirstRound()
160
    {
161
        $firstRoundFights = $this->group->championship->firstRoundFights->pluck('id')->toArray();
162
        if (in_array($this->id, $firstRoundFights)) return true;
163
        return false;
164
    }
165
166
    /**
167
     * @return bool
168
     */
169
    private function dontHave2Fighters()
170
    {
171
        return $this->c1 == null || $this->c2 == null;
172
    }
173
174
    /**
175
     * @return bool
176
     */
177
    private function dontHave0Fighters()
178
    {
179
        return $this->c1 != null || $this->c2 != null;
180
    }
181
182
183
    /**
184
     * @param Championship $championship
185
     */
186
    public static function generateFightsId(Championship $championship)
187
    {
188
        $order = 1;
189
        foreach ($championship->fights as $fight) {
190
            $order = $fight->updateShortId($order);
191
        }
192
    }
193
194
    /**
195
     * @param $order
196
     * @return int
197
     */
198
    public function updateShortId($order)
199
    {
200
        if ($this->shouldBeInFightList()) {
201
            $this->short_id = $order;
202
            $this->save();
203
            return ++$order;
204
        }
205
        return $order;
206
    }
207
208
}