]> git.localhorst.tv Git - alttp.git/commitdiff
listen to round updates
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 11 Mar 2022 10:17:27 +0000 (11:17 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 11 Mar 2022 10:17:27 +0000 (11:17 +0100)
app/Events/ProtocolAdded.php [new file with mode: 0644]
app/Events/RoundAdded.php [new file with mode: 0644]
app/Http/Controllers/RoundController.php
app/Models/Protocol.php
resources/js/components/pages/Tournament.js
routes/channels.php

diff --git a/app/Events/ProtocolAdded.php b/app/Events/ProtocolAdded.php
new file mode 100644 (file)
index 0000000..3abcb86
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Events;
+
+use App\Models\Protocol;
+use Illuminate\Broadcasting\Channel;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PresenceChannel;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class ProtocolAdded implements ShouldBroadcast
+{
+       use Dispatchable, InteractsWithSockets, SerializesModels;
+
+       /**
+        * Create a new event instance.
+        *
+        * @return void
+        */
+       public function __construct(Protocol $protocol)
+       {
+               $protocol->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 (file)
index 0000000..a1b8787
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+namespace App\Events;
+
+use App\Models\Round;
+use Illuminate\Broadcasting\Channel;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PresenceChannel;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class RoundAdded implements ShouldBroadcast
+{
+       use Dispatchable, InteractsWithSockets, SerializesModels;
+
+       /**
+        * Create a new event instance.
+        *
+        * @return void
+        */
+       public function __construct(Round $round)
+       {
+               $this->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;
+
+}
index 10e4d14857fd45000863df2fab536583a8404c6e..1488d97bff92e15004407c2bc6493f507761bfcd 100644 (file)
@@ -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();
        }
 
index fb93a374d744668941056175dcf5c8c383eb6428..bbfea732fe97fecfc5a7a48da553580577bc78bf 100644 (file)
@@ -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',
+       ];
+
 }
index cdafd69b12c2414140f3dce96d07c87cd2e68bf2..1f2a354c4cfaf39b1b1e2d2729ce733f84f8455e 100644 (file)
@@ -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 <Loading />;
        }
index 4302bb7d72871176f7d244756cdc14ca64197566..4581feb7df93cf85c1b5501370046d7c1e6cc18a 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use App\Models\Tournament;
 use Illuminate\Support\Facades\Broadcast;
 
 /*
@@ -20,3 +21,8 @@ Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
 Broadcast::channel('App.Control', function ($user) {
        return true;
 });
+
+Broadcast::channel('Tournament.{id}', function ($user, $id) {
+       $tournament = Tournament::findOrFail($id);
+       return true;
+});