Artifacts_Page.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Artifact 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 . "Custom_Filter.php");
  14. require_once(PATH::ENTITY . "Artifact.php");
  15. require_once(PATH::ENTITY . "Unit.php");
  16. /**
  17. * Artifact list page model.
  18. *
  19. * @category Page
  20. */
  21. class Artifacts_Page extends Page{
  22. /**
  23. * @var \Artifact[] Artiffacts to show.
  24. *
  25. * In this case, the monster member is not the id, but a monster
  26. * instance.
  27. */
  28. public $artifacts = [];
  29. /**
  30. * @var \Filter[] Custom filters for this page.
  31. */
  32. public $custom_filters = [];
  33. /**
  34. * Constructor.
  35. *
  36. * Retrieves the data and initializes the variables.
  37. *
  38. * @global int Player ID.
  39. * @global resource Database connection.
  40. */
  41. public function __construct(){
  42. global $UID;
  43. global $db;
  44. $this->view = PATH::VIEW . "artifacts.php";
  45. $this->parse_filters();
  46. $s = $this->build_query();
  47. $q = $db->query($s);
  48. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  49. $artifact = new Artifact($r["id"]);
  50. if ($r["unit"]){
  51. $artifact->unit = new Unit($r["unit"], false);
  52. }
  53. array_push($this->artifacts, $artifact);
  54. }
  55. $this->title = "Artifacts - SWDB";
  56. $this->description = "Artifact inventory";
  57. $this->canonical = URL::BASE . "artifacts/";
  58. }
  59. /**
  60. * Parses the request looking for the selected filters, validates them
  61. * and adds them to the $filters array.
  62. */
  63. private function parse_filters(){
  64. $this->filters = FILTER::ARTIFACT;
  65. if (isset($_GET["element"])){
  66. $elements = $_GET["element"];
  67. foreach ($elements as $element){
  68. array_push($this->filters["ELEMENT"], intval($element));
  69. }
  70. }
  71. if (isset($_GET["archetype"])){
  72. $archetypes = $_GET["archetype"];
  73. foreach ($archetypes as $archetype){
  74. array_push($this->filters["ARCHETYPE"], intval($archetype));
  75. }
  76. }
  77. if (isset($_GET["main_stat"])){
  78. $mains = $_GET["main_stat"];
  79. foreach ($mains as $main){
  80. array_push($this->filters["MAIN"], intval($main));
  81. }
  82. }
  83. if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
  84. $this->filters["MIN_LEVEL"] = $_GET["min_level"];
  85. }
  86. if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
  87. $this->filters["MAX_LEVEL"] = $_GET["max_level"];
  88. }
  89. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
  90. $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
  91. }
  92. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
  93. $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
  94. }
  95. if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_original_quality"]) <= QUALITY_ID::LEGEND){
  96. $this->filters["MIN_ORIGINAL_QUALITY"] = $_GET["min_original_quality"];
  97. }
  98. if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_original_quality"]) <= QUALITY_ID::LEGEND){
  99. $this->filters["MAX_ORIGINAL_QUALITY"] = $_GET["max_original_quality"];
  100. }
  101. if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
  102. $this->filters["ASSIGNED"] = $_GET["assigned"];
  103. }
  104. }
  105. /**
  106. * Builds the query to the artifact table using the selected or default
  107. * filters.
  108. *
  109. * @return string The query to be executed.
  110. * @global int Player ID.
  111. */
  112. private function build_query(){
  113. global $UID;
  114. global $MODE;
  115. $s = "
  116. SELECT
  117. id,
  118. (
  119. SELECT DISTINCT unit
  120. FROM unit_artifact
  121. WHERE
  122. artifact = artifact.id AND
  123. rta = $MODE
  124. ) AS unit
  125. FROM artifact
  126. WHERE
  127. uid = '$UID' AND
  128. level >= " . $this->filters["MIN_LEVEL"] . " AND
  129. level <= " . $this->filters["MAX_LEVEL"] . " AND
  130. quality >= " . $this->filters["MIN_QUALITY"] . " AND
  131. quality <= " . $this->filters["MAX_QUALITY"] . " AND
  132. original_quality >= " . $this->filters["MIN_ORIGINAL_QUALITY"] . " AND
  133. original_quality <= " . $this->filters["MAX_ORIGINAL_QUALITY"] . " AND
  134. efficiency >= " . $this->filters["MIN_EFFICIENCY"] . " AND
  135. efficiency <= " . $this->filters["MAX_EFFICIENCY"] . " AND
  136. max_efficiency >= " . $this->filters["MIN_MAX_EFFICIENCY"] . " AND
  137. max_efficiency <= " . $this->filters["MAX_MAX_EFFICIENCY"] . "
  138. ";
  139. if (count($this->filters["MAIN"]) > 0){
  140. $s = $s . " AND main_stat IN ( ";
  141. foreach($this->filters["MAIN"] as $t){
  142. $s = $s . "$t, ";
  143. }
  144. $s = $s . "-1) ";
  145. }
  146. if (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) == 0){
  147. // No element/archetype conditions
  148. }
  149. elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) == 0){
  150. // Only selected elements
  151. $s = $s . " AND attribute IN ( ";
  152. foreach($this->filters["ELEMENT"] as $t){
  153. $s = $s . "$t, ";
  154. }
  155. $s = $s . "-1) ";
  156. }
  157. elseif (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) > 0){
  158. // Only selected types
  159. $s = $s . " AND archetype IN ( ";
  160. foreach($this->filters["ARCHETYPE"] as $t){
  161. $s = $s . "$t, ";
  162. }
  163. $s = $s . "-1) ";
  164. }
  165. elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) > 0){
  166. // Selected elements and selected types
  167. $s = $s . " AND (archetype IN ( ";
  168. foreach($this->filters["ARCHETYPE"] as $t){
  169. $s = $s . "$t, ";
  170. }
  171. $s = $s . "-1) OR attribute IN ( ";
  172. foreach($this->filters["ELEMENT"] as $t){
  173. $s = $s . "$t, ";
  174. }
  175. $s = $s . "-1)) ";
  176. }
  177. switch ($this->filters["ASSIGNED"]){
  178. case 1:
  179. $s = $s . " AND unit IS NOT NULL ";
  180. break;
  181. case 2:
  182. $s = $s . " AND unit IS NULL ";
  183. break;
  184. case 3:
  185. $s = $s . " AND (unit IS NULL OR unit IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ")) ";
  186. break;
  187. case 4:
  188. $s = $s . " AND unit IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ") ";
  189. break;
  190. }
  191. $s = $s . " ORDER BY type, attribute, archetype, level DESC, original_quality DESC;";
  192. return $s;
  193. }
  194. }
  195. ?>