Runes_Page.php 11 KB

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