Unit.php 11 KB

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