Runes_Page.php 10 KB

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