Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Countdown [MYSQL]

Comments in 'Plugin Development' started by JackboyPlay, Jun 11, 2016.

  1. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    How can I make an countdown with PocketMine & MySQL?
  2. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    By countdown, do you mean something like this?
    If that's the case, you should use tasks, not MySQL.
    applqpak likes this.
  3. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    Yeah
  4. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
    Then MySQL is not recommended. There could be some delays between the database, and it is more of a large data storage instead of temporary memory.
    applqpak likes this.
  5. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    The countdown would be the same concept as if you werent doing it with MySQL. The only difference is that the numbers are stored in the database.
    Why would you want to make a countdown with MySQL anyways?
    applqpak likes this.
  6. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    For an chatmute system can you send it as code?
  7. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Store the UNIX timestamp for the expiry time in the database. Compare time() against the stored value to check whether the mute has expired.

    If you are using multiple servers (multiple machines), you might want to manually calibrate the system time so that there would not be time difference, e.g. the mute is delayed for one minute on another machine. Don't worry about the timezones however. UNIX timestamps aren't affected by timezones.
  8. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Or he can store year, month, day, hours and seconds and compare it (maybe compare all players muted in an async task) with date function.
    http://php.net/manual/es/function.date.php
  9. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    We are quite scared of timezone problems if you use date(). After all, saving timestamps has all the advantages over using date() apart from human-readability.

    Why use an AsyncTask? Just use this query:

    Code:
    SELECT player_name FROM muted_players WHERE expiry > unix_timestamp()
    
    Or, if the expiry column is in DATE/TIME/TIMESTAMP (can't understand the difference between them):
    Code:
    SELECT player_name FROM muted_players WHERE unix_timestamp(expiry) > unix_timestamp()
    
    Example code (async-compatible, abstract query methods):
    PHP:
    class MuteEntry{
      public 
    $playerName;
      public 
    $expiryTime;
      public 
    $reason;
    }
    class 
    MuteUtilities{
      private 
    $mutes = [];
      
    /**
       * @param string $playerName case-insensitive name of player to mute
       * @param int $duration duration to mute in seconds
       * @param string $reason actually, any extra data you want to put here
       */
      
    public function mutePlayer(string $playerNameint $durationstring $reason){
        
    $query "INSERT INTO muted_players (player_name, expiry, reason) VALUES (:s, unix_timestamp() + :d, :s)";
        
    execQueryAsync($query);
      }
      public function 
    updateMutes(){ // call this periodically
        
    execQueryAsync([$this"onMutesQueryResult"],
            
    "SELECT player_name, expiry - unix_timestamp() AS left, reason FROM muted_players WHERE expiry > unix_timestamp()");
      }
      public function 
    onMutesQueryResult(array $rows){
        
    $mutes = [];
        foreach(
    $rows as $row){
          
    $mute = new MuteEntry;
          
    $mute->playerName $row["player_name"];
          
    $mute->expiryTime microtime(true) + (float) $row["left"];
          
    $mute->reason $row["reason"];
          
    $mutes[] = $mute;
        }
        
    $this->mutes $mutes;
      }
      public function 
    isPlayerMuted(string $playerName){
        foreach(
    $this->mutes as $mute){
          if(
    $mute->playerName === $playerName){
            if(
    $mute->expiryTime microtime(true)){
              return 
    true;
            }
          }
        }
        return 
    false;
      }
    }
    P.S. I hate PHP. Java is much much better!
    Primus and applqpak like this.
  10. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    definitely NOT MySQL for something like this.
  11. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    To be honest, I don't see how Java is that much better than PHP.
  12. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Let's not get off-topic. But if you have an IDE, you would find that Java code is much more systematic than PHP, and Java code itself is usually already a draft/mindmap of what you are trying to code. In PHP, especially with the lack of type-strictness, one easily gets lost.
    Just that you need pretty much experience in order to make another person's code make a lot of sense to you as if a draft.
    applqpak, Primus and HotFireyDeath like this.
  13. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Why use an async task? Because you need to be constantly checking if the muted time has been ended.
    And, if you hate PHP, why are you publishing posts here? :)
  14. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    There is no contradiction. I hate PHP. I hate every programming language. I just hate Java less! And I hate C the most!
    applqpak likes this.
  15. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    So why you don't leave internet? :D
  16. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    I thought you guys agreed not to go off-topic... :(
    Yes, some do not like the fact that PocketMine itself was written in PHP. You should check out Nukkit sometime, it's a Java-based MCPE software.
    The problem with date() is that it is different for every time zone. time() is the same from everywhere in the world, therefore eliminating this issue. UNIX time was created to solve this kind of problem. It counts seconds elapsed since 00:00 January 1, 1970. See year 2038 issue: https://en.m.wikipedia.org/wiki/Year_2038_problem
    Survingo, applqpak and AndrewBit like this.
  17. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    In that case, date_default_timezone_set() will be a great solution :p
  18. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    No. I actually meant that you don't need to compare the player muted time in an AsyncTask. It is definitely OK to compare it at the main thread. And you can cache the mutes, not to check it every time (otherwise, you will have a bit delay for every chat message).
    Nukkit isn't really beautiful either. It is in Java and it is all OK, but it doesn't reflect the beautiful part of Java (that I recognize).
    Then why don't you use time() directly... Why must you compare them separately, which leads to more calculation? Fine, you are in an AsyncTask, but being in an AsyncTask doesn't mean that you should add extra operations to do needlessly, because they are still run on the same machine; and yes, they are very negligible difference. But why don't you compare a number to a number, which greatly reduces the complexity of the program?

    Regarding the Year 2038 problem, it is not a serious problem. In year 2038, probably nobody is still using 64-bit machines, much less 32-bit, right?
    Gamecrafter likes this.
  19. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    I have experience with JavaScript, which uses some scripts of Java. Its easy to overview, but the only thing I hate so much are the function names, as example when you want to get a line in a file in PHP just do file($dir)[7]; to get line 7, while you need to use some "BufferedReader", with multiple objects and so on, therefore you need to create larger functions to get lines as you do on PHP.
  20. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Did you just put JavaScript and Java in the same sentence?
    Primus likes this.

Share This Page

Advertisement