1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xoco70\LaravelTournaments\TreeGen; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Xoco70\LaravelTournaments\Models\Fight; |
7
|
|
|
use Xoco70\LaravelTournaments\Models\FighterGroupTeam; |
8
|
|
|
use Xoco70\LaravelTournaments\Models\FightersGroup; |
9
|
|
|
use Xoco70\LaravelTournaments\Models\Team; |
10
|
|
|
|
11
|
|
View Code Duplication |
class SingleEliminationTeamTreeGen extends SingleEliminationTreeGen |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* get Fighter by Id. |
15
|
|
|
* |
16
|
|
|
* @return Team |
17
|
|
|
*/ |
18
|
|
|
protected function getFighter($teamId) |
19
|
|
|
{ |
20
|
|
|
return Team::find($teamId); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Fighter is the name for competitor or team, depending on the case. |
25
|
|
|
* |
26
|
|
|
* @return Collection |
27
|
|
|
*/ |
28
|
|
|
protected function getFighters() |
29
|
|
|
{ |
30
|
|
|
return $this->championship->teams; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param FightersGroup $group |
35
|
|
|
* @param $fighters |
36
|
|
|
* |
37
|
|
|
* @return FightersGroup |
38
|
|
|
*/ |
39
|
|
|
public function syncGroup(FightersGroup $group, $fighters) |
40
|
|
|
{ |
41
|
|
|
// Add all competitors to Pivot Table |
42
|
|
|
return $group->syncTeams($fighters); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function createByeFighter() |
46
|
|
|
{ |
47
|
|
|
return new Team(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function addFighterToGroup(FightersGroup $group, $team) |
51
|
|
|
{ |
52
|
|
|
$group->teams()->attach($team->id); |
53
|
|
|
} |
54
|
|
|
/** |
55
|
|
|
* Chunk Fighters into groups for fighting, and optionnaly shuffle. |
56
|
|
|
* |
57
|
|
|
* @param $fightersByEntity |
58
|
|
|
* |
59
|
|
|
* @return Collection|null |
60
|
|
|
*/ |
61
|
|
|
protected function chunkAndShuffle(Collection $fightersByEntity) |
62
|
|
|
{ |
63
|
|
|
if ($this->championship->hasPreliminary()) { |
64
|
|
|
return (new PlayOffTeamTreeGen($this->championship, null))->chunkAndShuffle($fightersByEntity); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
$fightersGroup = null; |
|
|
|
|
67
|
|
|
|
68
|
|
|
$fightersGroup = $fightersByEntity->chunk(2); |
69
|
|
|
if (!app()->runningUnitTests()) { |
70
|
|
|
$fightersGroup = $fightersGroup->shuffle(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $fightersGroup; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param FightersGroup $group |
79
|
|
|
* @param Fight $fight |
80
|
|
|
* @param $oldTeams |
81
|
|
|
*/ |
82
|
|
|
protected function updateGroupFighters(FightersGroup $group, Fight $fight, $oldTeams) |
83
|
|
|
{ |
84
|
|
|
|
85
|
|
|
for ($num = 0; $num <=1;$num++){ |
86
|
|
|
$c1or2 = "c".($num+1); |
87
|
|
|
$fgc = FighterGroupTeam::where('fighters_group_id', $group->id) |
88
|
|
|
->where('team_id', $oldTeams[$num]) |
89
|
|
|
->first(); |
90
|
|
|
$fgc->team_id = $fight->$c1or2; |
91
|
|
|
} |
92
|
|
|
$fgc->save(); |
|
|
|
|
93
|
|
|
|
94
|
|
|
// Get the round of this FGC |
|
|
|
|
95
|
|
|
// $numRound = $fgc->group->round; |
96
|
|
|
// $groupsIdInThisRound = $this->championship->groupsByRound($numRound)->pluck('id'); |
97
|
|
|
// $count = FighterGroupCompetitor::whereIn('fighters_group_id', $groupsIdInThisRound) |
98
|
|
|
// ->where('competitor_id', $fgc->competitor_id) |
99
|
|
|
// ->count(); |
100
|
|
|
// |
101
|
|
|
// if ($count > 1) { |
102
|
|
|
// throw new DuplicatedFighterException(trans('laravel-tournaments::core.duplicated_fighter_in_group')); |
103
|
|
|
// } |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.