Runes_Page.php 11 KB

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