--- /dev/null
+<?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;
+
+}
--- /dev/null
+<?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;
+
+}
namespace App\Http\Controllers;
+use App\Events\RoundAdded;
+use App\Models\Protocol;
use App\Models\Round;
use App\Models\Tournament;
use Illuminate\Http\Request;
'tournament_id' => $validatedData['tournament_id'],
]);
+ Protocol::roundAdded(
+ $tournament,
+ $round,
+ $request->user(),
+ );
+
+ RoundAdded::dispatch($round);
+
return $round->toJson();
}
namespace App\Models;
+use App\Events\ProtocolAdded;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\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);
}
return $this->belongsTo(User::class);
}
+
+ protected $casts = [
+ 'details' => 'array',
+ ];
+
+ protected $fillable = [
+ 'details',
+ 'tournament_id',
+ 'type',
+ 'user_id',
+ ];
+
}
});
}, [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 />;
}
<?php
+use App\Models\Tournament;
use Illuminate\Support\Facades\Broadcast;
/*
Broadcast::channel('App.Control', function ($user) {
return true;
});
+
+Broadcast::channel('Tournament.{id}', function ($user, $id) {
+ $tournament = Tournament::findOrFail($id);
+ return true;
+});