]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/RoundController.php
04e1b865ca0fb3560be7ac9231e5f998b3974897
[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 setSeed(Request $request, Round $round) {
43                 $this->authorize('setSeed', $round);
44
45                 $validatedData = $request->validate([
46                         'seed' => 'required|url',
47                 ]);
48
49                 $round->seed = $validatedData['seed'];
50                 $round->update();
51
52                 Protocol::roundSeedSet(
53                         $round->tournament,
54                         $round,
55                         $request->user(),
56                 );
57
58                 RoundChanged::dispatch($round);
59
60                 $round->load(['results', 'results.user']);
61
62                 return $round->toJson();
63         }
64
65         public function lock(Request $request, Round $round) {
66                 $this->authorize('lock', $round);
67
68                 $round->locked = true;
69                 $round->update();
70
71                 Protocol::roundLocked(
72                         $round->tournament,
73                         $round,
74                         $request->user(),
75                 );
76
77                 RoundChanged::dispatch($round);
78
79                 $round->load(['results', 'results.user']);
80
81                 return $round->toJson();
82         }
83
84         public function unlock(Request $request, Round $round) {
85                 $this->authorize('unlock', $round);
86
87                 $round->locked = false;
88                 $round->update();
89
90                 Protocol::roundUnlocked(
91                         $round->tournament,
92                         $round,
93                         $request->user(),
94                 );
95
96                 RoundChanged::dispatch($round);
97
98                 $round->load(['results', 'results.user']);
99
100                 return $round->toJson();
101         }
102
103 }