Unit.php 22 KB

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