From 90c531f99b2ee73b1467d983704a4883b7fa4dfd Mon Sep 17 00:00:00 2001 From: Vari Date: Sat, 2 Mar 2024 18:06:45 +0100 Subject: [PATCH] Added Queueing Logic --- .../Api/Matchmaking/MatchmakingController.php | 80 +++++++++++++++++-- .../Api/Matchmaking/QueueResponse.php | 26 +++++- .../Models/Game/Matchmaking/QueuedPlayer.php | 12 ++- 3 files changed, 107 insertions(+), 11 deletions(-) diff --git a/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php b/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php index d73fd53..455ded0 100644 --- a/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php +++ b/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php @@ -2,12 +2,17 @@ namespace App\Http\Controllers\Api\Matchmaking; +use App\Enums\Game\Matchmaking\MatchmakingSide; use App\Enums\Game\Matchmaking\QueueStatus; use App\Http\Controllers\Controller; use App\Http\Requests\Api\Matchmaking\QueueRequest; +use App\Http\Responses\Api\Matchmaking\MatchData; +use App\Http\Responses\Api\Matchmaking\MatchProperties; use App\Http\Responses\Api\Matchmaking\QueueData; use App\Http\Responses\Api\Matchmaking\QueueResponse; -use Illuminate\Http\Request; +use App\Models\Game\Matchmaking\Game; +use App\Models\Game\Matchmaking\QueuedPlayer; +use Auth; class MatchmakingController extends Controller { @@ -18,13 +23,76 @@ public function getRegions() public function queue(QueueRequest $request) { + if($request->checkOnly) + return json_encode($this->checkQueueStatus($request)); + + return json_encode($this->addPlayerToQueue($request)); + } + + protected function checkQueueStatus(QueueRequest $request): QueueResponse + { + $user = Auth::user(); + $foundQueuedPlayer = QueuedPlayer::firstWhere('user_id', '=', $user->id); + + if($foundQueuedPlayer !== null) { + $response = new QueueResponse(); + $response->status = QueueStatus::Queued; + $response->queueData = new QueueData( + 1, + 10 + ); + + return $response; + } + + $foundGame = $user->games(); + + if($foundGame->count() < 1) + return 'test'; + + /** @var Game $foundGame */ + $foundGame = $foundGame->first(); + $response = new QueueResponse(); - $response->status = QueueStatus::Queued; - $response->queueData = new QueueData( - 0, - -100, + $matchData = new MatchData(); + + $matchData->matchId = $foundGame->id; + $matchData->category = $request->category; + $matchData->creationDateTime = $foundGame->created_at->getTimestamp(); + $matchData->status = $foundGame->status; + $matchData->creator = $foundGame->creator->creator_user_id; + + $players = $foundGame->players; + $matchData->players = $players->pluck('id')->toArray(); + $matchData->sideA = $players->where('pivot.side', MatchmakingSide::Hunter->value)->pluck('id')->toArray(); + $matchData->sideB = $players->where('pivot.side', MatchmakingSide::Runner->value)->pluck('id')->toArray(); + + $matchData->props = new MatchProperties( + $foundGame->matchConfiguration, ); - return json_encode($response); + $response->matchData = $matchData; + + return $response; + } + + protected function addPlayerToQueue(QueueRequest $request) + { + $user = Auth::user(); + $queued = QueuedPlayer::firstOrCreate(['user_id' => $user->id]); + $queued->leader()->disassociate(); + $queued->side = $request->side; + $queued->user()->associate($user->id); + $queued->save(); + + foreach ($request->additionalUserIds as $additionalUserId) { + $follower = QueuedPlayer::firstOrCreate(['user_id' => $additionalUserId]); + $follower->side = $request->side; + $follower->user()->associate($additionalUserId); + $follower->save(); + $queued->followingUsers()->save($follower); + } + + return $this->checkQueueStatus($request); } } diff --git a/dist/app/Http/Responses/Api/Matchmaking/QueueResponse.php b/dist/app/Http/Responses/Api/Matchmaking/QueueResponse.php index 679bf90..e99553f 100644 --- a/dist/app/Http/Responses/Api/Matchmaking/QueueResponse.php +++ b/dist/app/Http/Responses/Api/Matchmaking/QueueResponse.php @@ -4,6 +4,8 @@ use App\Enums\Game\Matchmaking\MatchStatus; use App\Enums\Game\Matchmaking\QueueStatus; +use App\Models\Game\Matchmaking\MatchConfiguration; +use Illuminate\Support\Facades\Hash; class QueueResponse { @@ -53,7 +55,7 @@ class MatchData { /** * @var string[] */ - public array $sideB; + public array $sideB = []; public object $customData; @@ -66,4 +68,26 @@ public function __construct() $this->customData = new \stdClass(); $this->props = new \stdClass(); } +} + +class MatchProperties implements \JsonSerializable { + public MatchConfiguration $matchConfiguration; + + protected string $platform = 'Windows'; + + public function __construct(MatchConfiguration $matchConfiguration) + { + $this->matchConfiguration = $matchConfiguration; + } + + public function jsonSerialize(): mixed + { + return [ + 'matchConfiguration' => $this->matchConfiguration->asset_path, + 'countA' => $this->matchConfiguration->hunters, + 'countB' => $this->matchConfiguration->runners, + 'gameMode' => md5($this->matchConfiguration->asset_path).'-Default', + 'platform' => $this->platform, + ]; + } } \ No newline at end of file diff --git a/dist/app/Models/Game/Matchmaking/QueuedPlayer.php b/dist/app/Models/Game/Matchmaking/QueuedPlayer.php index 4975837..4be6905 100644 --- a/dist/app/Models/Game/Matchmaking/QueuedPlayer.php +++ b/dist/app/Models/Game/Matchmaking/QueuedPlayer.php @@ -4,7 +4,6 @@ use App\Enums\Game\Matchmaking\MatchmakingSide; use App\Models\User\User; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -24,6 +23,11 @@ class QueuedPlayer extends Model 'side' => MatchmakingSide::class, ]; + protected static function boot() + { + parent::boot(); + } + public function user() { return $this->belongsTo(User::class); @@ -31,16 +35,16 @@ public function user() public function followingUsers() { - return $this->hasMany(QueuedPlayer::class, 'queued_runner_id'); + return $this->hasMany(QueuedPlayer::class, 'queued_player_id'); } public function leader() { - return $this->belongsTo(QueuedPlayer::class, 'queued_runner_id'); + return $this->belongsTo(QueuedPlayer::class, 'queued_player_id'); } public static function getLeaders(): \Illuminate\Support\Collection { - return static::whereNull('queued_runner_id')->get(); + return static::whereNull('queued_player_id')->get(); } }