3 namespace App\Http\Controllers;
5 use App\Events\ResultChanged;
6 use App\Models\DiscordBotCommand;
7 use App\Models\Participant;
8 use App\Models\Protocol;
11 use Illuminate\Http\Request;
13 class ResultController extends Controller
16 public function create(Request $request) {
17 $validatedData = $request->validate([
18 'comment' => 'string',
19 'forfeit' => 'boolean',
20 'participant_id' => 'required|exists:App\\Models\\Participant,id',
21 'round_id' => 'required|exists:App\\Models\\Round,id',
25 $participant = Participant::findOrFail($validatedData['participant_id']);
26 $round = Round::findOrFail($validatedData['round_id']);
28 $user = $request->user();
29 if ($user->id != $participant->user->id) {
30 $this->authorize('create', Result::class);
33 $result = Result::firstOrCreate([
34 'round_id' => $validatedData['round_id'],
35 'user_id' => $participant->user_id,
37 if (!$round->locked) {
38 if (isset($validatedData['forfeit'])) $result->forfeit = $validatedData['forfeit'];
39 if (isset($validatedData['time'])) $result->time = $validatedData['time'];
41 $result->comment = $validatedData['comment'] ? $validatedData['comment'] : null;
44 if ($result->wasChanged()) {
45 ResultChanged::dispatch($result);
48 if ($result->wasChanged(['forfeit', 'time'])) {
49 Protocol::resultReported(
54 DiscordBotCommand::queueResult($result);
55 } else if ($result->wasChanged('comment')) {
56 Protocol::resultCommented(
63 $round->load('results');
64 $round->updatePlacement();
65 $round->tournament->updatePlacement();
67 return $result->toJson();