K_Unit_Transformation.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Transformation 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 . "K_Skill.php");
  14. require_once(PATH::ENTITY . "K_Leader_Skill.php");
  15. require_once(PATH::ENTITY . "K_Source.php");
  16. /**
  17. * Unit transformation.
  18. *
  19. * Represents an object from the table 'k_unit_transformation'.
  20. *
  21. * @category Entity
  22. */
  23. class K_Unit_Transformation extends Entity{
  24. /**
  25. * @var int Transformed unit identifier.
  26. */
  27. public $id;
  28. /**
  29. * @var int Transformed unit family id.
  30. */
  31. public $family;
  32. /**
  33. * @var int ID of the unit that transformes into this.
  34. */
  35. public $unit;
  36. /**
  37. * @var \K_Monster_Skill[] Skills in the transformed form.
  38. */
  39. public $skill = [];
  40. /**
  41. * Constructor.
  42. *
  43. * Searches the database and retrieves the information about the
  44. * trnsformed unit, populating it and it's items.
  45. *
  46. * @param resource $db Connection to the database.
  47. * @param int $id TRansformed unit identifier.
  48. */
  49. public function __construct($db, $id){
  50. parent::__construct($db);
  51. $s = "
  52. SELECT
  53. id,
  54. family,
  55. unit
  56. FROM k_unit_transformation
  57. WHERE id = $id;
  58. ";
  59. $q = $this->db->query($s);
  60. $r = $q->fetchArray(SQLITE3_ASSOC);
  61. if ($r){
  62. $this->id = $r["id"];
  63. $this->family = $r["family"];
  64. $this->unit = $r["unit"];
  65. $this->load_skills();
  66. }
  67. }
  68. /**
  69. * Loads the information about the unit skills in the transformed form.
  70. */
  71. private function load_skills(){
  72. $this->skill = [];
  73. $s = "
  74. SELECT skill
  75. FROM
  76. k_skill,
  77. k_unit_skill
  78. WHERE
  79. id = skill AND
  80. unit = " . $this->id . "
  81. ORDER BY slot;
  82. ";
  83. $q = $this->db->query($s);
  84. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  85. array_push($this->skill, new K_Skill($this->db, $r["skill"]));
  86. }
  87. }
  88. /**
  89. * Gets the path to the unit image. The image must be in the
  90. * img/unit/ directory, and it's name must be the unit id,
  91. * padded with '0' to 8 digits, and the extension must be '.png'.
  92. *
  93. * @return string Image URL, or a fixed unknown image.
  94. */
  95. public function get_image(){
  96. if (file_exists(PATH::BASE . "img/unit/" . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png")){
  97. return URL::IMG["UNIT"] . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png";
  98. }
  99. else{
  100. return URL::IMG["UNKNOWN"];
  101. }
  102. }
  103. }
  104. ?>