1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xoco70\LaravelTournaments; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\QueryException; |
6
|
|
|
use Illuminate\Foundation\Auth\User; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Routing\Controller; |
9
|
|
|
use Illuminate\Support\Facades\DB; |
10
|
|
|
use Xoco70\LaravelTournaments\Exceptions\TreeGenerationException; |
11
|
|
|
use Xoco70\LaravelTournaments\Models\Championship; |
12
|
|
|
use Xoco70\LaravelTournaments\Models\ChampionshipSettings; |
13
|
|
|
use Xoco70\LaravelTournaments\Models\Competitor; |
14
|
|
|
use Xoco70\LaravelTournaments\Models\FighterGroupCompetitor; |
15
|
|
|
use Xoco70\LaravelTournaments\Models\FighterGroupTeam; |
16
|
|
|
use Xoco70\LaravelTournaments\Models\FightersGroup; |
17
|
|
|
use Xoco70\LaravelTournaments\Models\Team; |
18
|
|
|
use Xoco70\LaravelTournaments\Models\Tournament; |
19
|
|
|
|
20
|
|
|
class TreeController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Display a listing of trees. |
24
|
|
|
* |
25
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
26
|
|
|
*/ |
27
|
|
|
public function index() |
28
|
|
|
{ |
29
|
|
|
$tournament = Tournament::with( |
|
|
|
|
30
|
|
|
'competitors', |
31
|
|
|
'championships.settings', |
32
|
|
|
'championships.category')->first(); |
33
|
|
|
|
34
|
|
|
return view('laravel-tournaments::tree.index') |
|
|
|
|
35
|
|
|
->with('tournament', $tournament); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Build Tree. |
40
|
|
|
* |
41
|
|
|
* @param Request $request |
42
|
|
|
* |
43
|
|
|
* @return \Illuminate\Http\Response|string |
44
|
|
|
*/ |
45
|
|
|
public function store(Request $request, $championshipId) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$this->deleteEverything(); |
48
|
|
|
$numFighters = $request->numFighters; |
49
|
|
|
$isTeam = $request->isTeam ?? 0; |
50
|
|
|
$championship = $this->provisionObjects($request, $isTeam, $numFighters); |
51
|
|
|
$generation = $championship->chooseGenerationStrategy(); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
$generation->run(); |
55
|
|
|
} catch (TreeGenerationException $e) { |
56
|
|
|
redirect()->back() |
57
|
|
|
->withErrors($e->getMessage()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return back() |
61
|
|
|
->with('numFighters', $numFighters) |
62
|
|
|
->with('isTeam', $isTeam); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function deleteEverything() |
66
|
|
|
{ |
67
|
|
|
DB::table('fight')->delete(); |
68
|
|
|
DB::table('fighters_groups')->delete(); |
69
|
|
|
DB::table('fighters_group_competitor')->delete(); |
70
|
|
|
DB::table('fighters_group_team')->delete(); |
71
|
|
|
DB::table('competitor')->delete(); |
72
|
|
|
DB::table('team')->delete(); |
73
|
|
|
DB::table('users')->where('id', '<>', 1)->delete(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Request $request |
78
|
|
|
* @param $isTeam |
79
|
|
|
* @param $numFighters |
80
|
|
|
* |
81
|
|
|
* @return Championship |
82
|
|
|
*/ |
83
|
|
|
protected function provisionObjects(Request $request, $isTeam, $numFighters) |
84
|
|
|
{ |
85
|
|
|
if ($isTeam) { |
86
|
|
|
$championship = Championship::find(2); |
87
|
|
|
factory(Team::class, (int)$numFighters)->create(['championship_id' => $championship->id]); |
88
|
|
|
} else { |
89
|
|
|
$championship = Championship::find(1); |
90
|
|
|
$users = factory(User::class, (int)$numFighters)->create(); |
91
|
|
View Code Duplication |
foreach ($users as $user) { |
|
|
|
|
92
|
|
|
factory(Competitor::class)->create( |
93
|
|
|
['championship_id' => $championship->id, |
94
|
|
|
'user_id' => $user->id, |
95
|
|
|
'confirmed' => 1, |
96
|
|
|
'short_id' => $user->id, |
97
|
|
|
] |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
$championship->settings = ChampionshipSettings::createOrUpdate($request, $championship); |
102
|
|
|
|
103
|
|
|
return $championship; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Request $request |
108
|
|
|
* |
109
|
|
|
* @return \Illuminate\Http\RedirectResponse |
110
|
|
|
*/ |
111
|
|
|
public function update(Request $request, Championship $championship) |
112
|
|
|
{ |
113
|
|
|
$tree = $championship->chooseGenerationStrategy(); |
114
|
|
|
$fighters = $request->fighters; |
|
|
|
|
115
|
|
|
$tree->update($request); |
116
|
|
|
|
117
|
|
|
// $query = FightersGroup::with('fights') |
|
|
|
|
118
|
|
|
// ->where('championship_id', $championship->id); |
119
|
|
|
// |
120
|
|
|
// $numFighter = 0; |
121
|
|
|
// |
122
|
|
|
// $scores = $request->score; |
123
|
|
|
// if ($championship->hasPreliminary()) { |
124
|
|
|
// $query = $query->where('round', '>', 1); |
125
|
|
|
// $fighters = $request->preliminary_fighters; |
126
|
|
|
// } |
127
|
|
|
// $groups = $query->get(); |
128
|
|
|
|
129
|
|
|
// foreach ($groups as $group) { |
|
|
|
|
130
|
|
|
// foreach ($group->fights as $fight) { |
131
|
|
|
// // Find the fight in array, and update order |
132
|
|
|
// $oldC1 = $fight->c1; |
133
|
|
|
// $oldC2 = $fight->c2; |
134
|
|
|
// $fight->c1 = $fighters[$numFighter]; |
135
|
|
|
// $scores[$numFighter] != null |
136
|
|
|
// ? $fight->winner_id = $fighters[$numFighter] |
137
|
|
|
// : $fight->winner_id = null; |
138
|
|
|
// $numFighter++; |
139
|
|
|
// |
140
|
|
|
// $fight->c2 = $fighters[$numFighter]; |
141
|
|
|
// if ($fight->winner_id == null) { |
142
|
|
|
// $scores[$numFighter] != null |
143
|
|
|
// ? $fight->winner_id = $fighters[$numFighter] |
144
|
|
|
// : $fight->winner_id = null; |
145
|
|
|
// } |
146
|
|
|
// |
147
|
|
|
// |
148
|
|
|
// $numFighter++; |
149
|
|
|
// $fight->save(); |
150
|
|
|
// |
151
|
|
|
// |
152
|
|
|
// // Update group and group->competitor / group->team |
153
|
|
|
// if ($championship->isSingleEliminationCompetitor()) { |
154
|
|
|
// try { |
155
|
|
|
// |
156
|
|
|
// } catch (QueryException $exception) { |
157
|
|
|
// |
158
|
|
|
// } |
159
|
|
|
// $fgc = FighterGroupCompetitor::where('fighters_group_id', $group->id) |
160
|
|
|
// ->where('competitor_id', $oldC1) |
161
|
|
|
// ->first(); |
162
|
|
|
// |
163
|
|
|
// $fgc->competitor_id = $fight->c1; |
164
|
|
|
// $fgc->save(); |
165
|
|
|
// |
166
|
|
|
// $fgc = FighterGroupCompetitor::where('fighters_group_id', $group->id) |
167
|
|
|
// ->where('competitor_id', $oldC2) |
168
|
|
|
// ->first(); |
169
|
|
|
// |
170
|
|
|
// $fgc->competitor_id = $fight->c2; |
171
|
|
|
// $fgc->save(); |
172
|
|
|
// } |
173
|
|
|
// if ($championship->isSingleEliminationTeam()) { |
174
|
|
|
// $fgt = FighterGroupTeam::where('fighters_group_id', $group->id) |
175
|
|
|
// ->where('team_id', $oldC1) |
176
|
|
|
// ->first(); |
177
|
|
|
// $fgt->team_id = $fight->c1; |
178
|
|
|
// $fgt->save(); |
179
|
|
|
// |
180
|
|
|
// $fgt = FighterGroupTeam::where('fighters_group_id', $group->id) |
181
|
|
|
// ->where('team_id', $oldC2) |
182
|
|
|
// ->first(); |
183
|
|
|
//// dd($group->id,$oldC2); |
184
|
|
|
// $fgt->team_id = $fight->c2; |
185
|
|
|
// $fgt->save(); |
186
|
|
|
// |
187
|
|
|
// } |
188
|
|
|
// } |
189
|
|
|
// } |
190
|
|
|
return back(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
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: