Units_Page.php 8.4 KB

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