1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xoco70\KendoTournaments; |
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\KendoTournaments\Exceptions\TreeGenerationException; |
10
|
|
|
use Xoco70\KendoTournaments\Models\Championship; |
11
|
|
|
use Xoco70\KendoTournaments\Models\ChampionshipSettings; |
12
|
|
|
use Xoco70\KendoTournaments\Models\Competitor; |
13
|
|
|
use Xoco70\KendoTournaments\Models\FightersGroup; |
14
|
|
|
use Xoco70\KendoTournaments\Models\Team; |
15
|
|
|
use Xoco70\KendoTournaments\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
|
|
|
'championshipSettings', |
29
|
|
|
'championships.settings', |
30
|
|
|
'championships.category')->first(); |
31
|
|
|
|
32
|
|
|
return view('kendo-tournaments::tree.index') |
|
|
|
|
33
|
|
|
->with('tournament', $tournament) |
34
|
|
|
->with('championship', $tournament->championships[0]) |
35
|
|
|
->with('settings', $tournament->championships[0]->setting); |
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
|
|
|
try { |
53
|
|
|
$generation->run(); |
54
|
|
|
} catch (TreeGenerationException $e) { |
55
|
|
|
redirect()->back() |
56
|
|
|
->withErrors([$numFighters . "-" . $e->getMessage()]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$tournament = Tournament::with( |
|
|
|
|
60
|
|
|
'competitors', |
61
|
|
|
'championshipSettings', |
62
|
|
|
'championships.settings', |
63
|
|
|
'championships.category')->first(); |
64
|
|
|
|
65
|
|
|
return back() |
66
|
|
|
->with('numFighters',$numFighters) |
67
|
|
|
->with('isTeam',$isTeam); |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function deleteEverything() |
72
|
|
|
{ |
73
|
|
|
DB::table('fight')->delete(); |
74
|
|
|
DB::table('fighters_groups')->delete(); |
75
|
|
|
DB::table('fighters_group_competitor')->delete(); |
76
|
|
|
DB::table('fighters_group_team')->delete(); |
77
|
|
|
DB::table('competitor')->delete(); |
78
|
|
|
DB::table('team')->delete(); |
79
|
|
|
DB::table('users')->where('id', '<>', 1)->delete(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Request $request |
84
|
|
|
* @param $isTeam |
85
|
|
|
* @param $numFighters |
86
|
|
|
* @return Championship |
87
|
|
|
*/ |
88
|
|
|
protected function provisionObjects(Request $request, $isTeam, $numFighters) |
89
|
|
|
{ |
90
|
|
|
if ($isTeam) { |
91
|
|
|
$championship = Championship::find(2); |
92
|
|
|
factory(Team::class, (int)$numFighters)->create(['championship_id' => $championship->id]); |
93
|
|
|
} else { |
94
|
|
|
$championship = Championship::find(1); |
95
|
|
|
$users = factory(User::class, (int)$numFighters)->create(); |
96
|
|
|
foreach ($users as $user) { |
97
|
|
|
factory(Competitor::class)->create( |
98
|
|
|
['championship_id' => $championship->id, |
99
|
|
|
'user_id' => $user->id, |
100
|
|
|
'confirmed' => 1, |
101
|
|
|
'short_id' => $user->id |
102
|
|
|
] |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
$championship->settings = ChampionshipSettings::createOrUpdate($request, $championship); |
107
|
|
|
return $championship; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Request $request |
113
|
|
|
* @return \Illuminate\Http\RedirectResponse |
114
|
|
|
*/ |
115
|
|
|
public function update(Request $request, Championship $championship) |
116
|
|
|
{ |
117
|
|
|
$numFight = 0; |
118
|
|
|
$query = FightersGroup::with('fights') |
|
|
|
|
119
|
|
|
->where('championship_id', $championship->id); |
120
|
|
|
|
121
|
|
|
$fighters = $request->directElimination_fighters; |
122
|
|
|
if ($championship->hasPreliminary()) { |
123
|
|
|
$query = $query->where('round', '>', 1); |
124
|
|
|
$fighters = $request->preliminary_fighters; |
125
|
|
|
} |
126
|
|
|
$groups = $query->get(); |
127
|
|
|
|
128
|
|
|
foreach ($groups as $group) { |
129
|
|
|
foreach ($group->fights as $fight) { |
130
|
|
|
// Find the fight in array, and update order |
131
|
|
|
$fight->c1 = $fighters[$numFight++]; |
132
|
|
|
$fight->c2 = $fighters[$numFight++]; |
133
|
|
|
$fight->save(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return back(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
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: