Monsters_Page.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * @category Page
  8. */
  9. /**
  10. * Require dependent files if not present.
  11. */
  12. require_once(PATH::PAGE . "Page.php");
  13. require_once(PATH::ENTITY . "Unit.php");
  14. require_once(PATH::ENTITY . "K_Effect.php");
  15. /**
  16. * Monster list page model.
  17. *
  18. * @category Page
  19. */
  20. class Monsters_Page extends Page{
  21. /**
  22. * @var \Unit[] Units to show.
  23. */
  24. public $monsters = [];
  25. /**
  26. * @var mixed[] Default filter values.
  27. */
  28. public $filters = [
  29. "ELEMENT" => -1,
  30. "NAME" => "",
  31. "MIN_STARS" => 1,
  32. "MAX_STARS" => 6,
  33. "IN_STORAGE" => false,
  34. "WITHOUT_RUNES" => false,
  35. "EFFECT" => [],
  36. "EFFECT_OPTION" => "any"
  37. ];
  38. /**
  39. * @var K_Effect[] All availabe skill effects, for the tfilter selector.
  40. */
  41. public $effects = [];
  42. /**
  43. * Constructor.
  44. *
  45. * Retrieves the data and initializes the variables.
  46. *
  47. * @global resource Database connection.
  48. */
  49. public function __construct(){
  50. global $db;
  51. $this->view = PATH::VIEW . "monsters.php";
  52. $this->parse_filters();
  53. $s_mon = $this->build_query();
  54. $q_mon = $db->query($s_mon);
  55. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  56. array_push($this->monsters, new Unit($r_mon["id"]));
  57. }
  58. // Get skill effects
  59. $s_eff = "SELECT id FROM k_effect ORDER BY is_buff DESC";
  60. $q_eff = $db->query($s_eff);
  61. while ($r_eff = $q_eff->fetchArray(SQLITE3_ASSOC)){
  62. array_push($this->effects, new K_Effect($r_eff["id"]));
  63. }
  64. $this->title = "Monsters - SWDB";
  65. $this->description = "Owned monsters";
  66. $this->canonical = URL::BASE . "monsters/";
  67. }
  68. /**
  69. * Parses the request looking for the selected filters, validates them
  70. * and adds them to the $filters array.
  71. */
  72. private function parse_filters(){
  73. if (isset($_GET["element"]) && APPLICATION::valid_id("ELEMENT_ID", strtoupper($_GET["element"]))){
  74. $this->filters["ELEMENT"] = $_GET["element"];
  75. }
  76. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  77. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  78. }
  79. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 6){
  80. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  81. }
  82. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 6){
  83. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  84. }
  85. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  86. $this->filters["IN_STORAGE"] = true;
  87. }
  88. else{
  89. $this->filters["IN_STORAGE"] = false;
  90. }
  91. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  92. $this->filters["WITHOUT_RUNES"] = true;
  93. }
  94. if (isset($_GET["effect"])){
  95. $effects = $_GET["effect"];
  96. foreach ($effects as $effect){
  97. if (intval($effect) > 0){
  98. array_push($this->filters["EFFECT"], intval($effect));
  99. }
  100. }
  101. }
  102. if (isset($_GET["effect_option"]) && ($_GET["effect_option"] == "and" || $_GET["effect_option"] == "or")){
  103. $this->filters["EFFECT_OPTION"] = $_GET["effect_option"];
  104. }
  105. return;
  106. }
  107. /**
  108. * Builds the query to the rune table using the selected or default
  109. * filters.
  110. *
  111. * @return string The query to be executed.
  112. * @global int Player ID.
  113. */
  114. private function build_query(){
  115. global $UID;
  116. $s = "
  117. SELECT unit.id AS id
  118. FROM
  119. unit,
  120. k_unit
  121. WHERE
  122. unit.uid = '$UID' AND
  123. unit.unit = k_unit.id AND
  124. stars >= " . $this->filters["MIN_STARS"] . " AND
  125. stars <= " . $this->filters["MAX_STARS"];
  126. if (APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
  127. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE element = " . $this->filters["ELEMENT"] . ") ";
  128. }
  129. if ($this->filters["NAME"] != ""){
  130. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%') ";
  131. }
  132. if (!$this->filters["IN_STORAGE"]){
  133. $s = $s . " AND building NOT IN (SELECT id FROM building WHERE building = " . BUILDING_ID::MONSTER_STORAGE . ") ";
  134. }
  135. if (!$this->filters["WITHOUT_RUNES"]){
  136. $s = $s . " AND unit.id IN (SELECT DISTINCT unit FROM unit_rune) ";
  137. }
  138. if (sizeof($this->filters["EFFECT"]) > 0){
  139. if ($this->filters["EFFECT_OPTION"] == "and"){
  140. foreach ($this->filters["EFFECT"] as $effect){
  141. $s .= "
  142. AND k_unit.id IN (
  143. SELECT DISTINCT unit
  144. FROM
  145. k_unit_skill,
  146. k_skill_effect
  147. WHERE
  148. k_unit_skill.skill = k_skill_effect.skill AND
  149. k_skill_effect.effect = $effect
  150. )
  151. ";
  152. }
  153. }
  154. elseif ($this->filters["EFFECT_OPTION"] == "or"){
  155. $s .= " AND ( ";
  156. foreach ($this->filters["EFFECT"] as $effect){
  157. $s .= "
  158. k_unit.id IN (
  159. SELECT DISTINCT unit
  160. FROM
  161. k_unit_skill,
  162. k_skill_effect
  163. WHERE
  164. k_unit_skill.skill = k_skill_effect.skill AND
  165. k_skill_effect.effect = $effect
  166. ) OR
  167. ";
  168. }
  169. $s .= " 1 = 2 ) ";
  170. }
  171. }
  172. $s = $s . "
  173. ORDER BY
  174. stars DESC,
  175. level DESC,
  176. element = " . ELEMENT_ID::FIRE ." DESC,
  177. element = " . ELEMENT_ID::WATER ." DESC,
  178. element = " . ELEMENT_ID::WIND ." DESC,
  179. element = " . ELEMENT_ID::LIGHT ." DESC,
  180. element = " . ELEMENT_ID::DARK ." DESC
  181. ";
  182. return $s;
  183. }
  184. }
  185. ?>