1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How do i create an array of ALL players and loop though them all

Discussion in 'Development' started by Atomization, May 31, 2018.

  1. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    How do i create an array of ALL players from \players\ directory and loop though them all? i want to do this because i want to get a list of all players and see if any of them are offline.
     
  2. RyanShaw

    RyanShaw Witch

    Messages:
    69
    The players folder could contain up to thousands of players depending on how popular/old your server is. Why not make a much smaller array of the current players online and check if the player is online that way?
    PHP:
     foreach ($this->getServer()->getOnlinePlayers() as $player) {
    //check if the player is online here
     
  3. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    Ok, I have a plugin for ppl who bought a rank on my server, and I have stored all of them in a .Json file in the config folder

    I want to loop through them and check if any of them are offline because I want to have a timmer that continues even if the player is offline or online

    That better explains what I'm trying to do.
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Can you let us know how the JSON file looks like? Is it just a sequential array of player names or are they mapped to something?
     
    Kyd likes this.
  5. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    PHP:
    {
        
    "AtomizationYT": {
            
    "rank""vip",
            
    "timeout"20000
        
    },
        
    "JustRealPetey84": {
            
    "rank""vip",
            
    "timeout"20000
        
    }
    }
    i want to loop though everyone in this file and check if they are offline...
    This only works when the player is online, thus, the timer only goes down when they are online, but i want the opposite.
    PHP:
    class Timeout extends PluginTask{

    public 
    $defaultTime 86400//24 hours in seconds
     
        
    public function __construct($plugin){
            
    $this->plugin $plugin;
            
    parent::__construct($plugin);
        }
     
        public function 
    onRun($tick){

    $timeoutDB = new Config($this->plugin->getDataFolder()."players.json"Config::JSON);
    $timeoutDB->save();

    $onlinePlayers $this->plugin->getServer()->getOnlinePlayers();

    foreach(
    $onlinePlayers as $player){
    $name $player->getName();

    $time $timeoutDB->getNested($name.".timeout");

    if(
    $time > -5){
    $time--;

    $timeoutDB->setNested($name, ["rank"=>"vip""timeout"=>$time]);
    $timeoutDB->save();

    }

    }
    }
    }
     
    Last edited: May 31, 2018
  6. Aviv

    Aviv Baby Zombie

    Messages:
    156
    A smart idea is to use the time() function, so time() + 86400 will be 24 hours from now in seconds
     
  7. Tee7even

    Tee7even Slime

    Messages:
    81
    GitHub:
    tee7even
    It's better to save an expiration date and check if the player's rank is still active, when they join. Looping through all of your offline players will be bad for performance.
     
  8. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    second this over any other methods
    you should only store the expire date
    when the user join, check if rank is expired already
    if it is remove the rank, else do nothing

    for good measure, make a task that runs every 5 minute(make it like 30 min if you have more players)
    at each run, checks for online player's rank's expire date vs current date, and remove rank if time is overt
    this is good for players that joined with a vip for 10 minutes but they will get eternal VIP as long as they dont logoff this sloves that part normally extra 1hour-3hour ranks isnt too big of a deal if they didnt log off

    Using task can be bad as server do reboot, and having to constantly save it to file is even ineffecient
     
    Muqsit likes this.
  9. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    okay, how do i make an expire date? can i have a code example.
     
  10. Tee7even

    Tee7even Slime

    Messages:
    81
    GitHub:
    tee7even
    For example, time() + timeout in seconds.
     
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    The time() function returns the UNIX timestamp. The value of time() keeps changing.
    PHP:
    $time time();
    //wait 3 seconds
    $now_time time();

    echo 
    $now_time $time;//3
     
  12. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    This is going nowhere (to what i can see).. i want to know how to make a "Cool Down" for how long they must wait before getting the rank again
     
  13. Tee7even

    Tee7even Slime

    Messages:
    81
    GitHub:
    tee7even
    I don't get it, could you explain in more detail?
     
  14. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    a player runs a command to give him/her a free rank with features for a specific time. How do i create the cool down?
     
  15. Tee7even

    Tee7even Slime

    Messages:
    81
    GitHub:
    tee7even
    So, do you want to make it so the rank would expire, but a player could use the command with a specific cooldown time, which is independent from rank's expiration time (so the rank's expiration time would add up), or the rank would expire and only then the player could use the command again? If you mean the second option, then what @Thunder33345 explained is pretty much what you do, you just should make sure that you check if player's rank has expired or not, when the player uses the command. If you mean the first option, then you should save one more timestamp for the cooldown and check if current timestamp is bigger than the saved one, when the player uses the command.
     
  16. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    Yes, but I'm not sure how to do it.. May i have a code example?
     
  17. Tee7even

    Tee7even Slime

    Messages:
    81
    GitHub:
    tee7even
    May I hear what you mean? I mentioned two options and you just said "yes".
     
  18. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    I quoted option 1, which I figured you would understand thats what I wanted help with
     
  19. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I don't think you get the point.
    You can code anything in any way you want.
    If all you want to do is loop through every player, then sure go ahead and use scandir().
    PHP:
    $folder "/path/to/my/folder/";
    foreach(
    scandir($folder) as $file){
        
    $full_path $folder $file;
        
    $data json_decode(file_get_contents($full_path), true);
    }
    Sure, you can implement a feature in a million different ways by ignoring performance and CPU time.

    You should really look into PHP's time() function which returns the live Unix timestamp. For your case, that's just the right thing to use, that's what everyone is saying.
     
    Thunder33345 likes this.
  20. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Be aware that loading all players can waste memory unnecessarily. Assuming loading each player takes 1 KB of memory, on a medium-sized server with 1 million registered players, you would require 1 GB RAM.
     
    Thunder33345 likes this.
  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.