Rune.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. public $player;
  27. /**
  28. * @var int Rune identifier.
  29. */
  30. public $id;
  31. /**
  32. * @var 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. */
  117. public function __construct($id){
  118. $statement = get_context()->get_db()->prepare("
  119. SELECT
  120. id,
  121. type,
  122. slot,
  123. stars,
  124. level,
  125. ancient,
  126. quality,
  127. original_quality,
  128. value,
  129. efficiency,
  130. max_efficiency,
  131. locked
  132. FROM rune
  133. WHERE id = :id;
  134. ");
  135. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  136. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  137. if ($r){
  138. $this->id = $r["id"];
  139. $this->type = new Rune_Set($r["type"]);
  140. $this->slot = $r["slot"];
  141. $this->stars = $r["stars"];
  142. $this->level = $r["level"];
  143. $this->ancient = $r["ancient"];
  144. $this->quality = $r["quality"];
  145. $this->original_quality = $r["original_quality"];
  146. $this->value = $r["value"];
  147. $this->efficiency = $r["efficiency"];
  148. $this->max_efficiency = $r["max_efficiency"];
  149. $this->locked = $r["locked"];
  150. // Get substats
  151. $statement = get_context()->get_db()->prepare("
  152. SELECT slot
  153. FROM rune_stat
  154. WHERE rune = :rune
  155. ORDER BY slot;
  156. ");
  157. $statement->bindValue(':rune', $this->id, SQLITE3_TEXT);
  158. $q = $statement->execute();
  159. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  160. $stat = new Rune_Stat($this->id, $r["slot"]);
  161. array_push($this->stats, $stat);
  162. switch ($r["slot"]){
  163. case RUNE_STAT_SLOT_ID::MAIN:
  164. $this->main_stat = $stat;
  165. break;
  166. case RUNE_STAT_SLOT_ID::INNATE:
  167. $this->innate_stat = $stat;
  168. break;
  169. default:
  170. array_push($this->substats, $stat);
  171. break;
  172. }
  173. }
  174. $this->quality_name = $this->get_quality_name($this->quality);
  175. $this->original_quality_name = $this->get_quality_name($this->original_quality);
  176. if ($this->efficiency == null || $this->efficiency == "" || intval($this->efficiency) == 0){
  177. $this->efficiency = $this->calculate_efficiency();
  178. $this->max_efficiency = $this->calculate_maximum_efficiency();
  179. $statement = get_context()->get_db()->prepare("
  180. UPDATE rune
  181. SET
  182. efficiency = :efficiency,
  183. max_efficiency = :max_efficiency
  184. WHERE id = :rune
  185. ");
  186. $statement->bindValue(':efficiency', $this->efficiency, SQLITE3_FLOAT);
  187. $statement->bindValue(':max_efficiency', $this->max_efficiency, SQLITE3_FLOAT);
  188. $statement->bindValue(':rune', $this->id, SQLITE3_INTEGER);
  189. $q = $statement->execute();
  190. }
  191. }
  192. }
  193. /**
  194. * Gets the value of the main stat at an arbitrary level.
  195. *
  196. * @param int $level Desired level.
  197. * @return int Value of the main stat at the selected level.
  198. */
  199. public function get_main_stat_at_level($level){
  200. $level = intval($level);
  201. if ($level < 0 || $level > 15){
  202. return 0;
  203. }
  204. switch ($this->main_stat->stat){
  205. case RUNE_STAT_ID::HP:
  206. return RUNE_STAT_VALUES::HP[$this->stars][$level];
  207. case RUNE_STAT_ID::HP_P:
  208. return RUNE_STAT_VALUES::HP_P[$this->stars][$level];
  209. case RUNE_STAT_ID::ATK:
  210. return RUNE_STAT_VALUES::ATK[$this->stars][$level];
  211. case RUNE_STAT_ID::ATK_P:
  212. return RUNE_STAT_VALUES::ATK_P[$this->stars][$level];
  213. case RUNE_STAT_ID::DEF:
  214. return RUNE_STAT_VALUES::DEF[$this->stars][$level];
  215. case RUNE_STAT_ID::DEF_P:
  216. return RUNE_STAT_VALUES::DEF_P[$this->stars][$level];
  217. case RUNE_STAT_ID::SPD:
  218. return RUNE_STAT_VALUES::SPD[$this->stars][$level];
  219. case RUNE_STAT_ID::CRR:
  220. return RUNE_STAT_VALUES::CRR[$this->stars][$level];
  221. case RUNE_STAT_ID::CRD:
  222. return RUNE_STAT_VALUES::CRD[$this->stars][$level];
  223. case RUNE_STAT_ID::RES:
  224. return RUNE_STAT_VALUES::RES[$this->stars][$level];
  225. case RUNE_STAT_ID::ACC:
  226. return RUNE_STAT_VALUES::ACC[$this->stars][$level];
  227. default:
  228. return 0;
  229. }
  230. }
  231. /**
  232. * Gets a stat name frm the mappings.
  233. *
  234. * @param int $q Quality id.
  235. * @return string Quality (Uppercase)
  236. */
  237. private function get_quality_name($q){
  238. switch ($q){
  239. case QUALITY_ID::NORMAL:
  240. return "NORMAL";
  241. case QUALITY_ID::MAGIC:
  242. return "MAGIC";
  243. case QUALITY_ID::RARE:
  244. return "RARE";
  245. case QUALITY_ID::HERO:
  246. return "HERO";
  247. case QUALITY_ID::LEGEND:
  248. return "LEGEND";
  249. default:
  250. return "";
  251. }
  252. }
  253. /**
  254. * Calcuates the efficiency of the rune.
  255. *
  256. * @return float Efficiency
  257. */
  258. private function calculate_efficiency(){
  259. $eff = [0.0, 0.0, 0.0, 0.0, 0.0];
  260. $query = "
  261. SELECT
  262. min,
  263. max
  264. FROM
  265. rune_roll_type,
  266. rune_stat_roll
  267. WHERE
  268. rune_roll_type.id = rune_stat_roll.type AND
  269. rune_roll_type.ancient = :ancient AND
  270. rune_roll_type.initial = :initial AND
  271. rune_roll_type.upgrade = :upgrade AND
  272. rune_stat_roll.stars = :stars AND
  273. rune_stat_roll.stat = :stat
  274. ";
  275. // Fixed stat
  276. if ($this->innate_stat != null && $this->innate_stat->value > 0){
  277. $value = $this->innate_stat->value;
  278. $statement = get_context()->get_db()->prepare($query);
  279. $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
  280. $statement->bindValue(':initial', 1, SQLITE3_TEXT);
  281. $statement->bindValue(':upgrade', 0, SQLITE3_TEXT);
  282. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  283. $statement->bindValue(':stat', $this->innate_stat->stat, SQLITE3_TEXT);
  284. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  285. if ($r){
  286. $min_initial = 0;//$r["min"];
  287. $max_initial = $r["max"];
  288. $eff[0] = ($value - $min_initial) / (9 * ($max_initial - $min_initial));
  289. }
  290. }
  291. for ($i = 1; $i <= 4; $i ++){
  292. if (sizeof($this->stats) > $i && $this->stats[$i] != null && $this->stats[$i]->value > 0){
  293. $value = $this->stats[$i]->value;
  294. $value = $this->stats[$i]->value;
  295. $statement = get_context()->get_db()->prepare($query);
  296. $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
  297. $statement->bindValue(':initial', 1, SQLITE3_TEXT);
  298. $statement->bindValue(':upgrade', 0, SQLITE3_TEXT);
  299. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  300. $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_TEXT);
  301. $r_initial = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  302. $statement = get_context()->get_db()->prepare($query);
  303. $statement->bindValue(':ancient', $this->ancient, SQLITE3_TEXT);
  304. $statement->bindValue(':initial', 0, SQLITE3_TEXT);
  305. $statement->bindValue(':upgrade', 1, SQLITE3_TEXT);
  306. $statement->bindValue(':stars', $this->stars, SQLITE3_TEXT);
  307. $statement->bindValue(':stat', $this->stats[$i]->stat, SQLITE3_TEXT);
  308. $r_upgrade = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  309. if ($r_initial && $r_upgrade){
  310. $min_initial = 0;//$r_initial["min"];
  311. $max_initial = $r_initial["max"];
  312. $min_upgrade = 0;//$r_upgrade["min"];
  313. $max_upgrade = $r_upgrade["max"];
  314. $eff[$i] = ((5 * $value) - ( 4 * $min_upgrade + $min_initial)) / ( 9 * ($max_initial - $min_initial + (4 * ($max_upgrade + $min_upgrade))));
  315. }
  316. }
  317. }
  318. $efficiency = 0.0;
  319. for ($i = 0; $i <= 4; $i ++){
  320. $efficiency += $eff[$i];
  321. }
  322. $efficiency *= 100;
  323. return $efficiency;
  324. }
  325. /**
  326. * Calcuates the maximum efficiency of the rune.
  327. *
  328. * @return float Efficiency
  329. */
  330. private function calculate_maximum_efficiency(){
  331. $max_efficiency = $this->efficiency;
  332. if ($this->level < 12){
  333. $max_efficiency += (1/9);
  334. }
  335. if ($this->level < 9){
  336. $max_efficiency += (1/9);
  337. }
  338. if ($this->level < 6){
  339. $max_efficiency += (1/9);
  340. }
  341. if ($this->level < 3){
  342. $max_efficiency += (1/9);
  343. }
  344. return $max_efficiency;
  345. }
  346. }
  347. ?>