K_Unit.php.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. /**
  3. * Base unit 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. require_once(PATH::ENTITY . "K_Unit_Transformation.php");
  17. /**
  18. * A unit.
  19. *
  20. * Represents an object from the table 'k_unit'.
  21. *
  22. * @category Entity
  23. */
  24. class K_Unit extends Entity{
  25. /**
  26. * @var int Unit identifier.
  27. */
  28. public $id;
  29. /**
  30. * @var int Unit family id.
  31. */
  32. public $family;
  33. /**
  34. * @var string Unit name.
  35. */
  36. public $name;
  37. /**
  38. * @var int Unit element id.
  39. */
  40. public $element;
  41. /**
  42. * @var string Element name (title case)
  43. */
  44. public $element_name;
  45. /**
  46. * @var int Unit archetype id.
  47. */
  48. public $archetype;
  49. /**
  50. * @var string Archetype name (title case).
  51. */
  52. public $archetype_name;
  53. /**
  54. * @var int Default star level.
  55. */
  56. public $base_stars;
  57. /**
  58. *@var int Default star level.
  59. */
  60. public $natural_stars;
  61. /**
  62. * @var bool Indicates if the unit is obtainable.
  63. */
  64. public $obtainable;
  65. /**
  66. * @var bool Indicates if the unit can be awakened.
  67. */
  68. public $can_awaken;
  69. /**
  70. * @var string Bonus when awakening.
  71. */
  72. public $awaken_bonus;
  73. /**
  74. * @var int Required skill-ups.
  75. */
  76. public $skill_ups_to_max;
  77. /**
  78. * @var K_Leader_Skill Unit's leader skill.
  79. */
  80. public $leader_skill;
  81. /**
  82. * @var int Base HP.
  83. */
  84. public $hp;
  85. /**
  86. * @var int Base attack.
  87. */
  88. public $atk;
  89. /**
  90. * @var int Base defense.
  91. */
  92. public $defense;
  93. /**
  94. * @var int Speed.
  95. */
  96. public $speed;
  97. /**
  98. * @var int Critical rate.
  99. */
  100. public $crit_rate;
  101. /**
  102. * @var int Critical damage.
  103. */
  104. public $crit_damage;
  105. /**
  106. * @var int Resistance
  107. */
  108. public $resistance;
  109. /**
  110. * @var int Accuracy.
  111. */
  112. public $accuracy;
  113. /**
  114. * @var int Raw HP.
  115. */
  116. public $raw_hp;
  117. /**
  118. * @var int Raw attack.
  119. */
  120. public $raw_atk;
  121. /**
  122. * @var int Raw Defense.
  123. */
  124. public $raw_defense;
  125. /**
  126. * @var int HP at max_level.
  127. */
  128. public $max_lvl_hp;
  129. /**
  130. * @var int Attack at max_level.
  131. */
  132. public $max_lvl_atk;
  133. /**
  134. * @var int Defense at max_level.
  135. */
  136. public $max_lvl_defense;
  137. /**
  138. * @var K_Unit Unit it awakens from.
  139. */
  140. public $awakens_from;
  141. /**
  142. * @var K_Unit Unit it awakens to.
  143. */
  144. public $awakens_to;
  145. /**
  146. * @var bool Indicates if the unit is a fusion ingredient.
  147. */
  148. public $fusion_food;
  149. /**
  150. * @var bool Indicates if the unit is homunculus.
  151. */
  152. public $homunculus;
  153. /**
  154. * @var int The crafting cost (homunculus only).
  155. */
  156. public $craft_cost;
  157. /**
  158. * @var \K_Unit_Skill[] Unit skills.
  159. */
  160. public $skill = [];
  161. /**
  162. * @var mixed[] Required essences to awaken the unit.
  163. *
  164. * Formed by arrays of:
  165. * [
  166. * "element" => "",
  167. * "grade" => "",
  168. * "amount" =>
  169. * ]
  170. */
  171. public $essences = [];
  172. /**
  173. * @var int \K_Source[] Sources the unit can be get from.
  174. */
  175. public $sources = [];
  176. /**
  177. * @var string Unit title.
  178. * If awakened, it will be the awakened from name.
  179. * If unawakened, it will be <element> <name>.
  180. */
  181. public $title;
  182. /**
  183. * @var K_Unit_Transformation The transformation the unit can undergo.
  184. */
  185. public $transformation;
  186. /**
  187. * Constructor.
  188. *
  189. * Searches the database and retrieves the information about the
  190. * unit, populating it and it's items.
  191. *
  192. * @param resouce $db Connection to the database.
  193. * @param int $id Unit identifier.
  194. * @param bool $complete If false, query only k_unit.
  195. */
  196. public function __construct($db, $id, $complete = true){
  197. parent::__construct($db);
  198. $s =
  199. "SELECT " .
  200. " id, " .
  201. " family, " .
  202. " name, " .
  203. " element, " .
  204. " archetype, " .
  205. " base_stars, " .
  206. " natural_stars, " .
  207. " obtainable, " .
  208. " can_awaken, " .
  209. " awaken_bonus, " .
  210. " skill_ups_to_max, " .
  211. " leader_skill, " .
  212. " hp, " .
  213. " attack, " .
  214. " defense, " .
  215. " speed, " .
  216. " crit_rate, " .
  217. " crit_damage, " .
  218. " resistance, " .
  219. " accuracy, " .
  220. " raw_hp, " .
  221. " raw_attack, " .
  222. " raw_defense, " .
  223. " max_lvl_hp, " .
  224. " max_lvl_attack, " .
  225. " max_lvl_defense, " .
  226. " awakens_from, " .
  227. " awakens_to, " .
  228. " fusion_food, " .
  229. " homunculus, " .
  230. " craft_cost " .
  231. "FROM k_unit " .
  232. "WHERE id = $id; ";
  233. $q = $this->db->query($s);
  234. $r = $q->fetchArray(SQLITE3_ASSOC);
  235. if ($r){
  236. $this->id = $r["id"];
  237. $this->family = $r["family"];
  238. $this->name = HTML::e($r["name"]);
  239. $this->element = $r["element"];
  240. switch($this->element){
  241. case ELEMENT_ID::WATER:
  242. $this->element_name = "Water";
  243. break;
  244. case ELEMENT_ID::FIRE:
  245. $this->element_name = "Fire";
  246. break;
  247. case ELEMENT_ID::WIND:
  248. $this->element_name = "Wind";
  249. break;
  250. case ELEMENT_ID::LIGHT:
  251. $this->element_name = "Light";
  252. break;
  253. case ELEMENT_ID::DARK:
  254. $this->element_name = "Dark";
  255. break;
  256. }
  257. $this->archetype = $r["archetype"];
  258. switch($this->archetype){
  259. case ARCHETYPE_ID::ATTACK:
  260. $this->archetype_name = "Attack";
  261. break;
  262. case ARCHETYPE_ID::DEFENSE:
  263. $this->archetype_name = "Defense";
  264. break;
  265. case ARCHETYPE_ID::HP:
  266. $this->archetype_name = "HP";
  267. break;
  268. case ARCHETYPE_ID::SUPPORT:
  269. $this->archetype_name = "Support";
  270. break;
  271. case ARCHETYPE_ID::MATERIAL:
  272. $this->archetype_name = "Material";
  273. break;
  274. }
  275. $this->base_stars = $r["base_stars"];
  276. $this->natural_stars = $r["natural_stars"];
  277. $this->obtainable = $r["obtainable"];
  278. $this->can_awaken = $r["can_awaken"];
  279. $this->awaken_bonus = HTML::e($r["awaken_bonus"]);
  280. $this->skill_ups_to_max = $r["skill_ups_to_max"];
  281. if (strlen($r["leader_skill"]) > 0){
  282. $this->leader_skill = new K_Leader_Skill($this->db, $r["leader_skill"]);
  283. }
  284. $this->hp = $r["hp"];
  285. $this->attack = $r["attack"];
  286. $this->defense = $r["defense"];
  287. $this->speed = $r["speed"];
  288. $this->crit_rate = $r["crit_rate"];
  289. $this->crit_damage = $r["crit_damage"];
  290. $this->resistance = $r["resistance"];
  291. $this->accuracy = $r["accuracy"];
  292. $this->raw_hp = $r["raw_hp"];
  293. $this->raw_attack = $r["raw_attack"];
  294. $this->raw_defense = $r["raw_defense"];
  295. $this->max_lvl_hp = $r["max_lvl_hp"];
  296. $this->max_lvl_attack = $r["max_lvl_attack"];
  297. $this->max_lvl_defense = $r["max_lvl_defense"];
  298. $this->awakens_from = $r["awakens_from"];
  299. $this->awakens_to = $r["awakens_to"];
  300. $this->fusion_food = $r["fusion_food"];
  301. $this->homunculus = $r["homunculus"];
  302. $this->craft_cost = $r["craft_cost"];
  303. }
  304. if ($complete){
  305. $this->load_essences();
  306. $this->load_skills();
  307. $this->load_sources();
  308. $this->load_transformation();
  309. }
  310. // No to, no from. Silver unit.
  311. if ($this->awakens_from == "" && $this->awakens_to == ""){
  312. $this->title = $this->element_name . " " . $this->name;
  313. }
  314. // Awakened
  315. elseif ($this->awakens_from != ""){
  316. $this->title = $this->name;
  317. }
  318. // Gold
  319. elseif (strlen($this->awakens_to) > 0){
  320. $this->title = $this->element_name . " " . $this->name;
  321. }
  322. }
  323. /**
  324. * Loads the information about the essences to awake the unit.
  325. *
  326. * It is autoatically called at construction time if the
  327. * $complete parameter is true (by default). No point in calling it
  328. * twice.
  329. */
  330. public function load_essences(){
  331. $this->essences = [];
  332. $s =
  333. "SELECT " .
  334. " element, " .
  335. " grade, " .
  336. " amount " .
  337. "FROM k_unit_essence " .
  338. "WHERE " .
  339. " unit = " . $this->id . " " .
  340. "ORDER BY " .
  341. " element <> 'Magic', " .
  342. " grade <> 'Low', " .
  343. " grade <> 'Mid'; ";
  344. $q = $this->db->query($s);
  345. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  346. $e = [
  347. "element" => $r["element"],
  348. "grade" => $r["grade"],
  349. "amount" => $r["amount"]
  350. ];
  351. array_push($this->essences, $e);
  352. }
  353. }
  354. /**
  355. * Loads the unit transformation.
  356. *
  357. * It is automatically called at construction time if the
  358. * $complete parameter is true (by default). No point in calling it
  359. * twice.
  360. */
  361. public function load_transformation(){
  362. $this->transformation = null;
  363. $s = "
  364. SELECT id
  365. FROM k_unit_transformation
  366. WHERE unit = " . $this->id . ";";
  367. $q = $this->db->query($s);
  368. $r = $q->fetchArray(SQLITE3_ASSOC);
  369. if ($r){
  370. $this->transformation = new K_Unit_Transformation($this->db, $r["id"]);
  371. }
  372. }
  373. /**
  374. * Loads the information about the unit skills.
  375. *
  376. * It is autoatically called at construction time if the
  377. * $complete parameter is true (by default). No point in calling it
  378. * twice.
  379. */
  380. public function load_skills(){
  381. $this->skill = [];
  382. $s = "
  383. SELECT skill
  384. FROM
  385. k_skill,
  386. k_unit_skill
  387. WHERE
  388. id = skill AND
  389. unit = " . $this->id . "
  390. ORDER BY slot;
  391. ";
  392. $q = $this->db->query($s);
  393. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  394. array_push($this->skill, new K_Skill($this->db, $r["skill"]));
  395. }
  396. }
  397. /**
  398. * Loads the information about the unit sources.
  399. *
  400. * It is autoatically called at construction time if the
  401. * $complete parameter is true (by default). No point in calling it
  402. * twice.
  403. */
  404. public function load_sources(){
  405. $this->sources = [];
  406. $s =
  407. "SELECT source " .
  408. "FROM k_unit_source " .
  409. "WHERE unit = " . $this->id . ";";
  410. $q = $this->db->query($s);
  411. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  412. array_push($this->sources, new K_Source($this->db, $r["source"]));
  413. }
  414. }
  415. /**
  416. * Gets the path to the unit image. The image must be in the
  417. * img/unit/ directory, and it's name must be the unit id,
  418. * padded with '0' to 8 digits, and the extension must be '.png'.
  419. *
  420. * @return string Image URL, or a fixed unknown image.
  421. */
  422. public function get_image(){
  423. if (file_exists(PATH::BASE . "img/unit/" . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png")){
  424. return URL::IMG["UNIT"] . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png";
  425. }
  426. else{
  427. return URL::IMG["UNKNOWN"];
  428. }
  429. }
  430. }
  431. ?>