Monsters_Page.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Monster 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 . "Monster.php");
  13. /**
  14. * Monster list page model.
  15. *
  16. * @category Page
  17. */
  18. class Monsters_Page extends Page{
  19. /**
  20. * @var Monster[] List of monsters to show.
  21. */
  22. private $monsters = [];
  23. /**
  24. * @var int[] IDs of the monsters marked in the catalog.
  25. */
  26. private $monsters_open = [];
  27. /**
  28. * @var mixed[] Default filter values.
  29. */
  30. private $filters = [
  31. "ELEMENT" => -1,
  32. "NAME" => "",
  33. "MIN_STARS" => 1,
  34. "MAX_STARS" => 6,
  35. ];
  36. /**
  37. * Constructor.
  38. *
  39. * Retrieves the data and initializes the variables.
  40. */
  41. public function __construct(){
  42. parent::__construct();
  43. $this->set_public(true);
  44. $this->set_view("monsters.php");
  45. $this->set_title("Monsters");
  46. $this->set_description("Catalog of all monsters");
  47. $this->set_canonical("monsters/");
  48. $this->add_css("monsters.css");
  49. $this->parse_filters();
  50. $result_set = $this->prepare_statement()->execute();
  51. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  52. array_push($this->monsters, new Monster($result["id"], false));
  53. }
  54. // Get open monsters
  55. $statement = get_context()->get_db()->prepare("
  56. SELECT monster FROM collection WHERE player = :player AND open = :open");
  57. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  58. $statement->bindValue(":open", 1, SQLITE3_INTEGER);
  59. $result_set = $statement->execute();
  60. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  61. array_push($this->monsters_open, $result["monster"]);
  62. }
  63. $this->set_code(200);
  64. $this->set_message("OK");
  65. }
  66. /**
  67. * Parses the request looking for the selected filters, validates them
  68. * and adds them to the $filters array.
  69. */
  70. private function parse_filters(){
  71. if (isset($_GET["element"]) && APPLICATION::valid_id("ELEMENT_ID", strtoupper($_GET["element"]))){
  72. $this->filters["ELEMENT"] = $_GET["element"];
  73. }
  74. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  75. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  76. }
  77. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 5){
  78. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  79. }
  80. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 5){
  81. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  82. }
  83. return;
  84. }
  85. /**
  86. * Prepares the statement to execute against the database using the filter values.
  87. *
  88. * @return SQLite3Stmt prepared statement.
  89. */
  90. private function prepare_statement(){
  91. // Common items
  92. $query = "
  93. SELECT id
  94. FROM monster
  95. WHERE
  96. obtainable = 1 AND
  97. awakens_from IS NULL AND
  98. natural_stars BETWEEN :min_stars AND :max_stars
  99. ";
  100. if(APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
  101. $query .= " AND element = :element ";
  102. }
  103. if ($this->filters["NAME"] != ""){
  104. $query .= " AND upper(name) LIKE upper(:name) " ;
  105. }
  106. $query .= "
  107. ORDER BY
  108. element = :fire DESC,
  109. element = :water DESC,
  110. element = :wind DESC,
  111. element = :light DESC,
  112. element = :dark DESC,
  113. natural_stars;
  114. ";
  115. $statement = get_context()->get_db()->prepare($query);
  116. $statement->bindValue(":min_stars", $this->filters["MIN_STARS"], SQLITE3_INTEGER);
  117. $statement->bindValue(":max_stars", $this->filters["MAX_STARS"], SQLITE3_INTEGER);
  118. $statement->bindValue(":element", $this->filters["ELEMENT"], SQLITE3_INTEGER);
  119. $statement->bindValue(":name", "%" . $this->filters["NAME"] . "%", SQLITE3_TEXT);
  120. $statement->bindValue(":fire", ELEMENT_ID::FIRE, SQLITE3_INTEGER);
  121. $statement->bindValue(":water", ELEMENT_ID::WATER, SQLITE3_INTEGER);
  122. $statement->bindValue(":wind", ELEMENT_ID::WIND, SQLITE3_INTEGER);
  123. $statement->bindValue(":light", ELEMENT_ID::LIGHT, SQLITE3_INTEGER);
  124. $statement->bindValue(":dark", ELEMENT_ID::DARK, SQLITE3_INTEGER);
  125. return $statement;
  126. }
  127. /**
  128. * Retrieves the monsters to display
  129. *
  130. * @return Monster[] Selected monsters.
  131. */
  132. public function get_monsters(){
  133. return $this->monsters;
  134. }
  135. /**
  136. * Retrieves the page filters.
  137. *
  138. * @return mixed[] Page filtes
  139. */
  140. public function get_filters(){
  141. return $this->filters;
  142. }
  143. /**
  144. * Retrieves one filter.
  145. *
  146. * @param string $key
  147. * @return mixed Filter value, null if if doesn't exist.
  148. */
  149. public function get_filter($key){
  150. if (array_key_exists($key, $this->filters)){
  151. return $this->filters[$key];
  152. }
  153. else{
  154. return null;
  155. }
  156. }
  157. public function is_open($id){
  158. if (in_array($id, $this->monsters_open)){
  159. return true;
  160. }
  161. else{
  162. return false;
  163. }
  164. }
  165. }
  166. ?>