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