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