psn-php icon indicating copy to clipboard operation
psn-php copied to clipboard

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

Open Argosth opened this issue 1 year ago • 8 comments

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.

Argosth avatar Feb 07 '24 06:02 Argosth

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.

https://github.com/Tustin/psn-php/blob/5d9a357eed5e478e0b97112498c6cd5d6bac1749/src/Iterator/Filter/TrophyTitle/TrophyTitleNameFilter.php#L15-L18

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.

Ragowit avatar Feb 10 '24 09:02 Ragowit

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.

https://github.com/Tustin/psn-php/blob/5d9a357eed5e478e0b97112498c6cd5d6bac1749/src/Iterator/Filter/TrophyTitle/TrophyTitleNameFilter.php#L15-L18

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 avatar Feb 10 '24 10:02 Argosth

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.

Argosth avatar Feb 10 '24 16:02 Argosth

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 avatar Feb 10 '24 21:02 Ragowit

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.

Argosth avatar Feb 13 '24 12:02 Argosth

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.

https://github.com/Tustin/psn-php/blob/5d9a357eed5e478e0b97112498c6cd5d6bac1749/src/Iterator/AbstractApiIterator.php#L39-L53

Ragowit avatar Feb 19 '24 08:02 Ragowit

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}

pradella avatar Jul 31 '24 18:07 pradella

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

Ragowit avatar Jul 31 '24 18:07 Ragowit