Runes_Page.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. require_once($path["page"] . "Page.php");
  3. require_once($path["entity"] . "Rune.php");
  4. require_once($path["entity"] . "Unit.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" => 1,
  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. error_log($s);
  53. $q = $this->db->query($s);
  54. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  55. $rune = new Rune($db, $r["id"]);
  56. if (strlen($rune->assigned_to) > 0){
  57. $rune->assigned_to = new Unit($this->db, $rune->assigned_to, false);
  58. //$rune->assigned_to = null;
  59. }
  60. array_push($this->runes, $rune);
  61. }
  62. $this->title = "Runes - SWDB";
  63. $this->description = "Rune inventory";
  64. $this->canonical = $root . "runes/";
  65. }
  66. /**
  67. * Parses the request looking for the selected filters, validates them
  68. * and adds them to the $filters array.
  69. */
  70. private function parse_filters(){
  71. if (isset($_GET["type"])){
  72. $types = $_GET["type"];
  73. foreach ($types as $type){
  74. array_push($this->filters["type"], strtoupper(SQLite3::escapeString($type)));
  75. }
  76. }
  77. if (isset($_GET["main"])){
  78. $mains = $_GET["main"];
  79. foreach ($mains as $main){
  80. array_push($this->filters["main"], strtoupper(SQLite3::escapeString($main)));
  81. }
  82. }
  83. if (isset($_GET["sub"])){
  84. $subs = $_GET["sub"];
  85. foreach ($subs as $sub){
  86. array_push($this->filters["sub"], strtoupper(SQLite3::escapeString($sub)));
  87. }
  88. }
  89. if (isset($_GET["slot"])){
  90. $slots = $_GET["slot"];
  91. foreach ($slots as $slot){
  92. if (intval($slot) >= 1 && intval($slot) < 7){
  93. array_push($this->filters["slot"], intval($slot));
  94. }
  95. }
  96. }
  97. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  98. $this->filters["min_stars"] = $_GET["min_stars"];
  99. }
  100. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  101. $this->filters["max_stars"] = $_GET["max_stars"];
  102. }
  103. if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
  104. $this->filters["min_level"] = $_GET["min_level"];
  105. }
  106. if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
  107. $this->filters["max_level"] = $_GET["max_level"];
  108. }
  109. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= 0 && intval($_GET["min_quality"]) < 5){
  110. $this->filters["min_quality"] = $_GET["min_quality"];
  111. }
  112. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= 0 && intval($_GET["max_quality"]) < 5){
  113. $this->filters["max_quality"] = $_GET["max_quality"];
  114. }
  115. if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= 0 && intval($_GET["min_original_quality"]) < 5){
  116. $this->filters["min_original_quality"] = $_GET["min_original_quality"];
  117. }
  118. if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= 0 && intval($_GET["max_original_quality"]) < 5){
  119. $this->filters["max_original_quality"] = $_GET["max_original_quality"];
  120. }
  121. if (isset($_GET["min_efficiency"]) && intval($_GET["min_efficiency"]) >= 0 && intval($_GET["min_efficiency"]) <= 100){
  122. $this->filters["min_efficiency"] = $_GET["min_efficiency"];
  123. }
  124. if (isset($_GET["max_efficiency"]) && intval($_GET["max_efficiency"]) >= 0 && intval($_GET["max_efficiency"]) <= 100){
  125. $this->filters["max_efficiency"] = $_GET["max_efficiency"];
  126. }
  127. if (isset($_GET["min_max_efficiency"]) && intval($_GET["min_max_efficiency"]) >= 0 && intval($_GET["min_max_efficiency"]) <= 100){
  128. $this->filters["min_max_efficiency"] = $_GET["min_max_efficiency"];
  129. }
  130. if (isset($_GET["max_max_efficiency"]) && intval($_GET["max_max_efficiency"]) >= 0 && intval($_GET["max_max_efficiency"]) <= 100){
  131. $this->filters["max_max_efficiency"] = $_GET["max_max_efficiency"];
  132. }
  133. if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
  134. $this->filters["assigned"] = $_GET["assigned"];
  135. }
  136. if (isset($_GET["group"]) && ($_GET["group"] == "SLOT" || $_GET["group"] == "TYPE")){
  137. $this->filters["group"] = $_GET["group"];
  138. }
  139. return;
  140. }
  141. /**
  142. * Builds the query to the rune table using the selected or default
  143. * filters.
  144. *
  145. * @return The query to be executed.
  146. */
  147. private function build_query(){
  148. $s =
  149. "SELECT " .
  150. " id, " .
  151. " assigned_to " .
  152. "FROM rune " .
  153. "WHERE " .
  154. " stars >= " . $this->filters["min_stars"] . " AND " .
  155. " stars <= " . $this->filters["max_stars"] . " AND " .
  156. " level >= " . $this->filters["min_level"] . " AND " .
  157. " level <= " . $this->filters["max_level"] . " AND " .
  158. " quality >= " . $this->filters["min_quality"] . " AND " .
  159. " quality <= " . $this->filters["max_quality"] . " AND " .
  160. " original_quality >= " . $this->filters["min_original_quality"] . " AND " .
  161. " original_quality <= " . $this->filters["max_original_quality"] . " AND " .
  162. " efficiency >= " . $this->filters["min_efficiency"] . " AND " .
  163. " efficiency <= " . $this->filters["max_efficiency"] . " AND " .
  164. " max_efficiency >= " . $this->filters["min_max_efficiency"] . " AND " .
  165. " max_efficiency <= " . $this->filters["max_max_efficiency"] . " ";
  166. if (count($this->filters["type"]) > 0){
  167. $s = $s . " AND upper(type) IN ( ";
  168. foreach($this->filters["type"] as $t){
  169. $s = $s . "'$t', ";
  170. }
  171. $s = $s . "'DUMMY0') ";
  172. }
  173. if (count($this->filters["slot"]) > 0){
  174. $s = $s . " AND slot IN ( ";
  175. foreach($this->filters["slot"] as $t){
  176. $s = $s . "$t, ";
  177. }
  178. $s = $s . "-1) ";
  179. }
  180. switch ($this->filters["assigned"]){
  181. case 1:
  182. $s = $s . " AND assigned_to IS NOT NULL ";
  183. break;
  184. case 2:
  185. $s = $s . " AND assigned_to IS NULL ";
  186. break;
  187. case 3:
  188. $s = $s . " AND (assigned_to IS NULL OR assigned_to IN (SELECT DISTINCT id FROM monster WHERE in_storage = 0)) ";
  189. break;
  190. case 4:
  191. $s = $s . " AND assigned_to IN (SELECT DISTINCT id FROM monster WHERE in_storage = 0) ";
  192. break;
  193. }
  194. $s = $s . " ORDER BY ";
  195. if ($this->filters["group"] == "TYPE"){
  196. $s = $s . " type, slot, stars DESC, original_quality DESC, quality DESC, level;";
  197. }
  198. else{
  199. $s = $s . " slot, type, stars DESC, original_quality DESC, quality DESC, level, main_stat;";
  200. }
  201. return $s;
  202. }
  203. }
  204. ?>