Item.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Item entity file.
  4. *
  5. * Creates the entity and makes it available.
  6. *
  7. * @category Entity
  8. */
  9. /**
  10. * Require dependent entities if not present.
  11. */
  12. require_once(PATH::ENTITY . "Entity.php");
  13. require_once(PATH::ENTITY . "Monster.php");
  14. /**
  15. * An item in the players inventory inventory item.
  16. *
  17. * Represents an object from the table 'item'.
  18. *
  19. * @category Entity
  20. */
  21. class Item extends Entity{
  22. /**
  23. * @var string Owner's player identifier.
  24. */
  25. private $player;
  26. /**
  27. * @var int|Monster Item identifier. If its a monster piece, a Monster entity.
  28. */
  29. private $id;
  30. /**
  31. * @var string Item name.
  32. */
  33. private $name;
  34. /**
  35. * @var string Item type name.
  36. */
  37. private $type;
  38. /**
  39. * @var int Item owned amount.
  40. */
  41. private $amount;
  42. /**
  43. * @var bool Indicates if the item are monster pieces.
  44. */
  45. private $is_monster_pieces;
  46. /**
  47. * Constructor.
  48. *
  49. * Searches the database and retrieves the information about the
  50. * building, populating it and it's items.
  51. *
  52. * @param string $player Owner's player identifier.
  53. * @param int $id Item identifier.
  54. * @param int $type Item type identifier. Optional, will try to guess if null.
  55. */
  56. public function __construct($player, $id, $type = null){
  57. $s = "
  58. SELECT
  59. item.player AS player,
  60. item.id AS id,
  61. inventory.name AS name,
  62. inventory.description AS description,
  63. item.type AS type_ref,
  64. inventory_type.name AS type,
  65. item.amount AS amount
  66. FROM
  67. item,
  68. inventory,
  69. inventory_type
  70. WHERE
  71. item.player = :player AND
  72. item.id = inventory.id AND
  73. item.type = inventory.type AND
  74. inventory.type = inventory_type.id AND
  75. inventory.id = inventory.id AND
  76. inventory.type = inventory.type AND
  77. inventory.id = :id
  78. ";
  79. if ($type != null){
  80. $s .= "AND inventory.type = :type";
  81. }
  82. $statement = get_context()->get_db()->prepare($s);
  83. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  84. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  85. $statement->bindValue(':type', $type, SQLITE3_TEXT);
  86. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  87. if ($r){
  88. $this->player = $r["player"];
  89. $this->type = $r["type"];
  90. $this->amount = $r["amount"];
  91. if ($r["type"] == INVENTORY_TYPE_ID::GUILD_MONSTER_PIECE || $r["type"] == INVENTORY_TYPE_ID::MONSTER_PIECE){
  92. $this->is_monster_pieces = true;
  93. $this->id = new Monster($r["id"]);
  94. $this->name = HTML::e($this->id->get_title()) . " Pieces";
  95. $this->description = HTML::e($this->id->get_title()) . " Pieces";
  96. }
  97. else{
  98. $this->is_monster_pices = false;
  99. $this->id = $r["id"];
  100. $this->name = HTML::e($r["name"]);
  101. $this->description = HTML::e($r["description"]);
  102. }
  103. }
  104. }
  105. /**
  106. * Retrieves the owner's player identifier
  107. *
  108. * @return string Owner's ID.
  109. */
  110. public function get_player(){
  111. return $this->player;
  112. }
  113. /**
  114. * Retrieves the item identifier.
  115. * If the item is a monster piece, the unit instance will be returned instead
  116. *
  117. * @return int|Monster Item ID or monster instance
  118. */
  119. public function get_id(){
  120. return $this->id;
  121. }
  122. /**
  123. * Retrieves the item name.
  124. *
  125. * @return string Item name.
  126. */
  127. public function get_name(){
  128. return $this->name;
  129. }
  130. /**
  131. * Retrieves the item type.
  132. *
  133. * @return int Item type.
  134. */
  135. public function get_type(){
  136. return $this->type;
  137. }
  138. /**
  139. * Retrieves the owned amount of the item.
  140. *
  141. * @return int Owned amount.
  142. */
  143. public function get_amount(){
  144. return $this->amount;
  145. }
  146. /**
  147. * Checks if the item is a monste piece.
  148. *
  149. * @return boolean True for monster pieces, false otherwise.
  150. */
  151. public function is_monster_pieces(){
  152. return $this->is_monster_pieces;
  153. }
  154. /**
  155. * Gets the path to the item image.
  156. *
  157. * @return string Image URL, or a fixed unknown image.
  158. */
  159. public function get_image(){
  160. return APPLICATION::img("INVENTORY", $this->id);
  161. }
  162. }
  163. ?>