]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/RoundController.php
allow admins to lock/unlock rounds
[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                         'number' => intval($tournament->rounds_max_number) + 1,
26                         'tournament_id' => $validatedData['tournament_id'],
27                 ]);
28
29                 Protocol::roundAdded(
30                         $tournament,
31                         $round,
32                         $request->user(),
33                 );
34
35                 RoundAdded::dispatch($round);
36
37                 return $round->toJson();
38         }
39
40         public function setSeed(Request $request, Round $round) {
41                 $this->authorize('setSeed', $round);
42
43                 $validatedData = $request->validate([
44                         'seed' => 'required|url',
45                 ]);
46
47                 $round->seed = $validatedData['seed'];
48                 $round->update();
49
50                 Protocol::roundSeedSet(
51                         $round->tournament,
52                         $round,
53                         $request->user(),
54                 );
55
56                 RoundChanged::dispatch($round);
57
58                 $round->load('results');
59
60                 return $round->toJson();
61         }
62
63         public function lock(Request $request, Round $round) {
64                 $this->authorize('lock', $round);
65
66                 $round->locked = true;
67                 $round->update();
68
69                 Protocol::roundLocked(
70                         $round->tournament,
71                         $round,
72                         $request->user(),
73                 );
74
75                 RoundChanged::dispatch($round);
76
77                 $round->load('results');
78
79                 return $round->toJson();
80         }
81
82         public function unlock(Request $request, Round $round) {
83                 $this->authorize('unlock', $round);
84
85                 $round->locked = false;
86                 $round->update();
87
88                 Protocol::roundUnlocked(
89                         $round->tournament,
90                         $round,
91                         $request->user(),
92                 );
93
94                 RoundChanged::dispatch($round);
95
96                 $round->load('results');
97
98                 return $round->toJson();
99         }
100
101 }