Inventory.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 Building identifier.
  42. */
  43. public function __construct($db, $id){
  44. global $path;
  45. global $INVENTORY_TYPE;
  46. parent::__construct($db);
  47. $s = "
  48. SELECT
  49. inventory.id AS id,
  50. k_inventory.name AS name,
  51. k_inventory.description AS description,
  52. inventory.type AS type_ref,
  53. k_inventory_type.name AS type,
  54. inventory.amount AS amount
  55. FROM
  56. inventory,
  57. k_inventory_type
  58. LEFT JOIN k_inventory ON
  59. k_inventory.id = inventory.id
  60. WHERE
  61. inventory.type = k_inventory_type.id AND
  62. inventory.id = $id;
  63. ";
  64. $q = $this->db->query($s);
  65. $r = $q->fetchArray(SQLITE3_ASSOC);
  66. $this->type = $r["type"];
  67. $this->amount = $r["amount"];
  68. if ($r["type"] == $INVENTORY_TYPE["GUILD_MONSTER_PIECE"] || $r["type"] == $INVENTORY_TYPE["MONSTER_PIECE"]){
  69. $this->is_monster_pices = true;
  70. $this->id = new K_Unit($this->db, $r["id"]);
  71. $this->name = $this->id->title . " Pieces";
  72. $this->description = $this->id->title . " Pieces";
  73. }
  74. else{
  75. $this->is_monster_pices = false;
  76. $this->id = $r["id"];
  77. $this->name = $r["name"];
  78. $this->description = $r["description"];
  79. }
  80. }
  81. /**
  82. * Gets the path to the item image. The image must be in the
  83. * img/content/inventory/ directory, and its name must be the monster id,
  84. * padded with '0' to 9 digites, and the extension must be '.png'.
  85. *
  86. * @return path to the image, or a path to a fixed unknown image if it
  87. * doesn't exist.
  88. */
  89. public function get_image(){
  90. global $base_dir;
  91. global $path;
  92. if (file_exists($base_dir . "img/inventory/" . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png")){
  93. return $path["img"]["inventory"] . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png";
  94. }
  95. else{
  96. return $path["img"]["root"] . "unknown.png";
  97. }
  98. }
  99. }
  100. ?>