]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ResultController.php
open tournament type
[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\Protocol;
8 use App\Models\Result;
9 use App\Models\Round;
10 use App\Models\User;
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                         'round_id' => 'required|exists:App\\Models\\Round,id',
21                         'time' => 'numeric',
22                         'user_id' => 'required|exists:App\\Models\\User,id',
23                 ]);
24
25                 $round = Round::findOrFail($validatedData['round_id']);
26
27                 if ($validatedData['user_id'] != $request->user()->id) {
28                         $this->authorize('create', Result::class);
29                 }
30
31                 $result = Result::firstOrCreate([
32                         'round_id' => $validatedData['round_id'],
33                         'user_id' => $validatedData['user_id'],
34                 ]);
35                 if (!$round->locked) {
36                         if (isset($validatedData['forfeit'])) $result->forfeit = $validatedData['forfeit'];
37                         if (isset($validatedData['time'])) $result->time = $validatedData['time'];
38                 }
39                 $result->comment = $validatedData['comment'] ? $validatedData['comment'] : null;
40                 $result->save();
41
42                 if ($result->wasChanged()) {
43                         ResultChanged::dispatch($result);
44                 }
45
46                 if ($result->wasChanged(['forfeit', 'time'])) {
47                         Protocol::resultReported(
48                                 $round->tournament,
49                                 $result,
50                                 $request->user(),
51                         );
52                         DiscordBotCommand::queueResult($result);
53                 } else if ($result->wasChanged('comment')) {
54                         Protocol::resultCommented(
55                                 $round->tournament,
56                                 $result,
57                                 $request->user(),
58                         );
59                 }
60
61                 $round->load('results');
62                 $round->updatePlacement();
63                 if ($round->tournament->hasScoreboard()) {
64                         $round->tournament->updatePlacement();
65                 }
66
67                 $result->load('user');
68
69                 return $result->toJson();
70         }
71
72 }