Unit.php.txt 13 KB

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