Rune.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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_TEXT);
  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_TEXT);
  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. if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){
  179. $this->efficiency = $this->calculate_efficiency();
  180. $this->max_efficiency = $this->calculate_maximum_efficiency();
  181. $statement = $db->prepare("
  182. UPDATE rune
  183. SET
  184. efficiency = :efficiency,
  185. max_efficiency = :max_efficiency
  186. WHERE id = :rune
  187. ");
  188. $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT);
  189. $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT);
  190. $statement->bindValue(':rune', $this->id, SQLITE3_INTEGER);
  191. $q = $statement->execute();
  192. }
  193. }
  194. }
  195. /**
  196. * Gets the value of the main stat at an arbitrary level.
  197. *
  198. * @param int $level Desired level.
  199. * @return int Value of the main stat at the selected level.
  200. */
  201. public function get_main_stat_at_level($level){
  202. $level = intval($level);
  203. if ($level < 0 || $level > 15){
  204. return 0;
  205. }
  206. switch ($this->main_stat->stat){
  207. case RUNE_STAT_ID::HP:
  208. return RUNE_STAT_VALUES::HP[$this->stars][$level];
  209. case RUNE_STAT_ID::HP_P:
  210. return RUNE_STAT_VALUES::HP_P[$this->stars][$level];
  211. case RUNE_STAT_ID::ATK:
  212. return RUNE_STAT_VALUES::ATK[$this->stars][$level];
  213. case RUNE_STAT_ID::ATK_P:
  214. return RUNE_STAT_VALUES::ATK_P[$this->stars][$level];
  215. case RUNE_STAT_ID::DEF:
  216. return RUNE_STAT_VALUES::DEF[$this->stars][$level];
  217. case RUNE_STAT_ID::DEF_P:
  218. return RUNE_STAT_VALUES::DEF_P[$this->stars][$level];
  219. case RUNE_STAT_ID::SPD:
  220. return RUNE_STAT_VALUES::SPD[$this->stars][$level];
  221. case RUNE_STAT_ID::CRR:
  222. return RUNE_STAT_VALUES::CRR[$this->stars][$level];
  223. case RUNE_STAT_ID::CRD:
  224. return RUNE_STAT_VALUES::CRD[$this->stars][$level];
  225. case RUNE_STAT_ID::RES:
  226. return RUNE_STAT_VALUES::RES[$this->stars][$level];
  227. case RUNE_STAT_ID::ACC:
  228. return RUNE_STAT_VALUES::ACC[$this->stars][$level];
  229. default:
  230. return 0;
  231. }
  232. }
  233. /**
  234. * Gets a stat name frm the mappings.
  235. *
  236. * @param int $q Quality id.
  237. * @return string Quality (Uppercase)
  238. */
  239. private function get_quality_name($q){
  240. switch ($q){
  241. case QUALITY_ID::NORMAL:
  242. return "NORMAL";
  243. case QUALITY_ID::MAGIC:
  244. return "MAGIC";
  245. case QUALITY_ID::RARE:
  246. return "RARE";
  247. case QUALITY_ID::HERO:
  248. return "HERO";
  249. case QUALITY_ID::LEGEND:
  250. return "LEGEND";
  251. default:
  252. return "";
  253. }
  254. }
  255. /**
  256. * Calcuates the efficiency of the rune.
  257. *
  258. * @return float Efficiency
  259. */
  260. private function calculate_efficiency(){
  261. global $db;
  262. $eff = [0.0, 0.0, 0.0, 0.0, 0.0];
  263. $query = "
  264. SELECT
  265. min,
  266. max
  267. FROM
  268. k_rune_roll_type,
  269. k_rune_stat_roll
  270. WHERE
  271. k_rune_roll_type.id = k_rune_stat_roll.type AND
  272. k_rune_roll_type.ancient = :ancient AND
  273. k_rune_roll_type.initial = :initial AND
  274. k_rune_roll_type.upgrade = :upgrade AND
  275. k_rune_stat_roll.stars = :stars AND
  276. k_rune_stat_roll.stat = :stat
  277. ";
  278. // Fixed stat
  279. if ($this->innate_stat != null && $this->innate_stat->value > 0){
  280. $value = $this->innate_stat->value;
  281. $statement = $db->prepare($query);
  282. $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
  283. $statement->bindValue(':initial', 1, SQLITE3_TEXT);
  284. $statement->bindValue(':upgrade', 0, SQLITE3_TEXT);
  285. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  286. $statement->bindValue(':stat', $this->innate_stat->stat, SQLITE3_TEXT);
  287. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  288. if ($r){
  289. $min_initial = 0;//$r["min"];
  290. $max_initial = $r["max"];
  291. $eff[0] = ($value - $min_initial) / (9 * ($max_initial - $min_initial));
  292. }
  293. }
  294. for ($i = 1; $i <= 4; $i ++){
  295. if (sizeof($this->stats) > $i && $this->stats[$i] != null && $this->stats[$i]->value > 0){
  296. $value = $this->stats[$i]->value;
  297. $value = $this->stats[$i]->value;
  298. $statement = $db->prepare($query);
  299. $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
  300. $statement->bindValue(':initial', 1, SQLITE3_TEXT);
  301. $statement->bindValue(':upgrade', 0, SQLITE3_TEXT);
  302. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  303. $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_TEXT);
  304. $r_initial = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  305. $statement = $db->prepare($query);
  306. $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
  307. $statement->bindValue(':initial', 0, SQLITE3_TEXT);
  308. $statement->bindValue(':upgrade', 1, SQLITE3_TEXT);
  309. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  310. $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_TEXT);
  311. $r_upgrade = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  312. if ($r_initial && $r_upgrade){
  313. $min_initial = 0;//$r_initial["min"];
  314. $max_initial = $r_initial["max"];
  315. $min_upgrade = 0;//$r_upgrade["min"];
  316. $max_upgrade = $r_upgrade["max"];
  317. $eff[$i] = ((5 * $value) - ( 4 * $min_upgrade + $min_initial)) / ( 9 * ($max_initial - $min_initial + (4 * ($max_upgrade + $min_upgrade))));
  318. }
  319. }
  320. }
  321. $efficiency = 0.0;
  322. for ($i = 0; $i <= 4; $i ++){
  323. $efficiency += $eff[$i];
  324. }
  325. $efficiency *= 100;
  326. return $efficiency;
  327. }
  328. /**
  329. * Calcuates the maximum efficiency of the rune.
  330. *
  331. * @return float Efficiency
  332. */
  333. private function calculate_maximum_efficiency(){
  334. $max_efficiency = $this->efficiency;
  335. if ($this->level < 12){
  336. $max_efficiency += (1/9);
  337. }
  338. if ($this->level < 9){
  339. $max_efficiency += (1/9);
  340. }
  341. if ($this->level < 6){
  342. $max_efficiency += (1/9);
  343. }
  344. if ($this->level < 3){
  345. $max_efficiency += (1/9);
  346. }
  347. return $max_efficiency;
  348. }
  349. /**
  350. * Calcuates how many rolls each substat has.
  351. *
  352. * DON'T USE: Error margins are as high as 85%, which is ridiculous.
  353. *
  354. */
  355. private function calculate_rolls(){
  356. global $db;
  357. $query = "
  358. SELECT
  359. min,
  360. max
  361. FROM
  362. k_rune_roll_type,
  363. k_rune_stat_roll
  364. WHERE
  365. k_rune_roll_type.id = k_rune_stat_roll.type AND
  366. k_rune_roll_type.ancient = :ancient AND
  367. k_rune_roll_type.initial = :initial AND
  368. k_rune_roll_type.upgrade = :upgrade AND
  369. k_rune_stat_roll.stars = :stars AND
  370. k_rune_stat_roll.stat = :stat
  371. ";
  372. if ($this->innate_stat != null && $this->innate_stat->value > 0){
  373. $this->innate_stat->rolls = 0;
  374. $value = $this->innate_stat->value;
  375. $statement = $db->prepare($query);
  376. $statement->bindValue(':ancient', $this->ancient, SQLITE3_INTEGER);
  377. $statement->bindValue(':initial', 1, SQLITE3_INTEGER);
  378. $statement->bindValue(':upgrade', 0, SQLITE3_INTEGER);
  379. $statement->bindValue(':stars', $this->stars, SQLITE3_INTEGER);
  380. $statement->bindValue(':stat', $this->innate_stat->stat, SQLITE3_INTEGER);
  381. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  382. if ($r){
  383. $min_initial = 0;//$r["min"];
  384. $max_initial = $r["max"];
  385. $this->innate_stat->efficiency = 100 * (($value - $min_initial) / ($max_initial - $min_initial));
  386. }
  387. }
  388. $max_rolls = floor(max($this->level, 12) / 3);
  389. $rolls = 0;
  390. for ($i = 1; $i <= 4; $i ++){
  391. if ($this->stats[$i] != null && $this->stats[$i]->value > 0){
  392. $value = $this->stats[$i]->value;
  393. $statement = $db->prepare($query);
  394. $statement->bindValue(':ancient', $this->ancient, SQLITE3_INTEGER);
  395. $statement->bindValue(':initial', 1, SQLITE3_INTEGER);
  396. $statement->bindValue(':upgrade', 0, SQLITE3_INTEGER);
  397. $statement->bindValue(':stars', $this->stars, SQLITE3_INTEGER);
  398. $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_INTEGER);
  399. $r_initial = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  400. $statement = $db->prepare($query);
  401. $statement->bindValue(':ancient', $this->ancient, SQLITE3_INTEGER);
  402. $statement->bindValue(':initial', 0, SQLITE3_INTEGER);
  403. $statement->bindValue(':upgrade', 1, SQLITE3_INTEGER);
  404. $statement->bindValue(':stars', $this->stars, SQLITE3_INTEGER);
  405. $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_INTEGER);
  406. $r_upgrade = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  407. if ($r_initial && $r_upgrade){
  408. $min_initial = $r_initial["min"];
  409. $max_initial = $r_initial["max"];
  410. $avg_initial = ($min_initial + $max_initial) / 2;
  411. $min_upgrade = $r_upgrade["min"];
  412. $max_upgrade = $r_upgrade["max"];
  413. $avg_upgrade = ($min_upgrade + $max_upgrade) / 2;
  414. $rolls = round(max($value - $avg_initial, 0) / $avg_upgrade);
  415. $this->stats[$i]->rolls = $rolls;
  416. $this->stats[$i]->efficiency = ((5 * $value) - ( 4 * $min_upgrade + $min_initial)) / ( ($max_initial - $min_initial + (4 * ($max_upgrade + $min_upgrade))));
  417. }
  418. }
  419. }
  420. }
  421. }
  422. ?>