Runes_Page.php 11 KB

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