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

SQLITE Cooldown

Discussion in 'Development' started by Levi, Jun 15, 2018.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    code:
    PHP:
    public function giveKit(Player $player$kitName)
        {
            
    $config MainClass::getInstance()->kit;
            
    $kit $config->get($kitName);
            
    $items $kit["items"];
            if (isset(
    $items)) {
                foreach (
    $items as $i) {
                    
    $data explode(":"$i);
                    
    $item Item::get($data[0], $data[1], $data[2]);
                    if (isset(
    $data[3]) and isset($data[4])) {
                        
    $enchant Enchantment::getEnchantment($data[3]);
                        
    $item->addEnchantment(new EnchantmentInstance($enchant$data[4]));
                    }
                    if(isset(
    $data[5])){
                        
    $item->setCustomName(implode(" "$data[5]));
                    }
                    
    $player->getInventory()->addItem($item);
                }
                
    $sqlite MainClass::getInstance()->sqlite;
                
    $time time();
                
    $cd MainClass::getInstance()->kit->getNested($kitName.".cooldown");
                
    $cooldown $sqlite->prepare("INSERT INTO kitCooldowns (player, kit, cooldown) VALUES (:player, :kit, :cooldown);");
                
    $cooldown->bindValue(":player"$player->getName());
                
    $cooldown->bindValue(":cooldown"$time $cd);
                
    $cooldown->bindValue(":kit"$kitName);
                
    $cooldown->execute();
            }
        }

        public function 
    checkCooldown(Player $player)
        {
            
    $sqlite MainClass::getInstance()->sqlite;
            
    $info $sqlite->query("SELECT * FROM kitCooldowns;");
            
    $array $info->fetchArray(SQLITE3_ASSOC);
            if (!empty(
    $array)) {
                
    $banInfo $sqlite->query("SELECT * FROM kitCooldowns;");
                
    $i = -1;
                    
    $j $i 1;
                    
    $kitPlayer $array['player'];
                    if (
    $player->getName() == $kitPlayer) {
                        
    $banInfo $sqlite->query("SELECT * FROM kitCooldowns WHERE player = '$kitPlayer';");
                        
    $array $banInfo->fetchArray(SQLITE3_ASSOC);
                        if (!empty(
    $array)) {
                            
    $cooldown $array['cooldown'];
                            
    $kit $array['kit'];
                            
    $now time();
                            if (
    $cooldown $now) {
                                
    $remainingTime $cooldown $now;
                                
    $day floor($remainingTime 86400);
                                
    $hourSeconds $remainingTime 86400;
                                
    $hour floor($hourSeconds 3600);
                                
    $minuteSec $hourSeconds 3600;
                                
    $minute floor($minuteSec 60);
                                
    $remainingSec $minuteSec 60;
                                
    $second ceil($remainingSec);
                                
    $player->sendMessage(TextFormat::RED "Kit {$kit} is in cooldown for {$day} day(s) {$hour} hour(s) {$minute} minute(s) and {$second} seconds");
                            } else {
                                
    $banInfo $sqlite->query("SELECT * FROM kitCooldowns WHERE player = '$kitPlayer';");
                                
    $array $banInfo->fetchArray(SQLITE3_ASSOC);
                                if (!empty(
    $array)) {
                                    
    $sqlite->query("DELETE FROM kitCooldowns WHERE player = '$kitPlayer';");
                                }
                            }
                    }
                    
    $i $i 1;
                }
            }
        }
    creating sqlite
    PHP:
    $this->sqlite = new \SQLite3($this->getDataFolder() . "kits.db");
            
    $this->sqlite->exec("CREATE TABLE IF NOT EXISTS kitCooldowns(player TEXT, kit TEXT PRIMARY KEY, cooldown INTEGER);");
    onCommand
    //giving items
    PHP:
    $api = new API();
            
    $api->checkCooldown($sender);
            
    $api->giveKit($sender$kit);
    config
    Code:
    ---
    test:
      cooldown: 3600
      customName: Cool pickaxe
      items:
      - "278:0:1:15:10"
      - "307:0:1"
      - "364:0:15"
    ...
    error is when i do /kit test after the coooldown is set i get super fps lag and the server freeze
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Benchmark your sqlite3 statements
     
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    how
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    $t microtime(true);
    //code here
    $t microtime(true) - $t;
    var_dump(number_format($t10));//returns how many secs it took for code to execute
     
  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.