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