Skip to content

Commit

Permalink
fix: address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland committed Jan 17, 2025
1 parent 0d5e72d commit 545d663
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
32 changes: 13 additions & 19 deletions app/Helpers/database/player-game.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,32 +277,26 @@ function reactivateUserEventAchievements(User $user, array $userUnlocks): array

function GetAllUserProgress(User $user, int $consoleID): array
{
/** @var Collection<int, PlayerGame> $playerGames */
/** @var Collection<int, Game> $games */
$playerGames = $user->playerGames()

Check failure on line 281 in app/Helpers/database/player-game.php

View workflow job for this annotation

GitHub Actions / PHP Checks (analyse)

Variable $games in PHPDoc tag @var does not match assigned variable $playerGames.
->whereIn('game_id', function ($query) use ($consoleID) {
$query->select('ID')
->from('GameData')
->where('ConsoleID', $consoleID)
->where('achievements_published', '>', 0);
->whereHas('game', function ($query) use ($consoleID) {
$query->where('ConsoleID', $consoleID)
->where('achievements_published', '>', 0);
})
->get()
->keyBy('game_id');

/** @var Collection<int, Game> $games */
$games = Game::where('ConsoleID', $consoleID)
->where('achievements_published', '>', 0)
->where('achievements_unlocked', '>', 0)
->with(['game' => function ($query) {
$query->select('ID', 'achievements_published');
}])
->get();

/** @var array<int, array{NumAch: int, Earned: int, HCEarned: int}> $retVal */
$retVal = [];
foreach ($games as $game) {
foreach ($playerGames as $playerGame) {
/** @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,
$retVal[$playerGame->game_id] = [
'Achievements' => $playerGame->game->achievements_published,
'Unlocked' => $playerGame->achievements_unlocked,
'UnlockedHardcore' => $playerGame->achievements_unlocked_hardcore,
];
}

Expand Down
52 changes: 49 additions & 3 deletions tests/Feature/Connect/AllProgressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function testItReturnsUserProgress(): void
'Success' => true,
'Response' => [
$game->id => [
'NumAch' => $game->achievements_published,
'Earned' => 10,
'HCEarned' => 6,
'Achievements' => $game->achievements_published,
'Unlocked' => 10,
'UnlockedHardcore' => 6,
],
],
]);
Expand All @@ -64,4 +64,50 @@ public function testItIgnoresGamesWithNoPublishedAchievements(): void
'Response' => [],
]);
}

public function testItIgnoresGamesWithNoProgress(): 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' => 0,
'achievements_unlocked' => 0,
]);

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

public function testItIgnoresGamesFromOtherConsoles(): void
{
$system = System::factory()->create();
$otherSystem = System::factory()->create();

$game = Game::factory()->create([
'ConsoleID' => $otherSystem->id, // !! different console
'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' => [],
]);
}
}

0 comments on commit 545d663

Please sign in to comment.