namespace App\Http\Controllers;
-use App\Events\ResultReported;
+use App\Events\ResultChanged;
use App\Models\Participant;
use App\Models\Protocol;
use App\Models\Result;
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' => 'required|numeric',
+ 'time' => 'numeric',
]);
$participant = Participant::findOrFail($validatedData['participant_id']);
$this->authorize('create', Result::class);
}
- $result = Result::updateOrCreate([
+ $result = Result::firstOrCreate([
'round_id' => $validatedData['round_id'],
'user_id' => $participant->user_id,
- ], [
- 'time' => $validatedData['time'],
]);
+ 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);
+ }
- Protocol::resultReported(
- $round->tournament,
- $result,
- $request->user(),
- );
+ if ($result->wasChanged(['forfeit', 'time'])) {
+ Protocol::resultReported(
+ $round->tournament,
+ $result,
+ $request->user(),
+ );
+ } else if ($result->wasChanged('comment')) {
+ Protocol::resultCommented(
+ $round->tournament,
+ $result,
+ $request->user(),
+ );
+ }
- ResultReported::dispatch($result);
+ $round->load('results');
+ $round->updatePlacement();
+ $round->tournament->updatePlacement();
return $result->toJson();
}