Artifacts_Page.php 8.1 KB

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