Unit.php.txt 13 KB

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