Monster_Transformation.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Transformation 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 . "Skill.php");
  13. require_once(PATH::ENTITY . "Leader_Skill.php");
  14. require_once(PATH::ENTITY . "Source.php");
  15. /**
  16. * Monster transformation.
  17. *
  18. * Represents a transformation that a monster can achive in battle.
  19. *
  20. * @category Entity
  21. */
  22. class Monster_Transformation extends Entity{
  23. /**
  24. * @var int Transformed monster identifier.
  25. */
  26. private $id;
  27. /**
  28. * @var int Transformed monster family id.
  29. */
  30. private $family;
  31. /**
  32. * @var int ID of the monster that transformes into this.
  33. */
  34. private $monster;
  35. /**
  36. * @var bool Internal flag to check if the skills have been loaded into the entity.
  37. */
  38. private $flag_skills = false;
  39. /**
  40. * @var Skill[] Skills in the transformed form.
  41. */
  42. private $skill = [];
  43. /**
  44. * Constructor.
  45. *
  46. * Searches the database and retrieves the information about the
  47. * trnsformed monster, populating it and it's items.
  48. *
  49. * @param int $id Transformed monster identifier.
  50. */
  51. public function __construct($id){
  52. $statement = get_context()->get_db()->prepare("
  53. SELECT
  54. id,
  55. family,
  56. monster
  57. FROM monster_transformation
  58. WHERE id = :id;
  59. ");
  60. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  61. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  62. if ($r){
  63. $this->id = $r["id"];
  64. $this->family = $r["family"];
  65. $this->monster = $r["monster"];
  66. }
  67. }
  68. /**
  69. * Loads the information about the monster skills in the transformed form.
  70. *
  71. * Must be called only once before trying to get the skills. Sets the flag
  72. * {@link Monster_Transformation:$flag_skills} to true.
  73. */
  74. private function load_skills(){
  75. $this->skill = [];
  76. $statement = get_context()->get_db()->prepare("
  77. SELECT skill
  78. FROM
  79. skill,
  80. monster_skill
  81. WHERE
  82. id = skill AND
  83. monster = :id
  84. ORDER BY slot;
  85. ");
  86. $statement->bindValue(':id', $this->id, SQLITE3_TEXT);
  87. $q = $statement->execute();
  88. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  89. array_push($this->skill, new Skill($r["skill"]));
  90. }
  91. $this->flag_skills = true;
  92. }
  93. /**
  94. * Retrieves the identifier of the transformed monster.
  95. *
  96. * @return string Trasnformed monster ID.
  97. */
  98. public function get_id(){
  99. return $this->id;
  100. }
  101. /**
  102. * Retrieves the transofmred monster family identifier.
  103. *
  104. * @return int Family ID.
  105. */
  106. public function get_family(){
  107. return $this->family;
  108. }
  109. /**
  110. * Retrieves the identifier of the monster that transforms into this one.
  111. *
  112. * @return int Base monster ID.
  113. */
  114. public function get_base_id(){
  115. return $this->monster;
  116. }
  117. /**
  118. * Retrieves the skills of the transformed monster.
  119. *
  120. * @return Skill[] Array of skills.
  121. */
  122. public function get_skills(){
  123. if (! $this->flag_skills){
  124. $this->load_skills();
  125. }
  126. return $this->skill;
  127. }
  128. /**
  129. * Gets the path to the monster image.
  130. *
  131. * @return string Image URL, or a fixed unknown image.
  132. */
  133. public function get_image(){
  134. return APPLICATION::img("UNIT", $this->id);
  135. }
  136. }