Runes_Page.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Rune 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 . "Rune.php");
  15. require_once(PATH::ENTITY . "Unit.php");
  16. /**
  17. * Rune list page model.
  18. *
  19. * @category Page
  20. */
  21. class Runes_Page extends Page{
  22. /**
  23. * @var \Rune[] Runes to show.
  24. *
  25. * In this case, the monster member is not the id, but a monster
  26. * instance.
  27. */
  28. public $runes = [];
  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. * @param resource $db Connection to the database.
  39. * @global int Player ID.
  40. */
  41. public function __construct($db){
  42. global $UID;
  43. parent::__construct($db);
  44. $this->view = PATH::VIEW . "runes.php";
  45. $this->parse_filters();
  46. if (!isset($_GET["custom_filter"])){
  47. $s = $this->build_query();
  48. $this->filters["GROUP"] = 1;
  49. }
  50. else{
  51. $s = $this->build_custom_query($_GET["custom_filter"]);
  52. }
  53. $q = $this->db->query($s);
  54. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  55. $rune = new Rune($db, $r["id"]);
  56. if (strlen($rune->assigned_to) > 0){
  57. $rune->assigned_to = new Unit($this->db, $rune->assigned_to, false);
  58. }
  59. array_push($this->runes, $rune);
  60. }
  61. $s = "
  62. SELECT id
  63. FROM filter
  64. WHERE
  65. uid = '$UID' AND
  66. page = 'RUNES';
  67. ";
  68. $q = $this->db->query($s);
  69. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  70. array_push($this->custom_filters, new Custom_Filter($this->db, $r["id"]));
  71. }
  72. $this->title = "Runes - SWDB";
  73. $this->description = "Rune inventory";
  74. $this->canonical = URL::BASE . "runes/";
  75. }
  76. /**
  77. * Parses the request looking for the selected filters, validates them
  78. * and adds them to the $filters array.
  79. */
  80. private function parse_filters(){
  81. $this->filters = FILTER::RUNE;
  82. if (isset($_GET["type"])){
  83. $types = $_GET["type"];
  84. foreach ($types as $type){
  85. array_push($this->filters["TYPE"], intval($type));
  86. }
  87. }
  88. if (isset($_GET["main"])){
  89. $mains = $_GET["main"];
  90. foreach ($mains as $main){
  91. array_push($this->filters["MAIN"], intval($main));
  92. }
  93. }
  94. if (isset($_GET["sub"])){
  95. $subs = $_GET["sub"];
  96. foreach ($subs as $sub){
  97. array_push($this->filters["SUB"], intval($sub));
  98. }
  99. }
  100. if (isset($_GET["slot"])){
  101. $slots = $_GET["slot"];
  102. foreach ($slots as $slot){
  103. if (intval($slot) >= 1 && intval($slot) < 7){
  104. array_push($this->filters["SLOT"], intval($slot));
  105. }
  106. }
  107. }
  108. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  109. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  110. }
  111. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  112. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  113. }
  114. if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
  115. $this->filters["MIN_LEVEL"] = $_GET["min_level"];
  116. }
  117. if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
  118. $this->filters["MAX_LEVEL"] = $_GET["max_level"];
  119. }
  120. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
  121. $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
  122. }
  123. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
  124. $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
  125. }
  126. if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_original_quality"]) <= QUALITY_ID::LEGEND){
  127. $this->filters["MIN_ORIGINAL_QUALITY"] = $_GET["min_original_quality"];
  128. }
  129. if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_original_quality"]) <= QUALITY_ID::LEGEND){
  130. $this->filters["MAX_ORIGINAL_QUALITY"] = $_GET["max_original_quality"];
  131. }
  132. if (isset($_GET["min_efficiency"]) && intval($_GET["min_efficiency"]) >= 0 && intval($_GET["min_efficiency"]) <= 100){
  133. $this->filters["MIN_EFFICIENCY"] = $_GET["min_efficiency"];
  134. }
  135. if (isset($_GET["max_efficiency"]) && intval($_GET["max_efficiency"]) >= 0 && intval($_GET["max_efficiency"]) <= 100){
  136. $this->filters["MAX_EFFICIENCY"] = $_GET["max_efficiency"];
  137. }
  138. if (isset($_GET["min_max_efficiency"]) && intval($_GET["min_max_efficiency"]) >= 0 && intval($_GET["min_max_efficiency"]) <= 100){
  139. $this->filters["MIN_MAX_EFFICIENCY"] = $_GET["min_max_efficiency"];
  140. }
  141. if (isset($_GET["max_max_efficiency"]) && intval($_GET["max_max_efficiency"]) >= 0 && intval($_GET["max_max_efficiency"]) <= 100){
  142. $this->filters["MAX_MAX_EFFICIENCY"] = $_GET["max_max_efficiency"];
  143. }
  144. if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
  145. $this->filters["ASSIGNED"] = $_GET["assigned"];
  146. }
  147. if (isset($_GET["group"]) && ($_GET["group"] == "1" || $_GET["group"] == "0")){
  148. $this->filters["GROUP"] = $_GET["group"];
  149. }
  150. }
  151. /**
  152. * Builds the query to the rune table using the a custom filter.
  153. *
  154. * @param int $filter Custom filter ID.
  155. * @return string The query to be executed.
  156. * @global int Player ID.
  157. */
  158. private function build_custom_query($filter){
  159. global $UID;
  160. $s = "
  161. SELECT condition
  162. FROM filter
  163. WHERE
  164. uid = '$UID' AND
  165. id = $filter;
  166. ";
  167. $q = $this->db->query($s);
  168. $r = $q->fetchArray(SQLITE3_ASSOC);
  169. $cond = $r["condition"];
  170. $s = "
  171. SELECT
  172. id,
  173. assigned_to
  174. FROM rune
  175. WHERE uid = '$UID' AND (
  176. $cond
  177. )
  178. ORDER BY slot, type, stars DESC, original_quality DESC, quality DESC, level;
  179. ";
  180. return $s;
  181. }
  182. /**
  183. * Builds the query to the rune table using the selected or default
  184. * filters.
  185. *
  186. * @return string The query to be executed.
  187. * @global int Player ID.
  188. */
  189. private function build_query(){
  190. global $UID;
  191. $s =
  192. "SELECT " .
  193. " id, " .
  194. " assigned_to " .
  195. "FROM rune " .
  196. "WHERE " .
  197. " uid = '$UID' AND " .
  198. " stars >= " . $this->filters["MIN_STARS"] . " AND " .
  199. " stars <= " . $this->filters["MAX_STARS"] . " AND " .
  200. " level >= " . $this->filters["MIN_LEVEL"] . " AND " .
  201. " level <= " . $this->filters["MAX_LEVEL"] . " AND " .
  202. " quality >= " . $this->filters["MIN_QUALITY"] . " AND " .
  203. " quality <= " . $this->filters["MAX_QUALITY"] . " AND " .
  204. " original_quality >= " . $this->filters["MIN_ORIGINAL_QUALITY"] . " AND " .
  205. " original_quality <= " . $this->filters["MAX_ORIGINAL_QUALITY"] . " AND " .
  206. " efficiency >= " . $this->filters["MIN_EFFICIENCY"] . " AND " .
  207. " efficiency <= " . $this->filters["MAX_EFFICIENCY"] . " AND " .
  208. " max_efficiency >= " . $this->filters["MIN_MAX_EFFICIENCY"] . " AND " .
  209. " max_efficiency <= " . $this->filters["MAX_MAX_EFFICIENCY"] . " ";
  210. if (count($this->filters["TYPE"]) > 0){
  211. $s = $s . " AND upper(type) IN ( ";
  212. foreach($this->filters["TYPE"] as $t){
  213. $s = $s . "'$t', ";
  214. }
  215. $s = $s . "'DUMMY0') ";
  216. }
  217. if (count($this->filters["SLOT"]) > 0){
  218. $s = $s . " AND slot IN ( ";
  219. foreach($this->filters["SLOT"] as $t){
  220. $s = $s . "$t, ";
  221. }
  222. $s = $s . "-1) ";
  223. }
  224. switch ($this->filters["ASSIGNED"]){
  225. case 1:
  226. $s = $s . " AND assigned_to IS NOT NULL ";
  227. break;
  228. case 2:
  229. $s = $s . " AND assigned_to IS NULL ";
  230. break;
  231. case 3:
  232. $s = $s . " AND (assigned_to IS NULL OR assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ")) ";
  233. break;
  234. case 4:
  235. $s = $s . " AND assigned_to IN (SELECT DISTINCT id FROM unit WHERE building <> " . BUILDING_ID::MONSTER_STORAGE . ") ";
  236. break;
  237. }
  238. $s = $s . " ORDER BY ";
  239. if ($this->filters["GROUP"] == 1){ // 1: Type, 0: Slot
  240. $s = $s . " type, slot, stars DESC, original_quality DESC, quality DESC, level;";
  241. }
  242. else{
  243. $s = $s . " slot, type, stars DESC, original_quality DESC, quality DESC, level, main_stat;";
  244. }
  245. return $s;
  246. }
  247. }
  248. ?>