]> git.localhorst.tv Git - alttp.git/blobdiff - app/Http/Controllers/ResultController.php
server calculated scoring
[alttp.git] / app / Http / Controllers / ResultController.php
index 37ebf3b4acc51b995bf47b1f0f4d3686814f29da..5102c8aa01ebec790a2aa4deea0aacc90d32b574 100644 (file)
@@ -2,7 +2,7 @@
 
 namespace App\Http\Controllers;
 
-use App\Events\ResultReported;
+use App\Events\ResultChanged;
 use App\Models\Participant;
 use App\Models\Protocol;
 use App\Models\Result;
@@ -14,13 +14,18 @@ class ResultController extends Controller
 
        public function create(Request $request) {
                $validatedData = $request->validate([
+                       'forfeit' => 'boolean',
                        'participant_id' => 'required|exists:App\\Models\\Participant,id',
                        'round_id' => 'required|exists:App\\Models\\Round,id',
-                       'time' => 'required|numeric',
+                       'time' => 'required_if:forfeit,false|numeric',
                ]);
+               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) {
@@ -31,8 +36,15 @@ class ResultController extends Controller
                        'round_id' => $validatedData['round_id'],
                        'user_id' => $participant->user_id,
                ], [
-                       'time' => $validatedData['time'],
+                       'forfeit' => $validatedData['forfeit'],
+                       'time' => isset($validatedData['time']) ? $validatedData['time'] : 0,
                ]);
+               if ($result->wasChanged()) {
+                       ResultChanged::dispatch($result);
+               }
+               $round->load('results');
+               $round->updatePlacement();
+               $round->tournament->updatePlacement();
 
                Protocol::resultReported(
                        $round->tournament,
@@ -40,8 +52,6 @@ class ResultController extends Controller
                        $request->user(),
                );
 
-               ResultReported::dispatch($result);
-
                return $result->toJson();
        }