Rune.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 . "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. private $player;
  27. /**
  28. * @var int Rune identifier.
  29. */
  30. private $id;
  31. /**
  32. * @var Rune_Set Rune set.
  33. */
  34. private $type;
  35. /**
  36. * @var int Position of the rune type (1-6).
  37. */
  38. private $slot;
  39. /**
  40. * @var int Rune stars.
  41. */
  42. private $stars;
  43. /**
  44. * @var bool Indicates if the rune is ancient.
  45. */
  46. private $ancient;
  47. /**
  48. * @var int Level of the rune.
  49. */
  50. private $level;
  51. /**
  52. * @var int Rune quality.
  53. *
  54. * @see QUALITY_ID
  55. */
  56. private $quality;
  57. /**
  58. * @var int Rune original quality.
  59. *
  60. * @see QUALITY_ID
  61. */
  62. private $original_quality;
  63. /**
  64. * @var string Rune quality name (uppercase).
  65. */
  66. private $quality_name;
  67. /**
  68. * @var string Rune original quality name (uppercase).
  69. */
  70. private $original_quality_name;
  71. /**
  72. * @var int Price of the rune.
  73. */
  74. private $value;
  75. /**
  76. * @var float Current efficiency of the rune.
  77. */
  78. private $efficiency;
  79. /**
  80. * @var float Maximum potential efficiency of the rune.
  81. */
  82. private $max_efficiency;
  83. /**
  84. * @var int Indicates if the rune is locked.
  85. */
  86. private $locked;
  87. private $flag_stats;
  88. /**
  89. * @var Rune_Stat Main stat of the rune.
  90. */
  91. private $main_stat;
  92. /**
  93. * @var Rune_Stat Extra stat of the rune.
  94. */
  95. private $innate_stat;
  96. /**
  97. * @var \Rune_Stat[] Substats of the rune.
  98. */
  99. private $substats = [];
  100. /**
  101. * @var \Rune_Stat[] All stats.
  102. *
  103. * Includes main, innate and substats
  104. */
  105. private $stats = [];
  106. /**
  107. * @var Unit Unit the rune is assigned to.
  108. */
  109. private $unit;
  110. /**
  111. * Constructor.
  112. *
  113. * Searches the database and retrieves the information about the
  114. * rune, populating it and it's items.
  115. *
  116. * @param int $id Rune id.
  117. */
  118. public function __construct($id){
  119. $statement = get_context()->get_db()->prepare("
  120. SELECT
  121. id,
  122. type,
  123. slot,
  124. stars,
  125. level,
  126. ancient,
  127. quality,
  128. original_quality,
  129. value,
  130. efficiency,
  131. max_efficiency,
  132. locked
  133. FROM rune
  134. WHERE id = :id;
  135. ");
  136. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  137. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  138. if ($r){
  139. $this->id = $r["id"];
  140. $this->type = new Rune_Set($r["type"]);
  141. $this->slot = $r["slot"];
  142. $this->stars = $r["stars"];
  143. $this->level = $r["level"];
  144. $this->ancient = $r["ancient"];
  145. $this->quality = $r["quality"];
  146. $this->original_quality = $r["original_quality"];
  147. $this->value = $r["value"];
  148. $this->efficiency = $r["efficiency"];
  149. $this->max_efficiency = $r["max_efficiency"];
  150. $this->locked = filter_var($r["locked"], FILTER_VALIDATE_BOOLEAN);
  151. $this->quality_name = $this->map_quality_name($this->quality);
  152. $this->original_quality_name = $this->map_quality_name($this->original_quality);
  153. // Read efficiencies. If not set in DB, calculate and update.
  154. if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){
  155. $this->efficiency = $this->calculate_efficiency();
  156. $this->max_efficiency = $this->calculate_maximum_efficiency();
  157. $statement = get_context()->get_db()->prepare("
  158. UPDATE rune
  159. SET
  160. efficiency = :efficiency,
  161. max_efficiency = :max_efficiency
  162. WHERE id = :rune
  163. ");
  164. $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT);
  165. $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT);
  166. $statement->bindValue(':rune', $this->id, SQLITE3_INTEGER);
  167. $statement->execute();
  168. }
  169. }
  170. }
  171. private function load_stats(){
  172. $statement = get_context()->get_db()->prepare("
  173. SELECT slot
  174. FROM rune_stat
  175. WHERE rune = :rune
  176. ORDER BY slot;
  177. ");
  178. $statement->bindValue(':rune', $this->id, SQLITE3_TEXT);
  179. $q = $statement->execute();
  180. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  181. $stat = new Rune_Stat($this->id, $r["slot"]);
  182. array_push($this->stats, $stat);
  183. switch ($r["slot"]){
  184. case RUNE_STAT_SLOT_ID::MAIN:
  185. $this->main_stat = $stat;
  186. break;
  187. case RUNE_STAT_SLOT_ID::INNATE:
  188. $this->innate_stat = $stat;
  189. break;
  190. default:
  191. array_push($this->substats, $stat);
  192. break;
  193. }
  194. }
  195. $this->flag_stats = true;
  196. }
  197. private function load_unit(){
  198. $statement = get_context()->get_db()->prepare("
  199. SELECT unit
  200. FROM unit_rune
  201. WHERE
  202. rune = :rune AND
  203. rta = :mode;
  204. ");
  205. $statement->bindValue(':rune', $this->id, SQLITE3_TEXT);
  206. $statement->bindValue(':mode', get_context()->get_game_mode(), SQLITE3_TEXT);
  207. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  208. if ($r){
  209. $this->unit = new Unit($r["unit"]);
  210. }
  211. $this->flag_unit = true;
  212. }
  213. /**
  214. * Retrieves the owner's player identifier.
  215. *
  216. * @return number Player ID.
  217. */
  218. public function get_player(){
  219. return $this->player;
  220. }
  221. /**
  222. * Retrieves the rune identifier.
  223. *
  224. * @return number Rune ID.
  225. */
  226. public function get_id(){
  227. return $this->id;
  228. }
  229. /**
  230. * Retrieves the rune set.
  231. *
  232. * @return Rune_Set Set of the rune.
  233. */
  234. public function get_set(){
  235. return $this->type;
  236. }
  237. /**
  238. * Retrieves the rune slot.
  239. *
  240. * @return number Rune slot (1-6).
  241. */
  242. public function get_slot(){
  243. return $this->slot;
  244. }
  245. /**
  246. * Retrieves the rune stars (the grade).
  247. *
  248. * @return number Rune stars (1-6).
  249. */
  250. public function get_stars(){
  251. return $this->stars;
  252. }
  253. /**
  254. * Checks if it's an ancient rune.
  255. *
  256. * @return boolean True for ancient, false for normal.
  257. */
  258. public function is_ancient(){
  259. return $this->ancient;
  260. }
  261. /**
  262. * Retrieves the rune level
  263. *
  264. * @return number Rune level (0-15).
  265. */
  266. public function get_level(){
  267. return $this->level;
  268. }
  269. /**
  270. * Retrieves the rune quality identifier.
  271. *
  272. * @see QUALITY_ID
  273. *
  274. * @return number Quality identifier.
  275. */
  276. public function get_quality(){
  277. return $this->quality;
  278. }
  279. /**
  280. * Retrieves the rune original quality identifier.
  281. *
  282. * @see QUALITY_ID
  283. *
  284. * @return number Original quality identifier.
  285. */
  286. public function get_original_quality(){
  287. return $this->original_quality;
  288. }
  289. /**
  290. * Retrieves the rune quality name.
  291. *
  292. * @return string Quality name.
  293. */
  294. public function get_quality_name(){
  295. return $this->quality_name;
  296. }
  297. /**
  298. * Retrieves the rune original quality name.
  299. *
  300. * @return string Original quality name.
  301. */
  302. public function get_original_quality_name(){
  303. return $this->original_quality_name;
  304. }
  305. /**
  306. * Retrieves the rune value.
  307. *
  308. * @return number Rune value in mana stones.
  309. */
  310. public function get_value(){
  311. return $this->value;
  312. }
  313. /**
  314. * Retrieves the rune efficiency.
  315. *
  316. * @return number Rune efficiency (0-100)
  317. */
  318. public function get_efficiency(){
  319. return $this->efficiency;
  320. }
  321. /**
  322. * Retrieves the rune efficiency.
  323. *
  324. * @return number Rune efficiency (0-100)
  325. */
  326. public function get_max_efficiency(){
  327. return $this->max_efficiency;
  328. }
  329. /**
  330. * Checks if the rune is locked.
  331. *
  332. * @return bool True if locked, false otherwise.
  333. */
  334. public function is_locked(){
  335. return $this->locked;
  336. }
  337. /**
  338. * Retrieves the rune main_stat.
  339. *
  340. * @return Rune_Stat The main stat.
  341. */
  342. public function get_main_stat(){
  343. if (! $this->flag_stats){
  344. $this->load_stats();
  345. }
  346. return $this->main_stat;
  347. }
  348. /**
  349. * Retrieves the rune innate stat.
  350. *
  351. * @return Rune_Stat Innate stat, null if it hasn't.
  352. */
  353. public function get_innate_stat(){
  354. if (! $this->flag_stats){
  355. $this->load_stats();
  356. }
  357. return $this->innate_stat;
  358. }
  359. /**
  360. * Retrieves as many substats as the rune has, sorted.
  361. *
  362. * @return Rune_Stat[] Sorted array of the rune substats.
  363. */
  364. public function get_substats(){
  365. if (! $this->flag_stats){
  366. $this->load_stats();
  367. }
  368. return $this->substats;
  369. }
  370. /**
  371. * Retrieves all the rune stats, including main, innate and substats, in an unsorted array.
  372. *
  373. * @return Rune_Stat Every rune stat.
  374. */
  375. public function get_stats(){
  376. if (! $this->flag_stats){
  377. $this->load_stats();
  378. }
  379. return $this->stats;
  380. }
  381. /**
  382. * Retrieves the unit that has equipped the rune in the current game mode.
  383. *
  384. * @return Unit Unit that has the rune.
  385. */
  386. public function get_unit(){
  387. if (! $this->flag_unit){
  388. $this->load_unit();
  389. }
  390. return $this->unit;
  391. }
  392. /**
  393. * Gets the value of the main stat at an arbitrary level.
  394. *
  395. * @param int $level Desired level.
  396. * @return int Value of the main stat at the selected level.
  397. */
  398. public function get_main_stat_at_level($level){
  399. $level = intval($level);
  400. if ($level < 0 || $level > 15){
  401. return 0;
  402. }
  403. switch ($this->main_stat->get_stat()){
  404. case RUNE_STAT_ID::HP:
  405. return RUNE_STAT_VALUES::HP[$this->stars][$level];
  406. case RUNE_STAT_ID::HP_P:
  407. return RUNE_STAT_VALUES::HP_P[$this->stars][$level];
  408. case RUNE_STAT_ID::ATK:
  409. return RUNE_STAT_VALUES::ATK[$this->stars][$level];
  410. case RUNE_STAT_ID::ATK_P:
  411. return RUNE_STAT_VALUES::ATK_P[$this->stars][$level];
  412. case RUNE_STAT_ID::DEF:
  413. return RUNE_STAT_VALUES::DEF[$this->stars][$level];
  414. case RUNE_STAT_ID::DEF_P:
  415. return RUNE_STAT_VALUES::DEF_P[$this->stars][$level];
  416. case RUNE_STAT_ID::SPD:
  417. return RUNE_STAT_VALUES::SPD[$this->stars][$level];
  418. case RUNE_STAT_ID::CRR:
  419. return RUNE_STAT_VALUES::CRR[$this->stars][$level];
  420. case RUNE_STAT_ID::CRD:
  421. return RUNE_STAT_VALUES::CRD[$this->stars][$level];
  422. case RUNE_STAT_ID::RES:
  423. return RUNE_STAT_VALUES::RES[$this->stars][$level];
  424. case RUNE_STAT_ID::ACC:
  425. return RUNE_STAT_VALUES::ACC[$this->stars][$level];
  426. default:
  427. return 0;
  428. }
  429. }
  430. /**
  431. * Gets a stat name frm the mappings.
  432. *
  433. * @param int $q Quality id.
  434. * @return string Quality (Uppercase)
  435. */
  436. private function map_quality_name($q){
  437. switch ($q){
  438. case QUALITY_ID::NORMAL:
  439. return "NORMAL";
  440. case QUALITY_ID::MAGIC:
  441. return "MAGIC";
  442. case QUALITY_ID::RARE:
  443. return "RARE";
  444. case QUALITY_ID::HERO:
  445. return "HERO";
  446. case QUALITY_ID::LEGEND:
  447. return "LEGEND";
  448. default:
  449. return "";
  450. }
  451. }
  452. /**
  453. * Calcuates the efficiency of the rune.
  454. *
  455. * @return float Efficiency
  456. */
  457. private function calculate_efficiency(){
  458. $eff = [0.0, 0.0, 0.0, 0.0, 0.0];
  459. $query = "
  460. SELECT
  461. min,
  462. max
  463. FROM
  464. rune_roll_type,
  465. rune_stat_roll
  466. WHERE
  467. rune_roll_type.id = rune_stat_roll.type AND
  468. rune_roll_type.ancient = :ancient AND
  469. rune_roll_type.initial = :initial AND
  470. rune_roll_type.upgrade = :upgrade AND
  471. rune_stat_roll.stars = :stars AND
  472. rune_stat_roll.stat = :stat
  473. ";
  474. // Fixed stat
  475. if ($this->innate_stat != null && $this->innate_stat->get_value() > 0){
  476. $value = $this->innate_stat->get_value();
  477. $statement = get_context()->get_db()->prepare($query);
  478. $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
  479. $statement->bindValue(':initial', 1, SQLITE3_TEXT);
  480. $statement->bindValue(':upgrade', 0, SQLITE3_TEXT);
  481. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  482. $statement->bindValue(':stat', $this->innate_stat->stat, SQLITE3_TEXT);
  483. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  484. if ($r){
  485. $min_initial = 0;//$r["min"];
  486. $max_initial = $r["max"];
  487. $eff[0] = ($value - $min_initial) / (9 * ($max_initial - $min_initial));
  488. }
  489. }
  490. for ($i = 1; $i <= 4; $i ++){
  491. if (sizeof($this->stats) > $i && $this->stats[$i] != null && $this->stats[$i]->get_value() > 0){
  492. $value = $this->stats[$i]->get_value();
  493. $value = $this->stats[$i]->get_value();
  494. $statement = get_context()->get_db()->prepare($query);
  495. $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
  496. $statement->bindValue(':initial', 1, SQLITE3_TEXT);
  497. $statement->bindValue(':upgrade', 0, SQLITE3_TEXT);
  498. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  499. $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_TEXT);
  500. $r_initial = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  501. $statement = get_context()->get_db()->prepare($query);
  502. $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
  503. $statement->bindValue(':initial', 0, SQLITE3_TEXT);
  504. $statement->bindValue(':upgrade', 1, SQLITE3_TEXT);
  505. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  506. $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_TEXT);
  507. $r_upgrade = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  508. if ($r_initial && $r_upgrade){
  509. $min_initial = 0;//$r_initial["min"];
  510. $max_initial = $r_initial["max"];
  511. $min_upgrade = 0;//$r_upgrade["min"];
  512. $max_upgrade = $r_upgrade["max"];
  513. $eff[$i] = ((5 * $value) - ( 4 * $min_upgrade + $min_initial)) / ( 9 * ($max_initial - $min_initial + (4 * ($max_upgrade + $min_upgrade))));
  514. }
  515. }
  516. }
  517. $efficiency = 0.0;
  518. for ($i = 0; $i <= 4; $i ++){
  519. $efficiency += $eff[$i];
  520. }
  521. $efficiency *= 100;
  522. return $efficiency;
  523. }
  524. /**
  525. * Calcuates the maximum efficiency of the rune.
  526. *
  527. * @return float Efficiency
  528. */
  529. private function calculate_maximum_efficiency(){
  530. $max_efficiency = $this->efficiency;
  531. if ($this->level < 12){
  532. $max_efficiency += (1/9);
  533. }
  534. if ($this->level < 9){
  535. $max_efficiency += (1/9);
  536. }
  537. if ($this->level < 6){
  538. $max_efficiency += (1/9);
  539. }
  540. if ($this->level < 3){
  541. $max_efficiency += (1/9);
  542. }
  543. return $max_efficiency;
  544. }
  545. }
  546. ?>