Rune.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * Rune 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_Rune_Set.php");
  14. require_once(PATH::ENTITY . "Rune_Stat.php");
  15. /**
  16. * Owned rune.
  17. *
  18. * Represents an object from the table 'rune'.
  19. *
  20. * @category Entity
  21. */
  22. class Rune extends Entity{
  23. /**
  24. * @var int Owner identifier.
  25. */
  26. public $uid;
  27. /**
  28. * @var int Rune identifier.
  29. */
  30. public $id;
  31. /**
  32. * @var K_Rune_Set Rune set.
  33. */
  34. public $type;
  35. /**
  36. * @var int Position of the rune type (1-6).
  37. */
  38. public $slot;
  39. /**
  40. * @var int Rune stars.
  41. */
  42. public $stars;
  43. /**
  44. * @var bool Indicates if the rune is ancient.
  45. */
  46. public $ancient;
  47. /**
  48. * @var int Level of the rune.
  49. */
  50. public $level;
  51. /**
  52. * @var int Rune quality.
  53. *
  54. * @see QUALITY_ID
  55. */
  56. public $quality;
  57. /**
  58. * @var int Rune original quality.
  59. *
  60. * @see QUALITY_ID
  61. */
  62. public $original_quality;
  63. /**
  64. * @var string Rune quality name (uppercase).
  65. */
  66. public $quality_name;
  67. /**
  68. * @var string Rune original quality name (uppercase).
  69. */
  70. public $original_quality_name;
  71. /**
  72. * @var int Price of the rune.
  73. */
  74. public $value;
  75. /**
  76. * @var float Current efficiency of the rune.
  77. */
  78. public $efficiency;
  79. /**
  80. * @var float Maximum potential efficiency of the rune.
  81. */
  82. public $max_efficiency;
  83. /**
  84. * @var int Indicates if the rune is locked.
  85. */
  86. public $locked;
  87. /**
  88. * @var Rune_Stat Main stat of the rune.
  89. */
  90. public $main_stat;
  91. /**
  92. * @var Rune_Stat Extra stat of the rune.
  93. */
  94. public $innate_stat;
  95. /**
  96. * @var \Rune_Stat[] Substats of the rune.
  97. */
  98. public $substats = [];
  99. /**
  100. * @var \Rune_Stat[] All stats.
  101. *
  102. * Includes main, innate and substats
  103. */
  104. public $stats = [];
  105. /**
  106. * @var Unit Unit the rune is assigned to.
  107. */
  108. public $unit;
  109. /**
  110. * Constructor.
  111. *
  112. * Searches the database and retrieves the information about the
  113. * rune, populating it and it's items.
  114. *
  115. * @param int $id Rune id.
  116. * @global resource Database connection.
  117. */
  118. public function __construct($id){
  119. global $db;
  120. $statement = $db->prepare("
  121. SELECT
  122. id,
  123. type,
  124. slot,
  125. stars,
  126. level,
  127. ancient,
  128. quality,
  129. original_quality,
  130. value,
  131. efficiency,
  132. max_efficiency,
  133. locked
  134. FROM rune
  135. WHERE id = :id;
  136. ");
  137. $statement->bindValue(':id', $id, SQLITE3_INTEGER);
  138. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  139. if ($r){
  140. $this->id = $r["id"];
  141. $this->type = new K_Rune_Set($r["type"]);
  142. $this->slot = $r["slot"];
  143. $this->stars = $r["stars"];
  144. $this->level = $r["level"];
  145. $this->ancient = $r["ancient"];
  146. $this->quality = $r["quality"];
  147. $this->original_quality = $r["original_quality"];
  148. $this->value = $r["value"];
  149. //$this->efficiency = $r["efficiency"];
  150. //$this->max_efficiency = $r["max_efficiency"];
  151. $this->locked = $r["locked"];
  152. // Get substats
  153. $statement = $db->prepare("
  154. SELECT slot
  155. FROM rune_stat
  156. WHERE rune = :rune
  157. ORDER BY slot;
  158. ");
  159. $statement->bindValue(':rune', $this->id, SQLITE3_INTEGER);
  160. $q = $statement->execute();
  161. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  162. $stat = new Rune_Stat($this->id, $r["slot"]);
  163. array_push($this->stats, $stat);
  164. switch ($r["slot"]){
  165. case RUNE_STAT_SLOT_ID::MAIN:
  166. $this->main_stat = $stat;
  167. break;
  168. case RUNE_STAT_SLOT_ID::INNATE:
  169. $this->innate_stat = $stat;
  170. break;
  171. default:
  172. array_push($this->substats, $stat);
  173. break;
  174. }
  175. }
  176. $this->quality_name = $this->get_quality_name($this->quality);
  177. $this->original_quality_name = $this->get_quality_name($this->original_quality);
  178. $this->efficiency = $this->calculate_efficiency();
  179. $this->max_efficiency = $this->calculate_maximum_efficiency();
  180. }
  181. }
  182. /**
  183. * Gets the value of the main stat at an arbitrary level.
  184. *
  185. * @param int $level Desired level.
  186. * @return int Value of the main stat at the selected level.
  187. */
  188. public function get_main_stat_at_level($level){
  189. $level = intval($level);
  190. if ($level < 0 || $level > 15){
  191. return 0;
  192. }
  193. switch ($this->main_stat->stat){
  194. case RUNE_STAT_ID::HP:
  195. return RUNE_STAT_VALUES::HP[$this->stars][$level];
  196. case RUNE_STAT_ID::HP_P:
  197. return RUNE_STAT_VALUES::HP_P[$this->stars][$level];
  198. case RUNE_STAT_ID::ATK:
  199. return RUNE_STAT_VALUES::ATK[$this->stars][$level];
  200. case RUNE_STAT_ID::ATK_P:
  201. return RUNE_STAT_VALUES::ATK_P[$this->stars][$level];
  202. case RUNE_STAT_ID::DEF:
  203. return RUNE_STAT_VALUES::DEF[$this->stars][$level];
  204. case RUNE_STAT_ID::DEF_P:
  205. return RUNE_STAT_VALUES::DEF_P[$this->stars][$level];
  206. case RUNE_STAT_ID::SPD:
  207. return RUNE_STAT_VALUES::SPD[$this->stars][$level];
  208. case RUNE_STAT_ID::CRR:
  209. return RUNE_STAT_VALUES::CRR[$this->stars][$level];
  210. case RUNE_STAT_ID::CRD:
  211. return RUNE_STAT_VALUES::CRD[$this->stars][$level];
  212. case RUNE_STAT_ID::RES:
  213. return RUNE_STAT_VALUES::RES[$this->stars][$level];
  214. case RUNE_STAT_ID::ACC:
  215. return RUNE_STAT_VALUES::ACC[$this->stars][$level];
  216. default:
  217. return 0;
  218. }
  219. }
  220. /**
  221. * Gets a stat name frm the mappings.
  222. *
  223. * @param int $q Quality id.
  224. * @return string Quality (Uppercase)
  225. */
  226. private function get_quality_name($q){
  227. switch ($q){
  228. case QUALITY_ID::NORMAL:
  229. return "NORMAL";
  230. case QUALITY_ID::MAGIC:
  231. return "MAGIC";
  232. case QUALITY_ID::RARE:
  233. return "RARE";
  234. case QUALITY_ID::HERO:
  235. return "HERO";
  236. case QUALITY_ID::LEGEND:
  237. return "LEGEND";
  238. default:
  239. return "";
  240. }
  241. }
  242. /**
  243. * Calcuates the efficiency of the rune.
  244. *
  245. * @return float Efficiency
  246. */
  247. private function calculate_efficiency(){
  248. global $db;
  249. $eff = [0.0, 0.0, 0.0, 0.0, 0.0];
  250. $query = "
  251. SELECT
  252. min,
  253. max
  254. FROM
  255. k_rune_roll_type,
  256. k_rune_stat_roll
  257. WHERE
  258. k_rune_roll_type.id = k_rune_stat_roll.type AND
  259. k_rune_roll_type.ancient = :ancient AND
  260. k_rune_roll_type.initial = :initial AND
  261. k_rune_roll_type.upgrade = :upgrade AND
  262. k_rune_stat_roll.stars = :stars AND
  263. k_rune_stat_roll.stat = :stat
  264. ";
  265. // Fixed stat
  266. if ($this->innate_stat != null && $this->innate_stat->value > 0){
  267. $value = $this->innate_stat->value;
  268. $statement = $db->prepare($query);
  269. $statement->bindValue(':ancient', $this->ancient, SQLITE3_INTEGER);
  270. $statement->bindValue(':initial', 1, SQLITE3_INTEGER);
  271. $statement->bindValue(':upgrade', 0, SQLITE3_INTEGER);
  272. $statement->bindValue(':stars', $this->stars, SQLITE3_INTEGER);
  273. $statement->bindValue(':stat', $this->innate_stat->stat, SQLITE3_INTEGER);
  274. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  275. if ($r){
  276. $min_initial = 0;//$r["min"];
  277. $max_initial = $r["max"];
  278. $eff[0] = ($value - $min_initial) / (9 * ($max_initial - $min_initial));
  279. }
  280. }
  281. for ($i = 1; $i <= 4; $i ++){
  282. if ($this->stats[$i] != null && $this->stats[$i]->value > 0){
  283. $value = $this->stats[$i]->value;
  284. $value = $this->stats[$i]->value;
  285. $statement = $db->prepare($query);
  286. $statement->bindValue(':ancient', $this->ancient, SQLITE3_INTEGER);
  287. $statement->bindValue(':initial', 1, SQLITE3_INTEGER);
  288. $statement->bindValue(':upgrade', 0, SQLITE3_INTEGER);
  289. $statement->bindValue(':stars', $this->stars, SQLITE3_INTEGER);
  290. $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_INTEGER);
  291. $r_initial = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  292. $statement = $db->prepare($query);
  293. $statement->bindValue(':ancient', $this->ancient, SQLITE3_INTEGER);
  294. $statement->bindValue(':initial', 0, SQLITE3_INTEGER);
  295. $statement->bindValue(':upgrade', 1, SQLITE3_INTEGER);
  296. $statement->bindValue(':stars', $this->stars, SQLITE3_INTEGER);
  297. $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_INTEGER);
  298. $r_upgrade = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  299. if ($r_initial && $r_upgrade){
  300. $min_initial = 0;//$r_initial["min"];
  301. $max_initial = $r_initial["max"];
  302. $min_upgrade = 0;//$r_upgrade["min"];
  303. $max_upgrade = $r_upgrade["max"];
  304. $eff[$i] = ((5 * $value) - ( 4 * $min_upgrade + $min_initial)) / ( 9 * ($max_initial - $min_initial + (4 * ($max_upgrade + $min_upgrade))));
  305. }
  306. }
  307. }
  308. $efficiency = 0.0;
  309. for ($i = 0; $i <= 4; $i ++){
  310. $efficiency += $eff[$i];
  311. }
  312. $efficiency *= 100;
  313. return $efficiency;
  314. }
  315. /**
  316. * Calcuates the maximum efficiency of the rune.
  317. *
  318. * @return float Efficiency
  319. */
  320. private function calculate_maximum_efficiency(){
  321. $max_efficiency = $this->efficiency;
  322. if ($this->level < 12){
  323. $max_efficiency += (1/9);
  324. }
  325. if ($this->level < 9){
  326. $max_efficiency += (1/9);
  327. }
  328. if ($this->level < 6){
  329. $max_efficiency += (1/9);
  330. }
  331. if ($this->level < 3){
  332. $max_efficiency += (1/9);
  333. }
  334. return $max_efficiency;
  335. }
  336. }
  337. ?>