Unit.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 . "Monster.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. private $player;
  27. private $id;
  28. private $building;
  29. private $monster;
  30. private $stars;
  31. private $level;
  32. private $experience;
  33. private $exp_gained;
  34. private $exp_gain_rate;
  35. private $flag_experience = false;
  36. private $experience_level = 0;
  37. private $experience_level_up = 0;
  38. private $flag_storage = false;
  39. private $in_storage = false;
  40. private $title;
  41. private $costume;
  42. private $flag_source = false;
  43. private $source_id;
  44. private $source;
  45. private $create_time;
  46. private $homunculus_name;
  47. private $lock;
  48. private $base_stats = array(
  49. "hp" => 0,
  50. "atk" => 0,
  51. "def" => 0,
  52. "spd" => 0,
  53. "crr" => 0,
  54. "crd" => 0,
  55. "acc" => 0,
  56. "res" => 0,
  57. "ehp" => 0,
  58. "dmg" => 0,
  59. );
  60. private $rune_stats = array(
  61. "hp" => 0,
  62. "atk" => 0,
  63. "def" => 0,
  64. "spd" => 0,
  65. "crr" => 0,
  66. "crd" => 0,
  67. "acc" => 0,
  68. "res" => 0,
  69. "ehp" => 0,
  70. "dmg" => 0,
  71. );
  72. private $artifact_stats = array(
  73. "hp" => 0,
  74. "atk" => 0,
  75. "def" => 0,
  76. "spd" => 0,
  77. "crr" => 0,
  78. "crd" => 0,
  79. "acc" => 0,
  80. "res" => 0,
  81. "ehp" => 0,
  82. "dmg" => 0,
  83. );
  84. private $flag_buildings = false;
  85. private $building_stats = array(
  86. "hp" => 0,
  87. "atk" => 0,
  88. "def" => 0,
  89. "spd" => 0,
  90. "crr" => 0,
  91. "crd" => 0,
  92. "acc" => 0,
  93. "res" => 0,
  94. "ehp" => 0,
  95. "dmg" => 0,
  96. );
  97. private $flag_stats = false;
  98. private $stats = array(
  99. "hp" => 0,
  100. "atk" => 0,
  101. "def" => 0,
  102. "spd" => 0,
  103. "crr" => 0,
  104. "crd" => 0,
  105. "acc" => 0,
  106. "res" => 0,
  107. "ehp" => 0,
  108. "dmg" => 0,
  109. );
  110. private $flag_runes = false;
  111. private $avg_rune_efficiency = 0.0;
  112. private $rune = [];
  113. private $artifact_type;
  114. private $artifact_element;
  115. private $flag_teams = false;
  116. private $teams = [];
  117. private $flag_skills = false;
  118. private $skill_levels = [];
  119. /**
  120. * Retrieves the owner's player identifier.
  121. *
  122. * @return int Player ID.
  123. */
  124. public function get_player(){
  125. return $this->player;
  126. }
  127. /**
  128. * Retrieves the unit identifier.
  129. *
  130. * @return int Unit ID.
  131. */
  132. public function get_id(){
  133. return $this->id;
  134. }
  135. /**
  136. * Retrieves the identifier of the building the unit is in, if any.
  137. *
  138. * @return int Building ID, null if unit not in building.
  139. */
  140. public function get_building_id(){
  141. return $this->building;
  142. }
  143. /**
  144. * Retrieves the base monster.
  145. *
  146. * @return Monster The base monster.
  147. */
  148. public function get_monster(){
  149. return $this->monster;
  150. }
  151. /**
  152. * Retrieves the unit stars (grade).
  153. *
  154. * @return int Unit stars (1-6).
  155. */
  156. public function get_stars(){
  157. return $this->stars;
  158. }
  159. /**
  160. * Retrieves the unit current level.
  161. *
  162. * @return int Unit level.
  163. */
  164. public function get_level(){
  165. return $this->level;
  166. }
  167. /**
  168. * Retrieves the unit's base stats.
  169. *
  170. * It's an associative array with the following keys:
  171. * atk, def, hp, spd, crr, crd, acc, res, ehp, dmg
  172. *
  173. * @return int[] Unit stats.
  174. */
  175. public function get_base_stats(){
  176. return $this->base_stats;
  177. }
  178. /**
  179. * Retrieves the unit's stats provided by runes.
  180. *
  181. * It's an associative array with the following keys:
  182. * atk, def, hp, spd, crr, crd, acc, res, ehp, dmg
  183. *
  184. * @return int[] Rune stats.
  185. */
  186. public function get_rune_stats(){
  187. if (! $this->flag_runes){
  188. $this->load_runes();
  189. }
  190. return $this->rune_stats;
  191. }
  192. /**
  193. * Retrieves the unit's stats provided by artifacts.
  194. *
  195. * It's an associative array with the following keys:
  196. * atk, def, hp, spd, crr, crd, acc, res, ehp, dmg
  197. * Note that only "atk", "def", "hp", "dmg" and "ehp" can me non-zero.
  198. *
  199. * @return int[] Artifact stats.
  200. */
  201. public function get_artifact_stats(){
  202. if (! $this->flag_runes){
  203. $this->load_runes();
  204. }
  205. return $this->artifact_stats;
  206. }
  207. /**
  208. * Retrieves the unit's stat bonus provided by buildings.
  209. * Only building bonuses are considered.
  210. *
  211. * It's an associative array with the following keys:
  212. * atk, def, hp, spd, crr, crd, acc, res, ehp, dmg
  213. *
  214. * @return int[] Building stats.
  215. */
  216. public function get_building_stats(){
  217. if (! $this->flag_buildings){
  218. $this->load_buildings();
  219. }
  220. return $this->building_stats;
  221. }
  222. /**
  223. * Retrieves the unit's stats.
  224. *
  225. * It's an associative array with the following keys:
  226. * atk, def, hp, spd, crr, crd, acc, res, ehp, dmg
  227. *
  228. * Includes the unit base stats, the runes and artifact increases and the building bonuses.
  229. *
  230. * @return int[] Unit stats.
  231. */
  232. public function get_stats(){
  233. if (! $this->flag_stats){
  234. $this->load_stats();
  235. }
  236. return $this->stats;
  237. }
  238. /**
  239. * Retrieves the total experience earned by the unit.
  240. *
  241. * @return int Total experience.
  242. */
  243. public function get_experience(){
  244. return $this->experience;
  245. }
  246. /**
  247. * Retrieves the total experience earned by the unit since it's level is the current one..
  248. *
  249. * @return int Current level experience.
  250. */
  251. public function get_current_level_experience(){
  252. return $this->exp_gained;
  253. }
  254. /**
  255. * Retrieves the experience being gained by the hour from a building.
  256. *
  257. * @return int Experience by the hour, 0 if not in an exp building.
  258. */
  259. public function get_exp_gain_rate(){
  260. return $this->exp_gain_rate;
  261. }
  262. /**
  263. * Retrieves the current transmogrification identifier.
  264. *
  265. * @return int Transmogification ID, null if none.
  266. */
  267. public function get_costume(){
  268. return $this->costume;
  269. }
  270. /**
  271. * Retrieves the $source the unit was obtained from.
  272. *
  273. * @return Source Unit source.
  274. */
  275. public function get_source(){
  276. if (! $this->flag_source){
  277. $this->load_source();
  278. }
  279. return $this->source;
  280. }
  281. /**
  282. * Gets the date the unit was obtained.
  283. *
  284. * If $format is supplied, a string in the desired format will be returned. If not, the raw
  285. * DateTime object will e returned.
  286. *
  287. * @param string $format A date format (optional).
  288. * @return DateTime | string The raw date, or a formatted version.
  289. */
  290. public function get_creation_time($format = null){
  291. if (null == $format){
  292. return $this->create_time;
  293. }
  294. else{
  295. return $this->create_time->format($format);
  296. }
  297. }
  298. /**
  299. * Retrieves the name if the unit is an homunculus.
  300. *
  301. * @return string Homunculus name, null if not Homunculus.
  302. */
  303. public function get_homunculus_name(){
  304. return $this->homunculus_name;
  305. }
  306. /**
  307. * Checks if the unit is locked.
  308. *
  309. * @return boolean True if locked, false if unlocked.
  310. */
  311. public function is_locked(){
  312. return $this->lock;
  313. }
  314. /**
  315. * Retrieves the average rune efficiency.
  316. *
  317. * @return number Average efficiency of the runes.
  318. */
  319. public function get_avg_rune_efficiency(){
  320. if (! $this->flag_runes){
  321. $this->load_runes();
  322. }
  323. return $this->avg_rune_efficiency;
  324. }
  325. /**
  326. * Retrieves the currently equipped runes.
  327. *
  328. * @return Rune[]
  329. */
  330. public function get_runes(){
  331. if (! $this->flag_runes){
  332. $this->load_runes();
  333. }
  334. return $this->rune;
  335. }
  336. /**
  337. * Retrieves equipped artifacts.
  338. * Can be read with the indexes ARTIFACT_TYPE:ELEMENT and ARTIFACT_TYPE:ARCHETYPE.
  339. *
  340. * @see ARTIFACT_TYPE
  341. *
  342. * @return Artifact[] Equiped artifact array. Null positions if not equipped.
  343. */
  344. public function get_artifacts(){
  345. if (! $this->flag_runes){
  346. $this->load_runes();
  347. }
  348. return [$this->artifact_element, $this->artifact_type];
  349. }
  350. /**
  351. * Retrieves $teams
  352. *
  353. * @return multitype:Team
  354. */
  355. public function get_teams(){
  356. if (! $this->flag_teams){
  357. $this->load_teams();
  358. }
  359. return $this->teams;
  360. }
  361. /**
  362. * Retrieves $skill_levels
  363. *
  364. * @return multitype:number
  365. */
  366. public function get_skill_levels(){
  367. if (! $this->flag_skills){
  368. $this->load_skill();
  369. }
  370. return $this->skill_levels;
  371. }
  372. /**
  373. * Retrieves the total experience required for the current level.
  374. *
  375. * @return int Level experience.
  376. */
  377. public function get_experience_level(){
  378. if (! $this->flag_experience){
  379. $this->load_experience();
  380. }
  381. return $this->experience_level;
  382. }
  383. /**
  384. * Retrieves the experience required to level-up.
  385. *
  386. * @return int Experience to level-up.
  387. */
  388. public function get_experience_level_up(){
  389. if (! $this->flag_experience){
  390. $this->load_experience();
  391. }
  392. return $this->experience_level_up;
  393. }
  394. /**
  395. * Checks if the unit is in storage.
  396. *
  397. * @return boolean True if in storage, false otherwise.
  398. */
  399. public function is_in_storage(){
  400. if (! $this->flag_storage){
  401. $this->load_storage();
  402. }
  403. return $this->in_storage;
  404. }
  405. /**
  406. * Retrieves the unit's title.
  407. * If Homunculus,it will be it's name.
  408. * If awakened, it will be the awakened from name.
  409. * If unawakened, it will be <element> <name>.
  410. *
  411. * @return string The unit's title.
  412. */
  413. public function get_title(){
  414. return $this->title;
  415. }
  416. /**
  417. * Constructor.
  418. *
  419. * Searches the database and retrieves the information about the
  420. * monster, populating it and it's items.
  421. *
  422. * @param int $id Monster identifier.
  423. * @param bool $complete If false, query only unit and monster.
  424. */
  425. public function __construct($id, $complete = true, $player = -1){
  426. if ($player == -1 && null != get_context()->get_player()){
  427. $player = get_context()->get_player()->get_id();
  428. }
  429. $statement = get_context()->get_db()->prepare("
  430. SELECT
  431. player,
  432. id,
  433. building,
  434. monster,
  435. stars,
  436. level,
  437. hp,
  438. attack,
  439. defense,
  440. speed,
  441. crit_rate,
  442. crit_damage,
  443. resistance,
  444. accuracy,
  445. experience,
  446. exp_gained,
  447. exp_gain_rate,
  448. costume,
  449. source,
  450. create_time,
  451. homunculus_name,
  452. lock
  453. FROM unit
  454. WHERE
  455. id = :id;
  456. ");
  457. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  458. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  459. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  460. if ($r){
  461. $this->player = $r["player"];
  462. $this->id = $r["id"];
  463. if ($r["building"] != null && $r["building"] > 0){
  464. $this->building = $r["building"];
  465. }
  466. if (strlen($r["monster"]) > 0){
  467. $this->monster = new Monster($r["monster"]);
  468. }
  469. $this->stars = $r["stars"];
  470. $this->level = $r["level"];
  471. $this->base_stats["hp"] = $r["hp"];
  472. $this->base_stats["atk"] = $r["attack"];
  473. $this->base_stats["def"] = $r["defense"];
  474. $this->base_stats["spd"] = $r["speed"];
  475. $this->base_stats["crr"] = $r["crit_rate"];
  476. $this->base_stats["crd"] = $r["crit_damage"];
  477. $this->base_stats["res"] = $r["resistance"];
  478. $this->base_stats["acc"] = $r["accuracy"];
  479. $this->base_stats["ehp"] = APPLICATION::calculate_ehp($r["hp"], $r["defense"]);
  480. $this->base_stats["dmg"] = APPLICATION::calculate_dmg($r["attack"], $r["crit_rate"], $r["crit_damage"]);
  481. $this->experience = $r["experience"];
  482. $this->exp_gained = $r["exp_gained"];
  483. $this->exp_gain_rate = $r["exp_gain_rate"];
  484. $this->costume = $r["costume"];
  485. if ($r["source"]){
  486. $this->source_id = $r["source"];
  487. }
  488. $this->create_time = new Datetime();
  489. $this->create_time->setTimestamp($r["create_time"]);
  490. $this->homunculus_name = $r["homunculus_name"];
  491. if ($this->monster->is_homunculus()){
  492. $this->title = $this->homunculus_name;
  493. }
  494. else{
  495. $this->title = $this->monster->get_title();
  496. }
  497. $this->lock = $r["lock"];
  498. }
  499. }
  500. private function load_skills(){
  501. $statement = get_context()->get_db()->prepare("
  502. SELECT level
  503. FROM
  504. unit_skill,
  505. skill
  506. WHERE
  507. skill.id = unit_skill.skill AND
  508. unit_skill.unit = :unit
  509. ORDER BY slot;
  510. ");
  511. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  512. $q = $statement->execute();
  513. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  514. array_push($this->skill_levels, $r["level"]);
  515. }
  516. $this->flag_skills = true;
  517. }
  518. private function load_experience(){
  519. $statement = get_context()->get_db()->prepare("
  520. SELECT exp
  521. FROM experience
  522. WHERE
  523. stars = :stars AND
  524. level = :level;
  525. ");
  526. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  527. $statement->bindValue(':level', $this->level, SQLITE3_TEXT);
  528. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  529. if ($r){
  530. $this->experience_level = $r["exp"];
  531. $this->experience_level_up = $r["exp"] - $this->exp_gained;
  532. }
  533. $this->flag_experience = true;
  534. }
  535. /**
  536. * Checks if the monster is in storage and sets {@see $in_storage} to
  537. * true or false.
  538. */
  539. private function load_storage(){
  540. $statement = get_context()->get_db()->prepare("
  541. SELECT 1
  542. FROM
  543. unit,
  544. building
  545. WHERE
  546. unit.id = :id AND
  547. unit.player = :player AND
  548. building.player = :player AND
  549. unit.building = building.id AND
  550. building.buildable = :buildable;
  551. ");
  552. $statement->bindValue(':id', $this->id, SQLITE3_TEXT);
  553. $statement->bindValue(':player', $this->player, SQLITE3_TEXT);
  554. $statement->bindValue(':buildable', BUILDING_ID::MONSTER_STORAGE, SQLITE3_TEXT);
  555. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  556. if ($r){
  557. $this->in_storage = true;
  558. }
  559. else{
  560. $this->in_storage = false;
  561. }
  562. $this->flag_storage = true;
  563. }
  564. private function load_buildings(){
  565. $statement = get_context()->get_db()->prepare("
  566. SELECT
  567. affected_stat,
  568. bonus,
  569. element
  570. FROM
  571. decorative,
  572. decorative_level,
  573. decoration
  574. WHERE
  575. decoration.player = :player AND
  576. decorative.id = decoration.decorative AND
  577. decoration.decorative = decorative_level.decorative AND
  578. decoration.level = decorative_level.level AND
  579. area = :area AND
  580. affected_stat IN (:stat_atk, :stat_def, :stat_hp, :stat_spd, :stat_crd);
  581. ");
  582. $statement->bindValue(':player', $this->player, SQLITE3_TEXT);
  583. $statement->bindValue(':area', EFFECT_AREA_ID::GENERAL, SQLITE3_INTEGER);
  584. $statement->bindValue(':stat_atk', STAT_ID::ATK, SQLITE3_INTEGER);
  585. $statement->bindValue(':stat_def', STAT_ID::DEF, SQLITE3_INTEGER);
  586. $statement->bindValue(':stat_hp', STAT_ID::HP, SQLITE3_INTEGER);
  587. $statement->bindValue(':stat_spd', STAT_ID::SPD, SQLITE3_INTEGER);
  588. $statement->bindValue(':stat_crd', STAT_ID::CRIT_DMG, SQLITE3_INTEGER);
  589. $q = $statement->execute();
  590. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  591. switch($r["affected_stat"]){
  592. case STAT_ID::DEF:
  593. $this->building_stats["def"] += ($this->base_stats["def"]* $r["bonus"] / 100);
  594. break;
  595. case STAT_ID::HP:
  596. $this->building_stats["hp"] += ($this->base_stats["hp"]* $r["bonus"] / 100);
  597. break;
  598. case STAT_ID::SPD:
  599. $this->building_stats["spd"] += ($this->base_stats["spd"]* $r["bonus"] / 100);
  600. break;
  601. case STAT_ID::CRIT_DMG:
  602. $this->building_stats["crd"] += $r["bonus"];
  603. break;
  604. case STAT_ID::ATK:
  605. if(strlen($r["element"]) > 0 && $r["element"] == $this->monster->get_element()){
  606. $this->building_stats["atk"] += ($this->base_stats["atk"]* $r["bonus"] / 100);
  607. }
  608. else{
  609. $this->building_stats["atk"] += ($this->base_stats["atk"]* $r["bonus"] / 100);
  610. }
  611. break;
  612. }
  613. }
  614. $this->building_stats["hp"] = ceil($this->building_stats["hp"]);
  615. $this->building_stats["atk"] = ceil($this->building_stats["atk"]);
  616. $this->building_stats["def"] = ceil($this->building_stats["def"]);
  617. $this->building_stats["spd"] = ceil($this->building_stats["spd"]);
  618. $this->building_stats["ehp"] = APPLICATION::calculate_ehp($this->building_stats["hp"], $this->building_stats["def"]);
  619. $this->building_stats["dmg"] = APPLICATION::calculate_dmg($this->building_stats["atk"], $this->building_stats["crr"], $this->building_stats["crd"]);
  620. $this->flag_buildings = true;
  621. }
  622. private function load_source(){
  623. if ($this->source_id != null){
  624. $this->source = new Source($this->source_id);
  625. }
  626. $this->flag_source = true;
  627. }
  628. private function load_runes(){
  629. // Runes
  630. $statement = get_context()->get_db()->prepare("
  631. SELECT rune
  632. FROM unit_rune
  633. WHERE
  634. unit = :unit AND
  635. rta = :rta;
  636. ");
  637. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  638. $statement->bindValue(':rta', get_context()->get_game_mode(), SQLITE3_TEXT);
  639. $q = $statement->execute();
  640. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  641. array_push($this->rune, new Rune($r["rune"]));
  642. }
  643. foreach ($this->rune as $rune){
  644. foreach ($rune->get_stats() as $stat){
  645. $this->sum_rune_stat($stat->get_stat(), $stat->get_value() + $stat->get_grind());
  646. }
  647. }
  648. $this->rune_stats["atk"] = ceil($this->rune_stats["atk"]);
  649. $this->rune_stats["def"] = ceil($this->rune_stats["def"]);
  650. $this->rune_stats["hp"] = ceil($this->rune_stats["hp"]);
  651. $this->rune_stats["ehp"] = APPLICATION::calculate_ehp($this->rune_stats["hp"], $this->rune_stats["def"]);
  652. $this->rune_stats["dmg"] = APPLICATION::calculate_dmg($this->rune_stats["atk"], $this->rune_stats["crr"], $this->rune_stats["crd"]);
  653. // Artifacts
  654. $this->artifact = [];
  655. $statement = get_context()->get_db()->prepare("
  656. SELECT artifact
  657. FROM unit_artifact
  658. WHERE
  659. unit = :unit AND
  660. rta = :rta;
  661. ");
  662. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  663. $statement->bindValue(':rta', get_context()->get_game_mode(), SQLITE3_TEXT);
  664. $q = $statement->execute();
  665. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  666. $artifact = new Artifact($r["artifact"]);
  667. if ($artifact->is_archetypal()){
  668. $this->artifact_type = $artifact;
  669. }
  670. else{
  671. $this->artifact_element = $artifact;
  672. }
  673. switch($artifact->get_stat()->get_stat()){
  674. case ARTIFACT_STAT_ID::ATK:
  675. $this->artifact_stats["atk"] += $artifact->get_stat()->get_value();
  676. break;
  677. case ARTIFACT_STAT_ID::DEF:
  678. $this->artifact_stats["def"] += $artifact->get_stat()->get_value();
  679. break;
  680. case ARTIFACT_STAT_ID::HP:
  681. $this->artifact_stats["hp"] += $artifact->get_stat()->get_value();
  682. break;
  683. }
  684. }
  685. $this->artifact_stats["ehp"] = APPLICATION::calculate_ehp($this->artifact_stats["hp"], $this->artifact_stats["def"]);
  686. $this->artifact_stats["dmg"] = APPLICATION::calculate_dmg($this->artifact_stats["atk"], $this->artifact_stats["crr"], $this->artifact_stats["crd"]);
  687. $this->flag_runes = true;
  688. }
  689. // TODO: Other loaders??
  690. /**
  691. * Calculates the stats gained from runes, artifacts and buildings.
  692. * Calculates the totalls and the effective stats.
  693. *
  694. * @gobal Player Currently selected player.
  695. * @gobal int Game mode (Normal/RTA).
  696. */
  697. private function load_stats(){
  698. if (! $this->flag_buildings){
  699. $this->load_buildings();
  700. }
  701. if (! $this->flag_runes){
  702. $this->load_runes();
  703. }
  704. $keys = ["atk", "def", "hp", "spd", "crr", "crd", "res", "acc", "ehp", "dmg"];
  705. foreach ($keys as $key){
  706. $this->stats[$key] = $this->base_stats[$key] + $this->rune_stats[$key] + $this->artifact_stats[$key] + $this->building_stats[$key];
  707. }
  708. $this->flag_stats = true;
  709. }
  710. /**
  711. * Loads the {@see teams} array, with the teams the unit is on.
  712. */
  713. private function load_teams(){
  714. $statement = get_context()->get_db()->prepare("
  715. SELECT DISTINCT team.id AS id
  716. FROM
  717. team,
  718. team_unit
  719. WHERE
  720. team.player = :player AND
  721. team.id = team_unit.team AND
  722. team_unit.unit = :unit;
  723. ");
  724. $statement->bindValue(':player', $this->player, SQLITE3_TEXT);
  725. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  726. $q = $statement->execute();
  727. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  728. array_push($this->teams, new Team($r["id"], false));
  729. }
  730. $this->flag_teams = true;
  731. }
  732. /**
  733. * Calculates the stat increase by a rune property.
  734. *
  735. * @param string $stat Stat type.
  736. * @param int $value Stat increase value.
  737. */
  738. private function sum_rune_stat($stat, $value){
  739. switch ($stat){
  740. case RUNE_STAT_ID::ATK:
  741. $this->rune_stats["atk"] += $value;
  742. break;
  743. case RUNE_STAT_ID::DEF:
  744. $this->rune_stats["def"] += $value;
  745. break;
  746. case RUNE_STAT_ID::HP:
  747. $this->rune_stats["hp"] += $value;
  748. break;
  749. case RUNE_STAT_ID::SPD:
  750. $this->rune_stats["spd"] += $value;
  751. break;
  752. case RUNE_STAT_ID::CRR:
  753. $this->rune_stats["crr"] += $value;
  754. break;
  755. case RUNE_STAT_ID::CRD:
  756. $this->rune_stats["crd"] += $value;
  757. break;
  758. case RUNE_STAT_ID::ACC:
  759. $this->rune_stats["acc"] += $value;
  760. break;
  761. case RUNE_STAT_ID::RES:
  762. $this->rune_stats["res"] += $value;
  763. break;
  764. case RUNE_STAT_ID::ATK_P:
  765. $this->rune_stats["atk"] += ($this->base_stats["atk"] * $value / 100);
  766. break;
  767. case RUNE_STAT_ID::DEF_P:
  768. $this->rune_stats["def"] += ($this->base_stats["def"] * $value / 100);
  769. break;
  770. case RUNE_STAT_ID::HP_P:
  771. $this->rune_stats["hp"] += ($this->base_stats["hp"] * $value / 100);
  772. break;
  773. }
  774. }
  775. /**
  776. * Checks if the unit skills are completly maxed.
  777. *
  778. * @return bool True if all skills maxed, false otherwise.
  779. */
  780. public function is_fully_skilled(){
  781. if (! $this->flag_skills){
  782. $this->load_skills();
  783. }
  784. $maxed = true;
  785. $skills = $this->monster->get_skills();
  786. for ($i = 0; $i < sizeof($skills); $i ++){
  787. if ($skills[$i]->get_max_level() > $this->skill_levels[$i]){
  788. $maxed = false;
  789. break;
  790. }
  791. }
  792. return $maxed;
  793. }
  794. }
  795. ?>