Runes_Page.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. parent::__construct();
  57. $this->set_public(true);
  58. $this->set_view("runes.php");
  59. $this->set_title("Runes");
  60. $this->set_description(get_context()->get_player()->get_name() . "'s runes");
  61. $this->set_canonical(get_context()->get_player()->get_id() . "/runes/");
  62. $this->add_css("runes.css");
  63. $this->parse_filters();
  64. // TODO: Group hardcoded
  65. $this->filters["GROUP"] = 1;
  66. $result_set = $this->prepare_statement()->execute();
  67. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  68. array_push($this->runes, new Rune($result["id"]));
  69. }
  70. $this->set_code(200);
  71. $this->set_message("OK");
  72. }
  73. /**
  74. * Parses the request looking for the selected filters, validates them
  75. * and adds them to the $filters array.
  76. */
  77. private function parse_filters(){
  78. if (isset($_GET["type"])){
  79. $types = $_GET["type"];
  80. foreach ($types as $type){
  81. array_push($this->filters["TYPE"], intval($type));
  82. }
  83. }
  84. if (isset($_GET["main_stat"])){
  85. $mains = $_GET["main_stat"];
  86. foreach ($mains as $main){
  87. array_push($this->filters["MAIN"], intval($main));
  88. }
  89. }
  90. if (isset($_GET["sub_stat"])){
  91. $subs = $_GET["sub_stat"];
  92. foreach ($subs as $sub){
  93. array_push($this->filters["SUB"], intval($sub));
  94. }
  95. }
  96. if (isset($_GET["slot"])){
  97. $slots = $_GET["slot"];
  98. foreach ($slots as $slot){
  99. if (intval($slot) >= 1 && intval($slot) < 7){
  100. array_push($this->filters["SLOT"], intval($slot));
  101. }
  102. }
  103. }
  104. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  105. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  106. }
  107. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  108. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  109. }
  110. if (isset($_GET["min_level"]) && intval($_GET["min_level"]) >= 0 && intval($_GET["min_level"]) <= 15){
  111. $this->filters["MIN_LEVEL"] = $_GET["min_level"];
  112. }
  113. if (isset($_GET["max_level"]) && intval($_GET["max_level"]) >= 0 && intval($_GET["max_level"]) <= 15){
  114. $this->filters["MAX_LEVEL"] = $_GET["max_level"];
  115. }
  116. if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
  117. $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
  118. }
  119. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
  120. $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
  121. }
  122. if (isset($_GET["min_original_quality"]) && intval($_GET["min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_original_quality"]) <= QUALITY_ID::LEGEND){
  123. $this->filters["MIN_ORIGINAL_QUALITY"] = $_GET["min_original_quality"];
  124. }
  125. if (isset($_GET["max_original_quality"]) && intval($_GET["max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_original_quality"]) <= QUALITY_ID::LEGEND){
  126. $this->filters["MAX_ORIGINAL_QUALITY"] = $_GET["max_original_quality"];
  127. }
  128. if (isset($_GET["min_efficiency"]) && intval($_GET["min_efficiency"]) >= 0 && intval($_GET["min_efficiency"]) <= 100){
  129. $this->filters["MIN_EFFICIENCY"] = $_GET["min_efficiency"];
  130. }
  131. if (isset($_GET["max_efficiency"]) && intval($_GET["max_efficiency"]) >= 0 && intval($_GET["max_efficiency"]) <= 100){
  132. $this->filters["MAX_EFFICIENCY"] = $_GET["max_efficiency"];
  133. }
  134. if (isset($_GET["min_max_efficiency"]) && intval($_GET["min_max_efficiency"]) >= 0 && intval($_GET["min_max_efficiency"]) <= 100){
  135. $this->filters["MIN_MAX_EFFICIENCY"] = $_GET["min_max_efficiency"];
  136. }
  137. if (isset($_GET["max_max_efficiency"]) && intval($_GET["max_max_efficiency"]) >= 0 && intval($_GET["max_max_efficiency"]) <= 100){
  138. $this->filters["MAX_MAX_EFFICIENCY"] = $_GET["max_max_efficiency"];
  139. }
  140. if (isset($_GET["assigned"]) && intval($_GET["assigned"]) >= 0 && intval($_GET["assigned"]) <= 4){
  141. $this->filters["ASSIGNED"] = $_GET["assigned"];
  142. }
  143. if (isset($_GET["group"]) && ($_GET["group"] == "1" || $_GET["group"] == "0")){
  144. $this->filters["GROUP"] = $_GET["group"];
  145. }
  146. }
  147. /**
  148. * Prepares the statement to execute against the database using the filter values.
  149. *
  150. * @return SQLite3Stmt prepared statement.
  151. */
  152. private function prepare_statement(){
  153. // Common items
  154. $query = "
  155. SELECT
  156. id
  157. FROM
  158. rune
  159. WHERE
  160. player = :player AND
  161. stars BETWEEN :min_stars AND :max_stars AND
  162. level BETWEEN :min_level AND :max_level AND
  163. quality BETWEEN :min_quality AND :max_quality AND
  164. original_quality BETWEEN :min_original_quality AND :max_original_quality AND
  165. efficiency BETWEEN :min_efficiency AND :max_efficiency AND
  166. max_efficiency BETWEEN :min_max_efficiency AND :max_max_efficiency
  167. ";
  168. // Some main stat selected.
  169. if (count($this->filters["MAIN"]) > 0){
  170. $query .= " AND id IN (SELECT rune FROM rune_stat WHERE slot = :slot_main AND stat IN (";
  171. for ($i = 0; $i < sizeof($this->filters["MAIN"]); $i ++){
  172. $query .= ":main_$i, ";
  173. }
  174. $query = rtrim($query, ", ") . ")) ";
  175. }
  176. // Some substat selected.
  177. if (count($this->filters["SUB"]) > 0){
  178. $query .= " AND id IN (SELECT rune FROM rune_stat WHERE slot <> :slot_main AND stat IN (";
  179. for ($i = 0; $i < sizeof($this->filters["MAIN"]); $i ++){
  180. $query .= ":sub_$i, ";
  181. }
  182. $query = rtrim($query, ", ") . ")) ";
  183. }
  184. // Some sets selected.
  185. if (count($this->filters["TYPE"]) > 0){
  186. $query .= " AND type IN (";
  187. for ($i = 0; $i < sizeof($this->filters["TYPE"]); $i ++){
  188. $query .= ":type_$i, ";
  189. }
  190. $query = rtrim($query, ", ") . ") ";
  191. }
  192. // Some slots selected.
  193. if (count($this->filters["SLOT"]) > 0){
  194. $query .= " AND slot IN (";
  195. for ($i = 0; $i < sizeof($this->filters["SLOT"]); $i ++){
  196. $query .= ":slot_$i, ";
  197. }
  198. $query = rtrim($query, ", ") . ") ";
  199. }
  200. switch ($this->filters["ASSIGNED"]){
  201. case 0: // All
  202. break;
  203. case 1: // Assigned
  204. $query .= "
  205. AND id IN (
  206. SELECT DISTINCT rune
  207. FROM unit_rune
  208. WHERE rta = :rta) ";
  209. break;
  210. case 2: // Unassigned
  211. $query .= "
  212. AND id NOT IN (
  213. SELECT DISTINCT rune
  214. FROM unit_rune
  215. WHERE rta = :rta) ";
  216. break;
  217. case 3: // Unassigned or assigned to non-storage units
  218. $query .= "
  219. AND (
  220. id NOT IN (
  221. SELECT DISTINCT rune
  222. FROM unit_rune
  223. WHERE rta = :rta
  224. ) OR (
  225. id IN (
  226. SELECT DISTINCT unit_rune.rune
  227. FROM
  228. unit_rune,
  229. unit
  230. WHERE
  231. unit_rune.rta = :rta AND
  232. unit_rune.unit = unit.id AND
  233. unit.building = :storage_building
  234. )
  235. )
  236. )";
  237. break;
  238. case 4: // Assigned to non-storage units
  239. $query .= "
  240. AND (
  241. id IN (
  242. SELECT DISTINCT unit_rune.rune
  243. FROM
  244. unit_rune,
  245. unit
  246. WHERE
  247. unit_rune.rta = :rta AND
  248. unit_rune.unit = unit.id AND
  249. unit.building = :storage_building
  250. )
  251. )";
  252. break;
  253. }
  254. $query .= " ORDER BY ";
  255. if ($this->filters["GROUP"] == 1){ // 1: Type, 0: Slot
  256. $query .= " type, slot, stars DESC, original_quality DESC, quality DESC, level;";
  257. }
  258. else{
  259. $query .= " slot, type, stars DESC, original_quality DESC, quality DESC, level;";
  260. }
  261. $statement = get_context()->get_db()->prepare($query);
  262. $statement->bindValue(":rta", get_context()->get_game_mode(), SQLITE3_INTEGER);
  263. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  264. $statement->bindValue(":min_stars", $this->filters["MIN_STARS"], SQLITE3_INTEGER);
  265. $statement->bindValue(":max_stars", $this->filters["MAX_STARS"], SQLITE3_INTEGER);
  266. $statement->bindValue(":min_level", $this->filters["MIN_LEVEL"], SQLITE3_INTEGER);
  267. $statement->bindValue(":max_level", $this->filters["MAX_LEVEL"], SQLITE3_INTEGER);
  268. $statement->bindValue(":min_quality", $this->filters["MIN_QUALITY"], SQLITE3_INTEGER);
  269. $statement->bindValue(":max_quality", $this->filters["MAX_QUALITY"], SQLITE3_INTEGER);
  270. $statement->bindValue(":min_original_quality", $this->filters["MIN_ORIGINAL_QUALITY"], SQLITE3_INTEGER);
  271. $statement->bindValue(":max_original_quality", $this->filters["MAX_ORIGINAL_QUALITY"], SQLITE3_INTEGER);
  272. $statement->bindValue(":min_efficiency", $this->filters["MIN_EFFICIENCY"], SQLITE3_FLOAT);
  273. $statement->bindValue(":max_efficiency", $this->filters["MAX_EFFICIENCY"], SQLITE3_FLOAT);
  274. $statement->bindValue(":min_max_efficiency", $this->filters["MIN_MAX_EFFICIENCY"], SQLITE3_FLOAT);
  275. $statement->bindValue(":max_max_efficiency", $this->filters["MAX_MAX_EFFICIENCY"], SQLITE3_FLOAT);
  276. $statement->bindValue(":slot_main", RUNE_STAT_SLOT_ID::MAIN, SQLITE3_FLOAT);
  277. for ($i = 0; $i < sizeof($this->filters["MAIN"]); $i ++){
  278. $statement->bindValue(":main_$i", $this->filters["MAIN"][$i], SQLITE3_INTEGER);
  279. }
  280. for ($i = 0; $i < sizeof($this->filters["SUB"]); $i ++){
  281. $statement->bindValue(":sub_$i", $this->filters["SUB"][$i], SQLITE3_INTEGER);
  282. }
  283. for ($i = 0; $i < sizeof($this->filters["TYPE"]); $i ++){
  284. $statement->bindValue(":type_$i", $this->filters["TYPE"][$i], SQLITE3_INTEGER);
  285. }
  286. for ($i = 0; $i < sizeof($this->filters["SLOT"]); $i ++){
  287. $statement->bindValue(":slot_$i", $this->filters["SLOT"][$i], SQLITE3_INTEGER);
  288. }
  289. $statement->bindValue(":storage_building", BUILDING_ID::MONSTER_STORAGE, SQLITE3_INTEGER);
  290. return $statement;
  291. }
  292. /**
  293. * Retrieves the page filters.
  294. *
  295. * @return mixed[] Page filtes
  296. */
  297. public function get_filters(){
  298. return $this->filters;
  299. }
  300. /**
  301. * Retrieves one filter.
  302. *
  303. * @param string $key
  304. * @return mixed Filter value, null if if doesn't exist.
  305. */
  306. public function get_filter($key){
  307. if (array_key_exists($key, $this->filters)){
  308. return $this->filters[$key];
  309. }
  310. else{
  311. return null;
  312. }
  313. }
  314. /**
  315. * Retrieves the selection of runes.
  316. *
  317. * @return Rune[] Runes to show on the page.
  318. */
  319. public function get_runes(){
  320. return $this->runes;
  321. }
  322. }