Unit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. /**
  17. * A Unit.
  18. *
  19. * Represents an object from the table 'unit'.
  20. *
  21. * @category Entity
  22. */
  23. class Unit extends Entity{
  24. /**
  25. * @var int User ID.
  26. */
  27. public $uid;
  28. /**
  29. * @var int Unit ID.
  30. */
  31. public $id;
  32. /**
  33. * @var Building Building the unit is on.
  34. */
  35. public $building;
  36. /**
  37. * @var K_Unit Base unit.
  38. */
  39. public $unit;
  40. /**
  41. * @var int Unit stars.
  42. */
  43. public $stars;
  44. /**
  45. * @var int Unit level.
  46. */
  47. public $level;
  48. /**
  49. * @var int Unit base HP stat.
  50. */
  51. public $hp;
  52. /**
  53. * @var int Unit base ATK stat.
  54. */
  55. public $attack;
  56. /**
  57. * @var int Unit base DEF stat.
  58. */
  59. public $defense;
  60. /**
  61. * @var int Unit base SPD stat.
  62. */
  63. public $speed;
  64. /**
  65. * @var int Unit base CRR stat.
  66. */
  67. public $crit_rate;
  68. /**
  69. * @var int Unit base CRD stat.
  70. */
  71. public $crit_damage;
  72. /**
  73. * @var int Unit base RES stat.
  74. */
  75. public $resistance;
  76. /**
  77. * @var int Unit base ACC stat.
  78. */
  79. public $accuracy;
  80. /**
  81. * @var int Total gained experience.
  82. */
  83. public $experience;
  84. /**
  85. * @var int Total gained experience in its current level.
  86. */
  87. public $exp_gained;
  88. /**
  89. * @var int Experience / hour in the current building.
  90. */
  91. public $exp_gain_rate;
  92. /**
  93. * @var int Equipped transmogification.
  94. */
  95. public $costume;
  96. /**
  97. * @var int Unit attribute
  98. */
  99. public $attribute;
  100. /**
  101. * @var K_Source Source the unit was aquired from.
  102. */
  103. public $source;
  104. /**
  105. * @var Datetime Time the unit was obtained.
  106. */
  107. public $create_time;
  108. /**
  109. * @var bool Indicates if the unit is a Homunculus
  110. */
  111. public $homunculus;
  112. /**
  113. * @var string Indicates the Homunculus name
  114. */
  115. public $homunculus_name;
  116. /**
  117. * @var bool Indicates if the monster is locked.
  118. */
  119. public $lock;
  120. /**
  121. * @var int Unit rune HP stat.
  122. */
  123. public $rune_hp = 0;
  124. /**
  125. * @var int Unit rune ATK stat.
  126. */
  127. public $rune_attack = 0;
  128. /**
  129. * @var int Unit rune DEF stat.
  130. */
  131. public $rune_defense = 0;
  132. /**
  133. * @var int Unit rune SPD stat.
  134. */
  135. public $rune_speed = 0;
  136. /**
  137. * @var int Unit rune CRR stat.
  138. */
  139. public $rune_crit_rate = 0;
  140. /**
  141. * @var int Unit rune CRD stat.
  142. */
  143. public $rune_crit_damage = 0;
  144. /**
  145. * @var int Unit rune RES stat.
  146. */
  147. public $rune_resistance = 0;
  148. /**
  149. * @var int Unit rune ACC stat.
  150. */
  151. public $rune_accuracy = 0;
  152. /**
  153. * @var float Average rune efficiency.
  154. */
  155. public $avg_rune_efficiency = 0.0;
  156. /**
  157. * @var \Rune[] Equiped Runes.
  158. */
  159. public $rune = [];
  160. /**
  161. * @var \Team[] The Teamss the unit is on.
  162. */
  163. public $teams = [];
  164. /**
  165. * @var int[] Array with the levels of the skills, in orer.
  166. */
  167. public $skill_levels = [];
  168. /**
  169. * @var int Total experience required for the current level.
  170. */
  171. public $experience_level = 0;
  172. /**
  173. * @var int Experience required to level up.
  174. */
  175. public $experience_level_up = 0;
  176. /**
  177. * @var int Indcates if the unit is in storage.
  178. */
  179. public $in_storage = false;
  180. /**
  181. * @var string Unit title.
  182. * If Homunculus,it will be it's name.
  183. * If awakened, it will be the awakened from name.
  184. * If unawakened, it will be <element> <name>.
  185. */
  186. public $title;
  187. /**
  188. * Constructor.
  189. *
  190. * Searches the database and retrieves the information about the
  191. * monster, populating it and it's items.
  192. *
  193. * @param resource $db Connection to the database.
  194. * @param int $id Monster identifier.
  195. * @param bool $complete If false, query only monster and k_monster.
  196. * @global int Player ID.
  197. */
  198. public function __construct($db, $id, $complete = true){
  199. global $UID;
  200. parent::__construct($db);
  201. $s =
  202. "SELECT " .
  203. " uid, " .
  204. " id, " .
  205. " building, " .
  206. " unit, " .
  207. " stars, " .
  208. " level, " .
  209. " hp, " .
  210. " attack, " .
  211. " defense, " .
  212. " speed, " .
  213. " crit_rate, " .
  214. " crit_damage, " .
  215. " resistance, " .
  216. " accuracy, " .
  217. " experience, " .
  218. " exp_gained, " .
  219. " exp_gain_rate, " .
  220. " costume, " .
  221. " attribute, " .
  222. " source, " .
  223. " create_time, " .
  224. " homunculus, " .
  225. " homunculus_name, " .
  226. " lock " .
  227. "FROM unit " .
  228. "WHERE " .
  229. " uid = '$UID' AND " .
  230. " id = '$id'; ";
  231. $q = $this->db->query($s);
  232. $r = $q->fetchArray(SQLITE3_ASSOC);
  233. $this->uid = $r["uid"];
  234. $this->id = $r["id"];
  235. if (strlen($r["building"]) > 0){
  236. // TODO Entity $this->monster = new Building($this->db, $r["building"]);
  237. }
  238. if (strlen($r["unit"]) > 0){
  239. $this->unit = new K_Unit($this->db, $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"]; // TODO: Create entities?
  255. $this->attribute = $r["attribute"];
  256. $this->source = new K_Source($this->db, $r["source"]); // TODO: Instantiate?
  257. $this->create_time = date_create($r["create_time"]);
  258. $this->homunculus = $r["homunculus"];
  259. $this->homunculus_name = $r["homunculus_name"];
  260. #$this->avg_rune_efficiency = $r["avg_rune_efficiency"];
  261. #$this->fodder = $r["fodder"];
  262. #$this->in_storage = $r["in_storage"];
  263. #$this->ignore_for_fusion = $r["ignore_for_fusion"];
  264. #$this->priority = $r["priority"];
  265. #$this->notes = $r["notes"];
  266. if ($complete){
  267. $this->load_runes();
  268. $this->load_teams();
  269. $this->check_storage();
  270. $this->lock = $r["lock"];
  271. $s =
  272. "SELECT level " .
  273. "FROM " .
  274. " unit_skill, " .
  275. " k_skill " .
  276. "WHERE " .
  277. " k_skill.id = unit_skill.skill AND " .
  278. " unit = '$this->id' " .
  279. "ORDER BY slot;";
  280. $q = $this->db->query($s);
  281. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  282. array_push($this->skill_levels, $r["level"]);
  283. }
  284. $s =
  285. "SELECT exp " .
  286. "FROM k_experience " .
  287. "WHERE " .
  288. " stars = $this->stars AND " .
  289. " level = $this->level;";
  290. $q = $this->db->query($s);
  291. $r = $q->fetchArray(SQLITE3_ASSOC);
  292. $this->experience_level = $r["exp"];
  293. $this->experience_level_up = $r["exp"] - $this->exp_gained;
  294. }
  295. if ($this->homunculus){
  296. $this->title = $this->homunculus_name;
  297. }
  298. else{
  299. $this->title = $this->unit->title;
  300. }
  301. }
  302. /**
  303. * Checks if the monster is in storage and sets {@see $in_storage} to
  304. * true or false.
  305. *
  306. * @global int Player ID.
  307. */
  308. public function check_storage(){
  309. global $UID;
  310. $s = "
  311. SELECT 1
  312. FROM
  313. unit,
  314. building
  315. WHERE
  316. unit.id = $this->id AND
  317. unit.uid = '$UID' AND
  318. building.uid = '$UID' AND
  319. unit.building = building.id AND
  320. building.building = '" . BUILDING_ID::MONSTER_STORAGE . "';
  321. ";
  322. $q = $this->db->query($s);
  323. if ($q->fetchArray(SQLITE3_ASSOC) == false){
  324. $this->in_storage = false;
  325. }
  326. else{
  327. $this->in_storage = true;
  328. }
  329. }
  330. /**
  331. * Loads the {@see teams} array, with the teams the unit is on.
  332. *
  333. * @global int Player ID.
  334. */
  335. public function load_teams(){
  336. global $UID;
  337. $this->teams = [];
  338. $s = "
  339. SELECT DISTINCT team.id AS id
  340. FROM
  341. team,
  342. team_unit
  343. WHERE
  344. team.uid = '$UID' AND
  345. team.id = team_unit.team AND
  346. team_unit.unit = '" . $this->id . "';
  347. ";
  348. $q = $this->db->query($s);
  349. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  350. array_push($this->teams, new Team($this->db, $r["id"], false));
  351. }
  352. }
  353. /**
  354. * Loads the information about the equiped runes.
  355. * It is autoatically called at construction time if the
  356. * $complete parameter is true (by default). No point in calling it
  357. * twice.
  358. */
  359. public function load_runes(){
  360. $this->rune = [];
  361. $s =
  362. "SELECT id " .
  363. "FROM rune " .
  364. "WHERE assigned_to = '$this->id'; ";
  365. $q = $this->db->query($s);
  366. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  367. array_push($this->rune, new Rune($this->db, $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. * Calculates the stat increase by a rune property.
  383. *
  384. * @param string $stat Stat type.
  385. * @param int $value Stat increase value.
  386. */
  387. private function sum_rune_stat($stat, $value){
  388. switch ($stat){
  389. case RUNE_STAT_ID::ATK:
  390. $this->rune_attack += $value;
  391. break;
  392. case RUNE_STAT_ID::DEF:
  393. $this->rune_defense += $value;
  394. break;
  395. case RUNE_STAT_ID::HP:
  396. $this->rune_hp += $value;
  397. break;
  398. case RUNE_STAT_ID::SPD:
  399. $this->rune_speed += $value;
  400. break;
  401. case RUNE_STAT_ID::CRR:
  402. $this->rune_crit_rate += $value;
  403. break;
  404. case RUNE_STAT_ID::CRD:
  405. $this->rune_crit_damage += $value;
  406. break;
  407. case RUNE_STAT_ID::ACC:
  408. $this->rune_accuracy += $value;
  409. break;
  410. case RUNE_STAT_ID::RES:
  411. $this->rune_resistance += $value;
  412. break;
  413. case RUNE_STAT_ID::ATK_P:
  414. $this->rune_attack += ($this->attack * $value / 100);
  415. break;
  416. case RUNE_STAT_ID::DEF_P:
  417. $this->rune_defense += ($this->defense * $value / 100);
  418. break;
  419. case RUNE_STAT_ID::HP_P:
  420. $this->rune_hp += ($this->hp * $value / 100);
  421. break;
  422. }
  423. }
  424. /**
  425. * Checks if the unit skills are completly maxed.
  426. *
  427. * @return bool True if all skills maxed, false otherwise.
  428. */
  429. public function are_skills_maxed(){
  430. $maxed = true;
  431. for ($i = 0; $i < sizeof($this->unit->skill); $i ++){
  432. if ($this->unit->skill[$i]->max_level > $this->skill_levels[$i]){
  433. $maxed = false;
  434. break;
  435. }
  436. }
  437. return $maxed;
  438. }
  439. }
  440. ?>