3 namespace App\Http\Controllers;
5 use App\Events\ResultChanged;
6 use App\Models\DiscordBotCommand;
7 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 'round_id' => 'required|exists:App\\Models\\Round,id',
22 'user_id' => 'required|exists:App\\Models\\User,id',
25 $round = Round::findOrFail($validatedData['round_id']);
27 if ($validatedData['user_id'] != $request->user()->id) {
28 $this->authorize('create', Result::class);
31 $result = Result::firstOrCreate([
32 'round_id' => $validatedData['round_id'],
33 'user_id' => $validatedData['user_id'],
35 if (!$round->locked) {
36 if (isset($validatedData['forfeit'])) $result->forfeit = $validatedData['forfeit'];
37 if (isset($validatedData['time'])) $result->time = $validatedData['time'];
39 $result->comment = $validatedData['comment'] ? $validatedData['comment'] : null;
42 if ($result->wasChanged()) {
43 ResultChanged::dispatch($result);
46 if ($result->wasChanged(['forfeit', 'time'])) {
47 Protocol::resultReported(
52 DiscordBotCommand::queueResult($result);
53 } else if ($result->wasChanged('comment')) {
54 Protocol::resultCommented(
61 $round->load('results');
62 $round->updatePlacement();
63 if ($round->tournament->hasScoreboard()) {
64 $round->tournament->updatePlacement();
67 $result->load('user');
69 return $result->toJson();