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

fix(connect): remediate allprogress endpoint exception #3062

Merged
merged 6 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 26 additions & 12 deletions app/Helpers/database/player-game.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\User;
use App\Platform\Enums\AchievementFlag;
use App\Platform\Services\GameTopAchieversService;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;

function getGameRankAndScore(int $gameID, User $user): array
Expand Down Expand Up @@ -276,20 +277,33 @@ function reactivateUserEventAchievements(User $user, array $userUnlocks): array

function GetAllUserProgress(User $user, int $consoleID): array
{
$retVal = [];

$query = "SELECT gd.ID, gd.achievements_published AS NumAch,
COALESCE(pg.achievements_unlocked, 0) AS Earned,
COALESCE(pg.achievements_unlocked_hardcore, 0) AS HCEarned
FROM GameData AS gd
LEFT JOIN player_games pg ON pg.game_id = gd.ID AND pg.user_id={$user->id}
WHERE gd.achievements_published > 0 AND gd.ConsoleID = $consoleID";
/** @var Collection<int, PlayerGame> $playerGames */
$playerGames = $user->playerGames()
->whereIn('game_id', function ($query) use ($consoleID) {
$query->select('ID')
->from('GameData')
->where('ConsoleID', $consoleID)
->where('achievements_published', '>', 0);
})
->get()
->keyBy('game_id');

foreach (legacyDbFetchAll($query) as $row) {
$id = $row['ID'];
unset($row['ID']);
/** @var Collection<int, Game> $games */
$games = Game::where('ConsoleID', $consoleID)
->where('achievements_published', '>', 0)
->get();
Jamiras marked this conversation as resolved.
Show resolved Hide resolved

$retVal[$id] = $row;
/** @var array<int, array{NumAch: int, Earned: int, HCEarned: int}> $retVal */
$retVal = [];
foreach ($games as $game) {
/** @var ?PlayerGame $playerGame */
$playerGame = $playerGames->get($game->id);

$retVal[$game->id] = [
'NumAch' => $game->achievements_published,
'Earned' => $playerGame?->achievements_unlocked ?? 0,
'HCEarned' => $playerGame?->achievements_unlocked_hardcore ?? 0,
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
];
}

return $retVal;
Expand Down
2 changes: 1 addition & 1 deletion public/dorequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function DoRequestError(string $error, ?int $status = 200, ?string $code = null)
*/
case "allprogress":
$consoleID = (int) request()->input('c');
$response['Response'] = GetAllUserProgress($username, $consoleID);
$response['Response'] = GetAllUserProgress($user, $consoleID);
break;

case "badgeiter":
Expand Down
67 changes: 67 additions & 0 deletions tests/Feature/Connect/AllProgressTest.php
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Connect;

use App\Models\Game;
use App\Models\PlayerGame;
use App\Models\System;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AllProgressTest extends TestCase
{
use BootstrapsConnect;
use RefreshDatabase;

public function testItReturnsUserProgress(): void
{
$system = System::factory()->create();
$game = Game::factory()->create([
'ConsoleID' => $system->id,
'achievements_published' => 12,
]);

PlayerGame::create([
'user_id' => $this->user->id,
'game_id' => $game->id,
'achievements_unlocked_hardcore' => 6,
'achievements_unlocked' => 10,
]);

$this->get($this->apiUrl('allprogress', ['c' => $system->id]))
->assertExactJson([
'Success' => true,
'Response' => [
$game->id => [
'NumAch' => $game->achievements_published,
'Earned' => 10,
'HCEarned' => 6,
],
],
]);
}

public function testItIgnoresGamesWithNoPublishedAchievements(): void
{
$system = System::factory()->create();
$game = Game::factory()->create([
'ConsoleID' => $system->id,
'achievements_published' => 0, // !! set was demoted
]);

PlayerGame::create([
'user_id' => $this->user->id,
'game_id' => $game->id,
'achievements_unlocked_hardcore' => 6,
'achievements_unlocked' => 10,
]);

$this->get($this->apiUrl('allprogress', ['c' => $system->id]))
->assertExactJson([
'Success' => true,
'Response' => [],
]);
}
}