Item.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. inventory.type = inventory_type.id AND
  73. inventory.id = inventory.id AND
  74. inventory.type = inventory.type AND
  75. inventory.id = :id
  76. ";
  77. if ($type != null){
  78. $s .= "AND inventory.type = :type";
  79. }
  80. $statement = get_context()->get_db()->prepare($s);
  81. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  82. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  83. $statement->bindValue(':type', $type, SQLITE3_TEXT);
  84. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  85. if ($r){
  86. $this->player = $r["player"];
  87. $this->type = $r["type"];
  88. $this->amount = $r["amount"];
  89. if ($r["type"] == INVENTORY_TYPE_ID::GUILD_MONSTER_PIECE || $r["type"] == INVENTORY_TYPE_ID::MONSTER_PIECE){
  90. $this->is_monster_pieces = true;
  91. $this->id = new Monster($r["id"]);
  92. $this->name = HTML::e($this->id->get_title()) . " Pieces";
  93. $this->description = HTML::e($this->id->get_title()) . " Pieces";
  94. }
  95. else{
  96. $this->is_monster_pices = false;
  97. $this->id = $r["id"];
  98. $this->name = HTML::e($r["name"]);
  99. $this->description = HTML::e($r["description"]);
  100. }
  101. }
  102. }
  103. /**
  104. * Retrieves the owner's player identifier
  105. *
  106. * @return string Owner's ID.
  107. */
  108. public function get_player(){
  109. return $this->player;
  110. }
  111. /**
  112. * Retrieves the item identifier.
  113. * If the item is a monster piece, the unit instance will be returned instead
  114. *
  115. * @return int|Monster Item ID or monster instance
  116. */
  117. public function get_id(){
  118. return $this->id;
  119. }
  120. /**
  121. * Retrieves the item name.
  122. *
  123. * @return string Item name.
  124. */
  125. public function get_name(){
  126. return $this->name;
  127. }
  128. /**
  129. * Retrieves the item type.
  130. *
  131. * @return int Item type.
  132. */
  133. public function get_type(){
  134. return $this->type;
  135. }
  136. /**
  137. * Retrieves the owned amount of the item.
  138. *
  139. * @return int Owned amount.
  140. */
  141. public function get_amount(){
  142. return $this->amount;
  143. }
  144. /**
  145. * Checks if the item is a monste piece.
  146. *
  147. * @return boolean True for monster pieces, false otherwise.
  148. */
  149. public function is_monster_pieces(){
  150. return $this->is_monster_pieces;
  151. }
  152. /**
  153. * Gets the path to the item image.
  154. *
  155. * @return string Image URL, or a fixed unknown image.
  156. */
  157. public function get_image(){
  158. return APPLICATION::img("INVENTORY", $this->id);
  159. }
  160. }
  161. ?>