]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ResultController.php
b23c907f680b28edcc5dbb40efc1cbcd44c8933b
[alttp.git] / app / Http / Controllers / ResultController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Events\ResultReported;
6 use App\Models\Participant;
7 use App\Models\Protocol;
8 use App\Models\Result;
9 use App\Models\Round;
10 use Illuminate\Http\Request;
11
12 class ResultController extends Controller
13 {
14
15         public function create(Request $request) {
16                 $validatedData = $request->validate([
17                         'forfeit' => 'boolean',
18                         'participant_id' => 'required|exists:App\\Models\\Participant,id',
19                         'round_id' => 'required|exists:App\\Models\\Round,id',
20                         'time' => 'required_if:forfeit,false|numeric',
21                 ]);
22                 error_log(var_export($validatedData, true));
23
24                 $participant = Participant::findOrFail($validatedData['participant_id']);
25                 $round = Round::findOrFail($validatedData['round_id']);
26
27                 $user = $request->user();
28                 if ($user->id != $participant->user->id) {
29                         $this->authorize('create', Result::class);
30                 }
31
32                 $result = Result::updateOrCreate([
33                         'round_id' => $validatedData['round_id'],
34                         'user_id' => $participant->user_id,
35                 ], [
36                         'forfeit' => $validatedData['forfeit'],
37                         'time' => isset($validatedData['time']) ? $validatedData['time'] : 0,
38                 ]);
39
40                 Protocol::resultReported(
41                         $round->tournament,
42                         $result,
43                         $request->user(),
44                 );
45
46                 ResultReported::dispatch($result);
47
48                 return $result->toJson();
49         }
50
51 }