From 4388278823ac8a791641ac1d65fc675e9543b8e8 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 11 Mar 2022 11:17:27 +0100 Subject: [PATCH] listen to round updates --- app/Events/ProtocolAdded.php | 41 ++++++++++++++ app/Events/RoundAdded.php | 40 +++++++++++++ app/Http/Controllers/RoundController.php | 10 ++++ app/Models/Protocol.php | 63 +++++++++++++++++++++ resources/js/components/pages/Tournament.js | 16 ++++++ routes/channels.php | 6 ++ 6 files changed, 176 insertions(+) create mode 100644 app/Events/ProtocolAdded.php create mode 100644 app/Events/RoundAdded.php diff --git a/app/Events/ProtocolAdded.php b/app/Events/ProtocolAdded.php new file mode 100644 index 0000000..3abcb86 --- /dev/null +++ b/app/Events/ProtocolAdded.php @@ -0,0 +1,41 @@ +load('user'); + $this->protocol = $protocol; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('Tournament.'.$this->protocol->tournament_id); + } + + public $protocol; + +} diff --git a/app/Events/RoundAdded.php b/app/Events/RoundAdded.php new file mode 100644 index 0000000..a1b8787 --- /dev/null +++ b/app/Events/RoundAdded.php @@ -0,0 +1,40 @@ +round = $round; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('Tournament.'.$this->round->tournament_id); + } + + public $round; + +} diff --git a/app/Http/Controllers/RoundController.php b/app/Http/Controllers/RoundController.php index 10e4d14..1488d97 100644 --- a/app/Http/Controllers/RoundController.php +++ b/app/Http/Controllers/RoundController.php @@ -2,6 +2,8 @@ namespace App\Http\Controllers; +use App\Events\RoundAdded; +use App\Models\Protocol; use App\Models\Round; use App\Models\Tournament; use Illuminate\Http\Request; @@ -20,6 +22,14 @@ class RoundController extends Controller 'tournament_id' => $validatedData['tournament_id'], ]); + Protocol::roundAdded( + $tournament, + $round, + $request->user(), + ); + + RoundAdded::dispatch($round); + return $round->toJson(); } diff --git a/app/Models/Protocol.php b/app/Models/Protocol.php index fb93a37..bbfea73 100644 --- a/app/Models/Protocol.php +++ b/app/Models/Protocol.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Events\ProtocolAdded; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -9,6 +10,56 @@ class Protocol extends Model { use HasFactory; + public static function roundAdded(Tournament $tournament, Round $round, User $user) { + $protocol = static::create([ + 'tournament_id' => $tournament->id, + 'user_id' => $user->id, + 'type' => 'round.create', + 'details' => [ + 'tournament' => static::tournamentMemo($tournament), + 'round' => static::roundMemo($round), + ], + ]); + ProtocolAdded::dispatch($protocol); + } + + public static function tournamentCreated(Tournament $tournament, User $user) { + $protocol = static::create([ + 'tournament_id' => $tournament->id, + 'user_id' => $user->id, + 'type' => 'tournament.create', + 'details' => [ + 'tournament' => static::tournamentMemo($tournament), + ], + ]); + ProtocolAdded::dispatch($protocol); + } + + + protected static function roundMemo(Round $round) { + return [ + 'id' => $round->id, + 'seed' => $round->seed, + ]; + } + + protected static function tournamentMemo(Tournament $tournament) { + return [ + 'id' => $tournament->id, + 'title' => $tournament->title, + ]; + } + + protected static function userMemo(User $user) { + return [ + 'id' => $user->id, + 'username' => $user->username, + 'discriminator' => $user->discriminator, + 'avatar' => $user->avatar, + ]; + } + + public function tournament() { return $this->belongsTo(Tournament::class); } @@ -17,4 +68,16 @@ class Protocol extends Model return $this->belongsTo(User::class); } + + protected $casts = [ + 'details' => 'array', + ]; + + protected $fillable = [ + 'details', + 'tournament_id', + 'type', + 'user_id', + ]; + } diff --git a/resources/js/components/pages/Tournament.js b/resources/js/components/pages/Tournament.js index cdafd69..1f2a354 100644 --- a/resources/js/components/pages/Tournament.js +++ b/resources/js/components/pages/Tournament.js @@ -32,6 +32,22 @@ const Tournament = () => { }); }, [id]); + useEffect(() => { + window.Echo.private(`Tournament.${id}`) + .listen('RoundAdded', e => { + console.log(e); + if (e.round) { + setTournament(tournament => ({ + ...tournament, + rounds: [...tournament.rounds, e.round], + })); + } + }); + return () => { + window.Echo.leave(`Tournament.${id}`); + }; + }, [id]); + if (loading) { return ; } diff --git a/routes/channels.php b/routes/channels.php index 4302bb7..4581feb 100644 --- a/routes/channels.php +++ b/routes/channels.php @@ -1,5 +1,6 @@