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

use aggregate data for reset progress dropdowns #1933

Merged
merged 5 commits into from
Oct 27, 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
64 changes: 0 additions & 64 deletions app/Helpers/database/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,6 @@ function getUserUnlockDates(string $user, int $gameID, ?array &$dataOut): int
return count($dataOut);
}

/**
* @param array<string, mixed>|null $dataOut
*/
function getUserUnlocksDetailed(string $user, int $gameID, ?array &$dataOut): int
{
sanitize_sql_inputs($user);

$query = "SELECT ach.Title, ach.ID, ach.Points, aw.HardcoreMode
FROM Achievements AS ach
LEFT JOIN Awarded AS aw ON ach.ID = aw.AchievementID
WHERE ach.GameID = '$gameID' AND aw.User = '$user'
ORDER BY ach.ID, aw.HardcoreMode ";

$dbResult = s_mysql_query($query);

$dataOut = [];

if ($dbResult !== false) {
while ($data = mysqli_fetch_assoc($dbResult)) {
$dataOut[] = $data;
}
}

return count($dataOut);
}

function validateUsername(string $userIn): ?string
{
$user = User::firstWhere('User', $userIn);
Expand Down Expand Up @@ -211,44 +185,6 @@ function getUserPageInfo(string $user, int $numGames = 0, int $numRecentAchievem
return $libraryOut;
}

function getControlPanelUserInfo(string $user, ?array &$libraryOut): bool
{
sanitize_sql_inputs($user);

$libraryOut = [];
$libraryOut['Played'] = [];
// getUserActivityRange( $user, $firstLogin, $lastLogin );
// $libraryOut['MemberSince'] = $firstLogin;
// $libraryOut['LastLogin'] = $lastLogin;

$query = "SELECT gd.ID, c.Name AS ConsoleName, gd.Title AS GameTitle, COUNT(*) AS NumAwarded, Inner1.NumPossible
FROM Awarded AS aw
LEFT JOIN Achievements AS ach ON ach.ID = aw.AchievementID
LEFT JOIN GameData AS gd ON gd.ID = ach.GameID
LEFT JOIN Console AS c ON c.ID = gd.ConsoleID
LEFT JOIN (
SELECT ach.GameID, COUNT(*) AS NumPossible
FROM Achievements AS ach
WHERE ach.Flags = 3
GROUP BY ach.GameID ) AS Inner1 ON Inner1.GameID = gd.ID
WHERE aw.User = '$user' AND aw.HardcoreMode = 0
GROUP BY gd.ID, gd.ConsoleID, gd.Title
ORDER BY gd.Title, gd.ConsoleID";

$dbResult = s_mysql_query($query);
if (!$dbResult) {
log_sql_fail();

return false;
}

while ($db_entry = mysqli_fetch_assoc($dbResult)) {
$libraryOut['Played'][] = $db_entry;
} // use as raw array to preserve order!

return true;
}

function getUserListByPerms(int $sortBy, int $offset, int $count, ?array &$dataOut, ?string $requestedBy = null, int $perms = Permissions::Unregistered, bool $showUntracked = false): int
{
$whereQuery = null;
Expand Down
24 changes: 21 additions & 3 deletions public/request/user/list-games.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
<?php

use App\Site\Models\User;

if (!authenticateFromCookie($user, $permissions, $userDetails)) {
abort(401);
}

if (getControlPanelUserInfo($user, $userData)) {
return response()->json($userData['Played']);
$userModel = User::firstWhere('User', $user);
$games = $userModel
->games()
->with('system')
->where('player_games.achievements_unlocked', '>', 0)
->orderBy('Title')
->select(['GameData.ID', 'Title', 'ConsoleID', 'achievements_published', 'player_games.achievements_unlocked'])
->get();

$dataOut = [];
foreach ($games as $game) {
$dataOut[] = [
'ID' => $game->ID,
'GameTitle' => $game->Title,
'ConsoleName' => $game->system->Name,
'NumAwarded' => $game->achievements_unlocked,
'NumPossible' => $game->achievements_published,
];
}

abort(400);
return $dataOut;
luchaos marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 16 additions & 19 deletions public/request/user/list-unlocks.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

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

if (!authenticateFromCookie($user, $permissions, $userDetails, Permissions::Unregistered)) {
Expand All @@ -13,23 +13,20 @@
'game' => 'required|integer',
]);

getUserUnlocksDetailed($user, $input['game'], $dataOut);

$hardcoreUnlocks = (new Collection($dataOut))
->filter(fn ($achievement) => (bool) $achievement['HardcoreMode'])
->keyBy('ID');

$dataOut = (new Collection($dataOut))
// results in unique IDs
->keyBy('ID')
->filter(fn ($achievement) => !$achievement['HardcoreMode'])
// merge on top to make sure hardcore unlocks take precedence
->merge($hardcoreUnlocks)
->map(function ($achievement) {
$achievement['HardcoreMode'] = (int) $achievement['HardcoreMode'];

return $achievement;
})
->values();
$achievementsUnlocked = User::firstWhere('User', $user)
->achievements()->where('GameID', $input['game'])
->withPivot(['unlocked_at', 'unlocked_hardcore_at'])
->orderBy('Title')
->get();

$dataOut = [];
foreach ($achievementsUnlocked as $achievementUnlocked) {
$dataOut[] = [
'ID' => $achievementUnlocked->ID,
'Title' => $achievementUnlocked->Title,
'Points' => $achievementUnlocked->Points,
'HardcoreMode' => $achievementUnlocked->pivot->unlocked_hardcore_at ? 1 : 0,
];
}

return response()->json($dataOut);
Loading