Unit.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. if ($r){
  234. $this->uid = $r["uid"];
  235. $this->id = $r["id"];
  236. if ($r["building"] != null && $r["building"] > 0){
  237. $this->building = new Building($r["building"]);
  238. }
  239. if (strlen($r["unit"]) > 0){
  240. $this->unit = new K_Unit($r["unit"], $complete);
  241. }
  242. $this->stars = $r["stars"];
  243. $this->level = $r["level"];
  244. $this->hp = $r["hp"];
  245. $this->attack = $r["attack"];
  246. $this->defense = $r["defense"];
  247. $this->speed = $r["speed"];
  248. $this->crit_rate = $r["crit_rate"];
  249. $this->crit_damage = $r["crit_damage"];
  250. $this->resistance = $r["resistance"];
  251. $this->accuracy = $r["accuracy"];
  252. $this->experience = $r["experience"];
  253. $this->exp_gained = $r["exp_gained"];
  254. $this->exp_gain_rate = $r["exp_gain_rate"];
  255. $this->costume = $r["costume"];
  256. $this->source = new K_Source($r["source"]);
  257. $this->create_time = date_create($r["create_time"]);
  258. $this->homunculus_name = $r["homunculus_name"];
  259. if ($complete){
  260. $this->load_runes();
  261. $this->load_artifacts();
  262. $this->load_teams();
  263. $this->check_storage();
  264. $this->lock = $r["lock"];
  265. $s =
  266. "SELECT level " .
  267. "FROM " .
  268. " unit_skill, " .
  269. " k_skill " .
  270. "WHERE " .
  271. " k_skill.id = unit_skill.skill AND " .
  272. " unit = '$this->id' " .
  273. "ORDER BY slot;";
  274. $q = $db->query($s);
  275. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  276. array_push($this->skill_levels, $r["level"]);
  277. }
  278. $s =
  279. "SELECT exp " .
  280. "FROM k_experience " .
  281. "WHERE " .
  282. " stars = $this->stars AND " .
  283. " level = $this->level;";
  284. $q = $db->query($s);
  285. $r = $q->fetchArray(SQLITE3_ASSOC);
  286. $this->experience_level = $r["exp"];
  287. $this->experience_level_up = $r["exp"] - $this->exp_gained;
  288. }
  289. if ($this->unit->homunculus){
  290. $this->title = $this->homunculus_name;
  291. }
  292. else{
  293. $this->title = $this->unit->title;
  294. }
  295. }
  296. }
  297. /**
  298. * Checks if the monster is in storage and sets {@see $in_storage} to
  299. * true or false.
  300. *
  301. * @global int Player ID.
  302. * @global resource Database connection.
  303. */
  304. public function check_storage(){
  305. global $UID;
  306. global $db;
  307. $s = "
  308. SELECT 1
  309. FROM
  310. unit,
  311. building
  312. WHERE
  313. unit.id = $this->id AND
  314. unit.uid = '$UID' AND
  315. building.uid = '$UID' AND
  316. unit.building = building.id AND
  317. building.building = '" . BUILDING_ID::MONSTER_STORAGE . "';
  318. ";
  319. $q = $db->query($s);
  320. if ($q->fetchArray(SQLITE3_ASSOC) == false){
  321. $this->in_storage = false;
  322. }
  323. else{
  324. $this->in_storage = true;
  325. }
  326. }
  327. /**
  328. * Loads the {@see teams} array, with the teams the unit is on.
  329. *
  330. * @global int Player ID.
  331. * @global resource Database connection.
  332. */
  333. public function load_teams(){
  334. global $UID;
  335. global $db;
  336. $this->teams = [];
  337. $s = "
  338. SELECT DISTINCT team.id AS id
  339. FROM
  340. team,
  341. team_unit
  342. WHERE
  343. team.uid = '$UID' AND
  344. team.id = team_unit.team AND
  345. team_unit.unit = '" . $this->id . "';
  346. ";
  347. $q = $db->query($s);
  348. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  349. array_push($this->teams, new Team($r["id"], false));
  350. }
  351. }
  352. /**
  353. * Loads the information about the equiped runes.
  354. * It is autoatically called at construction time if the
  355. * $complete parameter is true (by default). No point in calling it
  356. * twice.
  357. *
  358. * @global resource Database connection.
  359. */
  360. public function load_runes(){
  361. global $db;
  362. $this->rune = [];
  363. $s =
  364. "SELECT id " .
  365. "FROM rune " .
  366. "WHERE assigned_to = '$this->id'; ";
  367. $q = $db->query($s);
  368. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  369. array_push($this->rune, new Rune($r["id"]));
  370. }
  371. foreach ($this->rune as $rune){
  372. $this->sum_rune_stat($rune->main_stat, $rune->main_stat_value);
  373. $this->sum_rune_stat($rune->innate_stat, $rune->innate_stat_value);
  374. $this->sum_rune_stat($rune->substat_1, $rune->substat_1_value + $rune->substat_1_grind);
  375. $this->sum_rune_stat($rune->substat_2, $rune->substat_2_value + $rune->substat_2_grind);
  376. $this->sum_rune_stat($rune->substat_3, $rune->substat_3_value + $rune->substat_3_grind);
  377. $this->sum_rune_stat($rune->substat_4, $rune->substat_4_value + $rune->substat_4_grind);
  378. }
  379. $this->rune_attack = ceil($this->rune_attack);
  380. $this->rune_defense = ceil($this->rune_defense);
  381. $this->rune_hp = ceil($this->rune_hp);
  382. }
  383. /**
  384. * Loads the information about the equiped artifacts.
  385. * It is autoatically called at construction time if the
  386. * $complete parameter is true (by default). No point in calling it
  387. * twice.
  388. *
  389. * @global resource Database connection.
  390. */
  391. public function load_artifacts(){
  392. global $db;
  393. $s =
  394. "SELECT id " .
  395. "FROM artifact " .
  396. "WHERE assigned_to = '$this->id'; ";
  397. $q = $db->query($s);
  398. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  399. $artifact = new Artifact($r["id"]);
  400. if ($artifact->type == ARTIFACT_TYPE::ARCHETYPE){
  401. $this->artifact_type = $artifact;
  402. }
  403. else{
  404. $this->artifact_element = $artifact;
  405. }
  406. $this->sum_rune_stat($artifact->main_stat, $artifact->main_stat_value);
  407. }
  408. }
  409. /**
  410. * Calculates the stat increase by a rune property.
  411. *
  412. * @param string $stat Stat type.
  413. * @param int $value Stat increase value.
  414. */
  415. private function sum_rune_stat($stat, $value){
  416. switch ($stat){
  417. case RUNE_STAT_ID::ATK:
  418. $this->rune_attack += $value;
  419. break;
  420. case RUNE_STAT_ID::DEF:
  421. $this->rune_defense += $value;
  422. break;
  423. case RUNE_STAT_ID::HP:
  424. $this->rune_hp += $value;
  425. break;
  426. case RUNE_STAT_ID::SPD:
  427. $this->rune_speed += $value;
  428. break;
  429. case RUNE_STAT_ID::CRR:
  430. $this->rune_crit_rate += $value;
  431. break;
  432. case RUNE_STAT_ID::CRD:
  433. $this->rune_crit_damage += $value;
  434. break;
  435. case RUNE_STAT_ID::ACC:
  436. $this->rune_accuracy += $value;
  437. break;
  438. case RUNE_STAT_ID::RES:
  439. $this->rune_resistance += $value;
  440. break;
  441. case RUNE_STAT_ID::ATK_P:
  442. $this->rune_attack += ($this->attack * $value / 100);
  443. break;
  444. case RUNE_STAT_ID::DEF_P:
  445. $this->rune_defense += ($this->defense * $value / 100);
  446. break;
  447. case RUNE_STAT_ID::HP_P:
  448. $this->rune_hp += ($this->hp * $value / 100);
  449. break;
  450. }
  451. }
  452. /**
  453. * Checks if the unit skills are completly maxed.
  454. *
  455. * @return bool True if all skills maxed, false otherwise.
  456. */
  457. public function are_skills_maxed(){
  458. $maxed = true;
  459. for ($i = 0; $i < sizeof($this->unit->skill); $i ++){
  460. if ($this->unit->skill[$i]->max_level > $this->skill_levels[$i]){
  461. $maxed = false;
  462. break;
  463. }
  464. }
  465. return $maxed;
  466. }
  467. }
  468. ?>