Runes_Page.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(PATH::PAGE . "Page.php");
  12. require_once(PATH::ENTITY . "Rune.php");
  13. require_once(PATH::ENTITY . "Unit.php");
  14. /**
  15. * Rune list page model.
  16. *
  17. * @category Page
  18. */
  19. class Runes_Page extends Page{
  20. /**
  21. * @var Rune[] Runes to show.
  22. *
  23. * In this case, the monster member is not the id, but a monster
  24. * instance.
  25. */
  26. private $runes = [];
  27. /**
  28. * @var mixed[] Default filter values.
  29. */
  30. private $filters = [
  31. "TYPE" => [],
  32. "MAIN" => [],
  33. "SUB" => [],
  34. "SLOT" => [],
  35. "MIN_QUALITY" => QUALITY_ID::NORMAL,
  36. "MAX_QUALITY" => QUALITY_ID::LEGEND,
  37. "MIN_ORIGINAL_QUALITY" => QUALITY_ID::NORMAL,
  38. "MAX_ORIGINAL_QUALITY" => QUALITY_ID::LEGEND,
  39. "MIN_STARS" => 1,
  40. "MAX_STARS" => 6,
  41. "MIN_LEVEL" => 0,
  42. "MAX_LEVEL" => 15,
  43. "MIN_EFFICIENCY" => 0,
  44. "MAX_EFFICIENCY" => 100,
  45. "MIN_MAX_EFFICIENCY" => 0,
  46. "MAX_MAX_EFFICIENCY" => 100,
  47. "ASSIGNED" => 1,
  48. "GROUP" => "SLOT"
  49. ];
  50. /**
  51. * Constructor.
  52. *
  53. * Retrieves the data and initializes the variables.
  54. */
  55. public function __construct(){
  56. $this->view = PATH::VIEW . "runes.php";
  57. $this->parse_filters();
  58. // TODO: Group hardcoded
  59. $this->filters["GROUP"] = 1;
  60. $result_set = $this->prepare_statement()->execute();
  61. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  62. array_push($this->runes, new Rune($result["id"]));
  63. }
  64. $this->title = "Runes - SWDB";
  65. $this->description = "Rune inventory";
  66. $this->canonical = URL::BASE . "runes/";
  67. }
  68. /**
  69. * Parses the request looking for the selected filters, validates them
  70. * and adds them to the $filters array.
  71. */
  72. private function parse_filters(){
  73. if (isset($_GET["type"])){
  74. $types = $_GET["type"];
  75. foreach ($types as $type){
  76. array_push($this->filters["TYPE"], intval($type));
  77. }
  78. }
  79. if (isset($_GET["main_stat"])){
  80. $mains = $_GET["main_stat"];
  81. foreach ($mains as $main){
  82. array_push($this->filters["MAIN"], intval($main));
  83. }
  84. }
  85. if (isset($_GET["sub_stat"])){
  86. $subs = $_GET["sub_stat"];
  87. foreach ($subs as $sub){
  88. array_push($this->filters["SUB"], intval($sub));
  89. }
  90. }
  91. if (isset($_GET["slot"])){
  92. $slots = $_GET["slot"];
  93. foreach ($slots as $slot){
  94. if (intval($slot) >= 1 && intval($slot) < 7){
  95. array_push($this->filters["SLOT"], intval($slot));
  96. }
  97. }
  98. }
  99. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  100. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  101. }
  102. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  103. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  104. }
  105. if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
  106. $this->filters["MIN_LEVEL"] = $_GET["min_level"];
  107. }
  108. if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
  109. $this->filters["MAX_LEVEL"] = $_GET["max_level"];
  110. }
  111. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
  112. $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
  113. }
  114. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
  115. $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
  116. }
  117. if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_original_quality"]) <= QUALITY_ID::LEGEND){
  118. $this->filters["MIN_ORIGINAL_QUALITY"] = $_GET["min_original_quality"];
  119. }
  120. if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_original_quality"]) <= QUALITY_ID::LEGEND){
  121. $this->filters["MAX_ORIGINAL_QUALITY"] = $_GET["max_original_quality"];
  122. }
  123. if (isset($_GET["min_efficiency"]) && intval($_GET["min_efficiency"]) >= 0 && intval($_GET["min_efficiency"]) <= 100){
  124. $this->filters["MIN_EFFICIENCY"] = $_GET["min_efficiency"];
  125. }
  126. if (isset($_GET["max_efficiency"]) && intval($_GET["max_efficiency"]) >= 0 && intval($_GET["max_efficiency"]) <= 100){
  127. $this->filters["MAX_EFFICIENCY"] = $_GET["max_efficiency"];
  128. }
  129. if (isset($_GET["min_max_efficiency"]) && intval($_GET["min_max_efficiency"]) >= 0 && intval($_GET["min_max_efficiency"]) <= 100){
  130. $this->filters["MIN_MAX_EFFICIENCY"] = $_GET["min_max_efficiency"];
  131. }
  132. if (isset($_GET["max_max_efficiency"]) && intval($_GET["max_max_efficiency"]) >= 0 && intval($_GET["max_max_efficiency"]) <= 100){
  133. $this->filters["MAX_MAX_EFFICIENCY"] = $_GET["max_max_efficiency"];
  134. }
  135. if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
  136. $this->filters["ASSIGNED"] = $_GET["assigned"];
  137. }
  138. if (isset($_GET["group"]) && ($_GET["group"] == "1" || $_GET["group"] == "0")){
  139. $this->filters["GROUP"] = $_GET["group"];
  140. }
  141. }
  142. /**
  143. * Prepares the statement to execute against the database using the filter values.
  144. *
  145. * @return SQLite3Stmt prepared statement.
  146. */
  147. private function prepare_statement(){
  148. // Common items
  149. $query = "
  150. SELECT
  151. id
  152. FROM
  153. rune
  154. WHERE
  155. player = :player AND
  156. stars BETWEEN :min_stars AND :max_stars AND
  157. level BETWEEN :min_level AND :max_level AND
  158. quality BETWEEN :min_quality AND :max_quality AND
  159. original_quality BETWEEN :min_original_quality AND :max_original_quality AND
  160. efficiency BETWEEN :min_efficiency AND :max_efficiency AND
  161. max_efficiency BETWEEN :min_max_efficiency AND :max_max_efficiency
  162. ";
  163. // Some main stat selected.
  164. if (count($this->filters["MAIN"]) > 0){
  165. $query .= " AND id IN (SELECT rune FROM rune_stat WHERE slot = :slot_main AND stat IN (";
  166. for ($i = 0; $i < sizeof($this->filters["MAIN"]); $i ++){
  167. $query .= ":main_$i, ";
  168. }
  169. $query = rtrim($query, ", ") . ")) ";
  170. }
  171. // Some substat selected.
  172. if (count($this->filters["SUB"]) > 0){
  173. $query .= " AND id IN (SELECT rune FROM rune_stat WHERE slot <> :slot_main AND stat IN (";
  174. for ($i = 0; $i < sizeof($this->filters["MAIN"]); $i ++){
  175. $query .= ":sub_$i, ";
  176. }
  177. $query = rtrim($query, ", ") . ")) ";
  178. }
  179. // Some sets selected.
  180. if (count($this->filters["TYPE"]) > 0){
  181. $query .= " AND type IN (";
  182. for ($i = 0; $i < sizeof($this->filters["TYPE"]); $i ++){
  183. $query .= ":type_$i, ";
  184. }
  185. $query = rtrim($query, ", ") . ") ";
  186. }
  187. // Some slots selected.
  188. if (count($this->filters["SLOT"]) > 0){
  189. $query .= " AND slot IN (";
  190. for ($i = 0; $i < sizeof($this->filters["SLOT"]); $i ++){
  191. $query .= ":slot_$i, ";
  192. }
  193. $query = rtrim($query, ", ") . ") ";
  194. }
  195. switch ($this->filters["ASSIGNED"]){
  196. case 0: // All
  197. break;
  198. case 1: // Assigned
  199. $query .= "
  200. AND id IN (
  201. SELECT DISTINCT rune
  202. FROM unit_rune
  203. WHERE rta = :rta) ";
  204. break;
  205. case 2: // Unassigned
  206. $query .= "
  207. AND id NOT IN (
  208. SELECT DISTINCT rune
  209. FROM unit_rune
  210. WHERE rta = :rta) ";
  211. break;
  212. case 3: // Unassigned or assigned to non-storage units
  213. $query .= "
  214. AND (
  215. id NOT IN (
  216. SELECT DISTINCT rune
  217. FROM unit_rune
  218. WHERE rta = :rta
  219. ) OR (
  220. id IN (
  221. SELECT DISTINCT unit_rune.rune
  222. FROM
  223. unit_rune,
  224. unit
  225. WHERE
  226. unit_rune.rta = :rta AND
  227. unit_rune.unit = unit.id AND
  228. unit.building = :storage_building
  229. )
  230. )
  231. )";
  232. break;
  233. case 4: // Assigned to non-storage units
  234. $query .= "
  235. AND (
  236. id IN (
  237. SELECT DISTINCT unit_rune.rune
  238. FROM
  239. unit_rune,
  240. unit
  241. WHERE
  242. unit_rune.rta = :rta AND
  243. unit_rune.unit = unit.id AND
  244. unit.building = :storage_building
  245. )
  246. )";
  247. break;
  248. }
  249. $query .= " ORDER BY ";
  250. if ($this->filters["GROUP"] == 1){ // 1: Type, 0: Slot
  251. $query .= " type, slot, stars DESC, original_quality DESC, quality DESC, level;";
  252. }
  253. else{
  254. $query .= " slot, type, stars DESC, original_quality DESC, quality DESC, level;";
  255. }
  256. $statement = get_context()->get_db()->prepare($query);
  257. $statement->bindValue(":rta", get_context()->get_game_mode(), SQLITE3_INTEGER);
  258. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  259. $statement->bindValue(":min_stars", $this->filters["MIN_STARS"], SQLITE3_INTEGER);
  260. $statement->bindValue(":max_stars", $this->filters["MAX_STARS"], SQLITE3_INTEGER);
  261. $statement->bindValue(":min_level", $this->filters["MIN_LEVEL"], SQLITE3_INTEGER);
  262. $statement->bindValue(":max_level", $this->filters["MAX_LEVEL"], SQLITE3_INTEGER);
  263. $statement->bindValue(":min_quality", $this->filters["MIN_QUALITY"], SQLITE3_INTEGER);
  264. $statement->bindValue(":max_quality", $this->filters["MAX_QUALITY"], SQLITE3_INTEGER);
  265. $statement->bindValue(":min_original_quality", $this->filters["MIN_ORIGINAL_QUALITY"], SQLITE3_INTEGER);
  266. $statement->bindValue(":max_original_quality", $this->filters["MAX_ORIGINAL_QUALITY"], SQLITE3_INTEGER);
  267. $statement->bindValue(":min_efficiency", $this->filters["MIN_EFFICIENCY"], SQLITE3_FLOAT);
  268. $statement->bindValue(":max_efficiency", $this->filters["MAX_EFFICIENCY"], SQLITE3_FLOAT);
  269. $statement->bindValue(":min_max_efficiency", $this->filters["MIN_MAX_EFFICIENCY"], SQLITE3_FLOAT);
  270. $statement->bindValue(":max_max_efficiency", $this->filters["MAX_MAX_EFFICIENCY"], SQLITE3_FLOAT);
  271. $statement->bindValue(":slot_main", RUNE_STAT_SLOT_ID::MAIN, SQLITE3_FLOAT);
  272. for ($i = 0; $i < sizeof($this->filters["MAIN"]); $i ++){
  273. $statement->bindValue(":main_$i", $this->filters["MAIN"][$i], SQLITE3_INTEGER);
  274. }
  275. for ($i = 0; $i < sizeof($this->filters["SUB"]); $i ++){
  276. $statement->bindValue(":sub_$i", $this->filters["SUB"][$i], SQLITE3_INTEGER);
  277. }
  278. for ($i = 0; $i < sizeof($this->filters["TYPE"]); $i ++){
  279. $statement->bindValue(":type_$i", $this->filters["TYPE"][$i], SQLITE3_INTEGER);
  280. }
  281. for ($i = 0; $i < sizeof($this->filters["SLOT"]); $i ++){
  282. $statement->bindValue(":slot_$i", $this->filters["SLOT"][$i], SQLITE3_INTEGER);
  283. }
  284. $statement->bindValue(":storage_building", BUILDING_ID::MONSTER_STORAGE, SQLITE3_INTEGER);
  285. return $statement;
  286. }
  287. /**
  288. * Retrieves the page filters.
  289. *
  290. * @return mixed[] Page filtes
  291. */
  292. public function get_filters(){
  293. return $this->filters;
  294. }
  295. /**
  296. * Retrieves one filter.
  297. *
  298. * @param string $key
  299. * @return mixed Filter value, null if if doesn't exist.
  300. */
  301. public function get_filter($key){
  302. if (array_key_exists($key, $this->filters)){
  303. return $this->filters[$key];
  304. }
  305. else{
  306. return null;
  307. }
  308. }
  309. /**
  310. * Retrieves the selection of runes.
  311. *
  312. * @return Rune[] Runes to show on the page.
  313. */
  314. public function get_runes(){
  315. return $this->runes;
  316. }
  317. }