Enchantment.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /**
  3. * Enchantments items entity file.
  4. *
  5. * Creates the entity and makes it available.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(PATH::ENTITY . "Entity.php");
  12. require_once(PATH::ENTITY . "Rune_Set.php");
  13. /**
  14. * Owned enchantments.
  15. *
  16. * Represent an enchantment, both grindstones and gems.
  17. *
  18. * @category Entity
  19. */
  20. class Enchantment extends Entity{
  21. /**
  22. * @var int Owner's player ID.
  23. */
  24. private $player_id;
  25. /**
  26. * @var int Enchantment ID.
  27. */
  28. private $id;
  29. /**
  30. * @var int Quality ID ({@see QUALITY_ID}).
  31. */
  32. private $quality;
  33. /**
  34. * @var string Quality name.
  35. */
  36. private $quality_name;
  37. /**
  38. * @var int Rune set ID ({@see RUNE_SET_ID}).
  39. */
  40. private $rune_set;
  41. /**
  42. * @var int ID of the stat affeced by the enchantmentt ({@see RUNE_STAT_ID}).
  43. */
  44. private $stat;
  45. /**
  46. * @var string Name of the affected stat.
  47. */
  48. private $stat_name;
  49. /**
  50. * @var bool Indicates if the enchantment is a gem.
  51. */
  52. private $gem;
  53. /**
  54. * @var bool Indicates if the enchantment is ancient.
  55. */
  56. private $ancient;
  57. /**
  58. * @var bool Indicates if the enchantment is inmemorial.
  59. */
  60. private $inmemorial;
  61. /**
  62. * @var string Enchantment name.
  63. */
  64. private $name;
  65. /**
  66. * @var int Minimal effect of the enchantment.
  67. */
  68. private $min;
  69. /**
  70. * @var int Maximum effect of the enchantment.
  71. */
  72. private $max;
  73. /**
  74. * @var int Value of the enchantment in mana stones.
  75. */
  76. private $value;
  77. /**
  78. * @var int Owned amount of the same enchantment.
  79. */
  80. private $amount;
  81. /**
  82. * Constructor.
  83. *
  84. * Searches the database and retrieves the information about the
  85. * rune, populating it and it's items.
  86. *
  87. * @param int $id Enchantment id.
  88. */
  89. public function __construct($id){
  90. $statement = get_context()->get_db()->prepare("
  91. SELECT
  92. enchantment.player AS player,
  93. enchantment.id AS id,
  94. enchantment.quality As quality,
  95. enchantment.rune AS rune,
  96. enchantment.stat AS stat,
  97. enchantment.value AS value,
  98. enchantment.amount AS amount,
  99. enchantment_type.name AS name,
  100. enchantment_type.gem AS gem,
  101. enchantment_type.inmemorial AS inmemorial,
  102. enchantment_type.ancient AS ancient,
  103. enchantment_range.min AS min,
  104. enchantment_range.max AS max
  105. FROM
  106. enchantment,
  107. enchantment_type,
  108. enchantment_range
  109. WHERE
  110. enchantment.id = :id AND
  111. enchantment.type = enchantment_type.id AND
  112. enchantment_type.gem = enchantment_range.gem AND
  113. enchantment_type.gem = enchantment_range.gem AND
  114. enchantment_type.ancient = enchantment_range.ancient AND
  115. enchantment.quality = enchantment_range.quality AND
  116. enchantment.stat = enchantment_range.stat;
  117. ");
  118. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  119. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  120. if ($r){
  121. $this->player_id = $r["player"];
  122. $this->id = $r["id"];
  123. if ($r["gem"] == 0){
  124. $this->gem = false;
  125. }
  126. else{
  127. $this->gem = true;
  128. }
  129. $this->quality = $r["quality"];
  130. $this->quality_name = $this->quality_name($r["quality"]);
  131. $this->stat = $r["stat"];
  132. $this->stat_name = $this->stat_name($r["stat"]);
  133. if ($r["ancient"] == 0){
  134. $this->ancient = false;
  135. }
  136. else{
  137. $this->ancient = true;
  138. }
  139. $this->inmemorial = $r["inmemorial"];
  140. if ($r["inmemorial"] == 0){
  141. $this->rune_set = new Rune_Set($r["rune"]);
  142. $this->name = $this->rune_set->get_name() . " " . $this->stat_name;
  143. $this->inmemorial = false;
  144. }
  145. else{
  146. $this->inmemorial = true;
  147. $this->name = "Inmemorial " . $this->stat_name;
  148. }
  149. $this->min = $r["min"];
  150. $this->max = $r["max"];
  151. $this->value = $r["value"];
  152. $this->amount = $r["amount"];
  153. }
  154. else{
  155. // Not found, maybe as a run drop?
  156. $statement = get_context()->get_db()->prepare("
  157. SELECT
  158. run_drop_enchantment.id AS id,
  159. type,
  160. run_drop_enchantment.quality AS quality,
  161. rune,
  162. run_drop_enchantment.stat AS stat,
  163. value,
  164. enchantment_type.inmemorial AS inmemorial,
  165. enchantment_type.ancient AS ancient,
  166. enchantment_range.min AS min,
  167. enchantment_range.max AS max
  168. FROM
  169. run_drop_enchantment,
  170. enchantment_type,
  171. enchantment_range
  172. WHERE
  173. run_drop_enchantment.id = :id AND
  174. run_drop_enchantment.type = enchantment_type.id AND
  175. enchantment_type.gem = enchantment_range.gem AND
  176. enchantment_type.gem = enchantment_range.gem AND
  177. enchantment_type.ancient = enchantment_range.ancient AND
  178. run_drop_enchantment.quality = enchantment_range.quality AND
  179. run_drop_enchantment.stat = enchantment_range.stat;
  180. ");
  181. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  182. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  183. if ($r){
  184. $this->player_id = get_context()->get_player()->get_id();
  185. if ($r["type"] == ENCHANTMENT_TYPE_ID::GEM || $r["type"] == ENCHANTMENT_TYPE_ID::INMEMORIAL_GEM){
  186. $this->gem = 1;
  187. }
  188. else{
  189. $this->gem = 0;
  190. }
  191. $this->quality = $r["quality"];
  192. $this->quality_name = $this->quality_name($r["quality"]);
  193. $this->value = 0;
  194. $this->stat = $r["stat"];
  195. $this->stat_name = $this->stat_name($r["stat"]);
  196. $this->ancient = filter_var($r["ancient"], FILTER_VALIDATE_BOOLEAN);
  197. $this->inmemorial = filter_var($r["inmemorial"], FILTER_VALIDATE_BOOLEAN);
  198. if ($r["inmemorial"] == 0){
  199. $this->rune_set = new Rune_Set($r["rune"]);
  200. $this->name = $this->rune_set->get_name() . " " . $this->stat_name;
  201. $this->inmemorial = false;
  202. }
  203. else{
  204. $this->inmemorial = true;
  205. $this->name = "Inmemorial " . $this->stat_name;
  206. }
  207. $this->min = $r["min"];
  208. $this->max = $r["max"];
  209. $this->value = $r["value"];
  210. $this->amount = 1;
  211. }
  212. }
  213. }
  214. /**
  215. * Gets a stat name from the maps.
  216. *
  217. * @param int $stat Stat id.
  218. * @return string Stat name (Uppercase)
  219. */
  220. private function stat_name($stat){
  221. switch ($stat){
  222. case RUNE_STAT_ID::HP:
  223. return "HP";
  224. case RUNE_STAT_ID::HP_P:
  225. return "HP%";
  226. case RUNE_STAT_ID::ATK:
  227. return "ATK";
  228. case RUNE_STAT_ID::ATK_P:
  229. return "ATK%";
  230. case RUNE_STAT_ID::DEF:
  231. return "DEF";
  232. case RUNE_STAT_ID::DEF_P:
  233. return "DEF%";
  234. case RUNE_STAT_ID::SPD:
  235. return "SPD";
  236. case RUNE_STAT_ID::CRR:
  237. return "CRR";
  238. case RUNE_STAT_ID::CRD:
  239. return "CRD";
  240. case RUNE_STAT_ID::RES:
  241. return "RES";
  242. case RUNE_STAT_ID::ACC:
  243. return "ACC";
  244. default:
  245. return "";
  246. }
  247. }
  248. /**
  249. * Gets a stat name from the maps.
  250. *
  251. * @param int $q Quality id.
  252. * @return string Quality (Uppercase)
  253. */
  254. private function quality_name($q){
  255. switch ($q){
  256. case QUALITY_ID::NORMAL:
  257. return "NORMAL";
  258. case QUALITY_ID::MAGIC:
  259. return "MAGIC";
  260. case QUALITY_ID::RARE:
  261. return "RARE";
  262. case QUALITY_ID::HERO:
  263. return "HERO";
  264. case QUALITY_ID::LEGEND:
  265. return "LEGEND";
  266. default:
  267. return "";
  268. }
  269. }
  270. /**
  271. * Retrieves the owner's player identifier.
  272. *
  273. * @return int player identifier.
  274. */
  275. public function get_player_id(){
  276. return $this->player_id;
  277. }
  278. /**
  279. * Retrieves the enchantment identifier.
  280. *
  281. * @return string Enchantment ID.
  282. */
  283. public function get_id(){
  284. return $this->id;
  285. }
  286. /**
  287. * Retrieves the quality of the enchantment.
  288. *
  289. * @see QUALITY_ID
  290. *
  291. * @return int Quality.
  292. */
  293. public function get_quality(){
  294. return $this->quality;
  295. }
  296. /**
  297. * Retrieves the quality name of the enchantment.
  298. *
  299. * @return string Quality name.
  300. */
  301. public function get_quality_name(){
  302. return $this->quality_name;
  303. }
  304. /**
  305. * Retrieves the rune set the enchantment can be used on.
  306. *
  307. * @return Rune_Set The set the enchantment can be used on, null for inmemorials.
  308. */
  309. public function get_rune_set(){
  310. return $this->rune_set;
  311. }
  312. /**
  313. * Retrieves the stat affected by the enchantment.
  314. *
  315. * @see RUNE_STAT_ID
  316. *
  317. * @return int Stat ID.
  318. */
  319. public function get_stat(){
  320. return $this->stat;
  321. }
  322. /**
  323. * Retrieves the name of the affected stat.
  324. *
  325. * @return string Stat name
  326. */
  327. public function get_stat_name(){
  328. return $this->stat_name;
  329. }
  330. /**
  331. * Checks if the enchantment is a enchanted gem.
  332. *
  333. * @return bool true for enchanted gems, false for grindstones.
  334. */
  335. public function is_gem(){
  336. return $this->gem;
  337. }
  338. /**
  339. * Checks if the enchantment is a grindstone.
  340. *
  341. * @return bool true for grindstones, false enchanted gems.
  342. */
  343. public function is_grind(){
  344. return ! $this->gem;
  345. }
  346. /**
  347. * Checks if it's an ancient enchantment.
  348. *
  349. * @return bool True if ancient, false if normal.
  350. */
  351. public function is_ancient(){
  352. return $this->ancient;
  353. }
  354. /**
  355. * Checks if the enchantment is inmemorial.
  356. *
  357. * @return bool True for inmemorial, false otherwise.
  358. */
  359. public function is_inmemorial(){
  360. return $this->inmemorial;
  361. }
  362. /**
  363. * Retrieves the enchantment name.
  364. *
  365. * @return string Enchantment name.
  366. */
  367. public function get_name(){
  368. return $this->name;
  369. }
  370. /**
  371. * Retrieves the minumum value for the enchantment.
  372. *
  373. * @return int Minimum enchant outcome.
  374. */
  375. public function get_min(){
  376. return $this->min;
  377. }
  378. /**
  379. * Retrieves the maximum value for the enchantment.
  380. *
  381. * @return int Maximum enchant outcome.
  382. */
  383. public function get_max(){
  384. return $this->max;
  385. }
  386. /**
  387. * Retrieves the value of the enchantment in mana stones.
  388. *
  389. * @return int Value of the enchantment.
  390. */
  391. public function get_value(){
  392. return $this->value;
  393. }
  394. /**
  395. * Retrieves the amount of the same enchantment owned by the user.
  396. *
  397. * @return int Number of the enchantments owned.
  398. */
  399. public function get_amount(){
  400. return $this->amount;
  401. }
  402. }