Skip to content
Open
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
31 changes: 18 additions & 13 deletions app/Platform/Actions/UpdateGamePlayerCountAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,26 @@ public function execute(Game $game): void
}
}

$playersQuery = PlayerGame::whereIn('game_id', $gameIds)
->leftJoin('unranked_users', 'player_games.user_id', '=', 'unranked_users.user_id')
->whereNull('unranked_users.id');

if (count($gameIds) > 1) {
$playersQuery->distinct('player_games.user_id');
}
// multi-gameId path means parent + bonus subset; the same user can appear in both,
// so we COUNT(DISTINCT user_id) there instead of SUM-ing rows directly
$isMultiGame = count($gameIds) > 1;
$countExpr = fn (string $column): string => $isMultiGame
? "COUNT(DISTINCT CASE WHEN player_games.{$column} > 0 THEN player_games.user_id END)"
: "SUM(CASE WHEN player_games.{$column} > 0 THEN 1 ELSE 0 END)";

$game->players_total = $playersQuery->clone()
->where('achievements_unlocked', '>', 0)
->count();
$row = PlayerGame::query()
->whereIn('player_games.game_id', $gameIds)
->leftJoin('unranked_users', 'player_games.user_id', '=', 'unranked_users.user_id')
->whereNull('unranked_users.id')
->selectRaw(sprintf(
'%s AS total, %s AS hardcore',
$countExpr('achievements_unlocked'),
$countExpr('achievements_unlocked_hardcore'),
))
->first();

$game->players_hardcore = $playersQuery->clone()
->where('achievements_unlocked_hardcore', '>', 0)
->count();
$game->players_total = (int) ($row->total ?? 0);
$game->players_hardcore = (int) ($row->hardcore ?? 0);

if ($game->isDirty()) {
$game->saveQuietly();
Expand Down
Loading