Artifacts_Page.php 7.9 KB

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