-
-
Notifications
You must be signed in to change notification settings - Fork 130
feat(api): add awardKind filter and multi-game filtering to player-games #5042
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 6 commits into
RetroAchievements:master
from
wescopeland:v2-player-game-award-kind-filtering
Jul 14, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
81ee7b1
feat(api): add awardKind filter and multi-game filtering to player-games
wescopeland 6414937
test: remove dynamic array key
wescopeland d8b030f
test: add a default value which throws
wescopeland 84cda47
fix: address feedback
wescopeland 78d90ff
chore: rename gameAwards filter
wescopeland 1f117b3
Merge branch 'master' into v2-player-game-award-kind-filtering
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Api\V2\PlayerGames; | ||
|
|
||
| use App\Community\Enums\AwardType; | ||
| use App\Models\PlayerGame; | ||
| use App\Platform\Enums\UnlockMode; | ||
| use Illuminate\Database\Eloquent\Builder; | ||
|
|
||
| /** | ||
| * Beaten milestones a player can reach on a game, matched on the denormalized | ||
| * player_games timestamps rather than PlayerBadge award rows. The values | ||
| * intentionally mirror the user-awards filter[kind] vocabulary. | ||
| * | ||
| * Completion and mastery are deliberately absent: games are beaten, but | ||
| * achievement sets are completed or mastered. That filtering belongs on the | ||
| * player-achievement-sets resource. | ||
| */ | ||
| enum PlayerGameAwardKind: string | ||
| { | ||
| case BeatenCasual = 'beaten-casual'; | ||
| case BeatenHardcore = 'beaten-hardcore'; | ||
|
|
||
| /** | ||
| * @param Builder<PlayerGame> $query | ||
| * @return Builder<PlayerGame> | ||
| */ | ||
| public function apply(Builder $query): Builder | ||
| { | ||
| return match ($this) { | ||
| self::BeatenCasual => $this->applyBeaten($query, 'beaten_at', UnlockMode::Casual), | ||
| self::BeatenHardcore => $this->applyBeaten($query, 'beaten_hardcore_at', UnlockMode::Hardcore), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @param Builder<PlayerGame> $query | ||
| * @return Builder<PlayerGame> | ||
| */ | ||
| private function applyBeaten(Builder $query, string $timestampColumn, int $unlockMode): Builder | ||
| { | ||
| return $query->where(function (Builder $query) use ($timestampColumn, $unlockMode) { | ||
| $query | ||
| ->whereNotNull($timestampColumn) | ||
| ->orWhereHas('badges', function (Builder $query) use ($unlockMode) { | ||
| $query | ||
| ->whereColumn('user_awards.user_id', 'player_games.user_id') | ||
| ->where('award_type', AwardType::Mastery) | ||
| ->where('award_tier', $unlockMode); | ||
| }); | ||
| }); | ||
| } | ||
| } |
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,82 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Api\V2\PlayerGames; | ||
|
|
||
| use App\Models\PlayerGame; | ||
| use Illuminate\Database\Eloquent\Builder; | ||
| use LaravelJsonApi\Core\Exceptions\JsonApiException; | ||
| use LaravelJsonApi\Eloquent\Contracts\Filter; | ||
|
|
||
| final class PlayerGameAwardKindFilter implements Filter | ||
| { | ||
| public function key(): string | ||
| { | ||
| return 'awardKind'; | ||
| } | ||
|
|
||
| public function isSingular(): bool | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * @param Builder<PlayerGame> $query | ||
| * @return Builder<PlayerGame> | ||
| */ | ||
| public function apply($query, $value) | ||
| { | ||
| $rawKinds = $this->parseRawKinds((string) $value); | ||
|
|
||
| if ($rawKinds === []) { | ||
| return $query; | ||
| } | ||
|
|
||
| $kinds = $this->parseKinds($rawKinds); | ||
|
|
||
| return $query->where(function (Builder $query) use ($kinds) { | ||
| foreach ($kinds as $kind) { | ||
| $query->orWhere(fn (Builder $query) => $kind->apply($query)); | ||
| } | ||
| }); | ||
|
Jamiras marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * @return list<string> | ||
| */ | ||
| private function parseRawKinds(string $value): array | ||
| { | ||
| return collect(explode(',', $value)) | ||
| ->map(fn (string $kind) => trim($kind)) | ||
| ->filter() | ||
| ->unique() | ||
| ->values() | ||
| ->all(); | ||
| } | ||
|
|
||
| /** | ||
| * @return list<PlayerGameAwardKind> | ||
| */ | ||
| private function parseKinds(array $kinds): array | ||
| { | ||
| return collect($kinds) | ||
| ->map(function (string $kind) { | ||
| $parsedKind = PlayerGameAwardKind::tryFrom(trim($kind)); | ||
|
|
||
| if (!$parsedKind) { | ||
| throw JsonApiException::error([ | ||
| 'status' => '400', | ||
| 'code' => 'invalid_filter', | ||
| 'title' => 'Invalid Filter', | ||
| 'detail' => "Unknown award kind [{$kind}].", | ||
| ]); | ||
| } | ||
|
|
||
| return $parsedKind; | ||
| }) | ||
| ->unique(fn (PlayerGameAwardKind $kind) => $kind->value) | ||
| ->values() | ||
| ->all(); | ||
| } | ||
| } | ||
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.
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.