Tustin / psn-php

A PHP wrapper for the PSN API
https://tustin.dev/psn-php/
MIT License
354 stars 73 forks source link

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

Open Argosth opened 7 months ago

Argosth commented 7 months ago

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 commented 7 months ago

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.

Argosth commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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 commented 6 months ago

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 commented 6 months ago

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

pradella commented 1 month ago

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 commented 1 month ago

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