فهرست منبع

More code encapsulation and optimization. Fixed some minor issues introduces with the latest changes.

Iñigo Valentin 5 سال پیش
والد
کامیت
c4e8d6d470
4فایلهای تغییر یافته به همراه272 افزوده شده و 180 حذف شده
  1. 102 68
      application/entity/Inventory.php
  2. 164 106
      application/entity/Item.php
  3. 4 4
      application/entity/Player.php
  4. 2 2
      application/helper/HTML_Helper.php

+ 102 - 68
application/entity/Inventory.php

@@ -1,57 +1,57 @@
 <?php
 <?php
+/**
+ * Inventory base entity file.
+ *
+ * Creates the entity and makes it available.
+ *
+ * @category Entity
+ */
+
+/**
+ * Require dependent entities if not present.
+ */
+require_once(PATH::ENTITY . "Entity.php");
+
+/**
+ * An in -game existing item.
+ *
+ * Represents an object from the table 'inventory'.
+ *
+ * @category Entity
+ */
+class Inventory extends Entity{
+    
     /**
     /**
-     * Inventory base entity file.
-     *
-     * Creates the entity and makes it available.
-     *
-     * @category Entity
+     * @var int Item identifier.
      */
      */
-
+    private $id;
+    
     /**
     /**
-     * Require dependent entities if not present.
+     * @var string Item name.
      */
      */
-    require_once(PATH::ENTITY . "Entity.php");
-
+    private $name;
+    
+    /**
+     * @var string Item type name.
+     */
+    private $type;
+    
+    /**
+     * @var string Item description.
+     */
+    private $description;
+    
     /**
     /**
-     * An inventory base item.
+     * Constructor.
      *
      *
-     * Represents an object from the table 'inventory'.
+     * Searches the database and retrieves the information about the
+     * building, populating it and it's items.
      *
      *
-     * @category Entity
+     * @param int $id Item identifier.
+     * @param int $type Item type identifier. Optional, will try to guess if null.
      */
      */
-    class Inventory extends Entity{
-
-        /**
-         * @var int Item identifier.
-         */
-        public $id;
-
-        /**
-         * @var string Item name.
-         */
-        public $name;
-
-        /**
-         * @var string Item type name.
-         */
-        public $type;
-
-        /**
-         * @var string Item description.
-         */
-        public $description;
-
-        /**
-         * Constructor.
-         *
-         * Searches the database and retrieves the information about the
-         * building, populating it and it's items.
-         *
-         * @param int $id Item identifier.
-         * @param int $type Item type identifier. Optional, will try to guess if null.
-         */
-        public function __construct($id, $type = null){
-            $s = "
+    public function __construct($id, $type = null){
+        $s = "
               SELECT
               SELECT
                 inventory.id AS id,
                 inventory.id AS id,
                 inventory.name AS name,
                 inventory.name AS name,
@@ -64,30 +64,64 @@
                 inventory.type = inventory_type.id AND
                 inventory.type = inventory_type.id AND
                 inventory.id = :id
                 inventory.id = :id
             ";
             ";
-            if ($type != null){
-                $s .= "AND inventory.type = :type";
-            }
-            $statement = get_context()->get_db()->prepare($s);
-            $statement->bindValue(':id', $id, SQLITE3_TEXT);
-            $statement->bindValue(':type', $type, SQLITE3_TEXT);
-            $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
-            if ($r){
-                $this->type = $r["type"];
-                $this->id = $r["id"];
-                $this->name = HTML::e($r["name"]);
-                $this->description = HTML::e($r["description"]);
-            }
+        if ($type != null){
+            $s .= "AND inventory.type = :type";
         }
         }
-
-        /**
-         * Gets the path to the item image. The image must be in the
-         * img/content/inventory/ directory, and its name must be the item id,
-         * padded with '0' to 9 digites, and the extension must be '.png'.
-         *
-         * @return string Image URL, or a fixed unknown image.
-         */
-        public function get_image(){
-            return APPLICATION::img("INVENTORY", $this->id);
+        $statement = get_context()->get_db()->prepare($s);
+        $statement->bindValue(':id', $id, SQLITE3_TEXT);
+        $statement->bindValue(':type', $type, SQLITE3_TEXT);
+        $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
+        if ($r){
+            $this->type = $r["type"];
+            $this->id = $r["id"];
+            $this->name = HTML::e($r["name"]);
+            $this->description = HTML::e($r["description"]);
         }
         }
     }
     }
+    
+    /**
+     * Retrieves the owned item identifier.
+     *
+     * @return int Item ID.
+     */
+    public function get_id(){
+        return $this->id;
+    }
+    
+    /**
+     * Retrieves the item name.
+     *
+     * @return string Item name.
+     */
+    public function get_name(){
+        return $this->name;
+    }
+    
+    /**
+     * Retrieves the item type.
+     *
+     * @return int Item type.
+     */
+    public function get_type(){
+        return $this->type;
+    }
+    
+    /**
+     * Retrieves the item description.
+     *
+     * @return string Item description.
+     */
+    public function get_description(){
+        return $this->description;
+    }
+    
+    /**
+     * Gets the path to the item image. 
+     *
+     * @return string Image URL, or a fixed unknown image.
+     */
+    public function get_image(){
+        return APPLICATION::img("INVENTORY", $this->id);
+    }
+}
 ?>
 ?>

+ 164 - 106
application/entity/Item.php

@@ -1,120 +1,178 @@
 <?php
 <?php
+/**
+ * Item entity file.
+ *
+ * Creates the entity and makes it available.
+ *
+ * @category Entity
+ */
+
+/**
+ * Require dependent entities if not present.
+ */
+require_once(PATH::ENTITY . "Entity.php");
+require_once(PATH::ENTITY . "Monster.php");
+
+/**
+ * An item in the players inventory inventory item.
+ *
+ * Represents an object from the table 'item'.
+ *
+ * @category Entity
+ */
+class Item extends Entity{
+    
     /**
     /**
-     * Item entity file.
-     *
-     * Creates the entity and makes it available.
-     *
-     * @category Entity
+     * @var string Owner's player identifier.
      */
      */
-
+    private $player;
+    
     /**
     /**
-     * Require dependent entities if not present.
+     * @var int|Monster Item identifier. If its a monster piece, a Monster entity.
      */
      */
-    require_once(PATH::ENTITY . "Entity.php");
-    require_once(PATH::ENTITY . "Monster.php");
-
+    private $id;
+    
+    /**
+     * @var string Item name.
+     */
+    private $name;
+    
+    /**
+     * @var string Item type name.
+     */
+    private $type;
+    
+    /**
+     * @var int Item owned amount.
+     */
+    private $amount;
+    
+    /**
+     * @var bool Indicates if the item are monster pieces.
+     */
+    private $is_monster_pieces;
+    
     /**
     /**
-     * An inventory item.
+     * Constructor.
      *
      *
-     * Represents an object from the table 'item'.
+     * Searches the database and retrieves the information about the
+     * building, populating it and it's items.
      *
      *
-     * @category Entity 
+     * @param string $player Owner's player identifier.
+     * @param int $id Item identifier.
+     * @param int $type Item type identifier. Optional, will try to guess if null.
      */
      */
-    class Item extends Entity{
-
-        /**
-         * @var int Owner's player identifier.
-         */
-        public $player;
-
-        /**
-         * @var int|Monster Item identifier. If its a monster piece, a Monster entity.
-         */
-        public $id;
-
-        /**
-         * @var string Item name.
-         */
-        public $name;
-
-        /**
-         * @var string Item type name.
-         */
-        public $type;
-
-        /**
-         * @var int Item owned amount.
-         */
-        public $amount;
-
-        /**
-         * @var bool Indicates if the item are monster pieces.
-         */
-        public $is_monster_pices;
-
-        /**
-         * Constructor.
-         *
-         * Searches the database and retrieves the information about the
-         * building, populating it and it's items.
-         *
-         * @param int $id Item identifier.
-         * @param int $type Item type identifier. Optional, will try to guess if null.
-         */
-        public function __construct($id, $type = null){
-
-            $s = "
-                SELECT
-                  item.id AS id,
-                  inventory.name AS name,
-                  inventory.description AS description,
-                  item.type AS type_ref,
-                  inventory_type.name AS type,
-                  item.amount AS amount
-                FROM
-                  item,
-                  inventory,
-                  inventory_type
-                WHERE
-                  inventory.type = inventory_type.id AND
-                  inventory.id = inventory.id AND
-                  inventory.type = inventory.type AND
-                  inventory.id = :id
-            ";
-            if ($type != null){
-                $s .= "AND inventory.type = :type";
+    public function __construct($player, $id, $type = null){
+        
+        $s = "
+          SELECT
+            item.player AS player,
+            item.id AS id,
+            inventory.name AS name,
+            inventory.description AS description,
+            item.type AS type_ref,
+            inventory_type.name AS type,
+            item.amount AS amount
+          FROM
+            item,
+            inventory,
+            inventory_type
+          WHERE
+            item.player = :player AND
+            inventory.type = inventory_type.id AND
+            inventory.id = inventory.id AND
+            inventory.type = inventory.type AND
+            inventory.id = :id
+        ";
+        if ($type != null){
+            $s .= "AND inventory.type = :type";
+        }
+        $statement = get_context()->get_db()->prepare($s);
+        $statement->bindValue(':player', $player, SQLITE3_TEXT);
+        $statement->bindValue(':id', $id, SQLITE3_TEXT);
+        $statement->bindValue(':type', $type, SQLITE3_TEXT);
+        $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
+        if ($r){
+            $this->player = $r["player"];
+            $this->type = $r["type"];
+            $this->amount = $r["amount"];
+            if ($r["type"] == INVENTORY_TYPE_ID::GUILD_MONSTER_PIECE || $r["type"] == INVENTORY_TYPE_ID::MONSTER_PIECE){
+                $this->is_monster_pieces = true;
+                $this->id = new Monster($r["id"]);
+                $this->name = HTML::e($this->id->get_title()) . " Pieces";
+                $this->description = HTML::e($this->id->get_title()) . " Pieces";
             }
             }
-            $statement = get_context()->get_db()->prepare($s);
-            $statement->bindValue(':id', $id, SQLITE3_TEXT);
-            $statement->bindValue(':type', $type, SQLITE3_TEXT);
-            $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
-            if ($r){
-                $this->type = $r["type"];
-                $this->amount = $r["amount"];
-                if ($r["type"] == INVENTORY_TYPE_ID::GUILD_MONSTER_PIECE || $r["type"] == INVENTORY_TYPE_ID::MONSTER_PIECE){
-                    $this->is_monster_pices = true;
-                    $this->id = new Monster($r["id"]);
-                    $this->name = HTML::e($this->id->title) . " Pieces";
-                    $this->description = HTML::e($this->id->title) . " Pieces";
-                }
-                else{
-                    $this->is_monster_pices = false;
-                    $this->id = $r["id"];
-                    $this->name = HTML::e($r["name"]);
-                    $this->description = HTML::e($r["description"]);
-                }
+            else{
+                $this->is_monster_pices = false;
+                $this->id = $r["id"];
+                $this->name = HTML::e($r["name"]);
+                $this->description = HTML::e($r["description"]);
             }
             }
         }
         }
-
-        /**
-         * Gets the path to the item image. The image must be in the
-         * img/content/inventory/ directory, and its name must be the item id,
-         * padded with '0' to 9 digites, and the extension must be '.png'.
-         *
-         * @return string Image URL, or a fixed unknown image.
-         */
-        public function get_image(){
-            return APPLICATION::img("INVENTORY", $this->id);
-        }
     }
     }
+    
+    /**
+     * Retrieves the owner's player identifier
+     *
+     * @return string Owner's ID.
+     */
+    public function get_player(){
+        return $this->player;
+    }
+    
+    /**
+     * Retrieves the item identifier.
+     * If the item is a monster piece, the unit instance will be returned instead
+     *
+     * @return int|Monster Item ID or monster instance
+     */
+    public function get_id(){
+        return $this->id;
+    }
+    
+    /**
+     * Retrieves the item name.
+     *
+     * @return string Item name.
+     */
+    public function get_name(){
+        return $this->name;
+    }
+    
+    /**
+     * Retrieves the item type.
+     *
+     * @return int Item type.
+     */
+    public function get_type(){
+        return $this->type;
+    }
+    
+    /**
+     * Retrieves the owned amount of the item.
+     *
+     * @return int Owned amount.
+     */
+    public function get_amount(){
+        return $this->amount;
+    }
+    
+    /**
+     * Checks if the item is a monste piece.
+     *
+     * @return boolean True for monster pieces, false otherwise.
+     */
+    public function is_monster_pieces(){
+        return $this->is_monster_pieces;
+    }
+    
+    /**
+     * Gets the path to the item image. 
+     *
+     * @return string Image URL, or a fixed unknown image.
+     */
+    public function get_image(){
+        return APPLICATION::img("INVENTORY", $this->id);
+    }
+}
 ?>
 ?>

+ 4 - 4
application/entity/Player.php

@@ -259,7 +259,7 @@
             $statement->bindValue(':type', INVENTORY_TYPE_ID::SCROLL, SQLITE3_TEXT);
             $statement->bindValue(':type', INVENTORY_TYPE_ID::SCROLL, SQLITE3_TEXT);
             $q = $statement->execute();
             $q = $statement->execute();
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
-                array_push($this->item_scroll, new item($r["id"], $r["type"]));
+                array_push($this->item_scroll, new Item($this->id, $r["id"], $r["type"]));
             }
             }
             // Rune crafting items are marked as generic crafting.
             // Rune crafting items are marked as generic crafting.
             $statement = get_context()->get_db()->prepare("
             $statement = get_context()->get_db()->prepare("
@@ -277,7 +277,7 @@
             $statement->bindValue(':type', INVENTORY_TYPE_ID::ENCHANTMENT, SQLITE3_TEXT);
             $statement->bindValue(':type', INVENTORY_TYPE_ID::ENCHANTMENT, SQLITE3_TEXT);
             $q = $statement->execute();
             $q = $statement->execute();
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
-                array_push($this->item_craft_rune, new item($r["id"], $r["type"]));
+                array_push($this->item_craft_rune, new Item($this->id, $r["id"], $r["type"]));
             }
             }
             $statement = get_context()->get_db()->prepare("
             $statement = get_context()->get_db()->prepare("
                 SELECT
                 SELECT
@@ -294,7 +294,7 @@
             $statement->bindValue(':type', INVENTORY_TYPE_ID::CRAFT_STUFF, SQLITE3_TEXT);
             $statement->bindValue(':type', INVENTORY_TYPE_ID::CRAFT_STUFF, SQLITE3_TEXT);
             $q = $statement->execute();
             $q = $statement->execute();
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
-                array_push($this->item_craft, new item($r["id"], $r["type"]));
+                array_push($this->item_craft, new Item($this->id, $r["id"], $r["type"]));
             }
             }
             $statement = get_context()->get_db()->prepare("
             $statement = get_context()->get_db()->prepare("
                 SELECT
                 SELECT
@@ -325,7 +325,7 @@
             $statement->bindValue(':type', INVENTORY_TYPE_ID::ESSENCES, SQLITE3_TEXT);
             $statement->bindValue(':type', INVENTORY_TYPE_ID::ESSENCES, SQLITE3_TEXT);
             $q = $statement->execute();
             $q = $statement->execute();
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
             while ($r = $q->fetchArray(SQLITE3_ASSOC)){
-                array_push($this->item_essence, new item($r["id"], $r["type"]));
+                array_push($this->item_essence, new Item($this->id, $r["id"], $r["type"]));
             }
             }
             $this->flag_item = true;
             $this->flag_item = true;
         }
         }

+ 2 - 2
application/helper/HTML_Helper.php

@@ -614,9 +614,9 @@
                 return "";
                 return "";
             }
             }
             $html = "<div class='item_panel'>\n";
             $html = "<div class='item_panel'>\n";
-            $html .= "<img title='" . $item->name . "' class='item' src='" . $item->get_image() . "'/>\n";
+            $html .= "<img title='" . $item->get_name() . "' class='item' src='" . $item->get_image() . "'/>\n";
             $html .= "<span class='amount'>\n";
             $html .= "<span class='amount'>\n";
-            $html .= $item->amount . "\n";
+            $html .= $item->get_amount() . "\n";
             $html .= "</span>\n";
             $html .= "</span>\n";
             $html .= "</div>\n";
             $html .= "</div>\n";
             return $html;
             return $html;