async task not keeping up with players joining simultaneously

Discussion in 'Development' started by Levi, May 25, 2019.

  1. Levi

    Levi Skeleton

    Messages:
    846
    GitHub:
    captainleviftw
    when players join, I retrieve their info from a database (MySQL) and store them to a variable. It seemed to work well until I tried joining the server at the same time with another player. What could be the solutions?
    PHP:
    SELECT
           MONEY
    ,
           
    SKIN,
    FROM pvdb WHERE NAME=:name;
    -- 
    #        }
    -- #      }
    -- #  }
    PHP:
    public function loadPlayer $player, callable $callable ): void
            
    {
             
                
    $this->connector->executeSelect"pbdb.load", [
                    
    "name" => $player->getName()
                ], 
    $callable );
            }
    PHP:
    public function retrieveMoney Player $player )
    {
    $balance = [];
    $this->load(
                    
    $player,
                    function ( array 
    $data ) use ( $uuid$player ) {
                     
                        foreach (
    $data as $_data) {
                          
    $this->balance[$player->getName()] = $_data"MONEY" ]
                        }
                    } );
    }
     
  2. wolfdale

    wolfdale Zombie

    Messages:
    300
    Try increase async pool size in pocketmine.yml: settings.async-workers
     
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,876
    GitHub:
    sof3
    Then more players join together, and you're still having the problem.
    Before executing something, check of player data are ready. If not loaded yet, push them to a queue to execute later. That's what async is supposed to mean.
     
    jasonwynn10 likes this.
  4. Levi

    Levi Skeleton

    Messages:
    846
    GitHub:
    captainleviftw
    how do i check if data are 'ready'?
     
    Last edited: May 25, 2019
  5. Levi

    Levi Skeleton

    Messages:
    846
    GitHub:
    captainleviftw
    Googled how to check if mysql data are ready and nothing came up helpful lol can u even check if data are ready
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,525
    GitHub:
    muqsit
    Several ways you can do that. Authentication plugins are a great example.
    HereAuth f.e completely locks the player (no interacting, no block placing or breaking, no chatting etc) until they type in their password. You can do something like that. Lock the player until their initial data has been fetched from the database.
    PHP:
    public function load(Player $player){
        
    lock($player);
        
    $this->connector->executeSelect($stmt$params, function() use($player){
            
    unlock($player);
        });
    }

    // Listen to PlayerInteractEvent, BlockBreakEvent etc at a low priority
    // and cancel if isLocked($player);
     
    HimbeersaftLP and wolfdale like this.
  7. Levi

    Levi Skeleton

    Messages:
    846
    GitHub:
    captainleviftw
    what if i send score hud to players when they joiN?
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,876
    GitHub:
    sof3
    I might develop a framework for this particular purpose
     
  9. Levi

    Levi Skeleton

    Messages:
    846
    GitHub:
    captainleviftw
    Nice stuff
     
  10. Levi

    Levi Skeleton

    Messages:
    846
    GitHub:
    captainleviftw
    Are you actually gonna do that
     
  11. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,876
    GitHub:
    sof3
    Still thinking how to elegantly do that without generics.
     
  12. Levi

    Levi Skeleton

    Messages:
    846
    GitHub:
    captainleviftw
    What is an alternative can i do while you think about that?
     
  13. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,876
    GitHub:
    sof3
    Add callables to a queue that are dispatched when the data arrive.
     
  14. Levi

    Levi Skeleton

    Messages:
    846
    GitHub:
    captainleviftw
    thats what i do already
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.