Unit.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. * Unit title.
  166. * If Homunculus,it will be it's name.
  167. * If awakened, it will be the awakened from name.
  168. * If unawakened, it will be <element> <name>.
  169. */
  170. public $title;
  171. /**
  172. * Constructor.
  173. *
  174. * Searches the database and retrieves the information about the
  175. * monster, populating it and it's items.
  176. *
  177. * @param SQLite3 $db Connection to the database.
  178. * @param int $id Monster identifier.
  179. * @param complete If false, query only monster and k_monster.
  180. */
  181. public function __construct($db, $id, $complete = true){
  182. global $path;
  183. parent::__construct($db);
  184. $s =
  185. "SELECT " .
  186. " uid, " .
  187. " id, " .
  188. " building, " .
  189. " unit, " .
  190. " stars, " .
  191. " level, " .
  192. " hp, " .
  193. " attack, " .
  194. " defense, " .
  195. " speed, " .
  196. " crit_rate, " .
  197. " crit_damage, " .
  198. " resistance, " .
  199. " accuracy, " .
  200. " experience, " .
  201. " exp_gained, " .
  202. " exp_gain_rate, " .
  203. " costume, " .
  204. " attribute, " .
  205. " source, " .
  206. " create_time, " .
  207. " homunculus, " .
  208. " homunculus_name, " .
  209. " lock " .
  210. "FROM unit " .
  211. "WHERE id = '$id'; ";
  212. $q = $this->db->query($s);
  213. $r = $q->fetchArray(SQLITE3_ASSOC);
  214. $this->uid = $r["uid"];
  215. $this->id = $r["id"];
  216. if (strlen($r["building"]) > 0){
  217. // TODO Entity $this->monster = new Building($this->db, $r["building"]);
  218. }
  219. if (strlen($r["unit"]) > 0){
  220. $this->unit = new K_Unit($this->db, $r["unit"], $complete);
  221. }
  222. $this->stars = $r["stars"];
  223. $this->level = $r["level"];
  224. $this->hp = $r["hp"];
  225. $this->attack = $r["attack"];
  226. $this->defense = $r["defense"];
  227. $this->speed = $r["speed"];
  228. $this->crit_rate = $r["crit_rate"];
  229. $this->crit_damage = $r["crit_damage"];
  230. $this->resistance = $r["resistance"];
  231. $this->accuracy = $r["accuracy"];
  232. $this->experience = $r["experience"];
  233. $this->exp_gained = $r["exp_gained"];
  234. $this->exp_gain_rate = $r["exp_gain_rate"];
  235. $this->costume = $r["costume"]; // TODO: Create entities?
  236. $this->attribute = $r["attribute"];
  237. $this->source = new K_Source($this->db, $r["source"]); // TODO: Instantiate?
  238. $this->create_time = date_create($r["create_time"]);
  239. $this->homunculus = $r["homunculus"];
  240. $this->homunculus_name = $r["homunculus_name"];
  241. #$this->avg_rune_efficiency = $r["avg_rune_efficiency"];
  242. #$this->fodder = $r["fodder"];
  243. #$this->in_storage = $r["in_storage"];
  244. #$this->ignore_for_fusion = $r["ignore_for_fusion"];
  245. #$this->priority = $r["priority"];
  246. #$this->notes = $r["notes"];
  247. if ($complete){
  248. $this->load_runes();
  249. $this->load_teams();
  250. $s =
  251. "SELECT level " .
  252. "FROM " .
  253. " unit_skill, " .
  254. " k_skill " .
  255. "WHERE " .
  256. " k_skill.id = unit_skill.skill AND " .
  257. " unit = '$this->id' " .
  258. "ORDER BY slot;";
  259. $q = $this->db->query($s);
  260. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  261. array_push($this->skill_levels, $r["level"]);
  262. }
  263. $s =
  264. "SELECT exp " .
  265. "FROM k_experience " .
  266. "WHERE " .
  267. " stars = $this->stars AND " .
  268. " level = $this->level;";
  269. $q = $this->db->query($s);
  270. $r = $q->fetchArray(SQLITE3_ASSOC);
  271. $this->experience_level = $r["exp"];
  272. $this->experience_level_up = $r["exp"] - $this->exp_gained;
  273. }
  274. if ($this->homunculus){
  275. $this->title = $this->homunculus_name;
  276. }
  277. else{
  278. $this->title = $this->unit->title;
  279. }
  280. }
  281. /**
  282. * Loads the teams array, with the @{Team}s the unit is on.
  283. */
  284. public function load_teams(){
  285. global $UID;
  286. $this->teams = [];
  287. $s = "
  288. SELECT DISTINCT team.id AS id
  289. FROM
  290. team,
  291. team_unit
  292. WHERE
  293. team.uid = '$UID' AND
  294. team.id = team_unit.team AND
  295. team_unit.unit = '" . $this->id . "';
  296. ";
  297. $q = $this->db->query($s);
  298. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  299. array_push($this->teams, new Team($this->db, $r["id"], false));
  300. }
  301. }
  302. /**
  303. * Loads the information about the equiped runes.
  304. * It is autoatically called at construction time if the
  305. * $complete parameter is true (by default). No point in calling it
  306. * twice.
  307. */
  308. public function load_runes(){
  309. $this->rune = [];
  310. $s =
  311. "SELECT id " .
  312. "FROM rune " .
  313. "WHERE assigned_to = '$this->id'; ";
  314. $q = $this->db->query($s);
  315. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  316. array_push($this->rune, new Rune($this->db, $r["id"]));
  317. }
  318. foreach ($this->rune as $rune){
  319. $this->sum_rune_stat($rune->main_stat, $rune->main_stat_value);
  320. $this->sum_rune_stat($rune->innate_stat, $rune->innate_stat_value);
  321. $this->sum_rune_stat($rune->substat_1, $rune->substat_1_value + $rune->substat_1_grind);
  322. $this->sum_rune_stat($rune->substat_2, $rune->substat_2_value + $rune->substat_2_grind);
  323. $this->sum_rune_stat($rune->substat_3, $rune->substat_3_value + $rune->substat_3_grind);
  324. $this->sum_rune_stat($rune->substat_4, $rune->substat_4_value + $rune->substat_4_grind);
  325. }
  326. $this->rune_attack = ceil($this->rune_attack);
  327. $this->rune_defense = ceil($this->rune_defense);
  328. $this->rune_hp = ceil($this->rune_hp);
  329. }
  330. /**
  331. * Calculates the stat increase by a rune property.
  332. *
  333. * @param string $stat Stat type.
  334. * @param int $value Stat increase value.
  335. */
  336. private function sum_rune_stat($stat, $value){
  337. global $RUNE_STAT;
  338. switch ($stat){
  339. case $RUNE_STAT["ATK"]:
  340. $this->rune_attack += $value;
  341. break;
  342. case $RUNE_STAT["DEF"]:
  343. $this->rune_defense += $value;
  344. break;
  345. case $RUNE_STAT["HP"]:
  346. $this->rune_hp += $value;
  347. break;
  348. case $RUNE_STAT["SPD"]:
  349. $this->rune_speed += $value;
  350. break;
  351. case $RUNE_STAT["CRR"]:
  352. $this->rune_crit_rate += $value;
  353. break;
  354. case $RUNE_STAT["CRD"]:
  355. $this->rune_crit_damage += $value;
  356. break;
  357. case $RUNE_STAT["ACC"]:
  358. $this->rune_accuracy += $value;
  359. break;
  360. case $RUNE_STAT["RES"]:
  361. $this->rune_resistance += $value;
  362. break;
  363. case $RUNE_STAT["ATK%"]:
  364. $this->rune_attack += ($this->attack * $value / 100);
  365. break;
  366. case $RUNE_STAT["DEF%"]:
  367. $this->rune_defense += ($this->defense * $value / 100);
  368. break;
  369. case $RUNE_STAT["HP%"]:
  370. $this->rune_hp += ($this->hp * $value / 100);
  371. break;
  372. }
  373. }
  374. }
  375. ?>