Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions app/Platform/Actions/GetUserProgressionStatusCountsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ class GetUserProgressionStatusCountsAction
/**
* @return array{
* avgCompletionPercentage: float,
* systemProgress: array<int, array{unfinishedCount: int, beatenSoftcoreCount: int, beatenHardcoreCount: int, completedCount: int, masteredCount: int, systemId: int}>,
* systemProgress: array<int, array{unfinishedCount: int, beatenSoftcoreCount: int, beatenHardcoreCount: int, completedCount: int, masteredCount: int, lastPlayedAt: string|null, systemId: int}>,
* 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 (
Expand All @@ -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,
Expand Down Expand Up @@ -100,13 +101,15 @@ 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,
'beatenSoftcoreCount' => $beatenSc,
'beatenHardcoreCount' => $beatenHc,
'completedCount' => $completed,
'masteredCount' => $mastered,
'lastPlayedAt' => $lastPlayedAt,
'systemId' => $systemId,
];

Expand All @@ -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) {
Expand Down Expand Up @@ -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<int, array{unfinishedCount: int, beatenSoftcoreCount: int, beatenHardcoreCount: int, completedCount: int, masteredCount: int, systemId: int}> $systemProgress
* @param array<int, array{unfinishedCount: int, beatenSoftcoreCount: int, beatenHardcoreCount: int, completedCount: int, masteredCount: int, lastPlayedAt: string|null, systemId: int}> $systemProgress
* @param array{unfinished: int, beatenSoftcore: int, beatenHardcore: int, completed: int, mastered: int} $totalCounts
*/
private function addOrphanBadgeCounts(User $user, array &$systemProgress, array &$totalCounts): void
Expand Down Expand Up @@ -223,6 +239,7 @@ private function addOrphanBadgeCounts(User $user, array &$systemProgress, array
'beatenHardcoreCount' => 0,
'completedCount' => 0,
'masteredCount' => 0,
'lastPlayedAt' => null,
'systemId' => $systemId,
];
}
Expand Down
9 changes: 1 addition & 8 deletions resources/views/pages-legacy/userInfo.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
Loading