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\Tournament; |
15
|
|
|
use Xoco70\KendoTournaments\TreeGen\DirectEliminationCompetitorTreeGen; |
16
|
|
|
use Xoco70\KendoTournaments\TreeGen\DirectEliminationTeamTreeGen; |
17
|
|
|
use Xoco70\KendoTournaments\TreeGen\PlayOffCompetitorTreeGen; |
18
|
|
|
use Xoco70\KendoTournaments\TreeGen\PlayOffTeamTreeGen; |
19
|
|
|
use Xoco70\KendoTournaments\TreeGen\TreeGen; |
20
|
|
|
|
21
|
|
|
class TreeController extends Controller |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Display a listing of trees. |
25
|
|
|
* |
26
|
|
|
* @param Request $request |
27
|
|
|
* |
28
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
29
|
|
|
*/ |
30
|
|
|
public function index(Request $request) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$tournament = Tournament::with( |
|
|
|
|
33
|
|
|
'competitors', |
34
|
|
|
'championshipSettings', |
35
|
|
|
'championships.settings', |
36
|
|
|
'championships.category')->first(); |
37
|
|
|
|
38
|
|
|
return view('kendo-tournaments::tree.index') |
|
|
|
|
39
|
|
|
->with('tournament', $tournament) |
40
|
|
|
->with('settings', $tournament->championships[0]->setting); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Build Tree. |
45
|
|
|
* |
46
|
|
|
* @param Request $request |
47
|
|
|
* @param Championship $championship |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Http\Response|string |
50
|
|
|
*/ |
51
|
|
|
public function store(Request $request, Championship $championship) |
52
|
|
|
{ |
53
|
|
|
DB::table('fight')->delete(); |
54
|
|
|
DB::table('fighters_groups')->delete(); |
55
|
|
|
DB::table('fighters_group_competitor')->delete(); |
56
|
|
|
DB::table('fighters_group_team')->delete(); |
57
|
|
|
DB::table('competitor')->delete(); |
58
|
|
|
DB::table('users')->where('id', '<>', 1)->delete(); |
59
|
|
|
|
60
|
|
|
$championship = Championship::with('teams', 'users', 'category', 'settings')->find($championship->id); |
|
|
|
|
61
|
|
|
$numFighters = $request->numFighters; |
62
|
|
|
|
63
|
|
|
$users = factory(User::class, (int)$numFighters)->create(); |
64
|
|
|
|
65
|
|
View Code Duplication |
foreach ($users as $user) { |
|
|
|
|
66
|
|
|
factory(Competitor::class)->create([ |
67
|
|
|
'championship_id' => $championship->id, |
68
|
|
|
'user_id' => $user->id, |
69
|
|
|
'confirmed' => 1, |
70
|
|
|
'short_id' => $user->id |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
$championship->settings = ChampionshipSettings::createOrUpdate($request, $championship); |
74
|
|
|
|
75
|
|
|
$generation = new TreeGen($championship, null); |
76
|
|
|
//TODO Set groupBy argument to NULL for now |
77
|
|
|
if ($championship->hasPreliminary() && $championship->category->isTeam()) { |
78
|
|
|
$generation = new PlayOffTeamTreeGen($championship, null); |
79
|
|
|
} |
80
|
|
|
if ($championship->hasPreliminary() && !$championship->category->isTeam()) { |
81
|
|
|
$generation = new PlayOffCompetitorTreeGen($championship, null); |
82
|
|
|
} |
83
|
|
View Code Duplication |
if (!$championship->hasPreliminary() && $championship->isDirectEliminationType() && $championship->category->isTeam()) { |
|
|
|
|
84
|
|
|
$generation = new DirectEliminationTeamTreeGen($championship, null); |
85
|
|
|
} |
86
|
|
View Code Duplication |
if (!$championship->hasPreliminary() && $championship->isDirectEliminationType() && !$championship->category->isTeam()) { |
|
|
|
|
87
|
|
|
$generation = new DirectEliminationCompetitorTreeGen($championship, null); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$fighterToUpdate = null; |
|
|
|
|
91
|
|
|
try { |
92
|
|
|
$generation->run(); |
93
|
|
|
FightersGroup::generateFights($championship); |
94
|
|
|
// For Now, We don't generate fights when Preliminary |
95
|
|
|
if ($championship->isDirectEliminationType() && !$championship->hasPreliminary()) { |
96
|
|
|
FightersGroup::generateNextRoundsFights($championship); |
97
|
|
|
} |
98
|
|
|
} catch (TreeGenerationException $e) { |
99
|
|
|
redirect()->back() |
100
|
|
|
->withErrors([$numFighters . "-" . $e->getMessage()]); |
101
|
|
|
} |
102
|
|
|
return redirect()->back() |
103
|
|
|
->with('numFighters', $numFighters) |
104
|
|
|
->with('hasPreliminary', $championship->settings->hasPreliminary) |
105
|
|
|
->with(['success', "Success"]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.