Runes_Page.php 11 KB

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