Runes_Page.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. require_once($path["page"] . "Page.php");
  3. require_once($path["entity"] . "Rune.php");
  4. require_once($path["entity"] . "Monster.php");
  5. /**
  6. * Rune list page.
  7. */
  8. class Runes_Page extends Page{
  9. /**
  10. * Array of @{see Rune}s.
  11. * NOTE: In this case, the monster member is not the id, but a monster
  12. * instance.
  13. */
  14. public $runes = [];
  15. /**
  16. * The filters the page can handle.
  17. */
  18. public $filters = [
  19. "type" => [],
  20. "main" => [],
  21. "sub" => [],
  22. "slot" => [],
  23. "min_quality" => 0,
  24. "max_quality" => 4,
  25. "min_original_quality" => 0,
  26. "max_original_quality" => 4,
  27. "min_stars" => 1,
  28. "max_stars" => 6,
  29. "min_level" => 0,
  30. "max_level" => 15,
  31. "min_efficiency" => 0,
  32. "max_efficiency" => 100,
  33. "min_max_efficiency" => 0,
  34. "max_max_efficiency" => 100,
  35. "assigned" => 0,
  36. "group" => "SLOT"
  37. ];
  38. /**
  39. * Constructor.
  40. *
  41. * Retrieves the data and initializes the variables.
  42. *
  43. * @param SQLite3 $db Connection to the database.
  44. */
  45. public function __construct($db){
  46. global $path;
  47. global $root;
  48. parent::__construct($db);
  49. $this->view = $path["view"] . "runes.php";
  50. $this->parse_filters();
  51. $s = $this->build_query();
  52. $q = $this->db->query($s);
  53. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  54. $rune = new Rune($db, $r["id"]);
  55. if (strlen($rune->assigned_to) > 0){
  56. $rune->assigned_to = new Monster($this->db, $rune->assigned_to);
  57. }
  58. array_push($this->runes, $rune);
  59. }
  60. $this->title = "Runes - SWDB";
  61. $this->description = "Rune inventory";
  62. $this->canonical = $root . "runes/";
  63. }
  64. /**
  65. * Parses the request looking for the selected filters, validates them
  66. * and adds them to the $filters array.
  67. */
  68. private function parse_filters(){
  69. if (isset($_GET["type"])){
  70. $types = $_GET["type"];
  71. foreach ($types as $type){
  72. array_push($this->filters["type"], strtoupper(SQLite3::escapeString($type)));
  73. }
  74. }
  75. if (isset($_GET["main"])){
  76. $mains = $_GET["main"];
  77. foreach ($mains as $main){
  78. array_push($this->filters["main"], strtoupper(SQLite3::escapeString($main)));
  79. }
  80. }
  81. if (isset($_GET["sub"])){
  82. $subs = $_GET["sub"];
  83. foreach ($subs as $sub){
  84. array_push($this->filters["sub"], strtoupper(SQLite3::escapeString($sub)));
  85. }
  86. }
  87. if (isset($_GET["slot"])){
  88. $slots = $_GET["slot"];
  89. foreach ($slots as $slot){
  90. if (intval($slot) >= 1 && intval($slot) < 7){
  91. array_push($this->filters["slot"], intval($slot));
  92. }
  93. }
  94. }
  95. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  96. $this->filters["min_stars"] = $_GET["min_stars"];
  97. }
  98. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  99. $this->filters["max_stars"] = $_GET["max_stars"];
  100. }
  101. if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
  102. $this->filters["min_level"] = $_GET["min_level"];
  103. }
  104. if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
  105. $this->filters["max_level"] = $_GET["max_level"];
  106. }
  107. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= 0 && intval($_GET["min_quality"]) < 5){
  108. $this->filters["min_quality"] = $_GET["min_quality"];
  109. }
  110. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= 0 && intval($_GET["max_quality"]) < 5){
  111. $this->filters["max_quality"] = $_GET["max_quality"];
  112. }
  113. if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= 0 && intval($_GET["min_original_quality"]) < 5){
  114. $this->filters["min_original_quality"] = $_GET["min_original_quality"];
  115. }
  116. if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= 0 && intval($_GET["max_original_quality"]) < 5){
  117. $this->filters["max_original_quality"] = $_GET["max_original_quality"];
  118. }
  119. if (isset($_GET["min_efficiency"]) && intval($_GET["min_efficiency"]) >= 0 && intval($_GET["min_efficiency"]) <= 100){
  120. $this->filters["min_efficiency"] = $_GET["min_efficiency"];
  121. }
  122. if (isset($_GET["max_efficiency"]) && intval($_GET["max_efficiency"]) >= 0 && intval($_GET["max_efficiency"]) <= 100){
  123. $this->filters["max_efficiency"] = $_GET["max_efficiency"];
  124. }
  125. if (isset($_GET["min_max_efficiency"]) && intval($_GET["min_max_efficiency"]) >= 0 && intval($_GET["min_max_efficiency"]) <= 100){
  126. $this->filters["min_max_efficiency"] = $_GET["min_max_efficiency"];
  127. }
  128. if (isset($_GET["max_max_efficiency"]) && intval($_GET["max_max_efficiency"]) >= 0 && intval($_GET["max_max_efficiency"]) <= 100){
  129. $this->filters["max_max_efficiency"] = $_GET["max_max_efficiency"];
  130. }
  131. if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
  132. $this->filters["assigned"] = $_GET["assigned"];
  133. }
  134. if (isset($_GET["group"]) && ($_GET["group"] == "SLOT" || $_GET["group"] == "TYPE")){
  135. $this->filters["group"] = $_GET["group"];
  136. }
  137. return;
  138. }
  139. /**
  140. * Builds the query to the rune table using the selected or default
  141. * filters.
  142. *
  143. * @return The query to be executed.
  144. */
  145. private function build_query(){
  146. $s =
  147. "SELECT " .
  148. " id, " .
  149. " assigned_to " .
  150. "FROM rune " .
  151. "WHERE " .
  152. " stars >= " . $this->filters["min_stars"] . " AND " .
  153. " stars <= " . $this->filters["max_stars"] . " AND " .
  154. " level >= " . $this->filters["min_level"] . " AND " .
  155. " level <= " . $this->filters["max_level"] . " AND " .
  156. " quality >= " . $this->filters["min_quality"] . " AND " .
  157. " quality <= " . $this->filters["max_quality"] . " AND " .
  158. " original_quality >= " . $this->filters["min_original_quality"] . " AND " .
  159. " original_quality <= " . $this->filters["max_original_quality"] . " AND " .
  160. " efficiency >= " . $this->filters["min_efficiency"] . " AND " .
  161. " efficiency <= " . $this->filters["max_efficiency"] . " AND " .
  162. " max_efficiency >= " . $this->filters["min_max_efficiency"] . " AND " .
  163. " max_efficiency <= " . $this->filters["max_max_efficiency"] . " ";
  164. if (count($this->filters["type"]) > 0){
  165. $s = $s . " AND upper(type) IN ( ";
  166. foreach($this->filters["type"] as $t){
  167. $s = $s . "'$t', ";
  168. }
  169. $s = $s . "'DUMMY0') ";
  170. }
  171. if (count($this->filters["slot"]) > 0){
  172. $s = $s . " AND slot IN ( ";
  173. foreach($this->filters["slot"] as $t){
  174. $s = $s . "$t, ";
  175. }
  176. $s = $s . "-1) ";
  177. }
  178. switch ($this->filters["assigned"]){
  179. case 1:
  180. $s = $s . " AND assigned_to IS NOT NULL ";
  181. break;
  182. case 2:
  183. $s = $s . " AND assigned_to IS NULL ";
  184. break;
  185. case 3:
  186. $s = $s . " AND (assigned_to IS NULL OR assigned_to IN (SELECT DISTINCT id FROM monster WHERE in_storage = 0)) ";
  187. break;
  188. case 4:
  189. $s = $s . " AND assigned_to IN (SELECT DISTINCT id FROM monster WHERE in_storage = 0) ";
  190. break;
  191. }
  192. $s = $s . " ORDER BY ";
  193. if ($this->filters["group"] == "TYPE"){
  194. $s = $s . " type, slot, stars DESC, original_quality DESC, quality DESC, level;";
  195. }
  196. else{
  197. $s = $s . " slot, type, stars DESC, original_quality DESC, quality DESC, level, main_stat;";
  198. }
  199. return $s;
  200. }
  201. }
  202. ?>