|
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
|
|
|
return back() |
|
59
|
|
|
->with('numFighters', $numFighters) |
|
60
|
|
|
->with('isTeam', $isTeam); |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function deleteEverything() |
|
65
|
|
|
{ |
|
66
|
|
|
DB::table('fight')->delete(); |
|
67
|
|
|
DB::table('fighters_groups')->delete(); |
|
68
|
|
|
DB::table('fighters_group_competitor')->delete(); |
|
69
|
|
|
DB::table('fighters_group_team')->delete(); |
|
70
|
|
|
DB::table('competitor')->delete(); |
|
71
|
|
|
DB::table('team')->delete(); |
|
72
|
|
|
DB::table('users')->where('id', '<>', 1)->delete(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param Request $request |
|
77
|
|
|
* @param $isTeam |
|
78
|
|
|
* @param $numFighters |
|
79
|
|
|
* @return Championship |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function provisionObjects(Request $request, $isTeam, $numFighters) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($isTeam) { |
|
84
|
|
|
$championship = Championship::find(2); |
|
85
|
|
|
factory(Team::class, (int)$numFighters)->create(['championship_id' => $championship->id]); |
|
86
|
|
|
} else { |
|
87
|
|
|
$championship = Championship::find(1); |
|
88
|
|
|
$users = factory(User::class, (int)$numFighters)->create(); |
|
89
|
|
|
foreach ($users as $user) { |
|
90
|
|
|
factory(Competitor::class)->create( |
|
91
|
|
|
['championship_id' => $championship->id, |
|
92
|
|
|
'user_id' => $user->id, |
|
93
|
|
|
'confirmed' => 1, |
|
94
|
|
|
'short_id' => $user->id |
|
95
|
|
|
] |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
$championship->settings = ChampionshipSettings::createOrUpdate($request, $championship); |
|
100
|
|
|
return $championship; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param Request $request |
|
106
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
107
|
|
|
*/ |
|
108
|
|
|
public function update(Request $request, Championship $championship) |
|
109
|
|
|
{ |
|
110
|
|
|
$numFight = 0; |
|
111
|
|
|
$query = FightersGroup::with('fights') |
|
|
|
|
|
|
112
|
|
|
->where('championship_id', $championship->id); |
|
113
|
|
|
|
|
114
|
|
|
$fighters = $request->directElimination_fighters; |
|
115
|
|
|
if ($championship->hasPreliminary()) { |
|
116
|
|
|
$query = $query->where('round', '>', 1); |
|
117
|
|
|
$fighters = $request->preliminary_fighters; |
|
118
|
|
|
} |
|
119
|
|
|
$groups = $query->get(); |
|
120
|
|
|
|
|
121
|
|
|
foreach ($groups as $group) { |
|
122
|
|
|
foreach ($group->fights as $fight) { |
|
123
|
|
|
// Find the fight in array, and update order |
|
124
|
|
|
$fight->c1 = $fighters[$numFight++]; |
|
125
|
|
|
$fight->c2 = $fighters[$numFight++]; |
|
126
|
|
|
$fight->save(); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return back(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
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: