Inventory.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. require_once($path["entity"] . "Entity.php");
  3. require_once($path["entity"] . "K_Unit.php");
  4. /**
  5. * An inventory item.
  6. *
  7. * Represents an object from the table 'inventory'.
  8. */
  9. class Inventory extends Entity{
  10. /**
  11. * Owner identifier.
  12. */
  13. public $uid;
  14. /**
  15. * Item identifier. If its a monster piece, a {@see K_Monster} entity.
  16. */
  17. public $id;
  18. /**
  19. * Item name.
  20. */
  21. public $name;
  22. /**
  23. * Item type name.
  24. */
  25. public $type;
  26. /**
  27. * Item type name.
  28. */
  29. public $ammount;
  30. /**
  31. * Indicates if the item are monster pieces.
  32. */
  33. public $is_monster_pices;
  34. /**
  35. * Constructor.
  36. *
  37. * Searches the database and retrieves the information about the
  38. * building, populating it and it's items.
  39. *
  40. * @param SQLite3 $db Connection to the database.
  41. * @param int $id Item identifier.
  42. * @param int $type Item type identifier. Optional, will try to guess if null.
  43. */
  44. public function __construct($db, $id, $type = null){
  45. global $path;
  46. global $INVENTORY_TYPE;
  47. parent::__construct($db);
  48. $s = "
  49. SELECT
  50. inventory.id AS id,
  51. k_inventory.name AS name,
  52. k_inventory.description AS description,
  53. inventory.type AS type_ref,
  54. k_inventory_type.name AS type,
  55. inventory.amount AS amount
  56. FROM
  57. inventory,
  58. k_inventory,
  59. k_inventory_type
  60. WHERE
  61. inventory.type = k_inventory_type.id AND
  62. inventory.id = k_inventory.id AND
  63. inventory.type = k_inventory.type AND
  64. inventory.id = $id
  65. ";
  66. if ($type != null){
  67. $s .= "AND inventory.type = $type";
  68. }
  69. $q = $this->db->query($s);
  70. $r = $q->fetchArray(SQLITE3_ASSOC);
  71. $this->type = $r["type"];
  72. $this->amount = $r["amount"];
  73. if ($r["type"] == $INVENTORY_TYPE["GUILD_MONSTER_PIECE"] || $r["type"] == $INVENTORY_TYPE["MONSTER_PIECE"]){
  74. $this->is_monster_pices = true;
  75. $this->id = new K_Unit($this->db, $r["id"]);
  76. $this->name = e($this->id->title) . " Pieces";
  77. $this->description = e($this->id->title) . " Pieces";
  78. }
  79. else{
  80. $this->is_monster_pices = false;
  81. $this->id = $r["id"];
  82. $this->name = e($r["name"]);
  83. $this->description = e($r["description"]);
  84. }
  85. }
  86. /**
  87. * Gets the path to the item image. The image must be in the
  88. * img/content/inventory/ directory, and its name must be the monster id,
  89. * padded with '0' to 9 digites, and the extension must be '.png'.
  90. *
  91. * @return path to the image, or a path to a fixed unknown image if it
  92. * doesn't exist.
  93. */
  94. public function get_image(){
  95. global $base_dir;
  96. global $path;
  97. if (file_exists($base_dir . "img/inventory/" . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png")){
  98. return $path["img"]["inventory"] . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png";
  99. }
  100. else{
  101. return $path["img"]["root"] . "unknown.png";
  102. }
  103. }
  104. }
  105. ?>