K_Unit_Transformation.php 3.1 KB

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