Unit.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. require_once($path["entity"] . "Entity.php");
  3. require_once($path["entity"] . "K_Unit.php");
  4. require_once($path["entity"] . "Rune.php");
  5. /**
  6. * A Unit.
  7. *
  8. * Represents an object from the table 'unit'.
  9. */
  10. class Unit extends Entity{
  11. /**
  12. * Owner identifier.
  13. */
  14. public $uid;
  15. /**
  16. * Monster identifier.
  17. */
  18. public $id;
  19. /**
  20. * {@see Building} the unit is in.
  21. */
  22. public $building;
  23. /**
  24. * Base {@see K_Unit}.
  25. */
  26. public $unit;
  27. /**
  28. * Unit stars.
  29. */
  30. public $stars;
  31. /**
  32. * Unit level.
  33. */
  34. public $level;
  35. /**
  36. * Monster base HP stat.
  37. */
  38. public $hp;
  39. /**
  40. * Monster base ATK stat.
  41. */
  42. public $attack;
  43. /**
  44. * Monster base DEF stat.
  45. */
  46. public $defense;
  47. /**
  48. * Monster base SPD stat.
  49. */
  50. public $speed;
  51. /**
  52. * Monster base CRR stat.
  53. */
  54. public $crit_rate;
  55. /**
  56. * Monster base CRD stat.
  57. */
  58. public $crit_damage;
  59. /**
  60. * Monster base RES stat.
  61. */
  62. public $resistance;
  63. /**
  64. * Monster base ACC stat.
  65. */
  66. public $accuracy;
  67. /**
  68. * Total gained experience.
  69. */
  70. public $experience;
  71. /**
  72. * Total gained experience in its current level.
  73. */
  74. public $exp_gained;
  75. /**
  76. * Experience / hour in the current building.
  77. */
  78. public $exp_gain_rate;
  79. /**
  80. * Equipped transmogification.
  81. */
  82. public $costume;
  83. /**
  84. * Unit attribute
  85. */
  86. public $attribute;
  87. /**
  88. * {@see K_Source} the unit was aquired from.
  89. */
  90. public $source;
  91. /**
  92. * Datetime the unit was obtained.
  93. */
  94. public $create_time;
  95. /**
  96. * Indicates if the unit is a Homunculus
  97. */
  98. public $homunculus;
  99. /**
  100. * Indicates the Homunculus name
  101. */
  102. public $homunculus_name;
  103. /**
  104. * Indicates if the monster is locked.
  105. */
  106. public $lock;
  107. /**
  108. * Unit rune HP stat.
  109. */
  110. public $rune_hp = 0;
  111. /**
  112. * Unit rune ATK stat.
  113. */
  114. public $rune_attack = 0;
  115. /**
  116. * Unit rune DEF stat.
  117. */
  118. public $rune_defense = 0;
  119. /**
  120. * Unit rune SPD stat.
  121. */
  122. public $rune_speed = 0;
  123. /**
  124. * Unit rune CRR stat.
  125. */
  126. public $rune_crit_rate = 0;
  127. /**
  128. * Unit rune CRD stat.
  129. */
  130. public $rune_crit_damage = 0;
  131. /**
  132. * Unit rune RES stat.
  133. */
  134. public $rune_resistance = 0;
  135. /**
  136. * Unit rune ACC stat.
  137. */
  138. public $rune_accuracy = 0;
  139. /**
  140. * Average rune efficiency.
  141. */
  142. public $avg_rune_efficiency;
  143. /**
  144. * Equiped {@see Rune}s
  145. */
  146. public $rune = [];
  147. /**
  148. * Array with the levels of the skills.
  149. */
  150. public $skill_levels = [];
  151. /**
  152. * Total experience required for the current level.
  153. */
  154. public $experience_level = 0;
  155. /**
  156. * Experience required to level up.
  157. */
  158. public $experience_level_up = 0;
  159. /**
  160. * Unit title.
  161. * If Homunculus,it will be it's name.
  162. * If awakened, it will be the awakened from name.
  163. * If unawakened, it will be <element> <name>.
  164. */
  165. public $title;
  166. /**
  167. * Constructor.
  168. *
  169. * Searches the database and retrieves the information about the
  170. * monster, populating it and it's items.
  171. *
  172. * @param SQLite3 $db Connection to the database.
  173. * @param int $id Monster identifier.
  174. * @param complete If false, query only monster and k_monster.
  175. */
  176. public function __construct($db, $id, $complete = true){
  177. global $path;
  178. parent::__construct($db);
  179. $s =
  180. "SELECT " .
  181. " uid, " .
  182. " id, " .
  183. " building, " .
  184. " unit, " .
  185. " stars, " .
  186. " level, " .
  187. " hp, " .
  188. " attack, " .
  189. " defense, " .
  190. " speed, " .
  191. " crit_rate, " .
  192. " crit_damage, " .
  193. " resistance, " .
  194. " accuracy, " .
  195. " experience, " .
  196. " exp_gained, " .
  197. " exp_gain_rate, " .
  198. " costume, " .
  199. " attribute, " .
  200. " source, " .
  201. " create_time, " .
  202. " homunculus, " .
  203. " homunculus_name, " .
  204. " lock " .
  205. "FROM unit " .
  206. "WHERE id = '$id'; ";
  207. $q = $this->db->query($s);
  208. $r = $q->fetchArray(SQLITE3_ASSOC);
  209. $this->uid = $r["uid"];
  210. $this->id = $r["id"];
  211. if (strlen($r["building"]) > 0){
  212. // TODO Entity $this->monster = new Building($this->db, $r["building"]);
  213. }
  214. if (strlen($r["unit"]) > 0){
  215. $this->unit = new K_Unit($this->db, $r["unit"], $complete);
  216. }
  217. $this->stars = $r["stars"];
  218. $this->level = $r["level"];
  219. $this->hp = $r["hp"];
  220. $this->attack = $r["attack"];
  221. $this->defense = $r["defense"];
  222. $this->speed = $r["speed"];
  223. $this->crit_rate = $r["crit_rate"];
  224. $this->crit_damage = $r["crit_damage"];
  225. $this->resistance = $r["resistance"];
  226. $this->accuracy = $r["accuracy"];
  227. $this->experience = $r["experience"];
  228. $this->exp_gained = $r["exp_gained"];
  229. $this->exp_gain_rate = $r["exp_gain_rate"];
  230. $this->costume = $r["costume"]; // TODO: Create entities?
  231. $this->attribute = $r["attribute"];
  232. $this->source = new K_Source($this->db, $r["source"]); // TODO: Instantiate?
  233. $this->create_time = date_create($r["create_time"]);
  234. $this->homunculus = $r["homunculus"];
  235. $this->homunculus_name = $r["homunculus_name"];
  236. #$this->avg_rune_efficiency = $r["avg_rune_efficiency"];
  237. #$this->fodder = $r["fodder"];
  238. #$this->in_storage = $r["in_storage"];
  239. #$this->ignore_for_fusion = $r["ignore_for_fusion"];
  240. #$this->priority = $r["priority"];
  241. #$this->notes = $r["notes"];
  242. if ($complete){
  243. $this->load_runes();
  244. $s =
  245. "SELECT level " .
  246. "FROM " .
  247. " unit_skill, " .
  248. " k_skill " .
  249. "WHERE " .
  250. " k_skill.id = unit_skill.skill AND " .
  251. " unit = '$this->id' " .
  252. "ORDER BY slot;";
  253. $q = $this->db->query($s);
  254. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  255. array_push($this->skill_levels, $r["level"]);
  256. }
  257. $s =
  258. "SELECT exp " .
  259. "FROM k_experience " .
  260. "WHERE " .
  261. " stars = $this->stars AND " .
  262. " level = $this->level;";
  263. $q = $this->db->query($s);
  264. $r = $q->fetchArray(SQLITE3_ASSOC);
  265. $this->experience_level = $r["exp"];
  266. $this->experience_level_up = $r["exp"] - $this->exp_gained;
  267. }
  268. if ($this->homunculus){
  269. $this->title = $this->homunculus_name;
  270. }
  271. else{
  272. $this->title = $this->unit->title;
  273. }
  274. }
  275. /**
  276. * Loads the information about the equiped runes.
  277. * It is autoatically called at construction time if the
  278. * $complete parameter is true (by default). No point in calling it
  279. * twice.
  280. */
  281. public function load_runes(){
  282. $this->rune = [];
  283. $s =
  284. "SELECT id " .
  285. "FROM rune " .
  286. "WHERE assigned_to = '$this->id'; ";
  287. $q = $this->db->query($s);
  288. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  289. array_push($this->rune, new Rune($this->db, $r["id"]));
  290. }
  291. foreach ($this->rune as $rune){
  292. $this->sum_rune_stat($rune->main_stat, $rune->main_stat_value);
  293. $this->sum_rune_stat($rune->innate_stat, $rune->innate_stat_value);
  294. $this->sum_rune_stat($rune->substat_1, $rune->substat_1_value + $rune->substat_1_grind);
  295. $this->sum_rune_stat($rune->substat_2, $rune->substat_2_value + $rune->substat_2_grind);
  296. $this->sum_rune_stat($rune->substat_3, $rune->substat_3_value + $rune->substat_3_grind);
  297. $this->sum_rune_stat($rune->substat_4, $rune->substat_4_value + $rune->substat_4_grind);
  298. }
  299. $this->rune_attack = ceil($this->rune_attack);
  300. $this->rune_defense = ceil($this->rune_defense);
  301. $this->rune_hp = ceil($this->rune_hp);
  302. }
  303. /**
  304. * Calculates the stat increase by a rune property.
  305. *
  306. * @param string $stat Stat type.
  307. * @param int $value Stat increase value.
  308. */
  309. private function sum_rune_stat($stat, $value){
  310. global $RUNE_STAT;
  311. switch ($stat){
  312. case $RUNE_STAT["ATK"]:
  313. $this->rune_attack += $value;
  314. break;
  315. case $RUNE_STAT["DEF"]:
  316. $this->rune_defense += $value;
  317. break;
  318. case $RUNE_STAT["HP"]:
  319. $this->rune_hp += $value;
  320. break;
  321. case $RUNE_STAT["SPD"]:
  322. $this->rune_speed += $value;
  323. break;
  324. case $RUNE_STAT["CRR"]:
  325. $this->rune_crit_rate += $value;
  326. break;
  327. case $RUNE_STAT["CRD"]:
  328. $this->rune_crit_damage += $value;
  329. break;
  330. case $RUNE_STAT["ACC"]:
  331. $this->rune_accuracy += $value;
  332. break;
  333. case $RUNE_STAT["RES"]:
  334. $this->rune_resistance += $value;
  335. break;
  336. case $RUNE_STAT["ATK%"]:
  337. $this->rune_attack += ($this->attack * $value / 100);
  338. break;
  339. case $RUNE_STAT["DEF%"]:
  340. $this->rune_defense += ($this->defense * $value / 100);
  341. break;
  342. case $RUNE_STAT["HP%"]:
  343. $this->rune_hp += ($this->hp * $value / 100);
  344. break;
  345. }
  346. }
  347. }
  348. ?>