dolejska-daniel / riot-api

Riot League of Legends & DataDragon API wrappers for PHP7 and PHP8.
GNU General Public License v3.0
112 stars 25 forks source link

A little Help with async request #95

Closed Lucasnl closed 3 years ago

Lucasnl commented 3 years ago

im trying to get the sum of all kills from the participant id =1 , but it seems that array_sum is not working properly inside the $onsucess function , im getting only the kills for each match and not the sum of them all.

do you have any tips for me ?

<?php

// Function to be called on request success
 $onSuccess = function (Objects\MatchDto  $match) {

  foreach ($match->participants as $team) {

        if ($team->participantId==1 and $team->teamId==100) {

              $t[]=$team->stats->kills.'</br>';

        }    
    } 
     var_dump (array_sum($t)); echo '</br>';

};
// Function to be called on request failure
$onFailure = function ($ex) {
    echo "Error occured: {$ex->getMessage()}";
};

 //I have a bunch of match_ids from league of legends games saved in my db
 // so,  foreach $custom is to get all match_ids

    foreach ($custom as $games) {

    $api->nextAsync($onSuccess, $onFailure);

    $match=$api->getMatch($games->match_id);

}

$api->commitAsync();
?>

aaads

im getting the kills from each game but the expected result is 122 , the sum of all those values

dolejska-daniel commented 3 years ago

Well what is happening here is that the $onSuccess function gets called for each fetched match entry. So if you print the result in that function you will always be printing values for the single match only, you could do this to fix that:

$kills = 0;
$onSuccess = function (Objects\MatchDto  $match) use ($kills) {
  foreach ($match->participants as $p)
    if ($p->participantId==1 && $p->teamId==100)
      $kills += $p->stats->kills;
};

// $onFailure = function ...
// foreach ($custom as $games) ...
// $api->commitAsync();

var_dump($kills);
echo "<br>";

This will aggregate the kills in the variable and after the commit is finished and all matches processed you print out the result.

Lucasnl commented 3 years ago

Thank you very much for the help ! i tried this and is returning 0; image

dolejska-daniel commented 3 years ago

Hey @Lucasnl, the last missing piece of the puzzle is that function (…) use ($var) does pass-by-value for $var, the only thing you need to do now is to add magical & which makes PHP to pass the $var by reference to the function which results in variable's value being correctly updated.

Fully working snippet:

$kills = 0;
$onSuccess = function (RiotAPI\LeagueAPI\Objects\MatchDto  $match) use (&$kills) {
    foreach ($match->participants as $p)
        if ($p->participantId == 1 && $p->teamId == 100)
            $kills += $p->stats->kills;
};
$onFailure = function ($ex) {
    echo "Error occured: {$ex->getMessage()}\n";
};

foreach ($custom as $games) {
    $api->nextAsync($onSuccess, $onFailure);
    $match = $api->getMatch($games->match_id);
}

$api->commitAsync();
var_dump($kills);
Lucasnl commented 3 years ago

Thanks again for your time and help ! it worked very well