Item.php 4.5 KB

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