Runes_Page.php 11 KB

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