Skip to content

Commit

Permalink
also truncate locked achievements when asking for a specific count (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Jun 30, 2023
1 parent ad7521e commit 6b7f367
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 68 deletions.
42 changes: 24 additions & 18 deletions app/Helpers/database/player-game.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ function getUserProgress(string $user, array $gameIDs, int $numRecentAchievement
$libraryOut['GameInfo'] = $gameInfo;
}

// magic numbers!
// -1 = don't populate RecentAchievements field
// 0 = return all achievements for each game, with the unlocked achievements first ordered by unlock date
// >0 = return the N most recent unlocks across all games queried, grouped by game
if ($numRecentAchievements >= 0) {
usort($unlockedAchievements, function ($a, $b) {
if ($a['When'] == $b['When']) {
Expand Down Expand Up @@ -222,24 +226,26 @@ function getUserProgress(string $user, array $gameIDs, int $numRecentAchievement
];
}

foreach ($lockedAchievements as $lockedAchievement) {
$gameData = $lockedAchievement['Game'];
$gameID = (int) $gameData['ID'];
$achievementData = $lockedAchievement['Achievement'];
$achievementID = (int) $achievementData['ID'];

$recentAchievements[$gameID][$achievementID] = [
'ID' => $achievementID,
'GameID' => $gameID,
'GameTitle' => $gameData['Title'],
'Title' => $achievementData['Title'],
'Description' => $achievementData['Description'],
'Points' => (int) $achievementData['Points'],
'BadgeName' => $achievementData['BadgeName'],
'IsAwarded' => '0',
'DateAwarded' => null,
'HardcoreAchieved' => null,
];
if ($numRecentAchievements === 0) {
foreach ($lockedAchievements as $lockedAchievement) {
$gameData = $lockedAchievement['Game'];
$gameID = (int) $gameData['ID'];
$achievementData = $lockedAchievement['Achievement'];
$achievementID = (int) $achievementData['ID'];

$recentAchievements[$gameID][$achievementID] = [
'ID' => $achievementID,
'GameID' => $gameID,
'GameTitle' => $gameData['Title'],
'Title' => $achievementData['Title'],
'Description' => $achievementData['Description'],
'Points' => (int) $achievementData['Points'],
'BadgeName' => $achievementData['BadgeName'],
'IsAwarded' => '0',
'DateAwarded' => null,
'HardcoreAchieved' => null,
];
}
}

$libraryOut['RecentAchievements'] = $recentAchievements;
Expand Down
2 changes: 1 addition & 1 deletion app/Helpers/database/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function getUserPageInfo(string $user, int $numGames = 0, int $numRecentAchievem
$gameIDs[] = $recentlyPlayed['GameID'];
}

if (!in_array($userInfo['LastGameID'], $gameIDs)) {
if ($userInfo['LastGameID'] && !in_array($userInfo['LastGameID'], $gameIDs)) {
$gameIDs[] = $userInfo['LastGameID'];
}

Expand Down
2 changes: 1 addition & 1 deletion public/API/API_GetUserSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
* string GameTitle name of the game
* string IsAwarded always "1"
* datetime DateAwarded when the user earned the achievement
* int? HardcoreAchieved 1 for hardcore award, 0 for non-hardcore award, null if not achieved
* int? HardcoreAchieved 1 for hardcore award, 0 for non-hardcore award
* int ContribCount achievements won by others
* int ContribYield points awarded to others
*/
Expand Down
168 changes: 120 additions & 48 deletions tests/Feature/Api/V1/UserSummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
use App\Site\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Tests\Feature\Platform\TestsPlayerAchievements;
use Tests\TestCase;

class UserSummaryTest extends TestCase
{
use RefreshDatabase;
use BootstrapsApiV1;
use TestsPlayerAchievements;

public function testItValidates(): void
{
Expand Down Expand Up @@ -242,30 +244,6 @@ public function testGetUserSummary(): void
'DateAwarded' => $unlock->Date->__toString(),
'HardcoreAchieved' => '1',
],
$publishedAchievements->get(1)->ID => [
'ID' => $publishedAchievements->get(1)->ID,
'Title' => $publishedAchievements->get(1)->Title,
'Description' => $publishedAchievements->get(1)->Description,
'Points' => $publishedAchievements->get(1)->Points,
'BadgeName' => $publishedAchievements->get(1)->BadgeName,
'GameID' => $game->ID,
'GameTitle' => $game->Title,
'IsAwarded' => '0',
'DateAwarded' => null,
'HardcoreAchieved' => null,
],
$publishedAchievements->get(2)->ID => [
'ID' => $publishedAchievements->get(2)->ID,
'Title' => $publishedAchievements->get(2)->Title,
'Description' => $publishedAchievements->get(2)->Description,
'Points' => $publishedAchievements->get(2)->Points,
'BadgeName' => $publishedAchievements->get(2)->BadgeName,
'GameID' => $game->ID,
'GameTitle' => $game->Title,
'IsAwarded' => '0',
'DateAwarded' => null,
'HardcoreAchieved' => null,
],
],
],
]);
Expand Down Expand Up @@ -354,32 +332,126 @@ public function testGetUserSummary(): void
'DateAwarded' => $unlock->Date->__toString(),
'HardcoreAchieved' => '1',
],
$publishedAchievements->get(1)->ID => [
'ID' => $publishedAchievements->get(1)->ID,
'Title' => $publishedAchievements->get(1)->Title,
'Description' => $publishedAchievements->get(1)->Description,
'Points' => $publishedAchievements->get(1)->Points,
'BadgeName' => $publishedAchievements->get(1)->BadgeName,
'GameID' => $game->ID,
'GameTitle' => $game->Title,
'IsAwarded' => '0',
'DateAwarded' => null,
'HardcoreAchieved' => null,
],
$publishedAchievements->get(2)->ID => [
'ID' => $publishedAchievements->get(2)->ID,
'Title' => $publishedAchievements->get(2)->Title,
'Description' => $publishedAchievements->get(2)->Description,
'Points' => $publishedAchievements->get(2)->Points,
'BadgeName' => $publishedAchievements->get(2)->BadgeName,
'GameID' => $game->ID,
'GameTitle' => $game->Title,
'IsAwarded' => '0',
'DateAwarded' => null,
'HardcoreAchieved' => null,
],
],
],
]);
}

public function testGetUserSummaryLimitRecentAchievements(): void
{
/** @var System $system */
$system = System::factory()->create();
/** @var Game $game */
$game = Game::factory()->create(['ConsoleID' => $system->ID]);
$publishedAchievements = Achievement::factory()->published()->count(7)->create(['GameID' => $game->ID]);

/** @var Game $game2 */
$game2 = Game::factory()->create(['ConsoleID' => $system->ID]);
$publishedAchievements2 = Achievement::factory()->published()->count(4)->create(['GameID' => $game2->ID]);

$now = Carbon::now();

$activity = new UserActivityLegacy([
'User' => $this->user->User,
'timestamp' => $now->clone()->subMinutes(35),
'lastupdate' => $now->clone()->subMinutes(1),
'activitytype' => ActivityType::StartedPlaying,
'data' => $game->ID,
]);
$activity->save();
$activity2 = new UserActivityLegacy([
'User' => $this->user->User,
'timestamp' => $now->clone()->subMinutes(100),
'lastupdate' => $now->clone()->subMinutes(75),
'activitytype' => ActivityType::StartedPlaying,
'data' => $game2->ID,
]);
$activity2->save();

$this->addHardcoreUnlock($this->user, $publishedAchievements->get(1), $now->clone()->subMinutes(3));
$this->addSoftcoreUnlock($this->user, $publishedAchievements->get(4), $now->clone()->subMinutes(6));
$this->addHardcoreUnlock($this->user, $publishedAchievements->get(6), $now->clone()->subMinutes(10));
$this->addHardcoreUnlock($this->user, $publishedAchievements->get(0), $now->clone()->subMinutes(20));
$this->addHardcoreUnlock($this->user, $publishedAchievements->get(2), $now->clone()->subMinutes(30));

$this->addHardcoreUnlock($this->user, $publishedAchievements2->get(2), $now->clone()->subMinutes(90));

$this->get($this->apiUrl('GetUserSummary', ['u' => $this->user->User, 'a' => 2]))
->assertSuccessful()
->assertJson([
'ID' => $this->user->ID,
'Awarded' => [
$game->ID => [
'NumPossibleAchievements' => 7,
'NumAchieved' => 5,
'NumAchievedHardcore' => 4,
],
$game2->ID => [
'NumPossibleAchievements' => 4,
'NumAchieved' => 1,
'NumAchievedHardcore' => 1,
],
],
'RecentAchievements' => [
$game->ID => [
$publishedAchievements->get(1)->ID => [
'HardcoreAchieved' => 1,
],
$publishedAchievements->get(4)->ID => [
'HardcoreAchieved' => 0,
],
],
],
])
// only two recent achievements were requested - both should be in game 1
// and nothing should be returned for game 2
->assertJsonCount(1, "RecentAchievements")
->assertJsonCount(2, "RecentAchievements.{$game->ID}");

// user only has 6 unlocks, so return all of them, and nothing more
$this->get($this->apiUrl('GetUserSummary', ['u' => $this->user->User, 'a' => 7]))
->assertSuccessful()
->assertJson([
'ID' => $this->user->ID,
'Awarded' => [
$game->ID => [
'NumPossibleAchievements' => 7,
'NumAchieved' => 5,
'NumAchievedHardcore' => 4,
],
$game2->ID => [
'NumPossibleAchievements' => 4,
'NumAchieved' => 1,
'NumAchievedHardcore' => 1,
],
],
'RecentAchievements' => [
$game->ID => [
$publishedAchievements->get(1)->ID => [
'HardcoreAchieved' => 1,
],
$publishedAchievements->get(4)->ID => [
'HardcoreAchieved' => 0,
],
$publishedAchievements->get(6)->ID => [
'HardcoreAchieved' => 1,
],
$publishedAchievements->get(0)->ID => [
'HardcoreAchieved' => 1,
],
$publishedAchievements->get(2)->ID => [
'HardcoreAchieved' => 1,
],
],
$game2->ID => [
$publishedAchievements2->get(2)->ID => [
'HardcoreAchieved' => 1,
],
],
],
])
->assertJsonCount(2, "RecentAchievements")
->assertJsonCount(5, "RecentAchievements.{$game->ID}")
->assertJsonCount(1, "RecentAchievements.{$game2->ID}");
}
}

0 comments on commit 6b7f367

Please sign in to comment.