]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/RoundController.php
temporary user select
[alttp.git] / app / Http / Controllers / RoundController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Events\RoundAdded;
6 use App\Events\RoundChanged;
7 use App\Models\Protocol;
8 use App\Models\Round;
9 use App\Models\Tournament;
10 use Illuminate\Http\Request;
11
12 class RoundController extends Controller
13 {
14
15         public function create(Request $request) {
16                 $validatedData = $request->validate([
17                         'tournament_id' => 'required|exists:App\\Models\\Tournament,id',
18                 ]);
19                 $tournament = Tournament::findOrFail($validatedData['tournament_id']);
20                 $this->authorize('addRound', $tournament);
21
22                 $tournament->loadMax('rounds', 'number');
23
24                 $round = Round::create([
25                         'game' => $tournament->game,
26                         'number' => intval($tournament->rounds_max_number) + 1,
27                         'no_record' => $tournament->no_record,
28                         'tournament_id' => $validatedData['tournament_id'],
29                 ]);
30
31                 Protocol::roundAdded(
32                         $tournament,
33                         $round,
34                         $request->user(),
35                 );
36
37                 RoundAdded::dispatch($round);
38
39                 return $round->toJson();
40         }
41
42         public function update(Request $request, Round $round) {
43                 $this->authorize('update', $round);
44
45                 $validatedData = $request->validate([
46                         'code' => 'array',
47                         'code.*' => 'string',
48                         'rolled_by' => 'nullable|exists:App\\Models\\User,id',
49                         'seed' => 'url',
50                         'title' => 'string',
51                 ]);
52
53                 $round->code = array_filter($validatedData['code']);
54                 $round->rolled_by = $validatedData['rolled_by'];
55                 $round->seed = $validatedData['seed'];
56                 $round->title = $validatedData['title'];
57                 $round->update();
58
59                 Protocol::roundEdited(
60                         $round->tournament,
61                         $round,
62                         $request->user(),
63                 );
64
65                 RoundChanged::dispatch($round);
66
67                 $round->load(['results', 'results.user']);
68
69                 return $round->toJson();
70         }
71
72         public function setSeed(Request $request, Round $round) {
73                 $this->authorize('setSeed', $round);
74
75                 $validatedData = $request->validate([
76                         'seed' => 'required|url',
77                 ]);
78
79                 $round->seed = $validatedData['seed'];
80                 $round->update();
81
82                 Protocol::roundSeedSet(
83                         $round->tournament,
84                         $round,
85                         $request->user(),
86                 );
87
88                 RoundChanged::dispatch($round);
89
90                 $round->load(['results', 'results.user']);
91
92                 return $round->toJson();
93         }
94
95         public function lock(Request $request, Round $round) {
96                 $this->authorize('lock', $round);
97
98                 $round->locked = true;
99                 $round->update();
100
101                 Protocol::roundLocked(
102                         $round->tournament,
103                         $round,
104                         $request->user(),
105                 );
106
107                 RoundChanged::dispatch($round);
108
109                 $round->load(['results', 'results.user']);
110
111                 return $round->toJson();
112         }
113
114         public function unlock(Request $request, Round $round) {
115                 $this->authorize('unlock', $round);
116
117                 $round->locked = false;
118                 $round->update();
119
120                 Protocol::roundUnlocked(
121                         $round->tournament,
122                         $round,
123                         $request->user(),
124                 );
125
126                 RoundChanged::dispatch($round);
127
128                 $round->load(['results', 'results.user']);
129
130                 return $round->toJson();
131         }
132
133 }