Tustin / psn-php

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

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

Open Askancy opened 4 days ago

Askancy commented 4 days ago

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 commented 4 days ago

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 commented 3 days ago

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 commented 3 days ago

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. https://github.com/Tustin/psn-php/blob/b4ecb23e16a39815237bd884f50950a777e882e7/src/Client.php#L203-L213

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: https://github.com/Tustin/psn-php/blob/b4ecb23e16a39815237bd884f50950a777e882e7/src/Model/User.php#L75-L94

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 commented 2 days ago

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 commented 2 days ago

https://andshrew.github.io/PlayStation-Trophies/#/APIv2?id=_2-retrieve-the-trophies-for-a-title

image

Askancy commented 11 hours ago

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...