K_Unit.php 12 KB

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