Units_Page.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Unit 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 . "Unit.php");
  13. require_once(PATH::ENTITY . "Effect.php");
  14. /**
  15. * Unit list page model.
  16. *
  17. * @category Page
  18. */
  19. class Units_Page extends Page{
  20. /**
  21. * @var Unit[] Units to show.
  22. */
  23. private $units = [];
  24. /**
  25. * @var mixed[] Default filter values.
  26. */
  27. private $filters = [
  28. "ELEMENT" => -1,
  29. "NAME" => "",
  30. "MIN_STARS" => 1,
  31. "MAX_STARS" => 6,
  32. "IN_STORAGE" => false,
  33. "IN_SHRINE" => false,
  34. "IN_MATERIAL" => false,
  35. "WITHOUT_RUNES" => false,
  36. "EFFECT" => [],
  37. "EFFECT_OPTION" => "any"
  38. ];
  39. /**
  40. * @var Effect[] All availabe skill effects, for the filter selector.
  41. */
  42. private $effects = [];
  43. /**
  44. * Constructor.
  45. *
  46. * Retrieves the data and initializes the variables.
  47. */
  48. public function __construct(){
  49. parent::__construct();
  50. $this->set_public(true);
  51. $this->set_view("units.php");
  52. $this->set_title("Units");
  53. $this->set_description(get_context()->get_player()->get_name() . "'s units");
  54. $this->set_canonical("player/" . get_context()->get_player()->get_id() . "/units/");
  55. $this->add_css("units.css");
  56. $this->parse_filters();
  57. $result_set = $this->prepare_statement()->execute();
  58. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  59. array_push($this->units, new Unit($result["id"]));
  60. }
  61. // Get skill effects
  62. $s_eff = "SELECT id FROM effect ORDER BY is_buff DESC";
  63. $q_eff = get_context()->get_db()->query($s_eff);
  64. while ($r_eff = $q_eff->fetchArray(SQLITE3_ASSOC)){
  65. array_push($this->effects, new Effect($r_eff["id"]));
  66. }
  67. $this->set_code(200);
  68. $this->set_message("OK");
  69. }
  70. /**
  71. * Parses the request looking for the selected filters, validates them
  72. * and adds them to the $filters array.
  73. */
  74. private function parse_filters(){
  75. if (isset($_GET["element"]) && APPLICATION::valid_id("ELEMENT_ID", strtoupper($_GET["element"]))){
  76. $this->filters["ELEMENT"] = $_GET["element"];
  77. }
  78. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  79. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  80. }
  81. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 6){
  82. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  83. }
  84. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 6){
  85. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  86. }
  87. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  88. $this->filters["IN_STORAGE"] = true;
  89. }
  90. else{
  91. $this->filters["IN_STORAGE"] = false;
  92. }
  93. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  94. $this->filters["WITHOUT_RUNES"] = true;
  95. }
  96. if (isset($_GET["effect"])){
  97. $effects = $_GET["effect"];
  98. foreach ($effects as $effect){
  99. if (intval($effect) > 0){
  100. array_push($this->filters["EFFECT"], intval($effect));
  101. }
  102. }
  103. }
  104. if (isset($_GET["effect_option"]) && ($_GET["effect_option"] == "and" || $_GET["effect_option"] == "or")){
  105. $this->filters["EFFECT_OPTION"] = $_GET["effect_option"];
  106. }
  107. return;
  108. }
  109. /**
  110. * Prepares the statement to execute against the database using the filter values.
  111. *
  112. * @return SQLite3Stmt prepared statement.
  113. */
  114. private function prepare_statement(){
  115. // Common items
  116. $query = "
  117. SELECT unit.id AS id
  118. FROM
  119. unit,
  120. monster
  121. WHERE
  122. unit.player = :player AND
  123. unit.monster = monster.id AND
  124. unit.stars BETWEEN :min_stars AND :max_stars
  125. ";
  126. if (APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
  127. $query .= " AND monster.element = :element ";
  128. }
  129. if ($this->filters["NAME"] != ""){
  130. $query .= " AND upper(monster.name) LIKE upper(:name) ";
  131. }
  132. if (! $this->filters["IN_STORAGE"]){
  133. $query .= " AND unit.building <> :storage_building ";
  134. }
  135. if (!$this->filters["WITHOUT_RUNES"]){
  136. $query .= " AND unit.id IN (SELECT DISTINCT unit FROM unit_rune WHERE rta = :rta) ";
  137. }
  138. if (sizeof($this->filters["EFFECT"]) > 0){
  139. if ($this->filters["EFFECT_OPTION"] == "and"){
  140. for ($i = 0; $i < sizeof($this->filters["EFFECT"]); $i ++){
  141. $query .= "
  142. AND monster.id IN (
  143. SELECT DISTINCT monster
  144. FROM
  145. monster_skill,
  146. skill_effect
  147. WHERE
  148. monster_skill.skill = skill_effect.skill AND
  149. skill_effect.effect = :effect_$i
  150. )
  151. ";
  152. }
  153. }
  154. elseif ($this->filters["EFFECT_OPTION"] == "or"){
  155. $query .= " AND ( ";
  156. for ($i = 0; $i < sizeof($this->filters["EFFECT"]); $i ++){
  157. $query .= "
  158. monster.id IN (
  159. SELECT DISTINCT monster
  160. FROM
  161. monster_skill,
  162. skill_effect
  163. WHERE
  164. monster_skill.skill = skill_effect.skill AND
  165. skill_effect.effect = :effect_$i
  166. ) OR ";
  167. }
  168. $query = rtrim($query, " OR ") . ") ";
  169. }
  170. }
  171. $query .= "
  172. ORDER BY
  173. stars DESC,
  174. level DESC,
  175. element = :fire DESC,
  176. element = :water DESC,
  177. element = :wind DESC,
  178. element = :light DESC,
  179. element = :dark DESC
  180. ";
  181. $statement = get_context()->get_db()->prepare($query);
  182. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  183. $statement->bindValue(":min_stars", $this->filters["MIN_STARS"], SQLITE3_INTEGER);
  184. $statement->bindValue(":max_stars", $this->filters["MAX_STARS"], SQLITE3_INTEGER);
  185. $statement->bindValue(":element", $this->filters["ELEMENT"], SQLITE3_INTEGER);
  186. $statement->bindValue(":name", "%" . $this->filters["NAME"] . "%", SQLITE3_TEXT);
  187. $statement->bindValue(":storage_building", BUILDING_ID::MONSTER_STORAGE, SQLITE3_INTEGER);
  188. $statement->bindValue(":rta", get_context()->get_game_mode(), SQLITE3_INTEGER);
  189. for ($i = 0; $i < sizeof($this->filters["EFFECT"]); $i ++){
  190. $statement->bindValue(":effect_$i", $this->filters["EFFECT"][$i], SQLITE3_INTEGER);
  191. }
  192. $statement->bindValue(":fire", ELEMENT_ID::FIRE, SQLITE3_INTEGER);
  193. $statement->bindValue(":water", ELEMENT_ID::WATER, SQLITE3_INTEGER);
  194. $statement->bindValue(":wind", ELEMENT_ID::WIND, SQLITE3_INTEGER);
  195. $statement->bindValue(":light", ELEMENT_ID::LIGHT, SQLITE3_INTEGER);
  196. $statement->bindValue(":dark", ELEMENT_ID::DARK, SQLITE3_INTEGER);
  197. return $statement;
  198. }
  199. /**
  200. * Retrieves the page filters.
  201. *
  202. * @return mixed[] Page filtes
  203. */
  204. public function get_filters(){
  205. return $this->filters;
  206. }
  207. /**
  208. * Retrieves one filter.
  209. *
  210. * @param string $key
  211. * @return mixed Filter value, null if if doesn't exist.
  212. */
  213. public function get_filter($key){
  214. if (array_key_exists($key, $this->filters)){
  215. return $this->filters[$key];
  216. }
  217. else{
  218. return null;
  219. }
  220. }
  221. /**
  222. * Retrieves the selection of units.
  223. *
  224. * @return Unit[] Unit to show on the page.
  225. */
  226. public function get_units(){
  227. return $this->units;
  228. }
  229. /**
  230. * Retrieves all existing skill effects.
  231. *
  232. * @return Effect[] Every skill effects.
  233. */
  234. public function get_effects(){
  235. return $this->effects;
  236. }
  237. }