Tustin / psn-php

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

How to get earned trophies for user? #226

Closed Sakreetos closed 1 year ago

Sakreetos commented 1 year ago

Hello. I am able to get trophies lists for every user title but there is no "earned" info.

Here is what I am doing:

 $titles = $me->trophyTitles(); // Get every trophy title

    foreach ($titles as $title) {
        foreach ($client->trophies($title->npCommunicationId(), $title->serviceName())->trophyGroups() as $trophyGroup) {
            foreach ($trophyGroup->trophies() as $trophy) {
                $test = $trophy->name();
                $test2 = $trophy->earned();
            }
        }
    }
Ragowit commented 1 year ago

I don't know what $me is, have you used $client->users()->find('your id number') on it?

Also, what version of psn-php are you using?

Sakreetos commented 1 year ago

Thank you. I was using the wrong approach.

Here is the working solution

foreach ($client->users()->search('xxxxx') as $user) {
        $games = $user->trophyTitles();
        foreach ($games as $game) {
            if ($game->hasTrophies()) {
                $trophyGroups = $game->trophyGroups();
                foreach ($trophyGroups as $trophyGroup) {
                    foreach ($trophyGroup->trophies() as $trophy) {
                        $test1 = $trophy->earned();
                    }
                }
            }
        }
    }