Unit.php 22 KB

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