K_Unit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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_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. $this->id = $id;
  199. $statement = $db->prepare("
  200. SELECT
  201. id,
  202. family,
  203. name,
  204. element,
  205. archetype,
  206. base_stars,
  207. natural_stars,
  208. obtainable,
  209. can_awaken,
  210. awaken_bonus,
  211. skill_ups_to_max,
  212. leader_skill,
  213. hp,
  214. attack,
  215. defense,
  216. speed,
  217. crit_rate,
  218. crit_damage,
  219. resistance,
  220. accuracy,
  221. raw_hp,
  222. raw_attack,
  223. raw_defense,
  224. max_lvl_hp,
  225. max_lvl_attack,
  226. max_lvl_defense,
  227. awakens_from,
  228. awakens_to,
  229. fusion_food,
  230. homunculus,
  231. craft_cost
  232. FROM k_unit
  233. WHERE id = :id;
  234. ");
  235. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  236. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  237. if ($r){
  238. $this->family = $r["family"];
  239. $this->name = HTML::e($r["name"]);
  240. $this->element = $r["element"];
  241. switch($this->element){
  242. case ELEMENT_ID::WATER:
  243. $this->element_name = "Water";
  244. break;
  245. case ELEMENT_ID::FIRE:
  246. $this->element_name = "Fire";
  247. break;
  248. case ELEMENT_ID::WIND:
  249. $this->element_name = "Wind";
  250. break;
  251. case ELEMENT_ID::LIGHT:
  252. $this->element_name = "Light";
  253. break;
  254. case ELEMENT_ID::DARK:
  255. $this->element_name = "Dark";
  256. break;
  257. }
  258. $this->archetype = $r["archetype"];
  259. switch($this->archetype){
  260. case ARCHETYPE_ID::ATTACK:
  261. $this->archetype_name = "Attack";
  262. break;
  263. case ARCHETYPE_ID::DEFENSE:
  264. $this->archetype_name = "Defense";
  265. break;
  266. case ARCHETYPE_ID::HP:
  267. $this->archetype_name = "HP";
  268. break;
  269. case ARCHETYPE_ID::SUPPORT:
  270. $this->archetype_name = "Support";
  271. break;
  272. case ARCHETYPE_ID::MATERIAL:
  273. $this->archetype_name = "Material";
  274. break;
  275. }
  276. $this->base_stars = $r["base_stars"];
  277. $this->natural_stars = $r["natural_stars"];
  278. $this->obtainable = $r["obtainable"];
  279. $this->can_awaken = $r["can_awaken"];
  280. $this->awaken_bonus = HTML::e($r["awaken_bonus"]);
  281. $this->skill_ups_to_max = $r["skill_ups_to_max"];
  282. if (strlen($r["leader_skill"]) > 0){
  283. $this->leader_skill = new K_Leader_Skill($r["leader_skill"]);
  284. }
  285. $this->hp = $r["hp"];
  286. $this->attack = $r["attack"];
  287. $this->defense = $r["defense"];
  288. $this->speed = $r["speed"];
  289. $this->crit_rate = $r["crit_rate"];
  290. $this->crit_damage = $r["crit_damage"];
  291. $this->resistance = $r["resistance"];
  292. $this->accuracy = $r["accuracy"];
  293. $this->raw_hp = $r["raw_hp"];
  294. $this->raw_attack = $r["raw_attack"];
  295. $this->raw_defense = $r["raw_defense"];
  296. $this->max_lvl_hp = $r["max_lvl_hp"];
  297. $this->max_lvl_attack = $r["max_lvl_attack"];
  298. $this->max_lvl_defense = $r["max_lvl_defense"];
  299. $this->awakens_from = $r["awakens_from"];
  300. $this->awakens_to = $r["awakens_to"];
  301. $this->fusion_food = $r["fusion_food"];
  302. $this->homunculus = $r["homunculus"];
  303. $this->craft_cost = $r["craft_cost"];
  304. }
  305. if ($complete){
  306. $this->load_essences();
  307. $this->load_skills();
  308. $this->load_sources();
  309. $this->load_transformation();
  310. }
  311. // Guess monster name
  312. if ($this->awakens_from == "" && $this->awakens_to == ""){
  313. // No to, no from. Silver unit.
  314. if ($this->base_stars >= 4){
  315. // SFV Collab event units, always awakened.
  316. $this->title = $this->name;
  317. }
  318. elseif($this->archetype == ARCHETYPE_ID::MATERIAL && $this->element == ELEMENT_ID::LIGHT){
  319. // Rainbowmon, always awakened
  320. $this->title = $this->name;
  321. }
  322. elseif($this->archetype == ARCHETYPE_ID::MATERIAL && $this->element == ELEMENT_ID::DARK){
  323. // Devilmon, always unawakened, but dont include element
  324. $this->title = $this->name;
  325. }
  326. else{
  327. // Unawakeable monster
  328. $this->title = $this->element_name . " " . $this->name;
  329. }
  330. }
  331. // Awakened
  332. elseif ($this->awakens_from != ""){
  333. $this->title = $this->name;
  334. }
  335. // Gold
  336. elseif (strlen($this->awakens_to) > 0){
  337. $this->title = $this->element_name . " " . $this->name;
  338. }
  339. }
  340. /**
  341. * Loads the information about the essences to awake the unit.
  342. *
  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. * @global resource Database connection.
  348. */
  349. public function load_essences(){
  350. global $db;
  351. $this->essences = [];
  352. $statement = $db->prepare("
  353. SELECT
  354. item,
  355. amount
  356. FROM k_unit_essence
  357. WHERE
  358. unit = :unit
  359. ORDER BY item;
  360. ");
  361. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  362. $q = $statement->execute();
  363. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  364. $e = [
  365. "item" => new K_Inventory($r["item"], INVENTORY_TYPE_ID::ESSENCES),
  366. "amount" => $r["amount"]
  367. ];
  368. array_push($this->essences, $e);
  369. }
  370. }
  371. /**
  372. * Loads the unit transformation.
  373. *
  374. * It is automatically called at construction time if the
  375. * $complete parameter is true (by default). No point in calling it
  376. * twice.
  377. *
  378. * @global resource Database connection.
  379. */
  380. public function load_transformation(){
  381. global $db;
  382. $this->transformation = null;
  383. $statement = $db->prepare("
  384. SELECT id
  385. FROM k_unit_transformation
  386. WHERE unit = :unit;
  387. ");
  388. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  389. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  390. if ($r){
  391. $this->transformation = new K_Unit_Transformation($r["id"]);
  392. }
  393. }
  394. /**
  395. * Loads the information about the unit skills.
  396. *
  397. * It is autoatically called at construction time if the
  398. * $complete parameter is true (by default). No point in calling it
  399. * twice.
  400. *
  401. * @global resource Database connection.
  402. */
  403. public function load_skills(){
  404. global $db;
  405. $this->skill = [];
  406. $statement = $db->prepare("
  407. SELECT skill
  408. FROM
  409. k_skill,
  410. k_unit_skill
  411. WHERE
  412. id = skill AND
  413. unit = :unit
  414. ORDER BY slot;
  415. ");
  416. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  417. $q = $statement->execute();
  418. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  419. array_push($this->skill, new K_Skill($r["skill"]));
  420. }
  421. }
  422. /**
  423. * Loads the information about the unit sources.
  424. *
  425. * It is autoatically called at construction time if the
  426. * $complete parameter is true (by default). No point in calling it
  427. * twice.
  428. *
  429. * @global resource Database connection.
  430. */
  431. public function load_sources(){
  432. global $db;
  433. $this->sources = [];
  434. $statement = $db->prepare("
  435. SELECT source
  436. FROM k_unit_source
  437. WHERE unit = :unit;
  438. ");
  439. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  440. $q = $statement->execute();
  441. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  442. array_push($this->sources, new K_Source($r["source"]));
  443. }
  444. }
  445. /**
  446. * Gets the path to the unit image. The image must be in the
  447. * img/unit/ directory, and it's name must be the unit id,
  448. * padded with '0' to 8 digits, and the extension must be '.png'.
  449. *
  450. * @return string Image URL, or a fixed unknown image.
  451. */
  452. public function get_image(){
  453. return APPLICATION::img("UNIT", $this->id);
  454. }
  455. }
  456. ?>