Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only fetch claimants for set requests on set requests page #2038

Merged
merged 2 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
85 changes: 9 additions & 76 deletions app/Helpers/database/set-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 4 additions & 2 deletions public/request/user-game-list/set-requests.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Site\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;

Expand All @@ -11,8 +12,9 @@
$gameID = (int) $input['game'];
$user = $input['user'];

$setRequestList = getUserRequestList($user);
$totalRequests = getUserRequestsInformation($user, $setRequestList, $gameID);
$userModel = User::firstWhere('User', $user);

$totalRequests = getUserRequestsInformation($userModel, $gameID);
$totalRequests['gameRequests'] = getSetRequestCount($gameID);

return response()->json($totalRequests);
32 changes: 30 additions & 2 deletions public/setRequestList.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?php

use App\Community\Enums\ClaimStatus;
use App\Community\Enums\UserGameListType;
use App\Community\Models\UserGameListEntry;
use App\Platform\Models\System;
use App\Site\Models\User;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\DB;

if (!authenticateFromCookie($user, $permissions, $userDetails)) {
abort(401);
Expand Down Expand Up @@ -45,8 +50,31 @@
$totalRequestedGames = getGamesWithRequests($selectedConsoleId, $selectedRequestStatus);
}
} else {
$setRequestList = getUserRequestList($username);
$userSetRequestInformation = getUserRequestsInformation($username, $setRequestList);
$userModel = User::firstWhere('User', $username);
if (!$userModel) {
abort(404);
}
$userSetRequestInformation = getUserRequestsInformation($userModel);

$setRequestList = UserGameListEntry::where('user_id', $userModel->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");
Expand Down
9 changes: 6 additions & 3 deletions public/userInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -39,8 +44,7 @@
$userPage = $userMassData['User'];
$userMotto = $userMassData['Motto'];
$userPageID = $userMassData['ID'];
$setRequestList = getUserRequestList($userPage);
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
$userSetRequestInformation = getUserRequestsInformation($userPage, $setRequestList);
$userSetRequestInformation = getUserRequestsInformation($userPageModel);
$userWallActive = $userMassData['UserWallActive'];
$userIsUntracked = $userMassData['Untracked'];

Expand Down Expand Up @@ -91,7 +95,6 @@

$userAwards = getUsersSiteAwards($userPage);

$userPageModel = User::firstWhere('User', $userPage);
$userScoreData = getAwardedList(
$userPageModel,
0,
Expand Down
Loading