dolejska-daniel/riot-api

A little Help with async request

Closed this issue · 4 comments

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.

  • it seems the $custom foreach is passing through the $onsucess function , if i erase everything on the $onsuccess function , and echo just "test" , it prints out "test" 33 times , the exact same number of matchid that i have on my db

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

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.

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

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);

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