Unit.php 13 KB

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