Unit.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. /**
  27. * @var int Player ID.
  28. */
  29. public $player;
  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 Monster Base monster.
  40. */
  41. public $monster;
  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 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 unit and monster.
  301. */
  302. public function __construct($id, $complete = true, $player = -1){
  303. if ($player == -1 && null != get_context()->get_player()){
  304. $player = get_context()->get_player()->get_id();
  305. }
  306. $statement = get_context()->get_db()->prepare("
  307. SELECT
  308. player,
  309. id,
  310. building,
  311. monster,
  312. stars,
  313. level,
  314. hp,
  315. attack,
  316. defense,
  317. speed,
  318. crit_rate,
  319. crit_damage,
  320. resistance,
  321. accuracy,
  322. experience,
  323. exp_gained,
  324. exp_gain_rate,
  325. costume,
  326. source,
  327. create_time,
  328. homunculus_name,
  329. lock
  330. FROM unit
  331. WHERE
  332. id = :id;
  333. ");
  334. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  335. $statement->bindValue(':player', $player, SQLITE3_TEXT);
  336. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  337. if ($r){
  338. $this->player = $r["player"];
  339. $this->id = $r["id"];
  340. if ($r["building"] != null && $r["building"] > 0){
  341. $this->building = new Building($r["building"]);
  342. }
  343. if (strlen($r["monster"]) > 0){
  344. $this->monster = new Monster($r["monster"], $complete);
  345. }
  346. $this->stars = $r["stars"];
  347. $this->level = $r["level"];
  348. $this->hp = $r["hp"];
  349. $this->attack = $r["attack"];
  350. $this->defense = $r["defense"];
  351. $this->speed = $r["speed"];
  352. $this->crit_rate = $r["crit_rate"];
  353. $this->crit_damage = $r["crit_damage"];
  354. $this->resistance = $r["resistance"];
  355. $this->accuracy = $r["accuracy"];
  356. $this->experience = $r["experience"];
  357. $this->exp_gained = $r["exp_gained"];
  358. $this->exp_gain_rate = $r["exp_gain_rate"];
  359. $this->costume = $r["costume"];
  360. $this->source = new Source($r["source"]);
  361. $this->create_time = date_create($r["create_time"]);
  362. $this->homunculus_name = $r["homunculus_name"];
  363. if ($complete){
  364. $this->load_stats();
  365. $this->load_teams();
  366. $this->check_storage();
  367. $this->lock = $r["lock"];
  368. $statement = get_context()->get_db()->prepare("
  369. SELECT level
  370. FROM
  371. unit_skill,
  372. skill
  373. WHERE
  374. skill.id = unit_skill.skill AND
  375. unit_skill.unit = :unit
  376. ORDER BY slot;
  377. ");
  378. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  379. $q = $statement->execute();
  380. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  381. array_push($this->skill_levels, $r["level"]);
  382. }
  383. $statement = get_context()->get_db()->prepare("
  384. SELECT exp
  385. FROM experience
  386. WHERE
  387. stars = :stars AND
  388. level = :level;
  389. ");
  390. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  391. $statement->bindValue(':level', $this->level, SQLITE3_TEXT);
  392. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  393. if ($r){
  394. $this->experience_level = $r["exp"];
  395. $this->experience_level_up = $r["exp"] - $this->exp_gained;
  396. }
  397. }
  398. if ($this->monster->homunculus){
  399. $this->title = $this->homunculus_name;
  400. }
  401. else{
  402. $this->title = $this->monster->title;
  403. }
  404. }
  405. }
  406. /**
  407. * Checks if the monster is in storage and sets {@see $in_storage} to
  408. * true or false.
  409. */
  410. public function check_storage(){
  411. $statement = get_context()->get_db()->prepare("
  412. SELECT 1
  413. FROM
  414. unit,
  415. building
  416. WHERE
  417. unit.id = :id AND
  418. unit.player = :player AND
  419. building.player = :player AND
  420. unit.building = building.id AND
  421. building.buildable = :buildable;
  422. ");
  423. $statement->bindValue(':id', $this->id, SQLITE3_TEXT);
  424. $statement->bindValue(':player', $this->player, SQLITE3_TEXT);
  425. $statement->bindValue(':buildable', BUILDING_ID::MONSTER_STORAGE, SQLITE3_TEXT);
  426. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  427. if ($r){
  428. $this->in_storage = true;
  429. }
  430. else{
  431. $this->in_storage = false;
  432. }
  433. }
  434. /**
  435. * Calculates the stats gained from runes, artifacts and buildings.
  436. * Calculates the totalls and the effective stats.
  437. *
  438. * @gobal Player Currently selected player.
  439. * @gobal int Game mode (Normal/RTA).
  440. */
  441. public function load_stats(){
  442. // Runes
  443. $this->rune = [];
  444. $statement = get_context()->get_db()->prepare("
  445. SELECT rune
  446. FROM unit_rune
  447. WHERE
  448. unit = :unit AND
  449. rta = :rta;
  450. ");
  451. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  452. $statement->bindValue(':rta', get_context()->get_game_mode(), SQLITE3_TEXT);
  453. $q = $statement->execute();
  454. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  455. array_push($this->rune, new Rune($r["rune"]));
  456. }
  457. foreach ($this->rune as $rune){
  458. foreach ($rune->stats as $stat){
  459. $this->sum_rune_stat($stat->stat, $stat->value + $stat->grind);
  460. }
  461. }
  462. $this->rune_attack = ceil($this->rune_attack);
  463. $this->rune_defense = ceil($this->rune_defense);
  464. $this->rune_hp = ceil($this->rune_hp);
  465. // Artifacts
  466. $this->artifact = [];
  467. $statement = get_context()->get_db()->prepare("
  468. SELECT artifact
  469. FROM unit_artifact
  470. WHERE
  471. unit = :unit AND
  472. rta = :rta;
  473. ");
  474. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  475. $statement->bindValue(':rta', get_context()->get_game_mode(), SQLITE3_TEXT);
  476. $q = $statement->execute();
  477. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  478. $artifact = new Artifact($r["artifact"]);
  479. if ($artifact->type == ARTIFACT_TYPE::ARCHETYPE){
  480. $this->artifact_type = $artifact;
  481. }
  482. else{
  483. $this->artifact_element = $artifact;
  484. }
  485. switch($artifact->stat->getStat()){
  486. case ARTIFACT_STAT_ID::ATK:
  487. $this->artifact_attack += $artifact->stat->getValue();
  488. break;
  489. case ARTIFACT_STAT_ID::DEF:
  490. $this->artifact_defense += $artifact->stat->getValue();
  491. break;
  492. case ARTIFACT_STAT_ID::HP:
  493. $this->artifact_hp += $artifact->stat->getValue();
  494. break;
  495. }
  496. }
  497. // Buildings
  498. $statement = get_context()->get_db()->prepare("
  499. SELECT
  500. affected_stat,
  501. bonus,
  502. element
  503. FROM
  504. decorative,
  505. decorative_level,
  506. decoration
  507. WHERE
  508. decoration.player = :player AND
  509. decorative.id = decoration.decorative AND
  510. decoration.decorative = decorative_level.decorative AND
  511. decoration.level = decorative_level.level AND
  512. area = :area AND
  513. affected_stat IN (:stat_atk, :stat_def, :stat_hp, :stat_spd, :stat_crd);
  514. ");
  515. $statement->bindValue(':player', $this->player, SQLITE3_TEXT);
  516. $statement->bindValue(':area', EFFECT_AREA_ID::GENERAL, SQLITE3_INTEGER);
  517. $statement->bindValue(':stat_atk', STAT_ID::ATK, SQLITE3_INTEGER);
  518. $statement->bindValue(':stat_def', STAT_ID::DEF, SQLITE3_INTEGER);
  519. $statement->bindValue(':stat_hp', STAT_ID::HP, SQLITE3_INTEGER);
  520. $statement->bindValue(':stat_spd', STAT_ID::SPD, SQLITE3_INTEGER);
  521. $statement->bindValue(':stat_crd', STAT_ID::CRIT_DMG, SQLITE3_INTEGER);
  522. $q = $statement->execute();
  523. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  524. switch($r["affected_stat"]){
  525. case STAT_ID::DEF:
  526. $this->building_defense += ($this->defense * $r["bonus"] / 100);
  527. break;
  528. case STAT_ID::HP:
  529. $this->building_hp += ($this->hp * $r["bonus"] / 100);
  530. break;
  531. case STAT_ID::SPD:
  532. $this->building_speed += ($this->speed * $r["bonus"] / 100);
  533. break;
  534. case STAT_ID::CRIT_DMG:
  535. $this->building_crit_damage += $r["bonus"];
  536. break;
  537. case STAT_ID::ATK:
  538. if(strlen($r["element"]) > 0 && $r["element"] == $this->monster->element){
  539. $this->building_attack += ($this->attack * $r["bonus"] / 100);
  540. }
  541. else{
  542. $this->building_attack += ($this->attack * $r["bonus"] / 100);
  543. }
  544. break;
  545. }
  546. }
  547. $this->building_attack = ceil($this->building_attack);
  548. $this->building_defense = ceil($this->building_defense);
  549. $this->building_hp = ceil($this->building_hp);
  550. $this->building_speed = ceil($this->building_speed);
  551. $this->building_crit_damage = ceil($this->building_crit_damage);
  552. // Totals
  553. $this->total_hp = $this->hp + $this->rune_hp + $this->building_hp + $this->artifact_hp;
  554. $this->total_attack = $this->attack + $this->rune_attack + $this->building_attack + $this->artifact_attack;
  555. $this->total_defense = $this->defense + $this->rune_defense + $this->building_defense + $this->artifact_defense;
  556. $this->total_speed = $this->speed + $this->rune_speed + $this->building_speed;
  557. $this->total_crit_rate = $this->crit_rate + $this->rune_crit_rate + $this->building_crit_rate;
  558. $this->total_crit_damage = $this->crit_damage + $this->rune_crit_damage + $this->building_crit_damage;
  559. $this->total_accuracy = $this->accuracy + $this->rune_accuracy + $this->building_accuracy;
  560. $this->total_resistance = $this->resistance + $this->rune_resistance + $this->building_resistance;
  561. // Effective stats
  562. $ehp = 0;
  563. $edmg = 0;
  564. // Calculate effective_hp
  565. $hp = $this->total_hp;
  566. $def = $this->total_defense;
  567. $ehp = ceil(((($def * 3.5) + 1140) * $hp) / 1000);
  568. $this->effective_hp = $ehp;
  569. // Calculate effective_dmg
  570. $atk = $this->total_attack;
  571. $crr = $this->total_crit_rate;
  572. if ($crr > 100){
  573. $crr = 100;
  574. }
  575. $crd = $this->total_crit_damage;
  576. $edmg = ceil(($atk * (100 - $crr) / 100) + (($atk + ($atk * $crd / 100)) * $crr / 100));
  577. $this->effective_dmg = $edmg;
  578. }
  579. /**
  580. * Loads the {@see teams} array, with the teams the unit is on.
  581. */
  582. public function load_teams(){
  583. $this->teams = [];
  584. $statement = get_context()->get_db()->prepare("
  585. SELECT DISTINCT team.id AS id
  586. FROM
  587. team,
  588. team_unit
  589. WHERE
  590. team.player = :player AND
  591. team.id = team_unit.team AND
  592. team_unit.unit = :unit;
  593. ");
  594. $statement->bindValue(':player', $this->player, SQLITE3_TEXT);
  595. $statement->bindValue(':unit', $this->id, SQLITE3_TEXT);
  596. $q = $statement->execute();
  597. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  598. array_push($this->teams, new Team($r["id"], false));
  599. }
  600. }
  601. /**
  602. * Calculates the stat increase by a rune property.
  603. *
  604. * @param string $stat Stat type.
  605. * @param int $value Stat increase value.
  606. */
  607. private function sum_rune_stat($stat, $value){
  608. switch ($stat){
  609. case RUNE_STAT_ID::ATK:
  610. $this->rune_attack += $value;
  611. break;
  612. case RUNE_STAT_ID::DEF:
  613. $this->rune_defense += $value;
  614. break;
  615. case RUNE_STAT_ID::HP:
  616. $this->rune_hp += $value;
  617. break;
  618. case RUNE_STAT_ID::SPD:
  619. $this->rune_speed += $value;
  620. break;
  621. case RUNE_STAT_ID::CRR:
  622. $this->rune_crit_rate += $value;
  623. break;
  624. case RUNE_STAT_ID::CRD:
  625. $this->rune_crit_damage += $value;
  626. break;
  627. case RUNE_STAT_ID::ACC:
  628. $this->rune_accuracy += $value;
  629. break;
  630. case RUNE_STAT_ID::RES:
  631. $this->rune_resistance += $value;
  632. break;
  633. case RUNE_STAT_ID::ATK_P:
  634. $this->rune_attack += ($this->attack * $value / 100);
  635. break;
  636. case RUNE_STAT_ID::DEF_P:
  637. $this->rune_defense += ($this->defense * $value / 100);
  638. break;
  639. case RUNE_STAT_ID::HP_P:
  640. $this->rune_hp += ($this->hp * $value / 100);
  641. break;
  642. }
  643. }
  644. /**
  645. * Checks if the unit skills are completly maxed.
  646. *
  647. * @return bool True if all skills maxed, false otherwise.
  648. */
  649. public function are_skills_maxed(){
  650. $maxed = true;
  651. for ($i = 0; $i < sizeof($this->monster->skill); $i ++){
  652. if ($this->monster->skill[$i]->max_level > $this->skill_levels[$i]){
  653. $maxed = false;
  654. break;
  655. }
  656. }
  657. return $maxed;
  658. }
  659. }
  660. ?>