Enchantments_Page.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Enchantment list page file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  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::PAGE . "Page.php");
  12. require_once(PATH::ENTITY . "Enchantment.php");
  13. /**
  14. * Enchantment list page model.
  15. *
  16. * @category Page
  17. */
  18. class Enchantments_Page extends Page{
  19. /**
  20. * @var Enchantment[] Selected enchantment gems.
  21. */
  22. private $gems = [];
  23. /**
  24. * @var Enchantment[] Selected enchantment grindstones.
  25. */
  26. private $grindstones = [];
  27. /**
  28. * @var Enchantment[] Selected enchantments.
  29. */
  30. private $enchantments = [];
  31. /**
  32. *
  33. * @var mixed[] Page filters.
  34. */
  35. private $filters = [
  36. "TYPE" => [],
  37. "RUNE" => [],
  38. "STAT" => [],
  39. "MIN_QUALITY" => QUALITY_ID::NORMAL,
  40. "MAX_QUALITY" => QUALITY_ID::LEGEND,
  41. ];
  42. /**
  43. * Constructor.
  44. *
  45. * Retrieves the data and initializes the variables.
  46. */
  47. public function __construct(){
  48. parent::__construct();
  49. $this->set_public(true);
  50. $this->set_view("enchantments.php");
  51. $this->set_title("Enchantments");
  52. $this->set_description(get_context()->get_player()->get_name() . "'s enchantments");
  53. $this->set_canonical("player/" . get_context()->get_player()->get_id() . "/enchantments/");
  54. $this->add_css("enchantments.css");
  55. $this->parse_filters();
  56. $result_set = $this->prepare_statement()->execute();
  57. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  58. $enchantment = new Enchantment($result["id"]);
  59. array_push($this->enchantments, $enchantment);
  60. if ($enchantment->is_gem()){
  61. array_push($this->gems, $enchantment);
  62. }
  63. else{
  64. array_push($this->grindstones, $enchantment);
  65. }
  66. }
  67. $this->set_code(200);
  68. $this->set_message("OK");
  69. }
  70. /**
  71. * Parses the request looking for the selected filters, validates them
  72. * and adds them to the $filters array.
  73. */
  74. private function parse_filters(){
  75. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
  76. $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
  77. }
  78. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
  79. $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
  80. }
  81. if (isset($_GET["stat"])){
  82. $stats = $_GET["stat"];
  83. foreach ($stats as $stat){
  84. array_push($this->filters["STAT"], intval($stat));
  85. }
  86. }
  87. if (isset($_GET["rune"])){
  88. $runes = $_GET["rune"];
  89. foreach ($runes as $rune){
  90. array_push($this->filters["RUNE"], intval($rune));
  91. }
  92. }
  93. if (isset($_GET["type"])){
  94. $types = $_GET["type"];
  95. foreach ($types as $type){
  96. array_push($this->filters["TYPE"], $type);
  97. }
  98. }
  99. }
  100. /**
  101. * Prepares the statement to execute against the database using the filter values.
  102. *
  103. * @return SQLite3Stmt prepared statement.
  104. */
  105. private function prepare_statement(){
  106. // Common items
  107. $query = "
  108. SELECT id
  109. FROM enchantment
  110. WHERE
  111. player = :player AND
  112. quality BETWEEN :min_quality AND :max_quality
  113. ";
  114. // Some main stat selected.
  115. if (count($this->filters["STAT"]) > 0){
  116. $query .= " AND stat IN (";
  117. for ($i = 0; $i < sizeof($this->filters["STAT"]); $i ++){
  118. $query .= ":stat_$i, ";
  119. }
  120. $query = rtrim($query, ", ") . ") ";
  121. }
  122. // Some sets selected.
  123. if (count($this->filters["RUNE"]) > 0){
  124. $query .= " AND (rune IN (";
  125. for ($i = 0; $i < sizeof($this->filters["RUNE"]); $i ++){
  126. $query .= ":rune_$i, ";
  127. }
  128. $query = rtrim($query, ", ");
  129. $query .= ") OR type IN (:inmemorial_grindstone, :inmemorial_gem))"; // Inmemorials
  130. }
  131. // Only one type selected.
  132. if (count($this->filters["TYPE"]) == 1){
  133. // Only one selectd, it's either gems or grindstones
  134. if (in_array("GEM", $this->filters["TYPE"])){
  135. $query .= " AND type IN (:type_gem, :type_inmemorial_gem) "; // Gems.
  136. }
  137. else{
  138. $query .= " AND type IN (:type_grindstone, :type_inmemorial_grindstone) "; // Grinds.
  139. }
  140. }
  141. $query .= "
  142. ORDER BY
  143. type = :type_inmemorial_grindstone DESC, -- Inmemorial Grindstone
  144. type = :type_inmemorial_gem DESC, -- Inmemorial Gem
  145. rune,
  146. type,
  147. quality DESC,
  148. stat
  149. ";
  150. $statement = get_context()->get_db()->prepare($query);
  151. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  152. $statement->bindValue(":min_quality", $this->filters["MIN_QUALITY"], SQLITE3_INTEGER);
  153. $statement->bindValue(":max_quality", $this->filters["MAX_QUALITY"], SQLITE3_INTEGER);
  154. for ($i = 0; $i < sizeof($this->filters["STAT"]); $i ++){
  155. $statement->bindValue(":stat_$i", $this->filters["STAT"][$i], SQLITE3_INTEGER);
  156. }
  157. for ($i = 0; $i < sizeof($this->filters["RUNE"]); $i ++){
  158. $statement->bindValue(":rune_$i", $this->filters["RUNE"][$i], SQLITE3_INTEGER);
  159. }
  160. $statement->bindValue(":inmemorial_gem", ENCHANTMENT_TYPE_ID::INMEMORIAL_GEM, SQLITE3_INTEGER);
  161. $statement->bindValue(":inmemorial_grindstone", ENCHANTMENT_TYPE_ID::INMEMORIAL_GRINDSTONE, SQLITE3_INTEGER);
  162. $statement->bindValue(":type_gem", ENCHANTMENT_TYPE_ID::GEM, SQLITE3_INTEGER);
  163. $statement->bindValue(":type_grindstone", ENCHANTMENT_TYPE_ID::GRINDSTONE, SQLITE3_INTEGER);
  164. $statement->bindValue(":type_inmemorial_gem", ENCHANTMENT_TYPE_ID::INMEMORIAL_GEM, SQLITE3_INTEGER);
  165. $statement->bindValue(":type_inmemorial_grindstone", ENCHANTMENT_TYPE_ID::INMEMORIAL_GRINDSTONE, SQLITE3_INTEGER);
  166. return $statement;
  167. }
  168. /**
  169. * Retrieves the gems to show.
  170. *
  171. * @return Enchantment[] Selected gems.
  172. */
  173. public function get_gems(){
  174. return $this->gems;
  175. }
  176. /**
  177. * Retrieves the grindstones to show.
  178. *
  179. * @return Enchantment[] Selected grindstones.
  180. */
  181. public function get_grindstones(){
  182. return $this->grindstones;
  183. }
  184. /**
  185. * Retrieves the enchantments to show, both gems and grindstones.
  186. *
  187. * @return Enchantment[] Selected enchantments.
  188. */
  189. public function get_enchantments(){
  190. return $this->enchantments;
  191. }
  192. /**
  193. * Retrieves the page filters.
  194. *
  195. * @return mixed[] Page filtes
  196. */
  197. public function get_filters(){
  198. return $this->filters;
  199. }
  200. /**
  201. * Retrieves one filter.
  202. *
  203. * @param string $key
  204. * @return mixed Filter value, null if if doesn't exist.
  205. */
  206. public function get_filter($key){
  207. if (array_key_exists($key, $this->filters)){
  208. return $this->filters[$key];
  209. }
  210. else{
  211. return null;
  212. }
  213. }
  214. }