Artifacts_Page.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. if (!isset($_GET["custom_filter"])){
  47. $s = $this->build_query();
  48. }
  49. else{
  50. $s = $this->build_custom_query($_GET["custom_filter"]);
  51. }
  52. $q = $db->query($s);
  53. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  54. $artifact = new Artifact($r["id"]);
  55. if (strlen($artifact->assigned_to) > 0){
  56. $artifact->assigned_to = new Unit($artifact->assigned_to, false);
  57. }
  58. array_push($this->artifacts, $artifact);
  59. }
  60. $s = "
  61. SELECT id
  62. FROM filter
  63. WHERE
  64. uid = '$UID' AND
  65. page = 'ARTIFACTS';
  66. ";
  67. $q = $db->query($s);
  68. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  69. array_push($this->custom_filters, new Custom_Filter($r["id"]));
  70. }
  71. $this->title = "Artifacts - SWDB";
  72. $this->description = "Artifact inventory";
  73. $this->canonical = URL::BASE . "artifacts/";
  74. }
  75. /**
  76. * Parses the request looking for the selected filters, validates them
  77. * and adds them to the $filters array.
  78. */
  79. private function parse_filters(){
  80. $this->filters = FILTER::ARTIFACT;
  81. if (isset($_GET["element"])){
  82. $elements = $_GET["element"];
  83. foreach ($elements as $element){
  84. array_push($this->filters["ELEMENT"], intval($element));
  85. }
  86. }
  87. if (isset($_GET["archetype"])){
  88. $archetypes = $_GET["archetype"];
  89. foreach ($archetypes as $archetype){
  90. array_push($this->filters["ARCHETYPE"], intval($archetype));
  91. }
  92. }
  93. if (isset($_GET["main_stat"])){
  94. $mains = $_GET["main_stat"];
  95. foreach ($mains as $main){
  96. array_push($this->filters["MAIN"], intval($main));
  97. }
  98. }
  99. if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
  100. $this->filters["MIN_LEVEL"] = $_GET["min_level"];
  101. }
  102. if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
  103. $this->filters["MAX_LEVEL"] = $_GET["max_level"];
  104. }
  105. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
  106. $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
  107. }
  108. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
  109. $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
  110. }
  111. if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_original_quality"]) <= QUALITY_ID::LEGEND){
  112. $this->filters["MIN_ORIGINAL_QUALITY"] = $_GET["min_original_quality"];
  113. }
  114. if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_original_quality"]) <= QUALITY_ID::LEGEND){
  115. $this->filters["MAX_ORIGINAL_QUALITY"] = $_GET["max_original_quality"];
  116. }
  117. if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
  118. $this->filters["ASSIGNED"] = $_GET["assigned"];
  119. }
  120. }
  121. /**
  122. * Builds the query to the artifact table using the a custom filter.
  123. *
  124. * @param int $filter Custom filter ID.
  125. * @return string The query to be executed.
  126. * @global int Player ID.
  127. * @global resource Database connection.
  128. */
  129. private function build_custom_query($filter){
  130. global $UID;
  131. global $db;
  132. $s = "
  133. SELECT condition
  134. FROM filter
  135. WHERE
  136. uid = '$UID' AND
  137. id = $filter;
  138. ";
  139. $q = $db->query($s);
  140. $r = $q->fetchArray(SQLITE3_ASSOC);
  141. $cond = $r["condition"];
  142. $s = "
  143. SELECT
  144. id,
  145. assigned_to
  146. FROM artifact
  147. WHERE uid = '$UID' AND (
  148. $cond
  149. )
  150. ORDER BY slot, type, stars DESC, original_quality DESC, quality DESC, level;
  151. ";
  152. return $s;
  153. }
  154. /**
  155. * Builds the query to the artifact table using the selected or default
  156. * filters.
  157. *
  158. * @return string The query to be executed.
  159. * @global int Player ID.
  160. */
  161. private function build_query(){
  162. global $UID;
  163. $s =
  164. "SELECT " .
  165. " id, " .
  166. " assigned_to " .
  167. "FROM artifact " .
  168. "WHERE " .
  169. " uid = '$UID' AND " .
  170. " level >= " . $this->filters["MIN_LEVEL"] . " AND " .
  171. " level <= " . $this->filters["MAX_LEVEL"] . " AND " .
  172. " quality >= " . $this->filters["MIN_QUALITY"] . " AND " .
  173. " quality <= " . $this->filters["MAX_QUALITY"] . " AND " .
  174. " original_quality >= " . $this->filters["MIN_ORIGINAL_QUALITY"] . " AND " .
  175. " original_quality <= " . $this->filters["MAX_ORIGINAL_QUALITY"] . " AND " .
  176. " efficiency >= " . $this->filters["MIN_EFFICIENCY"] . " AND " .
  177. " efficiency <= " . $this->filters["MAX_EFFICIENCY"] . " AND " .
  178. " max_efficiency >= " . $this->filters["MIN_MAX_EFFICIENCY"] . " AND " .
  179. " max_efficiency <= " . $this->filters["MAX_MAX_EFFICIENCY"] . " ";
  180. if (count($this->filters["MAIN"]) > 0){
  181. $s = $s . " AND main_stat IN ( ";
  182. foreach($this->filters["MAIN"] as $t){
  183. $s = $s . "$t, ";
  184. }
  185. $s = $s . "-1) ";
  186. }
  187. if (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) == 0){
  188. // No element/archetype conditions
  189. }
  190. elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) == 0){
  191. // Only selected elements
  192. $s = $s . " AND attribute IN ( ";
  193. foreach($this->filters["ELEMENT"] as $t){
  194. $s = $s . "$t, ";
  195. }
  196. $s = $s . "-1) ";
  197. }
  198. elseif (count($this->filters["ELEMENT"]) == 0 && count($this->filters["ARCHETYPE"]) > 0){
  199. // Only selected types
  200. $s = $s . " AND archetype IN ( ";
  201. foreach($this->filters["ARCHETYPE"] as $t){
  202. $s = $s . "$t, ";
  203. }
  204. $s = $s . "-1) ";
  205. }
  206. elseif (count($this->filters["ELEMENT"]) > 0 && count($this->filters["ARCHETYPE"]) > 0){
  207. // Selected elements and selected types
  208. $s = $s . " AND (archetype IN ( ";
  209. foreach($this->filters["ARCHETYPE"] as $t){
  210. $s = $s . "$t, ";
  211. }
  212. $s = $s . "-1) OR attribute IN ( ";
  213. foreach($this->filters["ELEMENT"] as $t){
  214. $s = $s . "$t, ";
  215. }
  216. $s = $s . "-1)) ";
  217. }
  218. switch ($this->filters["ASSIGNED"]){
  219. case 1:
  220. $s = $s . " AND assigned_to IS NOT NULL ";
  221. break;
  222. case 2:
  223. $s = $s . " AND assigned_to IS NULL ";
  224. break;
  225. case 3:
  226. $s = $s . " AND (assigned_to IS NULL OR assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ")) ";
  227. break;
  228. case 4:
  229. $s = $s . " AND assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ") ";
  230. break;
  231. }
  232. $s = $s . " ORDER BY type, attribute, archetype, level DESC, original_quality DESC;";
  233. return $s;
  234. }
  235. }
  236. ?>