Skip to content

Commit

Permalink
Adjusted Matchmaking now also removing games the user is the creator …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
Vari7921 committed Sep 6, 2024
1 parent ef2f8f5 commit 00ae7e1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
4 changes: 2 additions & 2 deletions dist/app/Console/Commands/MatchmakingCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
36 changes: 35 additions & 1 deletion dist/app/Console/Commands/ProcessMatchmaking.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -27,6 +29,8 @@ class ProcessMatchmaking extends Command
*/
protected $description = 'Command description';

protected static LoggerInterface $log;

/**
* Execute the console command.
*/
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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();

Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 17 additions & 7 deletions dist/app/Http/Middleware/AccessLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Middleware;

use App\Models\User\User;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 00ae7e1

Please sign in to comment.