Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A little Help with async request #95

Closed
Lucasnl opened this issue Dec 10, 2020 · 4 comments
Closed

A little Help with async request #95

Lucasnl opened this issue Dec 10, 2020 · 4 comments
Labels
question Just a simple question.

Comments

@Lucasnl
Copy link

Lucasnl commented Dec 10, 2020

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

@dolejska-daniel
Copy link
Owner

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.

@dolejska-daniel dolejska-daniel added the question Just a simple question. label Dec 10, 2020
@Lucasnl
Copy link
Author

Lucasnl commented Dec 10, 2020

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

@dolejska-daniel
Copy link
Owner

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
Copy link
Author

Lucasnl commented Dec 16, 2020

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

@Lucasnl Lucasnl closed this as completed Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Just a simple question.
Projects
None yet
Development

No branches or pull requests

2 participants