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. * NOTE: 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. if (isset($_GET["type"])){
  82. $types = $_GET["type"];
  83. foreach ($types as $type){
  84. array_push($this->filters["type"], intval($type));
  85. }
  86. }
  87. if (isset($_GET["main"])){
  88. $mains = $_GET["main"];
  89. foreach ($mains as $main){
  90. array_push($this->filters["main"], intval($main));
  91. }
  92. }
  93. if (isset($_GET["sub"])){
  94. $subs = $_GET["sub"];
  95. foreach ($subs as $sub){
  96. array_push($this->filters["sub"], intval($sub));
  97. }
  98. }
  99. if (isset($_GET["slot"])){
  100. $slots = $_GET["slot"];
  101. foreach ($slots as $slot){
  102. if (intval($slot) >= 1 && intval($slot) < 7){
  103. array_push($this->filters["slot"], intval($slot));
  104. }
  105. }
  106. }
  107. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  108. $this->filters["min_stars"] = $_GET["min_stars"];
  109. }
  110. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  111. $this->filters["max_stars"] = $_GET["max_stars"];
  112. }
  113. if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
  114. $this->filters["min_level"] = $_GET["min_level"];
  115. }
  116. if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
  117. $this->filters["max_level"] = $_GET["max_level"];
  118. }
  119. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
  120. $this->filters["min_quality"] = $_GET["min_quality"];
  121. }
  122. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
  123. $this->filters["max_quality"] = $_GET["max_quality"];
  124. }
  125. if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_original_quality"]) <= QUALITY_ID::LEGEND){
  126. $this->filters["min_original_quality"] = $_GET["min_original_quality"];
  127. }
  128. if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_original_quality"]) <= QUALITY_ID::LEGEND){
  129. $this->filters["max_original_quality"] = $_GET["max_original_quality"];
  130. }
  131. if (isset($_GET["min_efficiency"]) && intval($_GET["min_efficiency"]) >= 0 && intval($_GET["min_efficiency"]) <= 100){
  132. $this->filters["min_efficiency"] = $_GET["min_efficiency"];
  133. }
  134. if (isset($_GET["max_efficiency"]) && intval($_GET["max_efficiency"]) >= 0 && intval($_GET["max_efficiency"]) <= 100){
  135. $this->filters["max_efficiency"] = $_GET["max_efficiency"];
  136. }
  137. if (isset($_GET["min_max_efficiency"]) && intval($_GET["min_max_efficiency"]) >= 0 && intval($_GET["min_max_efficiency"]) <= 100){
  138. $this->filters["min_max_efficiency"] = $_GET["min_max_efficiency"];
  139. }
  140. if (isset($_GET["max_max_efficiency"]) && intval($_GET["max_max_efficiency"]) >= 0 && intval($_GET["max_max_efficiency"]) <= 100){
  141. $this->filters["max_max_efficiency"] = $_GET["max_max_efficiency"];
  142. }
  143. if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
  144. $this->filters["assigned"] = $_GET["assigned"];
  145. }
  146. if (isset($_GET["group"]) && ($_GET["group"] == "1" || $_GET["group"] == "0")){
  147. $this->filters["group"] = $_GET["group"];
  148. }
  149. }
  150. /**
  151. * Builds the query to the rune table using the a custom filter.
  152. *
  153. * @param int $filter Custom filter ID.
  154. * @return string The query to be executed.
  155. * @global int Player ID.
  156. */
  157. private function build_custom_query($filter){
  158. global $UID;
  159. $s = "
  160. SELECT condition
  161. FROM filter
  162. WHERE
  163. uid = '$UID' AND
  164. id = $filter;
  165. ";
  166. $q = $this->db->query($s);
  167. $r = $q->fetchArray(SQLITE3_ASSOC);
  168. $cond = $r["condition"];
  169. $s = "
  170. SELECT
  171. id,
  172. assigned_to
  173. FROM rune
  174. WHERE uid = '$UID' AND (
  175. $cond
  176. )
  177. ORDER BY slot, type, stars DESC, original_quality DESC, quality DESC, level;
  178. ";
  179. return $s;
  180. }
  181. /**
  182. * Builds the query to the rune table using the selected or default
  183. * filters.
  184. *
  185. * @return string The query to be executed.
  186. * @global int Player ID.
  187. */
  188. private function build_query(){
  189. global $UID;
  190. $s =
  191. "SELECT " .
  192. " id, " .
  193. " assigned_to " .
  194. "FROM rune " .
  195. "WHERE " .
  196. " uid = '$UID' AND " .
  197. " stars >= " . $this->filters["min_stars"] . " AND " .
  198. " stars <= " . $this->filters["max_stars"] . " AND " .
  199. " level >= " . $this->filters["min_level"] . " AND " .
  200. " level <= " . $this->filters["max_level"] . " AND " .
  201. " quality >= " . $this->filters["min_quality"] . " AND " .
  202. " quality <= " . $this->filters["max_quality"] . " AND " .
  203. " original_quality >= " . $this->filters["min_original_quality"] . " AND " .
  204. " original_quality <= " . $this->filters["max_original_quality"] . " AND " .
  205. " efficiency >= " . $this->filters["min_efficiency"] . " AND " .
  206. " efficiency <= " . $this->filters["max_efficiency"] . " AND " .
  207. " max_efficiency >= " . $this->filters["min_max_efficiency"] . " AND " .
  208. " max_efficiency <= " . $this->filters["max_max_efficiency"] . " ";
  209. if (count($this->filters["type"]) > 0){
  210. $s = $s . " AND upper(type) IN ( ";
  211. foreach($this->filters["type"] as $t){
  212. $s = $s . "'$t', ";
  213. }
  214. $s = $s . "'DUMMY0') ";
  215. }
  216. if (count($this->filters["slot"]) > 0){
  217. $s = $s . " AND slot IN ( ";
  218. foreach($this->filters["slot"] as $t){
  219. $s = $s . "$t, ";
  220. }
  221. $s = $s . "-1) ";
  222. }
  223. switch ($this->filters["assigned"]){
  224. case 1:
  225. $s = $s . " AND assigned_to IS NOT NULL ";
  226. break;
  227. case 2:
  228. $s = $s . " AND assigned_to IS NULL ";
  229. break;
  230. case 3:
  231. // TODO
  232. //$s = $s . " AND (assigned_to IS NULL OR assigned_to IN (SELECT DISTINCT id FROM unit WHERE in_storage = 0)) ";
  233. break;
  234. case 4:
  235. //$s = $s . " AND assigned_to IN (SELECT DISTINCT id FROM unit WHERE in_storage = 0) ";
  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. ?>