LeaguePHP / LeagueWrap

League of Legend API wrapper
MIT License
20 stars 7 forks source link

[question] How do i get select the champion id 0 in the stats api? #14

Closed ElvarP closed 7 years ago

ElvarP commented 7 years ago

Hey! just a question here, not a issue, sorry for posting here! but you probably have the best idea on how i could do this. (or maybe even a feature request?)

How could i select the champion id 0, in the stats section of the wrapper? Probably a basic question but i can't seem to get my head around it.

So i have this object:

http://pastebin.com/D9eDe8zD (sorry a lil bit to big for github)

So how could i echo the champion id 0?

I have this code:

echo "id: " . $stats[0]->id . "<br>";

And this of course only echoes for the first champion ID in the array.

I could use this:

echo "id: " . $stats[15]->id . "<br>";

But this project is going to be multiple different summers, so smaller/bigger array every time.

Any ideas? if you have an questions please ask!

luisbatista commented 7 years ago

Hello ElvarP,

One good (or simple) option is to convert to an array, more easier to get all values. Can use the object to read values or can convert to an array(). You need a loop (foreach is the best option to me) to get and read the 'champion' array.

To have an array(), you need to add ->raw() at the end of the call, something like that:

$gameCurrent = $riotapi->currentGame()->currentGame($summonerId)->raw();

Then can get the value like this:

$gameId = $gameCurrent['gameId']; foreach ($gameCurrent['participants'] as $p) { $teamId = $p['teamId']; .... }

Regards, LB

ElvarP commented 7 years ago

Hey, thanks for the help! I used a foreach loop (with the object) before, but since i'm updating to the database it just loops over and updates the database (and replaces) all the the champions till it gets to the last one, it wasn't very efficient.

I'm not sure on how i could use an array to loop over till i get to the last one without having the same problem as before, could you elaborate? also i'm using the https://github.com/LeaguePHP/LeagueWrap#stat stat's api not the https://github.com/LeaguePHP/LeagueWrap#current-game current game api

If you have any questions please ask :)

luisbatista commented 7 years ago

Sorry but not understand very well your question!

You want a way to "order" by 'id' or extract by 'id' ? This situation do not exist, you need to loop over all array to identify the correct 'id'.

Can create a class ( "elvarStats" maybe! :) ) to manage this, but need to order all the stuff. Or you can create a function to manage this (passing the object) and return the correct record you want.

Regards, LB

ElvarP commented 7 years ago

Yeah not making it very clear sorry :P

I just want to save to the database the champion id: 0 (and all of the stats that comes with it).

Ordering by id, then saving the first id could work, or creating a function that returns the correct id could also work, but I don't know i would do that.

The only "problem" i was having with looping over the array was that it replaced the database everytime it looped over, untill it got the to the last element of the array, it just didn't seem very efficient to me.

Thanks for the help, i'm sure i can figure this out (or just used to first solution i came up with)

luisbatista commented 7 years ago

Hello again :)

Saving to BD: If you don't maintain last stats information (you make a update replacing old stuff), then a more efficient approach is to:

  1. Delete old stuff: DELETE* FROM your_table WHERE id IN (0,1,....)
  2. Create a query string with all the content to be inserted, like this:
    $queryStr = "INSERT INTO  `your_table` (`id`,`field1`,`field2`, ....) VALUES ";
    foreach ($arrayToLoop as $a){
    $queryStr += "($a->id, $a->field1, $a->field2, .........),";
    }

    then make the query to the database: $resQuery = $mysqlObject->query($queryStr);

With one call can insert 1 or 15 records into database.

I don't know if I can help you.

Regards, LB

ElvarP commented 7 years ago

I just used a foreach loop for the stats, except i used REPLACE, instead of UPDATE, so it just iterated over the objects until it got to the last object, and then saved the last object. would your solution be more optimized for that? It seems to be it just does the same thing, but i'm a novice :P

Thanks again!

m1so commented 7 years ago

Couple of comments from my side:

If you have any more questions feel free to ask in the Riot API Discord

ElvarP commented 7 years ago

Thanks for the answer, i solved it by sorting the array by id.

Any ETA on the next release? Would rather implement that rather then sorting it for easier code maintenance.

m1so commented 7 years ago

I'm still not quite sure what the issue is, if you are calling the stats endpoint and wanting to get the combined stats (id of 0) just use

$stats = $api->stats()->ranked('...');
$combinedStats = $stats->champion(count($stats->champions) - 1); // add ->raw() for an array

there is no need to sort anything, if you want to be 100% safe in case there are some changes in the API just filter the array instead of sorting. You can also just require dev-master of this package until a new release is on packagist (which might take a while).

ElvarP commented 7 years ago

haha there's no issue :P sorting the array was just the best solution i came up with.

Didn't know i could use count, thanks for tip! It worked great.

Cheers.