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
|
|
|
*/ |
13
|
|
|
public function __construct($userId1 = null, $userId2 = null) |
14
|
|
|
{ |
15
|
|
|
$this->c1 = $userId1; |
16
|
|
|
$this->c2 = $userId2; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected $table = 'fight'; |
20
|
|
|
public $timestamps = true; |
21
|
|
|
|
22
|
|
|
protected $fillable = [ |
23
|
|
|
'group_id', |
24
|
|
|
'c1', |
25
|
|
|
'c2', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Championship $championship |
30
|
|
|
* |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
|
|
private static function getActorsToFights(Championship $championship, FightersGroup $group = null) |
34
|
|
|
{ |
35
|
|
|
if ($championship->category->isTeam) { |
36
|
|
|
$fighters = $group->teams; |
37
|
|
View Code Duplication |
if (sizeof($fighters) == 0){ |
|
|
|
|
38
|
|
|
$fighters->push(new Team()); |
39
|
|
|
$fighters->push(new Team()); |
40
|
|
|
} else if (count($fighters) % 2 != 0) { |
41
|
|
|
$fighters->push(new Team(['name' => 'BYE'])); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
} else { |
45
|
|
|
$fighters = $group->competitors; |
46
|
|
View Code Duplication |
if (sizeof($fighters) == 0){ // If |
|
|
|
|
47
|
|
|
$fighters->push(new Competitor()); |
48
|
|
|
$fighters->push(new Competitor()); |
49
|
|
|
}else if (count($fighters) % 2 != 0) { // If fighter is not pair, add a BYE |
50
|
|
|
$fighters->push(new Competitor()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $fighters; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get First Fighter. |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
63
|
|
|
*/ |
64
|
|
|
public function competitor1() |
65
|
|
|
{ |
66
|
|
|
return $this->belongsTo(Competitor::class, 'c1', 'id'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get Second Fighter. |
71
|
|
|
* |
72
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
73
|
|
|
*/ |
74
|
|
|
public function competitor2() |
75
|
|
|
{ |
76
|
|
|
return $this->belongsTo(Competitor::class, 'c2', 'id'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get First Fighter. |
81
|
|
|
* |
82
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
83
|
|
|
*/ |
84
|
|
|
public function team1() |
85
|
|
|
{ |
86
|
|
|
return $this->belongsTo(Team::class, 'c1', 'id'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get Second Fighter. |
91
|
|
|
* |
92
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
93
|
|
|
*/ |
94
|
|
|
public function team2() |
95
|
|
|
{ |
96
|
|
|
return $this->belongsTo(Team::class, 'c2', 'id'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Save a Fight. |
101
|
|
|
* |
102
|
|
|
* @param Collection $groups |
103
|
|
|
* @param int $numGroup |
104
|
|
|
*/ |
105
|
|
|
public static function savePreliminaryFightGroup($groups, $numGroup = 1) |
106
|
|
|
{ |
107
|
|
|
|
108
|
|
|
$c1 = $c2 = null; |
109
|
|
|
$order = 0; |
110
|
|
|
|
111
|
|
|
foreach ($groups as $group) { |
112
|
|
|
|
113
|
|
|
if ($group->championship->category->isTeam()) { |
114
|
|
|
$fighters = $group->teams; |
115
|
|
|
} else { |
116
|
|
|
$fighters = $group->competitors; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$fighter1 = $fighters->get(0); |
120
|
|
|
$fighter2 = $fighters->get(1); |
121
|
|
|
$fighter3 = $fighters->get(2); |
122
|
|
|
|
123
|
|
|
switch ($numGroup) { |
124
|
|
|
case 1: |
125
|
|
|
$c1 = $fighter1; |
126
|
|
|
$c2 = $fighter2; |
127
|
|
|
break; |
128
|
|
|
case 2: |
129
|
|
|
$c1 = $fighter2; |
130
|
|
|
$c2 = $fighter3; |
131
|
|
|
break; |
132
|
|
|
case 3: |
133
|
|
|
$c1 = $fighter3; |
134
|
|
|
$c2 = $fighter1; |
135
|
|
|
break; |
136
|
|
|
} |
137
|
|
|
$fight = new self(); |
138
|
|
|
$fight->fighters_group_id = $group->id; |
139
|
|
|
$fight->c1 = $c1 != null ? $c1->id : null; |
140
|
|
|
$fight->c2 = $c2 != null ? $c2->id : null; |
141
|
|
|
$fight->order = $order++; |
142
|
|
|
$fight->area = $group->area; |
143
|
|
|
$fight->save(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param Collection $groups |
150
|
|
|
*/ |
151
|
|
|
public static function saveGroupFights(Championship $championship, $groups) |
152
|
|
|
{ |
153
|
|
|
foreach ($groups as $group) { |
154
|
|
|
$fighters = self::getActorsToFights($championship, $group); |
155
|
|
|
|
156
|
|
|
$away = $fighters->splice(count($fighters) / 2); // 2 |
157
|
|
|
|
158
|
|
|
$home = $fighters; // 1 |
159
|
|
|
|
160
|
|
|
$order = 1; |
161
|
|
|
|
162
|
|
|
for ($i = 0; $i < count($home) + count($away) - 1; $i++) { // 0 -> 2 |
|
|
|
|
163
|
|
|
for ($j = 0; $j < count($home); $j++) { // 1 no mas |
|
|
|
|
164
|
|
|
|
165
|
|
|
$round[$i][$j]['Home'] = $home[$j]; |
|
|
|
|
166
|
|
|
$round[$i][$j]['Away'] = $away[$j]; |
|
|
|
|
167
|
|
|
$fight = new self(); |
168
|
|
|
$fight->fighters_group_id = $group->id; |
169
|
|
|
$fight->c1 = $round[$i][$j]['Home']->id; |
170
|
|
|
$fight->c2 = $round[$i][$j]['Away']->id; |
171
|
|
|
$fight->order = $order++; |
172
|
|
|
$fight->area = 1; |
173
|
|
|
|
174
|
|
|
$fight->save(); |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
if (count($home) + count($away) - 1 > 2) { |
178
|
|
|
$away->prepend($home->splice(1, 1)->shift()); |
179
|
|
|
$home->push($away->pop()); |
180
|
|
|
$order++; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
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.