]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/RoundController.php
2b6c617acfbaa03d93df72544f4881d2036eb812
[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                         'seed' => 'url',
47                         'title' => 'string',
48                 ]);
49
50                 $round->seed = $validatedData['seed'];
51                 $round->title = $validatedData['title'];
52                 $round->update();
53
54                 Protocol::roundEdited(
55                         $round->tournament,
56                         $round,
57                         $request->user(),
58                 );
59
60                 RoundChanged::dispatch($round);
61
62                 $round->load(['results', 'results.user']);
63
64                 return $round->toJson();
65         }
66
67         public function setSeed(Request $request, Round $round) {
68                 $this->authorize('setSeed', $round);
69
70                 $validatedData = $request->validate([
71                         'seed' => 'required|url',
72                 ]);
73
74                 $round->seed = $validatedData['seed'];
75                 $round->update();
76
77                 Protocol::roundSeedSet(
78                         $round->tournament,
79                         $round,
80                         $request->user(),
81                 );
82
83                 RoundChanged::dispatch($round);
84
85                 $round->load(['results', 'results.user']);
86
87                 return $round->toJson();
88         }
89
90         public function lock(Request $request, Round $round) {
91                 $this->authorize('lock', $round);
92
93                 $round->locked = true;
94                 $round->update();
95
96                 Protocol::roundLocked(
97                         $round->tournament,
98                         $round,
99                         $request->user(),
100                 );
101
102                 RoundChanged::dispatch($round);
103
104                 $round->load(['results', 'results.user']);
105
106                 return $round->toJson();
107         }
108
109         public function unlock(Request $request, Round $round) {
110                 $this->authorize('unlock', $round);
111
112                 $round->locked = false;
113                 $round->update();
114
115                 Protocol::roundUnlocked(
116                         $round->tournament,
117                         $round,
118                         $request->user(),
119                 );
120
121                 RoundChanged::dispatch($round);
122
123                 $round->load(['results', 'results.user']);
124
125                 return $round->toJson();
126         }
127
128 }