From 00ae7e16aa31ad071e180416533f9f7e0cbcbff6 Mon Sep 17 00:00:00 2001 From: Vari Date: Fri, 6 Sep 2024 11:36:10 +0200 Subject: [PATCH] Adjusted Matchmaking now also removing games the user is the creator of when freshly queueing. Added some loggin to the matchmaking job. Adjusted Matchmaking cleanup times. Changed Session log to now put the session logs of a user inside a folder with their name. --- .../Console/Commands/MatchmakingCleanup.php | 4 +-- .../Console/Commands/ProcessMatchmaking.php | 36 ++++++++++++++++++- .../Api/Matchmaking/MatchmakingController.php | 5 +++ dist/app/Http/Middleware/AccessLogger.php | 24 +++++++++---- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/dist/app/Console/Commands/MatchmakingCleanup.php b/dist/app/Console/Commands/MatchmakingCleanup.php index a413ea03..0c528ab1 100644 --- a/dist/app/Console/Commands/MatchmakingCleanup.php +++ b/dist/app/Console/Commands/MatchmakingCleanup.php @@ -13,10 +13,10 @@ class MatchmakingCleanup extends Command { // After how many minutes a queued palyer gets removed from the queue or from an open match in Minutes. - const PLAYER_HEARTBEAT_TIMEOUT = 1; + const PLAYER_HEARTBEAT_TIMEOUT = 0.25; // After how many minutes a closed game gets deleted automatically when it hasn't been killed normally yet. - const GAME_MAX_TIME = 15; + const GAME_MAX_TIME = 11; const CREATED_GAME_TIMEOUT = 30; diff --git a/dist/app/Console/Commands/ProcessMatchmaking.php b/dist/app/Console/Commands/ProcessMatchmaking.php index b7ca5ec5..4e8c7d0a 100644 --- a/dist/app/Console/Commands/ProcessMatchmaking.php +++ b/dist/app/Console/Commands/ProcessMatchmaking.php @@ -10,6 +10,8 @@ use App\Models\Game\Matchmaking\QueuedPlayer; use Illuminate\Console\Command; use Illuminate\Database\Eloquent\Collection; +use Log; +use Psr\Log\LoggerInterface; class ProcessMatchmaking extends Command { @@ -27,6 +29,8 @@ class ProcessMatchmaking extends Command */ protected $description = 'Command description'; + protected static LoggerInterface $log; + /** * Execute the console command. */ @@ -41,8 +45,10 @@ public function handle(): void ->get(); // If there are no players in the queue, stop here. - if($players->isEmpty()) + if($players->isEmpty()){ + static::log()->info('No Users in Queue, Stopping'); return; + } $runners = new Collection(); $hunters = new Collection(); @@ -55,6 +61,13 @@ public function handle(): void $runners->add($player); }); + static::log()->info('Users in Queue:'. json_encode([ + 'hunters' => $hunters->toArray(), + 'runners' => $runners->toArray(), + ], + JSON_PRETTY_PRINT + )); + $this->tryFillOpenGames($hunters, $runners); $playerCount = $this->getTotalPlayersCount($players); @@ -84,6 +97,8 @@ public function handle(): void $newGame->matchConfiguration()->associate($selectedConfig); $newGame->save(); + static::log()->info('New game created: '. json_encode($newGame->toArray(), JSON_PRETTY_PRINT)); + foreach ($hunterGroupsSet as $groupSize) { $foundQueuedPlayerIndex = $hunters->search(function (QueuedPlayer $hunter) use ($groupSize) { return ($hunter->following_users_count + 1) === $groupSize; @@ -108,6 +123,8 @@ protected function tryFillOpenGames(Collection|array &$hunters, Collection|array { $openGames = Game::where('status', '=', MatchStatus::Opened->value)->get(); + static::log()->info('Found Open Matches:' . json_encode($openGames->toArray(),JSON_PRETTY_PRINT)); + foreach ($openGames as $game) { $neededPlayers = $game->remainingPlayerCount(); @@ -131,6 +148,12 @@ protected function tryFillOpenGames(Collection|array &$hunters, Collection|array }); $foundHunter = $hunters->pull($foundQueuedPlayerIndex); + static::log()->info('Filled hunter slot on open game.'. json_encode([ + 'hunter' => $foundHunter, + 'game' => $game, + ], + JSON_PRETTY_PRINT) + ); $game->addQueuedPlayer($foundHunter); } } @@ -151,6 +174,12 @@ protected function tryFillOpenGames(Collection|array &$hunters, Collection|array }); $foundRunner = $runners->pull($foundQueuedPlayerIndex); + static::log()->info('Filled runner slot on open game.'. json_encode([ + 'hunter' => $foundHunter, + 'game' => $game, + ], + JSON_PRETTY_PRINT) + ); $game->addQueuedPlayer($foundRunner); } } @@ -189,4 +218,9 @@ private function determineMatchingPlayers(Collection &$queuedPlayers, int $targe return $result; return false; } + + public static function log(): LoggerInterface + { + return static::$log ?? static::$log = Log::channel('matchmaking'); + } } diff --git a/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php b/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php index 30c9189e..09d0470f 100644 --- a/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php +++ b/dist/app/Http/Controllers/Api/Matchmaking/MatchmakingController.php @@ -338,6 +338,11 @@ protected function addPlayerToQueue(QueueRequest $request) } } + // Delete any active matches where the newly queued user is the creator. + Game::where('creator_user_id', '=', $user->id) + ->whereIn('status', [MatchStatus::Opened, MatchStatus::Created]) + ->delete(); + $queued = QueuedPlayer::firstOrCreate(['user_id' => $user->id]); $queued->leader()->disassociate(); $queued->side = $request->side; diff --git a/dist/app/Http/Middleware/AccessLogger.php b/dist/app/Http/Middleware/AccessLogger.php index 0c054458..5ab6ff41 100644 --- a/dist/app/Http/Middleware/AccessLogger.php +++ b/dist/app/Http/Middleware/AccessLogger.php @@ -2,6 +2,7 @@ namespace App\Http\Middleware; +use App\Models\User\User; use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -64,21 +65,21 @@ public static function getSessionLogConfig(): LoggerInterface { $user = Auth::user(); if (!Session::has('sessionLogConfig')) { - $username = $user?->last_known_username ?? ''; - static::cleanupUsername($username); $logConfig = [ 'driver' => 'single', - 'path' => storage_path('logs/sessions/' . $username . '_' . Str::substr(Session::getId(), 0, 12) . '.log') + 'path' => static::getSessionLogPath($user), ]; Session::put('sessionLogConfig', $logConfig); } else $logConfig = Session::get('sessionLogConfig'); - if($user !== null && str_starts_with(basename($logConfig['path']), '_')) { + if($user !== null && str_starts_with(basename($logConfig['path']), '__UNKNOWN')) { $oldPath = $logConfig['path']; - $username = $user->last_known_username; - static::cleanupUsername($username); - $newPath = storage_path('logs/sessions/'.$username.basename($oldPath)); + $newPath = static::getSessionLogPath($user); + + if(!file_exists(dirname($newPath))) + mkdir(dirname($newPath), 0777, true); + rename($oldPath, $newPath); $logConfig['path'] = $newPath; Session::put('sessionLogConfig', $logConfig); @@ -87,6 +88,15 @@ public static function getSessionLogConfig(): LoggerInterface return Log::build($logConfig); } + public static function getSessionLogPath(?User $user): string { + $username = $user?->last_known_username ?? '__UNKNOWN'; + static::cleanupUsername($username); + $userid = $user?->id ?? 'no-id'; + $session = Str::substr(Session::getId(), 0, 12); + + return storage_path("logs/sessions/{$username}_$userid/{$username}_$session.log"); + } + public static function cleanupUsername(string &$username): void { $username = preg_replace('/[^\w\-\.]/', '', $username);