]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ResultController.php
add index option for tech sites
[alttp.git] / app / Http / Controllers / ResultController.php
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use App\Events\ResultChanged;
6 use App\Models\DiscordBotCommand;
7 use App\Models\Participant;
8 use App\Models\Protocol;
9 use App\Models\Result;
10 use App\Models\Round;
11 use Illuminate\Http\Request;
12
13 class ResultController extends Controller
14 {
15
16         public function create(Request $request) {
17                 $validatedData = $request->validate([
18                         'comment' => 'string',
19                         'forfeit' => 'boolean',
20                         'participant_id' => 'required|exists:App\\Models\\Participant,id',
21                         'round_id' => 'required|exists:App\\Models\\Round,id',
22                         'time' => 'numeric',
23                 ]);
24
25                 $participant = Participant::findOrFail($validatedData['participant_id']);
26                 $round = Round::findOrFail($validatedData['round_id']);
27
28                 $user = $request->user();
29                 if ($user->id != $participant->user->id) {
30                         $this->authorize('create', Result::class);
31                 }
32
33                 $result = Result::firstOrCreate([
34                         'round_id' => $validatedData['round_id'],
35                         'user_id' => $participant->user_id,
36                 ]);
37                 if (!$round->locked) {
38                         if (isset($validatedData['forfeit'])) $result->forfeit = $validatedData['forfeit'];
39                         if (isset($validatedData['time'])) $result->time = $validatedData['time'];
40                 }
41                 $result->comment = $validatedData['comment'] ? $validatedData['comment'] : null;
42                 $result->save();
43
44                 if ($result->wasChanged()) {
45                         ResultChanged::dispatch($result);
46                 }
47
48                 if ($result->wasChanged(['forfeit', 'time'])) {
49                         Protocol::resultReported(
50                                 $round->tournament,
51                                 $result,
52                                 $request->user(),
53                         );
54                         DiscordBotCommand::queueResult($result);
55                 } else if ($result->wasChanged('comment')) {
56                         Protocol::resultCommented(
57                                 $round->tournament,
58                                 $result,
59                                 $request->user(),
60                         );
61                 }
62
63                 $round->load('results');
64                 $round->updatePlacement();
65                 $round->tournament->updatePlacement();
66
67                 return $result->toJson();
68         }
69
70 }