3 namespace App\Http\Controllers;
5 use App\Events\ResultChanged;
6 use App\Models\Participant;
7 use App\Models\Protocol;
10 use Illuminate\Http\Request;
12 class ResultController extends Controller
15 public function create(Request $request) {
16 $validatedData = $request->validate([
17 'comment' => 'string',
18 'forfeit' => 'boolean',
19 'participant_id' => 'required|exists:App\\Models\\Participant,id',
20 'round_id' => 'required|exists:App\\Models\\Round,id',
24 $participant = Participant::findOrFail($validatedData['participant_id']);
25 $round = Round::findOrFail($validatedData['round_id']);
27 $user = $request->user();
28 if ($user->id != $participant->user->id) {
29 $this->authorize('create', Result::class);
32 $result = Result::firstOrCreate([
33 'round_id' => $validatedData['round_id'],
34 'user_id' => $participant->user_id,
36 if (!$round->locked) {
37 if (isset($validatedData['forfeit'])) $result->forfeit = $validatedData['forfeit'];
38 if (isset($validatedData['time'])) $result->time = $validatedData['time'];
40 $result->comment = $validatedData['comment'] ? $validatedData['comment'] : null;
43 if ($result->wasChanged()) {
44 ResultChanged::dispatch($result);
47 if ($result->wasChanged(['forfeit', 'time'])) {
48 Protocol::resultReported(
53 } else if ($result->wasChanged('comment')) {
54 Protocol::resultCommented(
61 $round->load('results');
62 $round->updatePlacement();
63 $round->tournament->updatePlacement();
65 return $result->toJson();