]> git.localhorst.tv Git - alttp.git/blobdiff - app/Http/Controllers/ResultController.php
chat bot protocol ui
[alttp.git] / app / Http / Controllers / ResultController.php
index d72a10020dbe4b3e4f29cb8a488b808e5b583125..ba57c4b0759f45c33b0bb72fe776ec02cc65a5c7 100644 (file)
@@ -2,11 +2,12 @@
 
 namespace App\Http\Controllers;
 
-use App\Events\ResultReported;
-use App\Models\Participant;
+use App\Events\ResultChanged;
+use App\Models\DiscordBotCommand;
 use App\Models\Protocol;
 use App\Models\Result;
 use App\Models\Round;
+use App\Models\User;
 use Illuminate\Http\Request;
 
 class ResultController extends Controller
@@ -14,39 +15,58 @@ class ResultController extends Controller
 
        public function create(Request $request) {
                $validatedData = $request->validate([
+                       'comment' => 'string',
                        'forfeit' => 'boolean',
-                       'participant_id' => 'required|exists:App\\Models\\Participant,id',
                        'round_id' => 'required|exists:App\\Models\\Round,id',
-                       'time' => 'required_if:forfeit,false|numeric',
+                       'time' => 'numeric',
+                       'user_id' => 'required|exists:App\\Models\\User,id',
+                       'vod' => 'string|url',
                ]);
-               error_log(var_export($validatedData, true));
 
-               $participant = Participant::findOrFail($validatedData['participant_id']);
                $round = Round::findOrFail($validatedData['round_id']);
-               if (!$round || $round->locked) {
-                       abort(403);
-               }
 
-               $user = $request->user();
-               if ($user->id != $participant->user->id) {
+               if ($validatedData['user_id'] != $request->user()->id) {
                        $this->authorize('create', Result::class);
                }
 
-               $result = Result::updateOrCreate([
+               $result = Result::firstOrCreate([
                        'round_id' => $validatedData['round_id'],
-                       'user_id' => $participant->user_id,
-               ], [
-                       'forfeit' => $validatedData['forfeit'],
-                       'time' => isset($validatedData['time']) ? $validatedData['time'] : 0,
+                       'user_id' => $validatedData['user_id'],
                ]);
+               if (!$round->locked) {
+                       if (isset($validatedData['forfeit'])) $result->forfeit = $validatedData['forfeit'];
+                       if (isset($validatedData['time'])) $result->time = $validatedData['time'];
+               }
+               $result->comment = !empty($validatedData['comment']) ? $validatedData['comment'] : null;
+               $result->vod = !empty($validatedData['vod']) ? $validatedData['vod'] : null;
+               $result->save();
+
+               if ($result->wasChanged()) {
+                       ResultChanged::dispatch($result);
+               }
 
-               Protocol::resultReported(
-                       $round->tournament,
-                       $result,
-                       $request->user(),
-               );
+               if ($result->wasChanged(['forfeit', 'time'])) {
+                       Protocol::resultReported(
+                               $round->tournament,
+                               $result,
+                               $request->user(),
+                       );
+                       DiscordBotCommand::queueResult($result);
+               } else if ($result->wasChanged(['comment', 'vod'])) {
+                       Protocol::resultCommented(
+                               $round->tournament,
+                               $result,
+                               $request->user(),
+                       );
+               }
+
+               $round->load('results');
+               $round->updatePlacement();
+               if ($round->tournament->hasScoreboard()) {
+                       $round->tournament->updatePlacement();
+               }
 
-               ResultReported::dispatch($result);
+               $result->load('user');
 
                return $result->toJson();
        }