Added Queueing Logic

This commit is contained in:
Vari
2024-03-02 18:06:45 +01:00
parent f179af9185
commit 90c531f99b
3 changed files with 107 additions and 11 deletions

View File

@@ -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);
}
}

View File

@@ -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,
];
}
}

View File

@@ -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();
}
}