K_Unit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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_Inventory.php");
  17. require_once(PATH::ENTITY . "K_Unit_Transformation.php");
  18. /**
  19. * A unit.
  20. *
  21. * Represents an object from the table 'k_unit'.
  22. *
  23. * @category Entity
  24. */
  25. class K_Unit extends Entity{
  26. /**
  27. * @var int Unit identifier.
  28. */
  29. public $id;
  30. /**
  31. * @var int Unit family id.
  32. */
  33. public $family;
  34. /**
  35. * @var string Unit name.
  36. */
  37. public $name;
  38. /**
  39. * @var int Unit element id.
  40. */
  41. public $element;
  42. /**
  43. * @var string Element name (title case)
  44. */
  45. public $element_name;
  46. /**
  47. * @var int Unit archetype id.
  48. */
  49. public $archetype;
  50. /**
  51. * @var string Archetype name (title case).
  52. */
  53. public $archetype_name;
  54. /**
  55. * @var int Default star level.
  56. */
  57. public $base_stars;
  58. /**
  59. *@var int Default star level.
  60. */
  61. public $natural_stars;
  62. /**
  63. * @var bool Indicates if the unit is obtainable.
  64. */
  65. public $obtainable;
  66. /**
  67. * @var bool Indicates if the unit can be awakened.
  68. */
  69. public $can_awaken;
  70. /**
  71. * @var string Bonus when awakening.
  72. */
  73. public $awaken_bonus;
  74. /**
  75. * @var int Required skill-ups.
  76. */
  77. public $skill_ups_to_max;
  78. /**
  79. * @var K_Leader_Skill Unit's leader skill.
  80. */
  81. public $leader_skill;
  82. /**
  83. * @var int Base HP.
  84. */
  85. public $hp;
  86. /**
  87. * @var int Base attack.
  88. */
  89. public $atk;
  90. /**
  91. * @var int Base defense.
  92. */
  93. public $defense;
  94. /**
  95. * @var int Speed.
  96. */
  97. public $speed;
  98. /**
  99. * @var int Critical rate.
  100. */
  101. public $crit_rate;
  102. /**
  103. * @var int Critical damage.
  104. */
  105. public $crit_damage;
  106. /**
  107. * @var int Resistance
  108. */
  109. public $resistance;
  110. /**
  111. * @var int Accuracy.
  112. */
  113. public $accuracy;
  114. /**
  115. * @var int Raw HP.
  116. */
  117. public $raw_hp;
  118. /**
  119. * @var int Raw attack.
  120. */
  121. public $raw_atk;
  122. /**
  123. * @var int Raw Defense.
  124. */
  125. public $raw_defense;
  126. /**
  127. * @var int HP at max_level.
  128. */
  129. public $max_lvl_hp;
  130. /**
  131. * @var int Attack at max_level.
  132. */
  133. public $max_lvl_atk;
  134. /**
  135. * @var int Defense at max_level.
  136. */
  137. public $max_lvl_defense;
  138. /**
  139. * @var K_Unit Unit it awakens from.
  140. */
  141. public $awakens_from;
  142. /**
  143. * @var K_Unit Unit it awakens to.
  144. */
  145. public $awakens_to;
  146. /**
  147. * @var bool Indicates if the unit is a fusion ingredient.
  148. */
  149. public $fusion_food;
  150. /**
  151. * @var bool Indicates if the unit is homunculus.
  152. */
  153. public $homunculus;
  154. /**
  155. * @var int The crafting cost (homunculus only).
  156. */
  157. public $craft_cost;
  158. /**
  159. * @var \K_Unit_Skill[] Unit skills.
  160. */
  161. public $skill = [];
  162. /**
  163. * @var mixed[] Required essences to awaken the unit.
  164. *
  165. * Formed by arrays of:
  166. * [
  167. * "item" => K_Inventory,
  168. * "amount" => int
  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 int $id Unit identifier.
  193. * @param bool $complete If false, query only k_unit.
  194. * @global resource Database connection.
  195. */
  196. public function __construct($id, $complete = true){
  197. global $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 = $db->query($s);
  234. $r = $q->fetchArray(SQLITE3_ASSOC);
  235. $this->id = $id;
  236. if ($r){
  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($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. // Guess monster name
  311. if ($this->awakens_from == "" && $this->awakens_to == ""){
  312. // No to, no from. Silver unit.
  313. if ($this->base_stars >= 4){
  314. // SFV Collab event units, always awakened.
  315. $this->title = $this->name;
  316. }
  317. elseif($this->archetype == ARCHETYPE_ID::MATERIAL && $this->element == ELEMENT_ID::LIGHT){
  318. // Rainbowmon, always awakened
  319. $this->title = $this->name;
  320. }
  321. elseif($this->archetype == ARCHETYPE_ID::MATERIAL && $this->element == ELEMENT_ID::DARK){
  322. // Devilmon, always unawakened, but dont include element
  323. $this->title = $this->name;
  324. }
  325. else{
  326. // Unawakeable monster
  327. $this->title = $this->element_name . " " . $this->name;
  328. }
  329. }
  330. // Awakened
  331. elseif ($this->awakens_from != ""){
  332. $this->title = $this->name;
  333. }
  334. // Gold
  335. elseif (strlen($this->awakens_to) > 0){
  336. $this->title = $this->element_name . " " . $this->name;
  337. }
  338. }
  339. /**
  340. * Loads the information about the essences to awake the unit.
  341. *
  342. * It is autoatically called at construction time if the
  343. * $complete parameter is true (by default). No point in calling it
  344. * twice.
  345. *
  346. * @global resource Database connection.
  347. */
  348. public function load_essences(){
  349. global $db;
  350. $this->essences = [];
  351. $s =
  352. "SELECT " .
  353. " item, " .
  354. " amount " .
  355. "FROM k_unit_essence " .
  356. "WHERE " .
  357. " unit = " . $this->id . " " .
  358. "ORDER BY item; ";
  359. error_log($s);
  360. $q = $db->query($s);
  361. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  362. $e = [
  363. "item" => new K_Inventory($r["item"], INVENTORY_TYPE_ID::ESSENCES),
  364. "amount" => $r["amount"]
  365. ];
  366. array_push($this->essences, $e);
  367. }
  368. }
  369. /**
  370. * Loads the unit transformation.
  371. *
  372. * It is automatically called at construction time if the
  373. * $complete parameter is true (by default). No point in calling it
  374. * twice.
  375. *
  376. * @global resource Database connection.
  377. */
  378. public function load_transformation(){
  379. global $db;
  380. $this->transformation = null;
  381. $s = "
  382. SELECT id
  383. FROM k_unit_transformation
  384. WHERE unit = " . $this->id . ";";
  385. $q = $db->query($s);
  386. $r = $q->fetchArray(SQLITE3_ASSOC);
  387. if ($r){
  388. $this->transformation = new K_Unit_Transformation($r["id"]);
  389. }
  390. }
  391. /**
  392. * Loads the information about the unit skills.
  393. *
  394. * It is autoatically called at construction time if the
  395. * $complete parameter is true (by default). No point in calling it
  396. * twice.
  397. *
  398. * @global resource Database connection.
  399. */
  400. public function load_skills(){
  401. global $db;
  402. $this->skill = [];
  403. $s = "
  404. SELECT skill
  405. FROM
  406. k_skill,
  407. k_unit_skill
  408. WHERE
  409. id = skill AND
  410. unit = " . $this->id . "
  411. ORDER BY slot;
  412. ";
  413. $q = $db->query($s);
  414. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  415. array_push($this->skill, new K_Skill($r["skill"]));
  416. }
  417. }
  418. /**
  419. * Loads the information about the unit sources.
  420. *
  421. * It is autoatically called at construction time if the
  422. * $complete parameter is true (by default). No point in calling it
  423. * twice.
  424. *
  425. * @global resource Database connection.
  426. */
  427. public function load_sources(){
  428. global $db;
  429. $this->sources = [];
  430. $s =
  431. "SELECT source " .
  432. "FROM k_unit_source " .
  433. "WHERE unit = " . $this->id . ";";
  434. $q = $db->query($s);
  435. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  436. array_push($this->sources, new K_Source($r["source"]));
  437. }
  438. }
  439. /**
  440. * Gets the path to the unit image. The image must be in the
  441. * img/unit/ directory, and it's name must be the unit id,
  442. * padded with '0' to 8 digits, and the extension must be '.png'.
  443. *
  444. * @return string Image URL, or a fixed unknown image.
  445. */
  446. public function get_image(){
  447. return APPLICATION::img("UNIT", $this->id);
  448. }
  449. }
  450. ?>