Inventory.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 type name.
  20. */
  21. public $type;
  22. /**
  23. * Item type name.
  24. */
  25. public $ammount;
  26. /**
  27. * Indicates if the item are monster pieces.
  28. */
  29. public $is_monster_pices;
  30. /**
  31. * Constructor.
  32. *
  33. * Searches the database and retrieves the information about the
  34. * building, populating it and it's items.
  35. *
  36. * @param SQLite3 $db Connection to the database.
  37. * @param int $id Building identifier.
  38. */
  39. public function __construct($db, $id){
  40. global $path;
  41. global $INVENTORY_TYPE;
  42. parent::__construct($db);
  43. $s = "
  44. SELECT
  45. inventory.id AS id,
  46. k_inventory.name AS name,
  47. k_inventory.description AS description,
  48. inventory.type AS type_ref,
  49. k_inventory_type.name AS type,
  50. inventory.amount AS amount
  51. FROM
  52. inventory,
  53. k_inventory_type
  54. LEFT JOIN k_inventory ON
  55. k_inventory.id = inventory.id
  56. WHERE
  57. inventory.type = k_inventory_type.id AND
  58. inventory.id = $id;
  59. ";
  60. $q = $this->db->query($s);
  61. $r = $q->fetchArray(SQLITE3_ASSOC);
  62. $this->type = $r["type"];
  63. $this->amount = $r["amount"];
  64. if ($r["type"] == $INVENTORY_TYPE["GUILD_MONSTER_PIECE"] || $r["type"] == $INVENTORY_TYPE["MONSTER_PIECE"]){
  65. $this->is_monster_pices = true;
  66. $this->id = new K_Unit($this->db, $r["id"]);
  67. $this->name = $this->id->title . " Pieces";
  68. $this->description = $this->id->title . " Pieces";
  69. }
  70. else{
  71. $this->is_monster_pices = false;
  72. $this->id = $r["id"];
  73. $this->name = $r["name"];
  74. $this->description = $r["description"];
  75. }
  76. }
  77. /**
  78. * Gets the path to the item image. The image must be in the
  79. * img/content/inventory/ directory, and its name must be the monster id,
  80. * padded with '0' to 9 digites, and the extension must be '.png'.
  81. *
  82. * @return path to the image, or a path to a fixed unknown image if it
  83. * doesn't exist.
  84. */
  85. public function get_image(){
  86. global $base_dir;
  87. global $path;
  88. if (file_exists($base_dir . "img/content/inventory/" . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png")){
  89. return $path["img"]["content"] . "inventory/" . str_pad($this->id, 9, '0', STR_PAD_LEFT) . ".png";
  90. }
  91. else{
  92. return $path["img"]["layout"] . "unknown.png";
  93. }
  94. }
  95. }
  96. ?>