]> git.localhorst.tv Git - alttp.git/blobdiff - app/Http/Controllers/ResultController.php
open tournament type
[alttp.git] / app / Http / Controllers / ResultController.php
index d31d444950178ce8829e09d8d5c759add4322242..57f78a157f2d1537cebc3bee96626cd629271a12 100644 (file)
@@ -2,9 +2,71 @@
 
 namespace App\Http\Controllers;
 
+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
 {
-       //
+
+       public function create(Request $request) {
+               $validatedData = $request->validate([
+                       'comment' => 'string',
+                       'forfeit' => 'boolean',
+                       'round_id' => 'required|exists:App\\Models\\Round,id',
+                       'time' => 'numeric',
+                       'user_id' => 'required|exists:App\\Models\\User,id',
+               ]);
+
+               $round = Round::findOrFail($validatedData['round_id']);
+
+               if ($validatedData['user_id'] != $request->user()->id) {
+                       $this->authorize('create', Result::class);
+               }
+
+               $result = Result::firstOrCreate([
+                       'round_id' => $validatedData['round_id'],
+                       '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 = $validatedData['comment'] ? $validatedData['comment'] : null;
+               $result->save();
+
+               if ($result->wasChanged()) {
+                       ResultChanged::dispatch($result);
+               }
+
+               if ($result->wasChanged(['forfeit', 'time'])) {
+                       Protocol::resultReported(
+                               $round->tournament,
+                               $result,
+                               $request->user(),
+                       );
+                       DiscordBotCommand::queueResult($result);
+               } else if ($result->wasChanged('comment')) {
+                       Protocol::resultCommented(
+                               $round->tournament,
+                               $result,
+                               $request->user(),
+                       );
+               }
+
+               $round->load('results');
+               $round->updatePlacement();
+               if ($round->tournament->hasScoreboard()) {
+                       $round->tournament->updatePlacement();
+               }
+
+               $result->load('user');
+
+               return $result->toJson();
+       }
+
 }