Runes_Page.php 11 KB

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