1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xoco70\LaravelTournaments; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Auth\User; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Routing\Controller; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use Xoco70\LaravelTournaments\Exceptions\TreeGenerationException; |
10
|
|
|
use Xoco70\LaravelTournaments\Models\Championship; |
11
|
|
|
use Xoco70\LaravelTournaments\Models\ChampionshipSettings; |
12
|
|
|
use Xoco70\LaravelTournaments\Models\Competitor; |
13
|
|
|
use Xoco70\LaravelTournaments\Models\FightersGroup; |
14
|
|
|
use Xoco70\LaravelTournaments\Models\Team; |
15
|
|
|
use Xoco70\LaravelTournaments\Models\Tournament; |
16
|
|
|
|
17
|
|
|
class TreeController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Display a listing of trees. |
21
|
|
|
* |
22
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
23
|
|
|
*/ |
24
|
|
|
public function index() |
25
|
|
|
{ |
26
|
|
|
$tournament = Tournament::with( |
|
|
|
|
27
|
|
|
'competitors', |
28
|
|
|
'championships.settings', |
29
|
|
|
'championships.category')->first(); |
30
|
|
|
|
31
|
|
|
return view('laravel-tournaments::tree.index') |
|
|
|
|
32
|
|
|
->with('tournament', $tournament); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Build Tree. |
37
|
|
|
* |
38
|
|
|
* @param Request $request |
39
|
|
|
* |
40
|
|
|
* @return \Illuminate\Http\Response|string |
41
|
|
|
*/ |
42
|
|
|
public function store(Request $request, $championshipId) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->deleteEverything(); |
45
|
|
|
$numFighters = $request->numFighters; |
46
|
|
|
$isTeam = $request->isTeam ?? 0; |
47
|
|
|
$championship = $this->provisionObjects($request, $isTeam, $numFighters); |
48
|
|
|
$generation = $championship->chooseGenerationStrategy(); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$generation->run(); |
52
|
|
|
} catch (TreeGenerationException $e) { |
53
|
|
|
redirect()->back() |
54
|
|
|
->withErrors($e->getMessage()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return back() |
58
|
|
|
->with('numFighters', $numFighters) |
59
|
|
|
->with('isTeam', $isTeam); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function deleteEverything() |
63
|
|
|
{ |
64
|
|
|
DB::table('fight')->delete(); |
65
|
|
|
DB::table('fighters_groups')->delete(); |
66
|
|
|
DB::table('fighters_group_competitor')->delete(); |
67
|
|
|
DB::table('fighters_group_team')->delete(); |
68
|
|
|
DB::table('competitor')->delete(); |
69
|
|
|
DB::table('team')->delete(); |
70
|
|
|
DB::table('users')->where('id', '<>', 1)->delete(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Request $request |
75
|
|
|
* @param $isTeam |
76
|
|
|
* @param $numFighters |
77
|
|
|
* |
78
|
|
|
* @return Championship |
79
|
|
|
*/ |
80
|
|
|
protected function provisionObjects(Request $request, $isTeam, $numFighters) |
81
|
|
|
{ |
82
|
|
|
if ($isTeam) { |
83
|
|
|
$championship = Championship::find(2); |
84
|
|
|
factory(Team::class, (int)$numFighters)->create(['championship_id' => $championship->id]); |
85
|
|
|
} else { |
86
|
|
|
$championship = Championship::find(1); |
87
|
|
|
$users = factory(User::class, (int)$numFighters)->create(); |
88
|
|
View Code Duplication |
foreach ($users as $user) { |
|
|
|
|
89
|
|
|
factory(Competitor::class)->create( |
90
|
|
|
['championship_id' => $championship->id, |
91
|
|
|
'user_id' => $user->id, |
92
|
|
|
'confirmed' => 1, |
93
|
|
|
'short_id' => $user->id, |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
$championship->settings = ChampionshipSettings::createOrUpdate($request, $championship); |
99
|
|
|
|
100
|
|
|
return $championship; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Request $request |
105
|
|
|
* |
106
|
|
|
* @return \Illuminate\Http\RedirectResponse |
107
|
|
|
*/ |
108
|
|
|
public function update(Request $request, Championship $championship) |
109
|
|
|
{ |
110
|
|
|
$numFighter = 0; |
111
|
|
|
$query = FightersGroup::with('fights') |
|
|
|
|
112
|
|
|
->where('championship_id', $championship->id); |
113
|
|
|
|
114
|
|
|
$fighters = $request->singleElimination_fighters; |
115
|
|
|
$scores = $request->score; |
116
|
|
|
if ($championship->hasPreliminary()) { |
117
|
|
|
$query = $query->where('round', '>', 1); |
118
|
|
|
$fighters = $request->preliminary_fighters; |
119
|
|
|
} |
120
|
|
|
$groups = $query->get(); |
121
|
|
|
|
122
|
|
|
foreach ($groups as $group) { |
123
|
|
|
foreach ($group->fights as $fight) { |
124
|
|
|
$fight->c1 = $fighters[$numFighter]; |
125
|
|
|
$fight->winner_id = $this->getWinnerId($fighters, $scores, $numFighter); |
126
|
|
|
$numFighter++; |
127
|
|
|
|
128
|
|
|
$fight->c2 = $fighters[$numFighter]; |
129
|
|
|
if ($fight->winner_id == null) { |
130
|
|
|
$fight->winner_id = $this->getWinnerId($fighters, $scores, $numFighter); |
131
|
|
|
} |
132
|
|
|
$numFighter++; |
133
|
|
|
$fight->save(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return back(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
function getWinnerId($fighters, $scores, $numFighter) |
141
|
|
|
{ |
142
|
|
|
return $scores[$numFighter] != null ? $fighters[$numFighter] : null; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: