Artifacts_Page.php 7.7 KB

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