From e969597f6d17633691d03bb53022d168af3877dd Mon Sep 17 00:00:00 2001 From: Jamiras Date: Sat, 18 Nov 2023 08:06:42 -0700 Subject: [PATCH] only fetch claimants for set requests on set requests page --- app/Helpers/database/set-request.php | 85 ++----------------- .../request/user-game-list/set-requests.php | 6 +- public/setRequestList.php | 32 ++++++- public/userInfo.php | 9 +- 4 files changed, 49 insertions(+), 83 deletions(-) diff --git a/app/Helpers/database/set-request.php b/app/Helpers/database/set-request.php index 5145884f24..9e140c2481 100644 --- a/app/Helpers/database/set-request.php +++ b/app/Helpers/database/set-request.php @@ -4,99 +4,32 @@ use App\Community\Enums\RequestStatus; use App\Community\Enums\UserGameListType; use App\Community\Models\UserGameListEntry; -use App\Platform\Enums\AchievementFlag; use App\Site\Models\User; -/** - * Gets a list of set requests made by a given user. - */ -function getUserRequestList(?string $user = null): array -{ - sanitize_sql_inputs($user); - - $retVal = []; - - $query = " - SELECT - sr.GameID as GameID, - gd.Title as GameTitle, - gd.ImageIcon as GameIcon, - c.name as ConsoleName, - GROUP_CONCAT(DISTINCT(IF(sc.Status IN (" . ClaimStatus::Active . ',' . ClaimStatus::InReview . "), sc.User, NULL))) AS Claims - FROM - SetRequest sr - LEFT JOIN - SetClaim sc ON (sr.GameID = sc.GameID) - LEFT JOIN - GameData gd ON (sr.GameID = gd.ID) - LEFT JOIN - Console c ON (gd.ConsoleID = c.ID) - WHERE - sr.user = '$user' AND sr.type='" . UserGameListType::AchievementSetRequest . "' - GROUP BY - sr.GameID - ORDER BY " . ifStatement("gd.Title LIKE '~%'", 1, 0) . ", gd.Title"; - - $dbResult = s_mysql_query($query); - - if ($dbResult !== false) { - $gameIDs = []; - while ($nextData = mysqli_fetch_assoc($dbResult)) { - $gameIDs[] = $nextData['GameID']; - $nextData['AchievementCount'] = 0; - $retVal[] = $nextData; - } - - if (!empty($gameIDs)) { - $query = "SELECT GameID, COUNT(ID) AS AchievementCount FROM Achievements" - . " WHERE GameID IN (" . implode(',', $gameIDs) . ")" - . " AND Flags = " . AchievementFlag::OfficialCore - . " GROUP BY GameID"; - - $dbResult = s_mysql_query($query); - - if ($dbResult !== false) { - while ($nextData = mysqli_fetch_assoc($dbResult)) { - foreach ($retVal as &$game) { - if ($game['GameID'] == $nextData['GameID']) { - $game['AchievementCount'] = $nextData['AchievementCount']; - break; - } - } - } - } else { - log_sql_fail(); - } - } - } else { - log_sql_fail(); - } - - return $retVal; -} - /** * Gets the total and remaining set requests left for the given user. */ -function getUserRequestsInformation(string $user, array $list, int $gameID = -1): array +function getUserRequestsInformation(User $user, int $gameID = -1): array { - /** @var User $userModel */ - $userModel = User::firstWhere('User', $user); - $requests = UserGameListEntry::getUserSetRequestsInformation($userModel); + $requests = UserGameListEntry::getUserSetRequestsInformation($user); $requests['used'] = 0; $requests['requestedThisGame'] = 0; // Determine how many of the users current requests are still valid. // Requests made for games that since received achievements do not count towards a used request - foreach ($list as $request) { + $setRequests = UserGameListEntry::where('user_id', $user->ID) + ->where('type', UserGameListType::AchievementSetRequest) + ->join('GameData', 'GameData.ID', '=', 'GameId') + ->select(['GameData.ID', 'GameData.achievements_published']); + foreach ($setRequests->get() as $request) { // If the game does not have achievements then it counts as a legit request - if ($request['AchievementCount'] == 0) { + if ($request['achievements_published'] == 0) { $requests['used']++; } // Determine if we have made a request for the input game - if ($request['GameID'] == $gameID) { + if ($request['ID'] == $gameID) { $requests['requestedThisGame'] = 1; } } diff --git a/public/request/user-game-list/set-requests.php b/public/request/user-game-list/set-requests.php index 4c2f75727a..09a26ac886 100644 --- a/public/request/user-game-list/set-requests.php +++ b/public/request/user-game-list/set-requests.php @@ -1,5 +1,6 @@ json($totalRequests); diff --git a/public/setRequestList.php b/public/setRequestList.php index eb31e00483..b1bf6110e6 100644 --- a/public/setRequestList.php +++ b/public/setRequestList.php @@ -1,7 +1,12 @@ ID) + ->where('type', UserGameListType::AchievementSetRequest) + ->join('GameData', 'GameData.ID', '=', 'GameId') + ->join('Console', 'Console.ID', '=', 'GameData.ConsoleID') + ->leftJoin('SetClaim', function ($join) { + $join->on('SetClaim.GameID', '=', 'GameData.ID') + ->whereIn('SetClaim.Status', [ClaimStatus::Active, ClaimStatus::InReview]); + }) + ->select([ + 'GameData.ID AS GameID', + 'GameData.Title AS GameTitle', + 'GameData.ImageIcon AS GameIcon', + 'Console.Name AS ConsoleName', + 'GameData.achievements_published AS AchievementCount', + DB::raw('GROUP_CONCAT(DISTINCT(SetClaim.User)) AS Claims'), + ]) + ->groupBy('GameData.ID') + ->orderBy(DB::raw(ifStatement("GameData.Title LIKE '~%'", 1, 0) . ", GameData.Title")) + ->get(); } RenderContentStart("Set Requests"); diff --git a/public/userInfo.php b/public/userInfo.php index b558e71890..2023b1dcf2 100644 --- a/public/userInfo.php +++ b/public/userInfo.php @@ -27,6 +27,11 @@ abort(400); } +$userPageModel = User::firstWhere('User', $userPage); +if (!$userPageModel) { + abort(404); +} + $userMassData = getUserPageInfo($userPage, numGames: $maxNumGamesToFetch); if (empty($userMassData)) { abort(404); @@ -39,8 +44,7 @@ $userPage = $userMassData['User']; $userMotto = $userMassData['Motto']; $userPageID = $userMassData['ID']; -$setRequestList = getUserRequestList($userPage); -$userSetRequestInformation = getUserRequestsInformation($userPage, $setRequestList); +$userSetRequestInformation = getUserRequestsInformation($userPageModel); $userWallActive = $userMassData['UserWallActive']; $userIsUntracked = $userMassData['Untracked']; @@ -91,7 +95,6 @@ $userAwards = getUsersSiteAwards($userPage); -$userPageModel = User::firstWhere('User', $userPage); $userScoreData = getAwardedList( $userPageModel, 0,