K_Unit.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. * Constructor.
  163. *
  164. * Searches the database and retrieves the information about the
  165. * monster, populating it and it's items.
  166. *
  167. * @param SQLite3 $db Connection to the database.
  168. * @param int $id Monster identifier.
  169. * @param complete If false, query only k_monster.
  170. */
  171. public function __construct($db, $id, $complete = true){
  172. global $path;
  173. global $ELEMENT;
  174. global $ARCHETYPE;
  175. parent::__construct($db);
  176. $s =
  177. "SELECT " .
  178. " id, " .
  179. " family, " .
  180. " name, " .
  181. " element, " .
  182. " archetype, " .
  183. " base_stars, " .
  184. " natural_stars, " .
  185. " obtainable, " .
  186. " can_awaken, " .
  187. " awaken_bonus, " .
  188. " skill_ups_to_max, " .
  189. " leader_skill, " .
  190. " hp, " .
  191. " attack, " .
  192. " defense, " .
  193. " speed, " .
  194. " crit_rate, " .
  195. " crit_damage, " .
  196. " resistance, " .
  197. " accuracy, " .
  198. " raw_hp, " .
  199. " raw_attack, " .
  200. " raw_defense, " .
  201. " max_lvl_hp, " .
  202. " max_lvl_attack, " .
  203. " max_lvl_defense, " .
  204. " awakens_from, " .
  205. " awakens_to, " .
  206. " fusion_food, " .
  207. " homunculus, " .
  208. " craft_cost " .
  209. "FROM k_unit " .
  210. "WHERE id = $id; ";
  211. $q = $this->db->query($s);
  212. $r = $q->fetchArray(SQLITE3_ASSOC);
  213. if ($r){
  214. $this->id = $r["id"];
  215. $this->family = $r["family"];
  216. $this->name = $r["name"];
  217. $this->element = $r["element"];
  218. switch($this->element){
  219. case $ELEMENT["WATER"]:
  220. $this->element_name = "Water";
  221. break;
  222. case $ELEMENT["FIRE"]:
  223. $this->element_name = "Fire";
  224. break;
  225. case $ELEMENT["WIND"]:
  226. $this->element_name = "Wind";
  227. break;
  228. case $ELEMENT["LIGHT"]:
  229. $this->element_name = "Light";
  230. break;
  231. case $ELEMENT["DARK"]:
  232. $this->element_name = "Dark";
  233. break;
  234. }
  235. $this->archetype = $r["archetype"];
  236. switch($this->archetype){
  237. case $ARCHETYPE["ATTACK"]:
  238. $this->archetype_name = "Attack";
  239. break;
  240. case $ARCHETYPE["DEFENSE"]:
  241. $this->archetype_name = "Defense";
  242. break;
  243. case $ARCHETYPE["HP"]:
  244. $this->archetype_name = "HP";
  245. break;
  246. case $ARCHETYPE["SUPPORT"]:
  247. $this->archetype_name = "Support";
  248. break;
  249. case $ARCHETYPE["MATERIAL"]:
  250. $this->archetype_name = "Material";
  251. break;
  252. }
  253. $this->base_stars = $r["base_stars"];
  254. $this->natural_stars = $r["natural_stars"];
  255. $this->obtainable = $r["obtainable"];
  256. $this->can_awaken = $r["can_awaken"];
  257. $this->awaken_bonus = $r["awaken_bonus"];
  258. $this->skill_ups_to_max = $r["skill_ups_to_max"];
  259. if (strlen($r["leader_skill"]) > 0){
  260. $this->leader_skill = new K_Leader_Skill($this->db, $r["leader_skill"]);
  261. }
  262. $this->hp = $r["hp"];
  263. $this->attack = $r["attack"];
  264. $this->defense = $r["defense"];
  265. $this->speed = $r["speed"];
  266. $this->crit_rate = $r["crit_rate"];
  267. $this->crit_damage = $r["crit_damage"];
  268. $this->resistance = $r["resistance"];
  269. $this->accuracy = $r["accuracy"];
  270. $this->raw_hp = $r["raw_hp"];
  271. $this->raw_attack = $r["raw_attack"];
  272. $this->raw_defense = $r["raw_defense"];
  273. $this->max_lvl_hp = $r["max_lvl_hp"];
  274. $this->max_lvl_attack = $r["max_lvl_attack"];
  275. $this->max_lvl_defense = $r["max_lvl_defense"];
  276. $this->awakens_from = $r["awakens_from"];
  277. $this->awakens_to = $r["awakens_to"];
  278. $this->fusion_food = $r["fusion_food"];
  279. $this->homunculus = $r["homunculus"];
  280. $this->craft_cost = $r["craft_cost"];
  281. }
  282. if ($complete){
  283. $this->load_essences();
  284. $this->load_skills();
  285. $this->load_sources();
  286. }
  287. }
  288. /**
  289. * Loads the information about the essences to awake the monster.
  290. * It is autoatically called at construction time if the
  291. * $complete parameter is true (by default). No point in calling it
  292. * twice.
  293. */
  294. public function load_essences(){
  295. $this->essences = [];
  296. $s =
  297. "SELECT " .
  298. " element, " .
  299. " grade, " .
  300. " amount " .
  301. "FROM k_unit_essence " .
  302. "WHERE " .
  303. " unit = " . $this->id . " " .
  304. "ORDER BY " .
  305. " element <> 'Magic', " .
  306. " grade <> 'Low', " .
  307. " grade <> 'Mid'; ";
  308. $q = $this->db->query($s);
  309. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  310. $e = [
  311. "element" => $r["element"],
  312. "grade" => $r["grade"],
  313. "amount" => $r["amount"]
  314. ];
  315. array_push($this->essences, $e);
  316. }
  317. }
  318. /**
  319. * Loads the information about the monster skills.
  320. * It is autoatically called at construction time if the
  321. * $complete parameter is true (by default). No point in calling it
  322. * twice.
  323. */
  324. public function load_skills(){
  325. $this->skill = [];
  326. $s =
  327. "SELECT " .
  328. " skill " .
  329. "FROM " .
  330. " k_skill, " .
  331. " k_unit_skill " .
  332. "WHERE " .
  333. " id = skill AND " .
  334. " unit = " . $this->id . " " .
  335. "ORDER BY slot; ";
  336. $q = $this->db->query($s);
  337. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  338. array_push($this->skill, new K_Skill($this->db, $r["skill"]));
  339. }
  340. }
  341. /**
  342. * Loads the information about the monster sources.
  343. * It is autoatically called at construction time if the
  344. * $complete parameter is true (by default). No point in calling it
  345. * twice.
  346. */
  347. public function load_sources(){
  348. $this->sources = [];
  349. $s =
  350. "SELECT source " .
  351. "FROM k_unit_source " .
  352. "WHERE unit = " . $this->id . ";";
  353. $q = $this->db->query($s);
  354. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  355. array_push($this->sources, new K_Source($this->db, $r["source"]));
  356. }
  357. }
  358. /**
  359. * Gets the pathto the monster image. The image must be in the
  360. * img/content/monster/ directory, and its name must be the monster id,
  361. * padded with '0' to 4 digites, and the extension must be '.png'.
  362. *
  363. * @return path to the image, or a path to a fixed unknown image if it
  364. * doesn't exist.
  365. */
  366. public function get_image(){
  367. global $dir;
  368. global $path;
  369. if (file_exists($dir . "img/content/unit/" . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png")){
  370. return $path["img"]["content"] . "unit/" . str_pad($this->id, 8, '0', STR_PAD_LEFT) . ".png";
  371. }
  372. else{
  373. return $path["img"]["layout"] . "unknown.png";
  374. }
  375. }
  376. }
  377. ?>