Advertisement
Guest User

Untitled

a guest
May 24th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.04 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Titus Louage
  5.  * Date: 08-May-18
  6.  * Time: 11:31
  7.  */
  8.  
  9. class Hearthstonedb
  10. {
  11.     private static $hearthstone = null;
  12.  
  13.     private $db;
  14.  
  15.     private function __construct()
  16.     {
  17.         try
  18.         {
  19.             $config = Config::getConfigInstantie();
  20.             $server = $config->getServer();
  21.             $database = $config->getDatabase();
  22.             $username = $config->getUsername();
  23.             $password = $config->getPassword();
  24.  
  25.             $this->db = new PDO("mysql:host=$server; dbname=$database; charset=utf8mb4",
  26.                 $username,
  27.                 $password,
  28.                 array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
  29.         }
  30.         catch (PDOException $e)
  31.         {
  32.             die($e->getMessage());
  33.         }
  34.     }
  35.  
  36.     public static function getHearthstonedb()
  37.     {
  38.         if(is_null(self::$hearthstone))
  39.         {
  40.             self::$hearthstone = new Hearthstonedb();
  41.         }
  42.         return self::$hearthstone;
  43.     }
  44.  
  45.     public function updatebd()
  46.     {
  47.         $jsondata = file_get_contents('minions.json');
  48.         $obj = json_decode($jsondata);
  49.         $i = 0;
  50.  
  51.         foreach ($obj as $card){
  52.             $i++;
  53.  
  54.             if (isset($card->dbfId)){
  55.                 $id = $card->dbfId;
  56.             }else{
  57.                 $id = null;
  58.             }
  59.             if(isset($card->name)){
  60.                 $name = $card->name;
  61.             }else{
  62.                 $name = null;
  63.             }
  64.             if(isset($card->cardSet)){
  65.                 $set = $card->cardSet;
  66.             }else{
  67.                 $set = null;
  68.             }
  69.             if(isset($card->type)){
  70.                 $type = $card->type;
  71.             }else{
  72.                 $type = null;
  73.             }
  74.             if (isset($card->text)){
  75.                 $text = $card->text;
  76.             }else{
  77.                 $text = null;
  78.             }
  79.             if(isset($card->cost)){
  80.                 $cost = $card->cost;
  81.             }else{
  82.                 $cost = 0;
  83.             }
  84.             if(isset($card->attack)){
  85.                 $attack = $card->attack;
  86.             }else{
  87.                 $attack = null;
  88.             }
  89.             if(isset($card->health)){
  90.                 $health = $card->health;
  91.             }else{
  92.                 $health = null;
  93.             }
  94.             if(isset($card->playerClass)){
  95.                 $class = $card->playerClass;
  96.             }else{
  97.                 $class = 'neutral';
  98.             }
  99.             if(isset($card->img)){
  100.                 $img = $card->img;
  101.             }else{
  102.                 $img = null;
  103.             }
  104.  
  105.             $battlecry = 0;
  106.             $charge = 0;
  107.             $deathrattle = 0;
  108.             $divineShield = 0;
  109.             $poison = 0;
  110.             $stealth = 0;
  111.             $taunt = 0;
  112.             $spellDamage = 0;
  113.             $windfury = 0;
  114.             $aura = 0;
  115.             $silence = 0;
  116.             $adjacentBuff = 0;
  117.             if (isset($card->mechanics)){
  118.                 $mechanics = $card->mechanics;
  119.                 foreach ($mechanics as $mechanic){
  120.                     switch ($mechanic->name){
  121.                         case 'Battlecry':
  122.                             $battlecry = 1;
  123.                             break;
  124.                         case 'Charge':
  125.                             $charge = 1;
  126.                             break;
  127.                         case 'Deathrattle':
  128.                             $deathrattle = 1;
  129.                             break;
  130.                         case 'Divine Shield':
  131.                             $divineShield = 1;
  132.                             break;
  133.                         case 'Poisonous':
  134.                             $poison = 1;
  135.                             break;
  136.                         case 'Stealth':
  137.                             $stealth = 1;
  138.                             break;
  139.                         case 'Taunt':
  140.                             $taunt = 1;
  141.                             break;
  142.                         case 'Spell Damage':
  143.                             $spellDamage = 1;
  144.                             break;
  145.                         case 'Windfury':
  146.                             $windfury = 1;
  147.                             break;
  148.                         case 'Aura':
  149.                             $aura = 1;
  150.                             break;
  151.                         case 'Silence':
  152.                             $silence = 1;
  153.                             break;
  154.                         case 'AdjacentBuff':
  155.                             $adjacentBuff = 1;
  156.                             break;
  157.                         default:
  158.                             var_dump('piemel');
  159.                             var_dump($mechanic);
  160.                             break;
  161.                     }
  162.                 }
  163.             }
  164.  
  165.             //var_dump("INSERT INTO cards VALUES (".$id.", '".$name."', '".$set."', '".$type."', '".$text."', ".$cost.", ".$attack.", ".$health.", '".$class."', '".$img."', FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE);");
  166.  
  167.             try {
  168.                 $sql = "INSERT INTO cards VALUES (:id, :name, :set, :type, :text, :cost, :attack, :health, :class, :img);INSERT INTO mechanics VALUES (:id, $aura, $adjacentBuff, $battlecry, $charge, $deathrattle, $divineShield, $poison, $stealth, $taunt, $spellDamage, $silence, $windfury)";
  169.                 var_dump($sql);
  170.                 $stmt = $this->db->prepare($sql);
  171.  
  172.                 $stmt->bindParam(':id', $id);
  173.                 $stmt->bindParam(':name', $name);
  174.                 $stmt->bindParam(':set', $set);
  175.                 $stmt->bindParam(':type', $type);
  176.                 $stmt->bindParam(':text', $text);
  177.                 $stmt->bindParam(':cost', $cost);
  178.                 $stmt->bindParam(':attack', $attack);
  179.                 $stmt->bindParam(':health', $health);
  180.                 $stmt->bindParam(':class', $class);
  181.                 $stmt->bindParam(':img', $img);
  182.  
  183.                 $stmt->execute();
  184.             }catch (PDOException $e){
  185.             }
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement