mirror of
https://github.com/Deathgarden-Rebirth/Deathgarden_Rebirth-Rewrite.git
synced 2026-09-08 03:06:03 -05:00
Added Matchmaking Register endpoint, this endpoints sets a game from 'Created' to 'Open' when the host has laoded the map, so that palyers can start to join.
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
use App\Enums\Game\Matchmaking\QueueStatus;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Api\Matchmaking\QueueRequest;
|
||||
use App\Http\Requests\Api\Matchmaking\RegisterMatchRequest;
|
||||
use App\Http\Responses\Api\Matchmaking\MatchData;
|
||||
use App\Http\Responses\Api\Matchmaking\MatchProperties;
|
||||
use App\Http\Responses\Api\Matchmaking\QueueData;
|
||||
@@ -31,7 +32,6 @@ public function getRegions()
|
||||
public function queue(QueueRequest $request)
|
||||
{
|
||||
$this->processQueue();
|
||||
$this->processGames();
|
||||
if($request->checkOnly)
|
||||
return json_encode($this->checkQueueStatus($request));
|
||||
|
||||
@@ -45,25 +45,31 @@ public function matchInfo(string $matchId)
|
||||
if($foundGame === null)
|
||||
return ['status' => 'Error', 'message' => 'Match not found.'];
|
||||
|
||||
$response = new MatchData();
|
||||
|
||||
$response->matchId = $foundGame->id;
|
||||
$response->creationDateTime = $foundGame->created_at->getTimestamp();
|
||||
$response->status = $foundGame->status;
|
||||
$response->creator = $foundGame->creator?->id;
|
||||
|
||||
$players = $foundGame->players;
|
||||
$response->players = $players->pluck('id')->toArray();
|
||||
$response->sideA = $players->where('pivot.side', MatchmakingSide::Hunter->value)->pluck('id')->toArray();
|
||||
$response->sideB = $players->where('pivot.side', MatchmakingSide::Runner->value)->pluck('id')->toArray();
|
||||
|
||||
$response->props = new MatchProperties(
|
||||
$foundGame->matchConfiguration,
|
||||
);
|
||||
$response = MatchData::fromGame($foundGame);
|
||||
|
||||
return json_encode($response);
|
||||
}
|
||||
|
||||
public function register(RegisterMatchRequest $request, string $matchId)
|
||||
{
|
||||
$foundGame = Game::find($matchId);
|
||||
$foundGame->session_settings = $request->sessionSettings;
|
||||
$foundGame->status = MatchStatus::Opened;
|
||||
$foundGame->save();
|
||||
|
||||
return json_encode(MatchData::fromGame($foundGame));
|
||||
}
|
||||
|
||||
public function seedFileGet()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function seedFilePost()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected function checkQueueStatus(QueueRequest $request): QueueResponse
|
||||
{
|
||||
$user = Auth::user();
|
||||
@@ -99,7 +105,6 @@ protected function checkQueueStatus(QueueRequest $request): QueueResponse
|
||||
/** @var Game $foundGame */
|
||||
$foundGame = $foundGame->first();
|
||||
$response = new QueueResponse();
|
||||
$matchData = new MatchData();
|
||||
|
||||
if($foundGame->status === MatchStatus::Opened) {
|
||||
$response->queueData = new QueueData(
|
||||
@@ -111,22 +116,7 @@ protected function checkQueueStatus(QueueRequest $request): QueueResponse
|
||||
else
|
||||
$response->status = QueueStatus::Matched;
|
||||
|
||||
$matchData->matchId = $foundGame->id;
|
||||
$matchData->category = $request->category;
|
||||
$matchData->creationDateTime = $foundGame->created_at->getTimestamp();
|
||||
$matchData->status = $foundGame->status;
|
||||
$matchData->creator = $foundGame->creator?->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,
|
||||
);
|
||||
|
||||
$response->matchData = $matchData;
|
||||
$response->matchData = MatchData::fromGame($foundGame);
|
||||
|
||||
return $response;
|
||||
}
|
||||
@@ -204,7 +194,7 @@ protected function processQueue(): void
|
||||
rsort($hunterGroupsSet, SORT_NUMERIC);
|
||||
|
||||
$newGame = new Game();
|
||||
$newGame->status = MatchStatus::Opened;
|
||||
$newGame->status = MatchStatus::Created;
|
||||
$newGame->matchConfiguration()->associate($selectedConfig);
|
||||
$newGame->save();
|
||||
|
||||
@@ -223,22 +213,8 @@ protected function processQueue(): void
|
||||
|
||||
$newGame->addQueuedPlayer($runners[$foundQueuedPlayer]);
|
||||
}
|
||||
}
|
||||
|
||||
private function processGames()
|
||||
{
|
||||
$games = Game::where('status', '=', MatchStatus::Opened)->get();
|
||||
|
||||
foreach ($games as $game) {
|
||||
if($game->remainingPlayerCount()->getTotal() == 0) {
|
||||
$hunter = $game->players()->firstWhere('side', '=', MatchmakingSide::Hunter->value);
|
||||
$game->creator()->associate($hunter);
|
||||
$game->status = MatchStatus::Created;
|
||||
$game->save();
|
||||
}
|
||||
|
||||
//Todo: Handle games with missing users later
|
||||
}
|
||||
$newGame->determineHost();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
36
dist/app/Http/Requests/Api/Matchmaking/RegisterMatchRequest.php
vendored
Normal file
36
dist/app/Http/Requests/Api/Matchmaking/RegisterMatchRequest.php
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Api\Matchmaking;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class RegisterMatchRequest extends FormRequest
|
||||
{
|
||||
public string $sessionSettings;
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return Auth::check();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'customData.SessionSettings' => 'required|string'
|
||||
];
|
||||
}
|
||||
|
||||
protected function passedValidation()
|
||||
{
|
||||
$this->sessionSettings = $this->input('customData.SessionSettings');
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Http\Responses\Api\Matchmaking;
|
||||
|
||||
use App\Enums\Game\Matchmaking\MatchmakingSide;
|
||||
use App\Enums\Game\Matchmaking\MatchStatus;
|
||||
use App\Models\Game\Matchmaking\Game;
|
||||
|
||||
class MatchData
|
||||
{
|
||||
@@ -48,4 +50,29 @@ public function __construct()
|
||||
$this->customData = new \stdClass();
|
||||
$this->props = new \stdClass();
|
||||
}
|
||||
|
||||
public static function fromGame(Game &$game): MatchData {
|
||||
$data = new MatchData();
|
||||
|
||||
$data->matchId = $game->id;
|
||||
$data->creationDateTime = $game->created_at->getTimestamp();
|
||||
$data->status = $game->status;
|
||||
$data->creator = $game->creator?->id;
|
||||
|
||||
$players = $game->players;
|
||||
$data->players = $players->pluck('id')->toArray();
|
||||
$data->sideA = $players->where('pivot.side', MatchmakingSide::Hunter->value)->pluck('id')->toArray();
|
||||
$data->sideB = $players->where('pivot.side', MatchmakingSide::Runner->value)->pluck('id')->toArray();
|
||||
|
||||
if($game->session_settings !== '')
|
||||
$data->customData = (object)[
|
||||
'SessionSettings' => $game->session_settings,
|
||||
];
|
||||
|
||||
$data->props = new MatchProperties(
|
||||
$game->matchConfiguration,
|
||||
);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
7
dist/app/Models/Game/Matchmaking/Game.php
vendored
7
dist/app/Models/Game/Matchmaking/Game.php
vendored
@@ -59,6 +59,13 @@ public function addQueuedPlayer(QueuedPlayer $player): void
|
||||
});
|
||||
}
|
||||
|
||||
public function determineHost(): void
|
||||
{
|
||||
$hunter = $this->players()->firstWhere('side', '=', MatchmakingSide::Hunter->value);
|
||||
$this->creator()->associate($hunter);
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public function remainingPlayerCount(): MatchmakingPlayerCount
|
||||
{
|
||||
$players = $this->players;
|
||||
|
||||
@@ -19,6 +19,7 @@ public function up(): void
|
||||
->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->foreignId('match_configuration_id')->constrained()
|
||||
->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->string('session_settings', 2000)->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
4
dist/routes/deathgardenApi.php
vendored
4
dist/routes/deathgardenApi.php
vendored
@@ -63,6 +63,10 @@
|
||||
|
||||
Route::post('queue', [MatchmakingController::class, 'queue']);
|
||||
Route::get('match/{matchId}', [MatchmakingController::class, 'matchInfo']);
|
||||
Route::post('match/{matchId}/register', [MatchmakingController::class, 'register']);
|
||||
|
||||
Route::post('file/{gameVersion}/{seed}/{mapName}', [MatchmakingController::class, 'seedFilePost']);
|
||||
Route::get('file/{gameVersion}/{seed}/{mapName}', [MatchmakingController::class, 'seedFileGet']);
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user