Units_Page.php 8.2 KB

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