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