* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3 * @package SWDB */ require_once(PATH::ENTITY . "Entity.php"); require_once(PATH::ENTITY . "Monster.php"); /** * An item in the players inventory inventory item. * * Represents an object owned by the player in any amount. * * @category Entity */ class Item extends Entity{ /** * @var string Owner's player identifier. */ private $player; /** * @var int|Monster Item identifier. If its a monster piece, a Monster entity. */ 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; /** * Constructor. * * Searches the database and retrieves the information about the * building, populating it and it's items. * * @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. */ 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 item.id = inventory.id AND item.type = inventory.type 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"; } else{ $this->is_monster_pices = false; $this->id = $r["id"]; $this->name = HTML::e($r["name"]); $this->description = HTML::e($r["description"]); } } } /** * 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 bool 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); } }