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

Search for a specific game of a user by npCommunicationId() instead of the name #254

Open
Argosth opened this issue Feb 7, 2024 · 8 comments

Comments

@Argosth
Copy link

Argosth commented Feb 7, 2024

Hello, I would like to know if the API has the possibility to search for a specific game of a user since the given method of the API by name is not exact when searching for a game and may return several:

$user = $Client->users()->find('4352824381498132139');

foreach ($user->trophyTitles()->withName('RESIDENT EVIL') as $game) {
    echo($game->name()."<br>");
    echo($game->iconUrl()."<br>");
}

I want it to return only the game Resident Evil, and not all that contain Resident Evil in their name, any ideas or solutions?

@Ragowit any idea about this?

Thank you very much.

@Ragowit
Copy link
Collaborator

Ragowit commented Feb 10, 2024

I don't think this tool has an iterator for exact match.

You could probably edit the current iterator, but you will loose the current behavior then.

public function accept(): bool
{
return stripos($this->current()->name(), $this->titleName) !== false;
}

I think changing the return to something like this should work for you:
return (strtolower($this->current()->name()) == strtolower($this->titleName));

Please note that this will only return exact matches now. So if a game have the name "Resident Evil™" so will you not find it by searching for "Resident Evil". It's also quite common for some games to have the name trophies in it, like "Resident Evil Trophies", which will also not be found if you search for "Resident Evil".

And as I said, this change will remove the current behavior. If you want both you have the create a new iterator and function, maybe "withExactName()" or something like that.

It might just be easier to change your own code to loop through the results for what you're looking for. It won't probably be that many games.

$user = $Client->users()->find('4352824381498132139');

$searchTerm = 'RESIDENT EVIL';
foreach ($user->trophyTitles()->withName($searchTerm) as $game) {
   if (strtolower($game->name()) == strtolower($searchTerm)) {
      // Do your thing
   }
}

Again, you will still have to look out for ™ and Trophies and other strange things in names.

@Argosth
Copy link
Author

Argosth commented Feb 10, 2024

I don't think this tool has an iterator for exact match.

You could probably edit the current iterator, but you will loose the current behavior then.

public function accept(): bool
{
return stripos($this->current()->name(), $this->titleName) !== false;
}

I think changing the return to something like this should work for you: return (strtolower($this->current()->name()) == strtolower($this->titleName));

Please note that this will only return exact matches now. So if a game have the name "Resident Evil™" so will you not find it by searching for "Resident Evil". It's also quite common for some games to have the name trophies in it, like "Resident Evil Trophies", which will also not be found if you search for "Resident Evil".

And as I said, this change will remove the current behavior. If you want both you have the create a new iterator and function, maybe "withExactName()" or something like that.

It might just be easier to change your own code to loop through the results for what you're looking for. It won't probably be that many games.

$user = $Client->users()->find('4352824381498132139');

$searchTerm = 'RESIDENT EVIL';
foreach ($user->trophyTitles()->withName($searchTerm) as $game) {
   if (strtolower($game->name()) == strtolower($searchTerm)) {
      // Do your thing
   }
}

Again, you will still have to look out for ™ and Trophies and other strange things in names.

Thank you very much, I will look for other options too, ty mate :)

@Argosth
Copy link
Author

Argosth commented Feb 10, 2024

One more question @Ragowit, would you know how to determine the size or the games that a player has from $user->trophyTitles() without having to loop through the entire foreach? I need to know the number of games a player has, and the only way I have found is by iterating through the entire loop and adding it to a counter.

@Ragowit
Copy link
Collaborator

Ragowit commented Feb 10, 2024

The API returns the total number of games. I'm uncertain if this number includes hidden games or not.

I'm also uncertain from the top of my head which function it was you should use, it's one of these two:
$user->trophyTitles()->count();
$user->trophyTitles()->getTotalResults();

@Argosth
Copy link
Author

Argosth commented Feb 13, 2024

The API returns the total number of games. I'm uncertain if this number includes hidden games or not.

I'm also uncertain from the top of my head which function it was you should use, it's one of these two: $user->trophyTitles()->count(); $user->trophyTitles()->getTotalResults();

@Ragowit I tried the two options but nothing, and I'm looking into TrophyTitles Factory and there is no an option to determine the object size I think.

@Ragowit
Copy link
Collaborator

Ragowit commented Feb 19, 2024

I was looking at https://github.com/Tustin/psn-php/blob/master/src/Iterator/TrophyTitlesIterator.php which inherits from https://github.com/Tustin/psn-php/blob/master/src/Iterator/AbstractApiIterator.php that has count() and getTotalResults(). Should be there if I understand the code right.

/**
* Gets the item count.
*/
public final function count(): int
{
return $this->getTotalResults();
}
/**
* Gets the total results.
*/
public function getTotalResults(): int
{
return $this->totalResults;
}

@pradella
Copy link

Hey @Ragowit do you know if there is a way to find titleId from a given npCommunicationId? I was able to do the opposite, but not this one...

For example: trophy/v1/users/${account_id}/trophyTitles brings all games including PS3 games with npCommunicationId, and no titleId.

But to fetch game details (such as media) I need titleId to use this endpoint: gamelist/v2/users/${account_id}/titles/${title_id}

@Ragowit
Copy link
Collaborator

Ragowit commented Jul 31, 2024

No, I don't know of such a way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants