]> git.localhorst.tv Git - alttp.git/blob - app/Http/Controllers/ResultController.php
basic map pins
[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                         'vod' => 'string|url',
24                 ]);
25
26                 $round = Round::findOrFail($validatedData['round_id']);
27
28                 if ($validatedData['user_id'] != $request->user()->id) {
29                         $this->authorize('create', Result::class);
30                 }
31
32                 $result = Result::firstOrCreate([
33                         'round_id' => $validatedData['round_id'],
34                         'user_id' => $validatedData['user_id'],
35                 ]);
36                 if (!$round->locked) {
37                         if (isset($validatedData['forfeit'])) $result->forfeit = $validatedData['forfeit'];
38                         if (isset($validatedData['time'])) $result->time = $validatedData['time'];
39                 }
40                 $result->comment = !empty($validatedData['comment']) ? $validatedData['comment'] : null;
41                 $result->vod = !empty($validatedData['vod']) ? $validatedData['vod'] : 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', 'vod'])) {
56                         Protocol::resultCommented(
57                                 $round->tournament,
58                                 $result,
59                                 $request->user(),
60                         );
61                 }
62
63                 $round->load('results');
64                 $round->updatePlacement();
65                 if ($round->tournament->hasScoreboard()) {
66                         $round->tournament->updatePlacement();
67                 }
68
69                 $result->load('user');
70
71                 return $result->toJson();
72         }
73
74 }