X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FResultController.php;h=ab1759d6faaf1bf97fb9c0d5d2c51014a8935d3a;hb=a4260a00251cef4ad806c9d5c44d4c444d6ab831;hp=5102c8aa01ebec790a2aa4deea0aacc90d32b574;hpb=d32516335ea2534e15256c948e9c38d3de40794b;p=alttp.git diff --git a/app/Http/Controllers/ResultController.php b/app/Http/Controllers/ResultController.php index 5102c8a..ab1759d 100644 --- a/app/Http/Controllers/ResultController.php +++ b/app/Http/Controllers/ResultController.php @@ -14,44 +14,54 @@ 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', ]); - 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) { $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, ]); + 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(), + ); + } else if ($result->wasChanged('comment')) { + Protocol::resultCommented( + $round->tournament, + $result, + $request->user(), + ); + } + $round->load('results'); $round->updatePlacement(); $round->tournament->updatePlacement(); - Protocol::resultReported( - $round->tournament, - $result, - $request->user(), - ); - return $result->toJson(); }