|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xoco70\KendoTournaments\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
|
|
8
|
|
|
class DirectEliminationFight extends Fight |
|
9
|
|
|
{ |
|
10
|
|
View Code Duplication |
public function __construct(Fight $fight = null) |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
if ($fight!=null){ |
|
13
|
|
|
$this->id= $fight->id; |
|
14
|
|
|
$this->short_id= $fight->short_id; |
|
15
|
|
|
$this->fighters_group_id= $fight->fighters_group_id; |
|
16
|
|
|
$this->c1= $fight->c1; |
|
17
|
|
|
$this->c2= $fight->c2; |
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
/** |
|
21
|
|
|
* @param Championship $championship |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function saveFights(Championship $championship, $fromRound = 1) |
|
24
|
|
|
{ |
|
25
|
|
|
$round = []; |
|
26
|
|
|
$groupsFromRound = $championship->groupsFromRound($fromRound)->get(); |
|
27
|
|
|
// dd($groupsFromRound->map->id); |
|
|
|
|
|
|
28
|
|
|
foreach ($groupsFromRound as $group) { |
|
29
|
|
|
$fighters = parent::getFightersWithByes($group); |
|
|
|
|
|
|
30
|
|
|
$away = $fighters->splice(count($fighters) / 2); // 2 |
|
31
|
|
|
$home = $fighters; // 1 |
|
32
|
|
|
|
|
33
|
|
|
$countHome = count($home); |
|
34
|
|
|
$countAway = count($away); |
|
35
|
|
|
for ($i = 0; $i < $countHome + $countAway - 1; $i++) { |
|
36
|
|
|
for ($j = 0; $j < $countHome; $j++) { |
|
37
|
|
|
$round[$i][$j]['Home'] = $home[$j]; |
|
38
|
|
|
$round[$i][$j]['Away'] = $away[$j]; |
|
39
|
|
|
$fight = new Fight(); |
|
40
|
|
|
$fight->fighters_group_id = $group->id; |
|
41
|
|
|
$fight->c1 = $round[$i][$j]['Home']->id; |
|
42
|
|
|
$fight->c2 = $round[$i][$j]['Away']->id; |
|
43
|
|
|
$fight->area = $group->area; |
|
44
|
|
|
$fight->save(); |
|
45
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
if ($countHome + $countAway - 1 > 2) { |
|
48
|
|
|
$away->prepend($home->splice(1, 1)->shift()); |
|
49
|
|
|
$home->push($away->pop()); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
} |
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.