Unit.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 float Average rune efficiency.
  148. */
  149. public $avg_rune_efficiency = 0.0;
  150. /**
  151. * @var \Rune[] Equiped Runes.
  152. */
  153. public $rune = [];
  154. /**
  155. * @var Artifact Type artifact equipped.
  156. */
  157. public $artifact_type;
  158. /**
  159. * @var Artifact Elemental artifact equipped.
  160. */
  161. public $artifact_element;
  162. /**
  163. * @var \Team[] The Teamss the unit is on.
  164. */
  165. public $teams = [];
  166. /**
  167. * @var int[] Array with the levels of the skills, in orer.
  168. */
  169. public $skill_levels = [];
  170. /**
  171. * @var int Total experience required for the current level.
  172. */
  173. public $experience_level = 0;
  174. /**
  175. * @var int Experience required to level up.
  176. */
  177. public $experience_level_up = 0;
  178. /**
  179. * @var int Indcates if the unit is in storage.
  180. */
  181. public $in_storage = false;
  182. /**
  183. * @var string Unit title.
  184. * If Homunculus,it will be it's name.
  185. * If awakened, it will be the awakened from name.
  186. * If unawakened, it will be <element> <name>.
  187. */
  188. public $title;
  189. /**
  190. * Constructor.
  191. *
  192. * Searches the database and retrieves the information about the
  193. * monster, populating it and it's items.
  194. *
  195. * @param int $id Monster identifier.
  196. * @param bool $complete If false, query only monster and k_monster.
  197. * @global int Player ID.
  198. * @global resource Database connection.
  199. */
  200. public function __construct($id, $complete = true){
  201. global $UID;
  202. global $db;
  203. $s =
  204. "SELECT " .
  205. " uid, " .
  206. " id, " .
  207. " building, " .
  208. " unit, " .
  209. " stars, " .
  210. " level, " .
  211. " hp, " .
  212. " attack, " .
  213. " defense, " .
  214. " speed, " .
  215. " crit_rate, " .
  216. " crit_damage, " .
  217. " resistance, " .
  218. " accuracy, " .
  219. " experience, " .
  220. " exp_gained, " .
  221. " exp_gain_rate, " .
  222. " costume, " .
  223. " source, " .
  224. " create_time, " .
  225. " homunculus_name, " .
  226. " lock " .
  227. "FROM unit " .
  228. "WHERE " .
  229. " uid = '$UID' AND " .
  230. " id = '$id'; ";
  231. $q = $db->query($s);
  232. $r = $q->fetchArray(SQLITE3_ASSOC);
  233. $this->uid = $r["uid"];
  234. $this->id = $r["id"];
  235. if ($r["building"] != null && $r["building"] > 0){
  236. $this->building = new Building($r["building"]);
  237. }
  238. if (strlen($r["unit"]) > 0){
  239. $this->unit = new K_Unit($r["unit"], $complete);
  240. }
  241. $this->stars = $r["stars"];
  242. $this->level = $r["level"];
  243. $this->hp = $r["hp"];
  244. $this->attack = $r["attack"];
  245. $this->defense = $r["defense"];
  246. $this->speed = $r["speed"];
  247. $this->crit_rate = $r["crit_rate"];
  248. $this->crit_damage = $r["crit_damage"];
  249. $this->resistance = $r["resistance"];
  250. $this->accuracy = $r["accuracy"];
  251. $this->experience = $r["experience"];
  252. $this->exp_gained = $r["exp_gained"];
  253. $this->exp_gain_rate = $r["exp_gain_rate"];
  254. $this->costume = $r["costume"];
  255. $this->source = new K_Source($r["source"]);
  256. $this->create_time = date_create($r["create_time"]);
  257. $this->homunculus_name = $r["homunculus_name"];
  258. if ($complete){
  259. $this->load_runes();
  260. $this->load_artifacts();
  261. $this->load_teams();
  262. $this->check_storage();
  263. $this->lock = $r["lock"];
  264. $s =
  265. "SELECT level " .
  266. "FROM " .
  267. " unit_skill, " .
  268. " k_skill " .
  269. "WHERE " .
  270. " k_skill.id = unit_skill.skill AND " .
  271. " unit = '$this->id' " .
  272. "ORDER BY slot;";
  273. $q = $db->query($s);
  274. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  275. array_push($this->skill_levels, $r["level"]);
  276. }
  277. $s =
  278. "SELECT exp " .
  279. "FROM k_experience " .
  280. "WHERE " .
  281. " stars = $this->stars AND " .
  282. " level = $this->level;";
  283. $q = $db->query($s);
  284. $r = $q->fetchArray(SQLITE3_ASSOC);
  285. $this->experience_level = $r["exp"];
  286. $this->experience_level_up = $r["exp"] - $this->exp_gained;
  287. }
  288. if ($this->unit->homunculus){
  289. $this->title = $this->homunculus_name;
  290. }
  291. else{
  292. $this->title = $this->unit->title;
  293. }
  294. }
  295. /**
  296. * Checks if the monster is in storage and sets {@see $in_storage} to
  297. * true or false.
  298. *
  299. * @global int Player ID.
  300. * @global resource Database connection.
  301. */
  302. public function check_storage(){
  303. global $UID;
  304. global $db;
  305. $s = "
  306. SELECT 1
  307. FROM
  308. unit,
  309. building
  310. WHERE
  311. unit.id = $this->id AND
  312. unit.uid = '$UID' AND
  313. building.uid = '$UID' AND
  314. unit.building = building.id AND
  315. building.building = '" . BUILDING_ID::MONSTER_STORAGE . "';
  316. ";
  317. $q = $db->query($s);
  318. if ($q->fetchArray(SQLITE3_ASSOC) == false){
  319. $this->in_storage = false;
  320. }
  321. else{
  322. $this->in_storage = true;
  323. }
  324. }
  325. /**
  326. * Loads the {@see teams} array, with the teams the unit is on.
  327. *
  328. * @global int Player ID.
  329. * @global resource Database connection.
  330. */
  331. public function load_teams(){
  332. global $UID;
  333. global $db;
  334. $this->teams = [];
  335. $s = "
  336. SELECT DISTINCT team.id AS id
  337. FROM
  338. team,
  339. team_unit
  340. WHERE
  341. team.uid = '$UID' AND
  342. team.id = team_unit.team AND
  343. team_unit.unit = '" . $this->id . "';
  344. ";
  345. $q = $db->query($s);
  346. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  347. array_push($this->teams, new Team($r["id"], false));
  348. }
  349. }
  350. /**
  351. * Loads the information about the equiped runes.
  352. * It is autoatically called at construction time if the
  353. * $complete parameter is true (by default). No point in calling it
  354. * twice.
  355. *
  356. * @global resource Database connection.
  357. */
  358. public function load_runes(){
  359. global $db;
  360. $this->rune = [];
  361. $s =
  362. "SELECT id " .
  363. "FROM rune " .
  364. "WHERE assigned_to = '$this->id'; ";
  365. $q = $db->query($s);
  366. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  367. array_push($this->rune, new Rune($r["id"]));
  368. }
  369. foreach ($this->rune as $rune){
  370. $this->sum_rune_stat($rune->main_stat, $rune->main_stat_value);
  371. $this->sum_rune_stat($rune->innate_stat, $rune->innate_stat_value);
  372. $this->sum_rune_stat($rune->substat_1, $rune->substat_1_value + $rune->substat_1_grind);
  373. $this->sum_rune_stat($rune->substat_2, $rune->substat_2_value + $rune->substat_2_grind);
  374. $this->sum_rune_stat($rune->substat_3, $rune->substat_3_value + $rune->substat_3_grind);
  375. $this->sum_rune_stat($rune->substat_4, $rune->substat_4_value + $rune->substat_4_grind);
  376. }
  377. $this->rune_attack = ceil($this->rune_attack);
  378. $this->rune_defense = ceil($this->rune_defense);
  379. $this->rune_hp = ceil($this->rune_hp);
  380. }
  381. /**
  382. * Loads the information about the equiped artifacts.
  383. * It is autoatically called at construction time if the
  384. * $complete parameter is true (by default). No point in calling it
  385. * twice.
  386. *
  387. * @global resource Database connection.
  388. */
  389. public function load_artifacts(){
  390. global $db;
  391. $s =
  392. "SELECT id " .
  393. "FROM artifact " .
  394. "WHERE assigned_to = '$this->id'; ";
  395. $q = $db->query($s);
  396. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  397. $artifact = new Artifact($r["id"]);
  398. if ($artifact->type == ARTIFACT_TYPE::ARCHETYPE){
  399. $this->artifact_type = $artifact;
  400. }
  401. else{
  402. $this->artifact_element = $artifact;
  403. }
  404. $this->sum_rune_stat($artifact->main_stat, $artifact->main_stat_value);
  405. }
  406. }
  407. /**
  408. * Calculates the stat increase by a rune property.
  409. *
  410. * @param string $stat Stat type.
  411. * @param int $value Stat increase value.
  412. */
  413. private function sum_rune_stat($stat, $value){
  414. switch ($stat){
  415. case RUNE_STAT_ID::ATK:
  416. $this->rune_attack += $value;
  417. break;
  418. case RUNE_STAT_ID::DEF:
  419. $this->rune_defense += $value;
  420. break;
  421. case RUNE_STAT_ID::HP:
  422. $this->rune_hp += $value;
  423. break;
  424. case RUNE_STAT_ID::SPD:
  425. $this->rune_speed += $value;
  426. break;
  427. case RUNE_STAT_ID::CRR:
  428. $this->rune_crit_rate += $value;
  429. break;
  430. case RUNE_STAT_ID::CRD:
  431. $this->rune_crit_damage += $value;
  432. break;
  433. case RUNE_STAT_ID::ACC:
  434. $this->rune_accuracy += $value;
  435. break;
  436. case RUNE_STAT_ID::RES:
  437. $this->rune_resistance += $value;
  438. break;
  439. case RUNE_STAT_ID::ATK_P:
  440. $this->rune_attack += ($this->attack * $value / 100);
  441. break;
  442. case RUNE_STAT_ID::DEF_P:
  443. $this->rune_defense += ($this->defense * $value / 100);
  444. break;
  445. case RUNE_STAT_ID::HP_P:
  446. $this->rune_hp += ($this->hp * $value / 100);
  447. break;
  448. }
  449. }
  450. /**
  451. * Checks if the unit skills are completly maxed.
  452. *
  453. * @return bool True if all skills maxed, false otherwise.
  454. */
  455. public function are_skills_maxed(){
  456. $maxed = true;
  457. for ($i = 0; $i < sizeof($this->unit->skill); $i ++){
  458. if ($this->unit->skill[$i]->max_level > $this->skill_levels[$i]){
  459. $maxed = false;
  460. break;
  461. }
  462. }
  463. return $maxed;
  464. }
  465. }
  466. ?>