K_Unit.php 13 KB

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