diff --git a/app/Platform/Actions/GetUserProgressionStatusCountsAction.php b/app/Platform/Actions/GetUserProgressionStatusCountsAction.php index e3aea11671..deded2dea9 100644 --- a/app/Platform/Actions/GetUserProgressionStatusCountsAction.php +++ b/app/Platform/Actions/GetUserProgressionStatusCountsAction.php @@ -18,14 +18,14 @@ class GetUserProgressionStatusCountsAction /** * @return array{ * avgCompletionPercentage: float, - * systemProgress: array, + * systemProgress: array, * topSystemId: int|null, * totalCounts: array{unfinished: int, beatenSoftcore: int, beatenHardcore: int, completed: int, mastered: int}, * totalHardcoreAchievements: int, * totalSoftcoreAchievements: int, * } */ - public function execute(User $user, ?int $recentSystemId = null): array + public function execute(User $user): array { // Subsets are identified by their core achievement set being linked to another game as a non-core type. $subsetExistsSubquery = "EXISTS ( @@ -47,6 +47,7 @@ public function execute(User $user, ?int $recentSystemId = null): array $results = $this->buildQualifyingGamesQuery($user) ->selectRaw(" games.system_id, + MAX(player_games.last_played_at) as last_played_at, SUM(player_games.achievements_unlocked_hardcore) as total_hc_achievements, SUM(GREATEST(0, CAST(player_games.achievements_unlocked AS SIGNED) - CAST(player_games.achievements_unlocked_hardcore AS SIGNED))) as total_sc_achievements, SUM(mastery_hc.id IS NOT NULL) as mastered_count, @@ -100,6 +101,7 @@ public function execute(User $user, ?int $recentSystemId = null): array $beatenHc = (int) $row->beaten_hc_count; $beatenSc = (int) $row->beaten_sc_count; $unfinished = (int) $row->unfinished_count; + $lastPlayedAt = $row->last_played_at?->format('Y-m-d H:i:s'); $systemProgress[$systemId] = [ 'unfinishedCount' => $unfinished, @@ -107,6 +109,7 @@ public function execute(User $user, ?int $recentSystemId = null): array 'beatenHardcoreCount' => $beatenHc, 'completedCount' => $completed, 'masteredCount' => $mastered, + 'lastPlayedAt' => $lastPlayedAt, 'systemId' => $systemId, ]; @@ -120,16 +123,29 @@ public function execute(User $user, ?int $recentSystemId = null): array $totalSoftcoreAchievements += (int) $row->total_sc_achievements; } + $avgCompletionPercentage = $this->calculateAvgCompletionExcludingSubsets($user); + + // topSystem is the most recent valid system where the user has earned achievements. + $topSystemId = null; + $latestTimestamp = null; + + foreach ($systemProgress as $systemId => $progress) { + if ($progress['lastPlayedAt'] === null) { + continue; + } + + $timestamp = strtotime($progress['lastPlayedAt']) ?: 0; + + if ($latestTimestamp === null || $timestamp > $latestTimestamp) { + $latestTimestamp = $timestamp; + $topSystemId = $systemId; + } + } + // Add counts for "orphan badges" - badges for games not in the main query. // This handles demoted games, games with < 6 achievements, etc. $this->addOrphanBadgeCounts($user, $systemProgress, $totalCounts); - $avgCompletionPercentage = $this->calculateAvgCompletionExcludingSubsets($user); - - $topSystemId = ($recentSystemId !== null && isset($systemProgress[$recentSystemId])) - ? $recentSystemId - : array_key_first($systemProgress); - // Sort: topSystem first, then by total games played descending. uasort($systemProgress, function ($a, $b) use ($topSystemId) { if ($a['systemId'] === $topSystemId) { @@ -159,7 +175,7 @@ public function execute(User $user, ?int $recentSystemId = null): array * Finds badges for games not included in the main query and adds them to the counts. * This handles edge cases like demoted games, etc. * - * @param array $systemProgress + * @param array $systemProgress * @param array{unfinished: int, beatenSoftcore: int, beatenHardcore: int, completed: int, mastered: int} $totalCounts */ private function addOrphanBadgeCounts(User $user, array &$systemProgress, array &$totalCounts): void @@ -223,6 +239,7 @@ private function addOrphanBadgeCounts(User $user, array &$systemProgress, array 'beatenHardcoreCount' => 0, 'completedCount' => 0, 'masteredCount' => 0, + 'lastPlayedAt' => null, 'systemId' => $systemId, ]; } diff --git a/resources/views/pages-legacy/userInfo.blade.php b/resources/views/pages-legacy/userInfo.blade.php index 5b36543617..57da656885 100644 --- a/resources/views/pages-legacy/userInfo.blade.php +++ b/resources/views/pages-legacy/userInfo.blade.php @@ -43,15 +43,8 @@ $userWallActive = $userMassData['UserWallActive']; $userIsUntracked = $userMassData['Untracked']; -$recentlyPlayedSystemId = collect($userMassData['RecentlyPlayed'] ?? []) - ->filter(fn ($game) => System::isGameSystem($game['ConsoleID'] ?? 0) && ($game['AchievementsTotal'] ?? 0) > 0) - ->sortByDesc('LastPlayed') - ->pluck('ConsoleID') - ->first(); - $progressionCounts = (new GetUserProgressionStatusCountsAction())->execute( - $userPageModel, - $recentlyPlayedSystemId + $userPageModel ); $averageCompletionPercentage = sprintf("%01.2f", $progressionCounts['avgCompletionPercentage']);