Monsters_Page.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. require_once($path["page"] . "Page.php");
  3. require_once($path["entity"] . "Monster.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" => true,
  21. "without_runes" => true
  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 Monster($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"] = false;
  85. }
  86. if (isset($_GET["without_runes"]) && $_GET["without_runes"] != "on"){
  87. $this->filters["without_runes"] = false;
  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 monster.id AS id FROM monster, k_monster WHERE monster.monster = k_monster.id AND stars >= " . $this->filters["min_stars"] . " AND stars <= " . $this->filters["max_stars"];
  99. if (strlen($this->filters["element"]) > 0){
  100. $s = $s . " AND monster IN (SELECT id FROM k_monster WHERE upper(element) = '" . strtoupper($this->filters["element"]) . "') ";
  101. }
  102. if ($this->filters["name"] != ""){
  103. $s = $s . " AND monster IN (SELECT id FROM k_monster 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 monster.id IN (SELECT DISTINCT monster FROM monster_rune) ";
  110. }
  111. $s = $s . " ORDER BY stars DESC, level DESC, element;";
  112. return $s;
  113. }
  114. /**
  115. * Calculates the stats gained from bulding upgrades and populates the
  116. * $building_stats array.
  117. */
  118. private function calculate_buildings(){
  119. $s =
  120. "SELECT " .
  121. " affected_stat, " .
  122. " bonus, " .
  123. " element " .
  124. "FROM " .
  125. " k_building, " .
  126. " k_building_level, " .
  127. " building " .
  128. "WHERE " .
  129. " k_building.id = building.building AND " .
  130. " building.building = k_building_level.building AND " .
  131. " building.level = k_building_level.level AND " .
  132. " area = 'Everywhere' AND " .
  133. " affected_stat IN ('ATK', 'DEF', 'HP', 'SPD', 'CRI Dmg');";
  134. $q = $this->db->query($s);
  135. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  136. switch($r["affected_stat"]){
  137. case "DEF":
  138. $this->building_stats["def"] += $r["bonus"];
  139. break;
  140. case "HP":
  141. $this->building_stats["hp"] += $r["bonus"];
  142. break;
  143. case "SPD":
  144. $this->building_stats["spd"] += $r["bonus"];
  145. break;
  146. case "CRI Dmg":
  147. $this->building_stats["crd"] += $r["bonus"];
  148. break;
  149. case "ATK":
  150. if(strlen($r["element"]) > 0){
  151. $this->building_stats["elm"][$r["element"]] += $r["bonus"];
  152. }
  153. else{
  154. $this->building_stats["atk"] += $r["bonus"];
  155. }
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. ?>