Runes_Page.php 10 KB

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