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',
23 'vod' => 'string|url',
26 $round = Round::findOrFail($validatedData['round_id']);
28 if ($validatedData['user_id'] != $request->user()->id) {
29 $this->authorize('create', Result::class);
32 $result = Result::firstOrCreate([
33 'round_id' => $validatedData['round_id'],
34 'user_id' => $validatedData['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 = !empty($validatedData['comment']) ? $validatedData['comment'] : null;
41 $result->vod = !empty($validatedData['vod']) ? $validatedData['vod'] : 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', 'vod'])) {
56 Protocol::resultCommented(
63 $round->load('results');
64 $round->updatePlacement();
65 if ($round->tournament->hasScoreboard()) {
66 $round->tournament->updatePlacement();
69 $result->load('user');
71 return $result->toJson();