Monsters_Page.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. require_once($path["page"] . "Page.php");
  3. require_once($path["entity"] . "Unit.php");
  4. /**
  5. * Monster list page.
  6. */
  7. class Monsters_Page extends Page{
  8. /**
  9. * Array of @{see Monsters}s.
  10. */
  11. public $monsters = [];
  12. /**
  13. * The filters the page can handle.
  14. */
  15. public $filters = [
  16. "element" => "",
  17. "name" => "",
  18. "min_stars" => 1,
  19. "max_stars" => 6,
  20. "in_storage" => false,
  21. "without_runes" => false
  22. ];
  23. /**
  24. * Stats gained by building.
  25. */
  26. public $building_stats = [
  27. "atk" => 0,
  28. "def" => 0,
  29. "hp" => 0,
  30. "spd" => 0,
  31. "crd" => 0,
  32. "elm" => [
  33. "Fire" => 0,
  34. "Water" => 0,
  35. "Wind" => 0,
  36. "Light" => 0,
  37. "Dark" => 0
  38. ]
  39. ];
  40. /**
  41. * Constructor.
  42. *
  43. * Retrieves the data and initializes the variables.
  44. *
  45. * @param SQLite3 $db Connection to the database.
  46. */
  47. public function __construct($db){
  48. global $path;
  49. global $root;
  50. parent::__construct($db);
  51. $this->view = $path["view"] . "monsters.php";
  52. $this->parse_filters();
  53. $this->calculate_buildings();
  54. $s_mon = $this->build_query();
  55. $q_mon = $this->db->query($s_mon);
  56. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  57. array_push($this->monsters, new Unit($db, $r_mon["id"]));
  58. }
  59. $this->title = "Monsters - SWDB";
  60. $this->description = "Owned monsters";
  61. $this->canonical = $root . "monsters/";
  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"])){
  69. $f_element = strtoupper($_GET["element"]);
  70. if ($f_element == "FIRE" || $f_element == "WATER" || $f_element == "WIND" || $f_element == "LIGHT" || $f_element == "DARK"){
  71. $this->filters["element"] = $_GET["element"];
  72. }
  73. }
  74. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  75. $this->filters["name"] = SQLite3::escapeString($_GET["name"]);
  76. }
  77. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  78. $this->filters["min_stars"] = $_GET["min_stars"];
  79. }
  80. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  81. $this->filters["max_stars"] = $_GET["max_stars"];
  82. }
  83. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  84. $this->filters["in_storage"] = true;
  85. }
  86. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  87. $this->filters["without_runes"] = true;
  88. }
  89. return;
  90. }
  91. /**
  92. * Builds the query to the rune table using the selected or default
  93. * filters.
  94. *
  95. * @return The query to be executed.
  96. */
  97. private function build_query(){
  98. $s = "SELECT unit.id AS id FROM unit, k_unit WHERE unit.unit = k_unit.id AND stars >= " . $this->filters["min_stars"] . " AND stars <= " . $this->filters["max_stars"];
  99. if (strlen($this->filters["element"]) > 0){
  100. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(element) = '" . strtoupper($this->filters["element"]) . "') ";
  101. }
  102. if ($this->filters["name"] != ""){
  103. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%') ";
  104. }
  105. if ($this->filters["in_storage"]){
  106. $s = $s . " AND in_storage = 0 ";
  107. }
  108. if (!$this->filters["without_runes"]){
  109. $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
  110. }
  111. $s = $s .
  112. " ORDER BY " .
  113. " stars DESC, " .
  114. " level DESC, " .
  115. " element = 'Fire' DESC, " .
  116. " element = 'Water' DESC, " .
  117. " element = 'Wind' DESC, " .
  118. " element = 'Light' DESC, " .
  119. " element = 'Dark' DESC; ";
  120. return $s;
  121. }
  122. /**
  123. * Calculates the stats gained from bulding upgrades and populates the
  124. * $building_stats array.
  125. */
  126. private function calculate_buildings(){
  127. $s =
  128. "SELECT " .
  129. " affected_stat, " .
  130. " bonus, " .
  131. " element " .
  132. "FROM " .
  133. " k_building, " .
  134. " k_building_level, " .
  135. " building " .
  136. "WHERE " .
  137. " k_building.id = building.building AND " .
  138. " building.building = k_building_level.building AND " .
  139. " building.level = k_building_level.level AND " .
  140. " area = 'Everywhere' AND " .
  141. " affected_stat IN ('ATK', 'DEF', 'HP', 'SPD', 'CRI Dmg');";
  142. $q = $this->db->query($s);
  143. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  144. switch($r["affected_stat"]){
  145. case "DEF":
  146. $this->building_stats["def"] += $r["bonus"];
  147. break;
  148. case "HP":
  149. $this->building_stats["hp"] += $r["bonus"];
  150. break;
  151. case "SPD":
  152. $this->building_stats["spd"] += $r["bonus"];
  153. break;
  154. case "CRI Dmg":
  155. $this->building_stats["crd"] += $r["bonus"];
  156. break;
  157. case "ATK":
  158. if(strlen($r["element"]) > 0){
  159. $this->building_stats["elm"][$r["element"]] += $r["bonus"];
  160. }
  161. else{
  162. $this->building_stats["atk"] += $r["bonus"];
  163. }
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. ?>