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

How do I retrieve the trophy list for a specific game? #264

Open
Askancy opened this issue Nov 20, 2024 · 6 comments
Open

How do I retrieve the trophy list for a specific game? #264

Askancy opened this issue Nov 20, 2024 · 6 comments

Comments

@Askancy
Copy link

Askancy commented Nov 20, 2024

It is not clear to me how I can retrieve the trophy list of a game.

In the documentation under ‘trophy titles’ I have:

$titles = $user->trophyTitles(); // Get each trophy title

so, if i write:

$titles = $user->trophyTitles()->withName('Red Dead Redemption');

foreach ($titles as $title) {
    echo $title->name() .'<br>';
}

I would expect to find a list of the game Red Dead Redemption, no?

Instead, I receive as output the games that have Red Dead Redemption as their name, namely:

Red Dead Redemption
Red Dead Redemption
Red Dead Redemption 2

using:

    $titles = $user->trophyTitles()->withName('Red Dead Redemption');
   dd($titles);

-lastResponse: ? GuzzleHttp\Psr7\Response
#platforms: []
-withName: "Red Dead Redemption"
-hasTrophyGroups: null

I want to receive the list of Red Dead Redemption trophies, but in the documentation I can't figure out how to do it....

@Ragowit
Copy link
Collaborator

Ragowit commented Nov 20, 2024

Yeah, the documentation is missing quite a bit.

$titles = $user->trophyTitles()->withName('Red Dead Redemption');

This line of code is looking for games with the name "Red Dead Redemption" for this current user. It will return all games found, as you noticed.

To get the trophy list, you have to do something like this:

foreach ($user->trophyTitles()->withName('Red Dead Redemption') as $trophyTitle) {
   foreach ($client->trophies($trophyTitle->npCommunicationId(), $trophyTitle->serviceName())->trophyGroups() as $trophyGroup) {
      // Handle "group" data here
      
      foreach ($trophyGroup->trophies() as $trophy) {
         // Handle "trophy" data here. This is only game trophy data, not user earned trophy data.
      }
   }

   foreach ($trophyTitle->trophyGroups() as $trophyGroup) {
      foreach ($trophyGroup->trophies() as $trophy) {
         // This has trophy earned data for current user.
      }
   }
}

@Askancy
Copy link
Author

Askancy commented Nov 21, 2024

This line of code is looking for games with the name "Red Dead Redemption" for this current user. It will return all games found,

But what if I want to recover a game that this user has NEVER played, can I do so?

However I'm trying to get the main trophy data, and when I call type() I get the error:

echo $trophy->name() .' - '. $trophy->detail().' - '. $trophy->type() .' <br>'. $trophy->iconUrl(). '<br>';

Cannot instantiate enum Tustin\PlayStation\Enum\TrophyType {"userId":2,"exception":"[object] (Error(code: 0): Cannot instantiate enum Tustin\PlayStation\Enum\TrophyType at /home//public_html//vendor/tustin/psn-php/src/Model/Trophy/Trophy.php:58)
[stacktrace]

if I remove type(), the other values are printed correctly instead

also for greater precision, for example when I search for games such as:

LEGO® Horizon Adventures™

is it possible to search it by title ID?

example: PPSA14632

In the PlayStation-Trophies API it is possible with:

npCommId

@Ragowit
Copy link
Collaborator

Ragowit commented Nov 21, 2024

But what if I want to recover a game that this user has NEVER played, can I do so?

Simple answer, no. The $client->trophies() function requires you to send in "np communication id" (NPWR..._00), which is only obtained by looking through users gamelist. So unless you somehow already have a huge list of "np communication id"s from somewhere (I don't), the only option is to go through a users game list.

psn-php/src/Client.php

Lines 203 to 213 in b4ecb23

/**
* Gets a trophy title from the API using a communication id (NPWRxxxxx_00).
*
* @param string $npCommunicationId
* @param string $serviceName
* @return TrophyTitle
*/
public function trophies(string $npCommunicationId, string $serviceName = 'trophy'): TrophyTitle
{
return new TrophyTitle($this->getHttpClient(), $npCommunicationId, $serviceName);
}

Regarding $trophy->type(), I don't know from your code if you're inside $user->trophyTitles()->trophyGroups()->trophies() or $client->trophies()->trophyGroups()->trophies(). Please remember that trophy-data you get is different from which one you're calling from.

Look at these docs as an example:
https://andshrew.github.io/PlayStation-Trophies/#/APIv2?id=output-json-response-1
https://andshrew.github.io/PlayStation-Trophies/#/APIv2?id=output-json-response-2

However, trophy type should be in both. Can you try $trophy->type()->value; ?

EDIT: New questions were added to post while I was writing.

There's this from the $user:

/**
* Get the communication id (NPWR...) from a title id (CUSA...)
*
* Only works for PS4/PS5 titles.
* Doesn't work with PPSA... title ids.
*/
public function titleIdToCommunicationId($npTitleId): string
{
$body = [
'npTitleIds' => $npTitleId
];
$results = $this->get('trophy/v1/users/' . $this->accountId() . '/titles/trophyTitles', $body);
if (count($results->titles[0]->trophyTitles) == 0) {
return '';
}
return $results->titles[0]->trophyTitles[0]->npCommunicationId;
}

I haven't used it myself though and don't know much about it. Don't know if it can return an answer about games that the user doesn't have.

@Askancy
Copy link
Author

Askancy commented Nov 22, 2024

Regarding $trophy->type(), I don't know from your code if you're inside $user->trophyTitles()->trophyGroups()->trophies() or $client->trophies()->trophyGroups()->trophies(). Please remember that trophy-data you get is different from which one you're calling from.

ok it works it was my mistake.

for the second question, that of getting the data of the games using their title ID or communication id

$users = $user->titleIdToCommunicationId('PPSA01284_00'); //returnal

I receive as output

NPWR20004_00

* Gets a trophy title from the API using a communication id (NPWRxxxxx_00).

so i pass communication id inside trophies, But in inside $client->trophies as $serviceName of trophies what do I put?

$npCommunicationId = $user->titleIdToCommunicationId('PPSA01284_00');
$trophyGroups =  $client->trophies($npCommunicationId)->trophyGroups();
foreach ($trophyGroups as $trophyGroup) {
   foreach ($trophyGroup->trophies() as $trophy) {
       echo $trophy->name();
   }
}

@Ragowit
Copy link
Collaborator

Ragowit commented Nov 22, 2024

@Askancy
Copy link
Author

Askancy commented Nov 24, 2024

I haven't used it myself though and don't know much about it. Don't know if it can return an answer about games that the user doesn't have.

In my case, I tried PPSA08709, which is equivalent to Silent Hill 2 which I haven't played. and nothing appears...

I also tried the command on powershell and nothing....

Invoke-RestMethod -Uri "https://m.np.playstation.com/api/trophy/v1/npCommunicationIds/PPSA08709_00/trophyGroups/all/trophies?npServiceName=trophy" -Authentication Bearer -Token $token | ConvertTo-Json -Depth 3

So there's really no way to retrieve the trophy list for the games I don't have in my profile.... List of trophies in Italian language cannot be found, so it is also difficult to make a scrape...

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

2 participants