X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=app%2FHttp%2FControllers%2FResultController.php;h=8698e19838aef615ee45b3ec5f4579ba58bb88d1;hb=a5e53546a5960f54fb45e06767f89e9dfeef6a47;hp=d31d444950178ce8829e09d8d5c759add4322242;hpb=55f2d7cd6c290a0d26db177d54d20c393f890bbb;p=alttp.git diff --git a/app/Http/Controllers/ResultController.php b/app/Http/Controllers/ResultController.php index d31d444..8698e19 100644 --- a/app/Http/Controllers/ResultController.php +++ b/app/Http/Controllers/ResultController.php @@ -2,9 +2,69 @@ namespace App\Http\Controllers; +use App\Events\ResultChanged; +use App\Models\DiscordBotCommand; +use App\Models\Participant; +use App\Models\Protocol; +use App\Models\Result; +use App\Models\Round; use Illuminate\Http\Request; 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' => 'numeric', + ]); + + $participant = Participant::findOrFail($validatedData['participant_id']); + $round = Round::findOrFail($validatedData['round_id']); + + $user = $request->user(); + if ($user->id != $participant->user->id) { + $this->authorize('create', Result::class); + } + + $result = Result::firstOrCreate([ + 'round_id' => $validatedData['round_id'], + 'user_id' => $participant->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(); + $round->tournament->updatePlacement(); + + return $result->toJson(); + } + }