Unit.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. <?php
  2. /**
  3. * 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_Unit.php");
  14. require_once(PATH::ENTITY . "Team.php");
  15. require_once(PATH::ENTITY . "Rune.php");
  16. require_once(PATH::ENTITY . "Artifact.php");
  17. require_once(PATH::ENTITY . "Building.php");
  18. /**
  19. * A Unit.
  20. *
  21. * Represents an object from the table 'unit'.
  22. *
  23. * @category Entity
  24. */
  25. class Unit extends Entity{
  26. /**
  27. * @var int User ID.
  28. */
  29. public $uid;
  30. /**
  31. * @var int Unit ID.
  32. */
  33. public $id;
  34. /**
  35. * @var Building Building the unit is on.
  36. */
  37. public $building;
  38. /**
  39. * @var K_Unit Base unit.
  40. */
  41. public $unit;
  42. /**
  43. * @var int Unit stars.
  44. */
  45. public $stars;
  46. /**
  47. * @var int Unit level.
  48. */
  49. public $level;
  50. /**
  51. * @var int Unit base HP stat.
  52. */
  53. public $hp;
  54. /**
  55. * @var int Unit base ATK stat.
  56. */
  57. public $attack;
  58. /**
  59. * @var int Unit base DEF stat.
  60. */
  61. public $defense;
  62. /**
  63. * @var int Unit base SPD stat.
  64. */
  65. public $speed;
  66. /**
  67. * @var int Unit base CRR stat.
  68. */
  69. public $crit_rate;
  70. /**
  71. * @var int Unit base CRD stat.
  72. */
  73. public $crit_damage;
  74. /**
  75. * @var int Unit base RES stat.
  76. */
  77. public $resistance;
  78. /**
  79. * @var int Unit base ACC stat.
  80. */
  81. public $accuracy;
  82. /**
  83. * @var int Total gained experience.
  84. */
  85. public $experience;
  86. /**
  87. * @var int Total gained experience in its current level.
  88. */
  89. public $exp_gained;
  90. /**
  91. * @var int Experience / hour in the current building.
  92. */
  93. public $exp_gain_rate;
  94. /**
  95. * @var int Equipped transmogification.
  96. */
  97. public $costume;
  98. /**
  99. * @var K_Source Source the unit was aquired from.
  100. */
  101. public $source;
  102. /**
  103. * @var Datetime Time the unit was obtained.
  104. */
  105. public $create_time;
  106. /**
  107. * @var string Indicates the Homunculus name
  108. */
  109. public $homunculus_name;
  110. /**
  111. * @var bool Indicates if the monster is locked.
  112. */
  113. public $lock;
  114. /**
  115. * @var int Unit rune HP stat.
  116. */
  117. public $rune_hp = 0;
  118. /**
  119. * @var int Unit rune ATK stat.
  120. */
  121. public $rune_attack = 0;
  122. /**
  123. * @var int Unit rune DEF stat.
  124. */
  125. public $rune_defense = 0;
  126. /**
  127. * @var int Unit rune SPD stat.
  128. */
  129. public $rune_speed = 0;
  130. /**
  131. * @var int Unit rune CRR stat.
  132. */
  133. public $rune_crit_rate = 0;
  134. /**
  135. * @var int Unit rune CRD stat.
  136. */
  137. public $rune_crit_damage = 0;
  138. /**
  139. * @var int Unit rune RES stat.
  140. */
  141. public $rune_resistance = 0;
  142. /**
  143. * @var int Unit rune ACC stat.
  144. */
  145. public $rune_accuracy = 0;
  146. /**
  147. * @var int Unit artifact HP stat.
  148. */
  149. public $artifact_hp = 0;
  150. /**
  151. * @var int Unit artifact ATK stat.
  152. */
  153. public $artifact_attack = 0;
  154. /**
  155. * @var int Unit artifact DEF stat.
  156. */
  157. public $artifact_defense = 0;
  158. /**
  159. * @var int Unit artifact SPD stat.
  160. */
  161. public $artifact_speed = 0;
  162. /**
  163. * @var int Unit artifact CRR stat.
  164. */
  165. public $artifact_crit_rate = 0;
  166. /**
  167. * @var int Unit artifact CRD stat.
  168. */
  169. public $artifact_crit_damage = 0;
  170. /**
  171. * @var int Unit artifact RES stat.
  172. */
  173. public $artifact_resistance = 0;
  174. /**
  175. * @var int Unit artifact ACC stat.
  176. */
  177. public $artifact_accuracy = 0;
  178. /**
  179. * @var int Unit building HP stat.
  180. */
  181. public $building_hp = 0;
  182. /**
  183. * @var int Unit building ATK stat.
  184. */
  185. public $building_attack = 0;
  186. /**
  187. * @var int Unit building DEF stat.
  188. */
  189. public $building_defense = 0;
  190. /**
  191. * @var int Unit building SPD stat.
  192. */
  193. public $building_speed = 0;
  194. /**
  195. * @var int Unit building CRR stat.
  196. */
  197. public $building_crit_rate = 0;
  198. /**
  199. * @var int Unit building CRD stat.
  200. */
  201. public $building_crit_damage = 0;
  202. /**
  203. * @var int Unit building RES stat.
  204. */
  205. public $building_resistance = 0;
  206. /**
  207. * @var int Unit building ACC stat.
  208. */
  209. public $building_accuracy = 0;
  210. /**
  211. * @var int Unit total HP stat.
  212. */
  213. public $total_hp = 0;
  214. /**
  215. * @var int Unit total ATK stat.
  216. */
  217. public $total_attack = 0;
  218. /**
  219. * @var int Unit total DEF stat.
  220. */
  221. public $total_defense = 0;
  222. /**
  223. * @var int Unit total SPD stat.
  224. */
  225. public $total_speed = 0;
  226. /**
  227. * @var int Unit total CRR stat.
  228. */
  229. public $total_crit_rate = 0;
  230. /**
  231. * @var int Unit total CRD stat.
  232. */
  233. public $total_crit_damage = 0;
  234. /**
  235. * @var int Unit total RES stat.
  236. */
  237. public $total_resistance = 0;
  238. /**
  239. * @var int Unit total ACC stat.
  240. */
  241. public $total_accuracy = 0;
  242. /**
  243. * @var int Unit effective HP
  244. */
  245. public $effective_hp = 0;
  246. /**
  247. * @var int Unit effective damage
  248. */
  249. public $effective_dmg = 0;
  250. /**
  251. * @var float Average rune efficiency.
  252. */
  253. public $avg_rune_efficiency = 0.0;
  254. /**
  255. * @var \Rune[] Equiped Runes.
  256. */
  257. public $rune = [];
  258. /**
  259. * @var Artifact Type artifact equipped.
  260. */
  261. public $artifact_type;
  262. /**
  263. * @var Artifact Elemental artifact equipped.
  264. */
  265. public $artifact_element;
  266. /**
  267. * @var \Team[] The Teamss the unit is on.
  268. */
  269. public $teams = [];
  270. /**
  271. * @var int[] Array with the levels of the skills, in orer.
  272. */
  273. public $skill_levels = [];
  274. /**
  275. * @var int Total experience required for the current level.
  276. */
  277. public $experience_level = 0;
  278. /**
  279. * @var int Experience required to level up.
  280. */
  281. public $experience_level_up = 0;
  282. /**
  283. * @var int Indcates if the unit is in storage.
  284. */
  285. public $in_storage = false;
  286. /**
  287. * @var string Unit title.
  288. * If Homunculus,it will be it's name.
  289. * If awakened, it will be the awakened from name.
  290. * If unawakened, it will be <element> <name>.
  291. */
  292. public $title;
  293. /**
  294. * Constructor.
  295. *
  296. * Searches the database and retrieves the information about the
  297. * monster, populating it and it's items.
  298. *
  299. * @param int $id Monster identifier.
  300. * @param bool $complete If false, query only monster and k_monster.
  301. * @global int Player ID.
  302. * @global resource Database connection.
  303. */
  304. public function __construct($id, $complete = true, $uid = 0){
  305. global $UID;
  306. global $db;
  307. if ($uid == 0){
  308. $uid = $UID;
  309. }
  310. $statement = $db->prepare("
  311. SELECT
  312. uid,
  313. id,
  314. building,
  315. unit,
  316. stars,
  317. level,
  318. hp,
  319. attack,
  320. defense,
  321. speed,
  322. crit_rate,
  323. crit_damage,
  324. resistance,
  325. accuracy,
  326. experience,
  327. exp_gained,
  328. exp_gain_rate,
  329. costume,
  330. source,
  331. create_time,
  332. homunculus_name,
  333. lock
  334. FROM unit
  335. WHERE
  336. uid = :uid AND
  337. id = :id;
  338. ");
  339. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  340. $statement->bindValue(':uid', $uid, SQLITE3_TEXT);
  341. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  342. if ($r){
  343. $this->uid = $r["uid"];
  344. $this->id = $r["id"];
  345. if ($r["building"] != null && $r["building"] > 0){
  346. $this->building = new Building($r["building"]);
  347. }
  348. if (strlen($r["unit"]) > 0){
  349. $this->unit = new K_Unit($r["unit"], $complete);
  350. }
  351. $this->stars = $r["stars"];
  352. $this->level = $r["level"];
  353. $this->hp = $r["hp"];
  354. $this->attack = $r["attack"];
  355. $this->defense = $r["defense"];
  356. $this->speed = $r["speed"];
  357. $this->crit_rate = $r["crit_rate"];
  358. $this->crit_damage = $r["crit_damage"];
  359. $this->resistance = $r["resistance"];
  360. $this->accuracy = $r["accuracy"];
  361. $this->experience = $r["experience"];
  362. $this->exp_gained = $r["exp_gained"];
  363. $this->exp_gain_rate = $r["exp_gain_rate"];
  364. $this->costume = $r["costume"];
  365. $this->source = new K_Source($r["source"]);
  366. $this->create_time = date_create($r["create_time"]);
  367. $this->homunculus_name = $r["homunculus_name"];
  368. if ($complete){
  369. $this->load_stats();
  370. $this->load_teams();
  371. $this->check_storage();
  372. $this->lock = $r["lock"];
  373. $statement = $db->prepare("
  374. SELECT level
  375. FROM
  376. unit_skill,
  377. k_skill
  378. WHERE
  379. k_skill.id = unit_skill.skill AND
  380. unit = :unit
  381. ORDER BY slot;
  382. ");
  383. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  384. $q = $statement->execute();
  385. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  386. array_push($this->skill_levels, $r["level"]);
  387. }
  388. $statement = $db->prepare("
  389. SELECT exp
  390. FROM k_experience
  391. WHERE
  392. stars = :stars AND
  393. level = :level;
  394. ");
  395. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  396. $statement->bindValue(':level', $this->level, SQLITE3_TEXT);
  397. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  398. if ($r){
  399. $this->experience_level = $r["exp"];
  400. $this->experience_level_up = $r["exp"] - $this->exp_gained;
  401. }
  402. }
  403. if ($this->unit->homunculus){
  404. $this->title = $this->homunculus_name;
  405. }
  406. else{
  407. $this->title = $this->unit->title;
  408. }
  409. }
  410. }
  411. /**
  412. * Checks if the monster is in storage and sets {@see $in_storage} to
  413. * true or false.
  414. *
  415. * @global int Player ID.
  416. * @global resource Database connection.
  417. */
  418. public function check_storage(){
  419. global $UID;
  420. global $db;
  421. $statement = $db->prepare("
  422. SELECT 1
  423. FROM
  424. unit,
  425. building
  426. WHERE
  427. unit.id = :id AND
  428. unit.uid = :uid AND
  429. building.uid = :uid AND
  430. unit.building = building.id AND
  431. building.building = :building;
  432. ");
  433. $statement->bindValue(':id', $this->id, SQLITE3_TEXT);
  434. $statement->bindValue(':uid', $UID, SQLITE3_TEXT);
  435. $statement->bindValue(':building', BUILDING_ID::MONSTER_STORAGE, SQLITE3_TEXT);
  436. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  437. if ($r){
  438. $this->in_storage = true;
  439. }
  440. else{
  441. $this->in_storage = false;
  442. }
  443. }
  444. /**
  445. * Calculates the stats gained from runes, artifacts and buildings.
  446. * Calculates the totalls and the effective stats.
  447. *
  448. * @gobal int User ID.
  449. * @gobal int Game mode (Normal/RTA).
  450. * @global resource Database connection.
  451. */
  452. public function load_stats(){
  453. global $UID;
  454. global $MODE;
  455. global $db;
  456. // Runes
  457. $this->rune = [];
  458. $statement = $db->prepare("
  459. SELECT rune
  460. FROM unit_rune
  461. WHERE
  462. unit = :unit AND
  463. rta = :rta;
  464. ");
  465. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  466. $statement->bindValue(':rta', $MODE, SQLITE3_TEXT);
  467. $q = $statement->execute();
  468. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  469. array_push($this->rune, new Rune($r["rune"]));
  470. }
  471. foreach ($this->rune as $rune){
  472. foreach ($rune->stats as $stat){
  473. $this->sum_rune_stat($stat->stat, $stat->value + $stat->grind);
  474. }
  475. }
  476. $this->rune_attack = ceil($this->rune_attack);
  477. $this->rune_defense = ceil($this->rune_defense);
  478. $this->rune_hp = ceil($this->rune_hp);
  479. // Artifacts
  480. $this->artifact = [];
  481. $statement = $db->prepare("
  482. SELECT artifact
  483. FROM unit_artifact
  484. WHERE
  485. unit = :unit AND
  486. rta = :rta;
  487. ");
  488. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  489. $statement->bindValue(':rta', $MODE, SQLITE3_TEXT);
  490. $q = $statement->execute();
  491. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  492. $artifact = new Artifact($r["artifact"]);
  493. if ($artifact->type == ARTIFACT_TYPE::ARCHETYPE){
  494. $this->artifact_type = $artifact;
  495. }
  496. else{
  497. $this->artifact_element = $artifact;
  498. }
  499. switch($artifact->stat->stat){
  500. case ARTIFACT_STAT_ID::ATK:
  501. $this->artifact_attack += $artifact->stat->value;
  502. break;
  503. case ARTIFACT_STAT_ID::DEF:
  504. $this->artifact_defense += $artifact->stat->value;
  505. break;
  506. case ARTIFACT_STAT_ID::HP:
  507. $this->artifact_hp += $artifact->stat->value;
  508. break;
  509. }
  510. }
  511. // Buildings
  512. $statement = $db->prepare("
  513. SELECT
  514. affected_stat,
  515. bonus,
  516. element
  517. FROM
  518. k_decoration,
  519. k_decoration_level,
  520. decoration
  521. WHERE
  522. decoration.uid = :uid AND
  523. k_decoration.id = decoration.decoration AND
  524. decoration.decoration = k_decoration_level.decoration AND
  525. decoration.level = k_decoration_level.level AND
  526. area = :area AND
  527. affected_stat IN (:stat_atk, :stat_def, :stat_hp, :stat_spd, :stat_crd);
  528. ");
  529. $statement->bindValue(':uid', $UID, SQLITE3_TEXT);
  530. $statement->bindValue(':area', EFFECT_AREA_ID::GENERAL, SQLITE3_INTEGER);
  531. $statement->bindValue(':stat_atk', STAT_ID::ATK, SQLITE3_INTEGER);
  532. $statement->bindValue(':stat_def', STAT_ID::DEF, SQLITE3_INTEGER);
  533. $statement->bindValue(':stat_hp', STAT_ID::HP, SQLITE3_INTEGER);
  534. $statement->bindValue(':stat_spd', STAT_ID::SPD, SQLITE3_INTEGER);
  535. $statement->bindValue(':stat_crd', STAT_ID::CRIT_DMG, SQLITE3_INTEGER);
  536. $q = $statement->execute();
  537. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  538. switch($r["affected_stat"]){
  539. case STAT_ID::DEF:
  540. $this->building_defense += ($this->defense * $r["bonus"] / 100);
  541. break;
  542. case STAT_ID::HP:
  543. $this->building_hp += ($this->hp * $r["bonus"] / 100);
  544. break;
  545. case STAT_ID::SPD:
  546. $this->building_speed += ($this->speed * $r["bonus"] / 100);
  547. break;
  548. case STAT_ID::CRIT_DMG:
  549. $this->building_crit_damage += $r["bonus"];
  550. break;
  551. case STAT_ID::ATK:
  552. if(strlen($r["element"]) > 0 && $r["element"] == $this->unit->element){
  553. $this->building_attack += ($this->attack * $r["bonus"] / 100);
  554. }
  555. else{
  556. $this->building_attack += ($this->attack * $r["bonus"] / 100);
  557. }
  558. break;
  559. }
  560. }
  561. $this->building_attack = ceil($this->building_attack);
  562. $this->building_defense = ceil($this->building_defense);
  563. $this->building_hp = ceil($this->building_hp);
  564. $this->building_speed = ceil($this->building_speed);
  565. $this->building_crit_damage = ceil($this->building_crit_damage);
  566. // Totals
  567. $this->total_hp = $this->hp + $this->rune_hp + $this->building_hp + $this->artifact_hp;
  568. $this->total_attack = $this->attack + $this->rune_attack + $this->building_attack + $this->artifact_attack;
  569. $this->total_defense = $this->defense + $this->rune_defense + $this->building_defense + $this->artifact_defense;
  570. $this->total_speed = $this->speed + $this->rune_speed + $this->building_speed;
  571. $this->total_crit_rate = $this->crit_rate + $this->rune_crit_rate + $this->building_crit_rate;
  572. $this->total_crit_damage = $this->crit_damage + $this->rune_crit_damage + $this->building_crit_damage;
  573. $this->total_accuracy = $this->accuracy + $this->rune_accuracy + $this->building_accuracy;
  574. $this->total_resistance = $this->resistance + $this->rune_resistance + $this->building_resistance;
  575. // Effective stats
  576. $ehp = 0;
  577. $edmg = 0;
  578. // Calculate effective_hp
  579. $hp = $this->total_hp;
  580. $def = $this->total_defense;
  581. $ehp = ceil(((($def * 3.5) + 1140) * $hp) / 1000);
  582. $this->effective_hp = $ehp;
  583. // Calculate effective_dmg
  584. $atk = $this->total_attack;
  585. $crr = $this->total_crit_rate;
  586. if ($crr > 100){
  587. $crr = 100;
  588. }
  589. $crd = $this->total_crit_damage;
  590. $edmg = ceil(($atk * (100 - $crr) / 100) + (($atk + ($atk * $crd / 100)) * $crr / 100));
  591. $this->effective_dmg = $edmg;
  592. }
  593. /**
  594. * Loads the {@see teams} array, with the teams the unit is on.
  595. *
  596. * @global int Player ID.
  597. * @global resource Database connection.
  598. */
  599. public function load_teams(){
  600. global $UID;
  601. global $db;
  602. $this->teams = [];
  603. $statement = $db->prepare("
  604. SELECT DISTINCT team.id AS id
  605. FROM
  606. team,
  607. team_unit
  608. WHERE
  609. team.uid = :uid AND
  610. team.id = team_unit.team AND
  611. team_unit.unit = :unit;
  612. ");
  613. $statement->bindValue(':uid', $UID, SQLITE3_TEXT);
  614. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  615. $q = $statement->execute();
  616. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  617. array_push($this->teams, new Team($r["id"], false));
  618. }
  619. }
  620. /**
  621. * Calculates the stat increase by a rune property.
  622. *
  623. * @param string $stat Stat type.
  624. * @param int $value Stat increase value.
  625. */
  626. private function sum_rune_stat($stat, $value){
  627. switch ($stat){
  628. case RUNE_STAT_ID::ATK:
  629. $this->rune_attack += $value;
  630. break;
  631. case RUNE_STAT_ID::DEF:
  632. $this->rune_defense += $value;
  633. break;
  634. case RUNE_STAT_ID::HP:
  635. $this->rune_hp += $value;
  636. break;
  637. case RUNE_STAT_ID::SPD:
  638. $this->rune_speed += $value;
  639. break;
  640. case RUNE_STAT_ID::CRR:
  641. $this->rune_crit_rate += $value;
  642. break;
  643. case RUNE_STAT_ID::CRD:
  644. $this->rune_crit_damage += $value;
  645. break;
  646. case RUNE_STAT_ID::ACC:
  647. $this->rune_accuracy += $value;
  648. break;
  649. case RUNE_STAT_ID::RES:
  650. $this->rune_resistance += $value;
  651. break;
  652. case RUNE_STAT_ID::ATK_P:
  653. $this->rune_attack += ($this->attack * $value / 100);
  654. break;
  655. case RUNE_STAT_ID::DEF_P:
  656. $this->rune_defense += ($this->defense * $value / 100);
  657. break;
  658. case RUNE_STAT_ID::HP_P:
  659. $this->rune_hp += ($this->hp * $value / 100);
  660. break;
  661. }
  662. }
  663. /**
  664. * Checks if the unit skills are completly maxed.
  665. *
  666. * @return bool True if all skills maxed, false otherwise.
  667. */
  668. public function are_skills_maxed(){
  669. $maxed = true;
  670. for ($i = 0; $i < sizeof($this->unit->skill); $i ++){
  671. if ($this->unit->skill[$i]->max_level > $this->skill_levels[$i]){
  672. $maxed = false;
  673. break;
  674. }
  675. }
  676. return $maxed;
  677. }
  678. }
  679. ?>