-
-
Notifications
You must be signed in to change notification settings - Fork 121
feat(api): add V2 JSON:API relationship endpoint for PlayerAchievementSet resources #4562
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
Merged
wescopeland
merged 7 commits into
RetroAchievements:master
from
wescopeland:v2-api-player-ach-sets
Feb 23, 2026
+1,080
−16
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a75ead
feat(api): add V2 JSON:API relationship endpoint for PlayerAchievemen…
wescopeland bf66832
chore: pint
wescopeland ea31a1f
fix: address feedback
wescopeland 0649a16
Merge branch 'master' into v2-api-player-ach-sets
wescopeland 1720aae
feat: add achievementSetId to setContext
wescopeland fea46e6
Merge branch 'master' into v2-api-player-ach-sets
wescopeland e1e70c4
Merge branch 'master' into v2-api-player-ach-sets
wescopeland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/Api/V2/PlayerAchievementSets/PlayerAchievementSetResource.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Api\V2\PlayerAchievementSets; | ||
|
|
||
| use App\Api\V2\BaseJsonApiResource; | ||
| use App\Models\PlayerAchievementSet; | ||
| use App\Platform\Enums\AchievementSetType; | ||
| use Illuminate\Http\Request; | ||
| use LaravelJsonApi\Core\Document\Links; | ||
|
|
||
| /** | ||
| * @property PlayerAchievementSet $resource | ||
| */ | ||
| class PlayerAchievementSetResource extends BaseJsonApiResource | ||
| { | ||
| /** | ||
| * Get the resource's attributes. | ||
| * | ||
| * @param Request|null $request | ||
| */ | ||
| public function attributes($request): iterable | ||
| { | ||
| return [ | ||
| 'achievementsUnlocked' => $this->resource->achievements_unlocked, | ||
| 'achievementsUnlockedHardcore' => $this->resource->achievements_unlocked_hardcore, | ||
|
|
||
| 'points' => $this->resource->points, | ||
| 'pointsHardcore' => $this->resource->points_hardcore, | ||
| 'pointsWeighted' => $this->resource->points_weighted, | ||
|
|
||
| 'completionPercentage' => $this->resource->completion_percentage, | ||
| 'completionPercentageHardcore' => $this->resource->completion_percentage_hardcore, | ||
|
|
||
| 'lastUnlockAt' => $this->resource->last_unlock_at, | ||
| 'lastUnlockHardcoreAt' => $this->resource->last_unlock_hardcore_at, | ||
| 'completedAt' => $this->resource->completed_at, | ||
| 'completedHardcoreAt' => $this->resource->completed_hardcore_at, | ||
|
|
||
| 'timeTakenSeconds' => $this->resource->time_taken, | ||
| 'timeTakenHardcoreSeconds' => $this->resource->time_taken_hardcore, | ||
|
|
||
| 'setContext' => $this->getSetContext(), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get the resource's relationships. | ||
| * | ||
| * @param Request|null $request | ||
| */ | ||
| public function relationships($request): iterable | ||
| { | ||
| return [ | ||
| 'achievementSet' => $this->relation('achievementSet')->withoutLinks(), | ||
| 'game' => $this->relation('game')->withoutLinks(), | ||
Jamiras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @param Request|null $request | ||
| */ | ||
| public function links($request): Links | ||
| { | ||
| // Player achievement sets have no dedicated web URL. | ||
| return new Links(); | ||
| } | ||
|
|
||
| /** | ||
| * Get game/type pairs so callers know the game context | ||
| * without needing to include the full game resource. | ||
| * | ||
| * @return array<array{gameId: int, type: string}> | ||
| */ | ||
| private function getSetContext(): array | ||
| { | ||
| $gameAchievementSets = $this->resource->achievementSet->gameAchievementSets; | ||
|
|
||
| $hasCoreAttachment = $gameAchievementSets->contains( | ||
| fn ($gas) => $gas->type === AchievementSetType::Core | ||
| ); | ||
| $hasNonCoreAttachment = $gameAchievementSets->contains( | ||
| fn ($gas) => $gas->type !== AchievementSetType::Core | ||
| ); | ||
|
|
||
| $setsToInclude = ($hasCoreAttachment && $hasNonCoreAttachment) | ||
| ? $gameAchievementSets->filter(fn ($gas) => $gas->type !== AchievementSetType::Core) | ||
| : $gameAchievementSets; | ||
|
|
||
| return $setsToInclude->map(fn ($gas) => [ | ||
| 'achievementSetId' => $gas->achievement_set_id, | ||
| 'gameId' => $gas->game_id, | ||
| 'type' => $gas->type instanceof AchievementSetType ? $gas->type->value : $gas->type, | ||
| ])->values()->all(); | ||
| } | ||
| } | ||
106 changes: 106 additions & 0 deletions
106
app/Api/V2/PlayerAchievementSets/PlayerAchievementSetSchema.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Api\V2\PlayerAchievementSets; | ||
|
|
||
| use App\Models\PlayerAchievementSet; | ||
| use LaravelJsonApi\Eloquent\Contracts\Paginator; | ||
| use LaravelJsonApi\Eloquent\Fields\ArrayList; | ||
| use LaravelJsonApi\Eloquent\Fields\DateTime; | ||
| use LaravelJsonApi\Eloquent\Fields\ID; | ||
| use LaravelJsonApi\Eloquent\Fields\Number; | ||
| use LaravelJsonApi\Eloquent\Fields\Relations\BelongsTo; | ||
| use LaravelJsonApi\Eloquent\Fields\Relations\HasOneThrough; | ||
| use LaravelJsonApi\Eloquent\Filters\Scope; | ||
| use LaravelJsonApi\Eloquent\Filters\Where; | ||
| use LaravelJsonApi\Eloquent\Filters\WhereIdIn; | ||
| use LaravelJsonApi\Eloquent\Pagination\PagePagination; | ||
| use LaravelJsonApi\Eloquent\Schema; | ||
|
|
||
| class PlayerAchievementSetSchema extends Schema | ||
| { | ||
| /** | ||
| * The model the schema corresponds to. | ||
| */ | ||
| public static string $model = PlayerAchievementSet::class; | ||
|
|
||
| /** | ||
| * Relationships that should always be eager loaded. | ||
| */ | ||
| protected array $with = ['achievementSet.gameAchievementSets']; | ||
|
|
||
| /** | ||
| * Default pagination parameters when client doesn't provide any. | ||
| * This prevents unbounded result sets. | ||
| */ | ||
| protected ?array $defaultPagination = ['number' => 1]; | ||
|
|
||
| /** | ||
| * Default sort order when client doesn't provide any. | ||
| * Shows sets with most recently unlocked achievements first. | ||
| */ | ||
| protected $defaultSort = '-lastUnlockAt'; | ||
|
|
||
| /** | ||
| * Get the resource type. | ||
| */ | ||
| public static function type(): string | ||
| { | ||
| return 'player-achievement-sets'; | ||
| } | ||
|
|
||
| /** | ||
| * Get the resource fields. | ||
| */ | ||
| public function fields(): array | ||
| { | ||
| return [ | ||
| ID::make(), | ||
|
|
||
| Number::make('achievementsUnlocked', 'achievements_unlocked')->readOnly(), | ||
| Number::make('achievementsUnlockedHardcore', 'achievements_unlocked_hardcore')->readOnly(), | ||
|
|
||
| Number::make('points', 'points')->sortable()->readOnly(), | ||
| Number::make('pointsHardcore', 'points_hardcore')->sortable()->readOnly(), | ||
| Number::make('pointsWeighted', 'points_weighted')->sortable()->readOnly(), | ||
|
|
||
| Number::make('completionPercentage', 'completion_percentage')->sortable()->readOnly(), | ||
| Number::make('completionPercentageHardcore', 'completion_percentage_hardcore')->sortable()->readOnly(), | ||
|
|
||
| DateTime::make('lastUnlockAt', 'last_unlock_at')->sortable()->readOnly(), | ||
| DateTime::make('lastUnlockHardcoreAt', 'last_unlock_hardcore_at')->sortable()->readOnly(), | ||
| DateTime::make('completedAt', 'completed_at')->sortable()->readOnly(), | ||
| DateTime::make('completedHardcoreAt', 'completed_hardcore_at')->sortable()->readOnly(), | ||
|
|
||
| Number::make('timeTakenSeconds', 'time_taken')->sortable()->readOnly(), | ||
| Number::make('timeTakenHardcoreSeconds', 'time_taken_hardcore')->sortable()->readOnly(), | ||
|
|
||
| ArrayList::make('setContext')->readOnly(), | ||
|
|
||
| BelongsTo::make('achievementSet')->type('achievement-sets')->readOnly(), | ||
| HasOneThrough::make('game')->type('games'), // HasOneThrough is always implicitly ->readOnly() | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get the resource filters. | ||
| */ | ||
| public function filters(): array | ||
| { | ||
| return [ | ||
| WhereIdIn::make($this), | ||
| Where::make('achievementSetId', 'achievement_set_id'), | ||
| Scope::make('gameId', 'forGameId'), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Get the resource paginator. | ||
| */ | ||
| public function pagination(): ?Paginator | ||
| { | ||
| return PagePagination::make() | ||
| ->withDefaultPerPage(50); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.