From 6a204c69acceeb2fc6eab31ceee81b7cbe3d100f Mon Sep 17 00:00:00 2001 From: "m.razaghi" Date: Thu, 20 Aug 2026 17:37:48 +0330 Subject: [PATCH] Revert "revert commit" This reverts commit 641332e5ba12b94e6f9aa7a2f4aa297d1ea2c64d. --- .env.pusher => .env | 0 .gitignore | 4 +-- app/Events/MatchEnded.php | 62 ++++++++++++++++----------------- app/Events/MatchStarted.php | 68 ++++++++++++++++++------------------- config/app.php | 2 +- routes/channels.php | 21 +++++++++++- 6 files changed, 88 insertions(+), 69 deletions(-) rename .env.pusher => .env (100%) diff --git a/.env.pusher b/.env similarity index 100% rename from .env.pusher rename to .env diff --git a/.gitignore b/.gitignore index e4a8c41..ce4783c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,7 @@ /public/storage /storage/*.key /vendor -.env.pusher +.env .env.backup .env.production .phpunit.result.cache @@ -17,4 +17,4 @@ yarn-error.log /.fleet /.idea /.vscode -*.DS_Store +*.DS_Store \ No newline at end of file diff --git a/app/Events/MatchEnded.php b/app/Events/MatchEnded.php index 5e1d82e..354337d 100644 --- a/app/Events/MatchEnded.php +++ b/app/Events/MatchEnded.php @@ -1,32 +1,32 @@ matchId = $matchId; -// } -// -// public function broadcastOn() -// { -// // ارسال پیام به کانال پرایوت بازی که هر دو نفر داخل آن هستند -// return new PrivateChannel('pvp-match.' . $this->matchId); -// } -// -// public function broadcastAs() -// { -// return 'match.ended'; -// } -//} + +namespace App\Events; + +use Illuminate\Broadcasting\InteractsWithSockets; +use Illuminate\Broadcasting\PrivateChannel; +use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; +use Illuminate\Foundation\Events\Dispatchable; +use Illuminate\Queue\SerializesModels; + +class MatchEnded implements ShouldBroadcastNow +{ + use Dispatchable, InteractsWithSockets, SerializesModels; + + public $matchId; + + public function __construct($matchId) + { + $this->matchId = $matchId; + } + + public function broadcastOn() + { + // ارسال پیام به کانال پرایوت بازی که هر دو نفر داخل آن هستند + return new PrivateChannel('pvp-match.' . $this->matchId); + } + + public function broadcastAs() + { + return 'match.ended'; + } +} diff --git a/app/Events/MatchStarted.php b/app/Events/MatchStarted.php index 58e7d5b..0ceef6f 100644 --- a/app/Events/MatchStarted.php +++ b/app/Events/MatchStarted.php @@ -1,35 +1,35 @@ matchData = $matchData; -// $this->ticketId = $ticketId; -// } -// -// public function broadcastOn() -// { -// // ارسال پیام به کانال پرایوت مختص تیکت کاربری که در صف انتظار بود -// return new PrivateChannel('matchmaking.' . $this->ticketId); -// } -// -// public function broadcastAs() -// { -// // نام ایونتی که کاتلین باید به آن گوش دهد -// return 'match.started'; -// } -//} + +namespace App\Events; + +use Illuminate\Broadcasting\InteractsWithSockets; +use Illuminate\Broadcasting\PrivateChannel; +use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; +use Illuminate\Foundation\Events\Dispatchable; +use Illuminate\Queue\SerializesModels; + +class MatchStarted implements ShouldBroadcastNow +{ + use Dispatchable, InteractsWithSockets, SerializesModels; + + public $matchData; + public $ticketId; + + public function __construct($matchData, $ticketId) + { + $this->matchData = $matchData; + $this->ticketId = $ticketId; + } + + public function broadcastOn() + { + // ارسال پیام به کانال پرایوت مختص تیکت کاربری که در صف انتظار بود + return new PrivateChannel('matchmaking.' . $this->ticketId); + } + + public function broadcastAs() + { + // نام ایونتی که کاتلین باید به آن گوش دهد + return 'match.started'; + } +} diff --git a/config/app.php b/config/app.php index 343bf6f..8cdb290 100644 --- a/config/app.php +++ b/config/app.php @@ -165,7 +165,7 @@ return [ */ App\Providers\AppServiceProvider::class, App\Providers\AuthServiceProvider::class, - // App\Providers\BroadcastServiceProvider::class, + App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, App\Providers\FortifyServiceProvider::class, diff --git a/routes/channels.php b/routes/channels.php index 5d451e1..9afc718 100644 --- a/routes/channels.php +++ b/routes/channels.php @@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Broadcast; | Broadcast Channels |-------------------------------------------------------------------------- | -| Here you may register all of the event broadcasting channels that your +| Here you may register all of the Events broadcasting channels that your | application supports. The given channel authorization callbacks are | used to check if an authenticated user can listen to the channel. | @@ -16,3 +16,22 @@ use Illuminate\Support\Facades\Broadcast; Broadcast::channel('App.Models.User.{id}', function ($user, $id) { return (int) $user->id === (int) $id; }); + + + +// کانال انتظار برای پیدا شدن حریف +Broadcast::channel('matchmaking.{ticketId}', function ($user, $ticketId) { + // در این مرحله، هر کاربری که لاگین باشد و تیکت خودش را داشته باشد اجازه دسترسی دارد + return true; +}); + +// کانال داخل بازی (برای ارسال پایان بازی یا تغییرات زنده) +Broadcast::channel('pvp-match.{matchId}', function ($user, $matchId) { + $match = \App\Models\PvpMatch::find($matchId); + if (!$match) { + return false; + } + + // فقط اگر این کاربر یکی از دو بازیکنِ این مسابقه باشد، اجازه دسترسی به کانال را دارد + return $match->players()->where('user_id', $user->id)->exists(); +});