1n9i9c7om / ClashOfClans-API-PHP

PHP wrapper for SuperCell's official ClashOfClans API.
MIT License
34 stars 13 forks source link

Slow loading league badges #5

Closed RHITNL closed 8 years ago

RHITNL commented 8 years ago

Hi,

Thanks for this wrapper, really useful. Only have one issue. As soon as I include the league badges for player by setting values for League.class.php, the load time of the whole clanmembers list is extremely slow. When I disable the images, it fast as lightning. Using the most recent version of all files so it should already have the caching changes in it. Any idea what could cause this?

Regards,

Ralph

1n9i9c7om commented 8 years ago

Hello,

I think I know what's causing the issue. It's actually the same as to why loading the rank toplist was slow in #3 - I fixed it for clans, but forgot about Leagues (and Locations).

I'm currently heading back from school, expect an update later today or tomorrow - most likely today, tho. :)

I'll comment back on this issue after its fixed.

1n9i9c7om commented 8 years ago

It's fixed, you have to change a small thing about your code, though.
Right now, you're passing the league ID to the constructor of the *League.class - now, you can also pass the league itself, preventing an additional request per member (which is what causes the slow page loading).

Right now, you're probably doing something like this:

$clan = new CoC_Clan("#22UCCU0J");

foreach ($clan->getAllMembers() as $member) 
{
    $member = new CoC_Member($member);
    $league = new CoC_League($member->getLeagueId());

   echo "<img src=\"" . $league->getLeagueIcon("small") . "\">";
}

Now, with the latest commit I pushed, you can do it like this:

$clan = new CoC_Clan("#22UCCU0J");

foreach ($clan->getAllMembers() as $member) 
{
    $member = new CoC_Member($member);
    $league = new CoC_League($member->getLeague()); //call getLeague(), not getLeagueId()

   echo "<img src=\"" . $league->getLeagueIcon("small") . "\">";
}

Example to test loading times Script Execution took 0.71272897720337 for 31 member. Same code except that I'm using getLeagueId() Script Execution took 7.0390779972076 for 31 member.
Sorry for forgetting about Leagues and Locations while fixing it for clans. :stuck_out_tongue:
I hope it works for you now!

RHITNL commented 8 years ago

Brilliant! Was looking into that part but couldn't wrap my finger around it. It will save supercell a lot of server requests now 😀 Works fast and clean now! http://dutchbros.tk if you are interested to see my use of your wrapper! (Work in progress)