Monsters_Page.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /**
  17. * Stats gained by building.
  18. */
  19. public $building_stats = [
  20. "atk" => 0,
  21. "def" => 0,
  22. "hp" => 0,
  23. "spd" => 0,
  24. "crd" => 0,
  25. "elm" => [
  26. 0 => 0,
  27. 1 => 0,
  28. 2 => 0,
  29. 3 => 0,
  30. 4 => 0
  31. ]
  32. ];
  33. /**
  34. * Constructor.
  35. *
  36. * Retrieves the data and initializes the variables.
  37. *
  38. * @param SQLite3 $db Connection to the database.
  39. */
  40. public function __construct($db){
  41. global $path;
  42. global $root;
  43. parent::__construct($db);
  44. $this->view = $path["view"] . "monsters.php";
  45. $this->parse_filters();
  46. $this->calculate_buildings();
  47. $s_mon = $this->build_query();
  48. $q_mon = $this->db->query($s_mon);
  49. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  50. array_push($this->monsters, new Unit($db, $r_mon["id"]));
  51. }
  52. $this->title = "Monsters - SWDB";
  53. $this->description = "Owned monsters";
  54. $this->canonical = $root . "monsters/";
  55. }
  56. /**
  57. * Parses the request looking for the selected filters, validates them
  58. * and adds them to the $filters array.
  59. */
  60. private function parse_filters(){
  61. global $FILTER_CATALOG;
  62. $this->filters = $FILTER_CATALOG;
  63. if (isset($_GET["element"]) && in_array($_GET["element"], $ELEMENT)){
  64. $this->filters["element"] = $_GET["element"];
  65. }
  66. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  67. $this->filters["name"] = SQLite3::escapeString($_GET["name"]);
  68. }
  69. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 6){
  70. $this->filters["min_stars"] = $_GET["min_stars"];
  71. }
  72. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 6){
  73. $this->filters["max_stars"] = $_GET["max_stars"];
  74. }
  75. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  76. $this->filters["in_storage"] = true;
  77. }
  78. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  79. $this->filters["without_runes"] = true;
  80. }
  81. return;
  82. }
  83. /**
  84. * Builds the query to the rune table using the selected or default
  85. * filters.
  86. *
  87. * @return The query to be executed.
  88. */
  89. private function build_query(){
  90. $s = "
  91. SELECT unit.id AS id
  92. FROM
  93. unit,
  94. k_unit
  95. WHERE
  96. unit.unit = k_unit.id AND
  97. stars >= " . $this->filters["min_stars"] . " AND
  98. 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. // TODO $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 = " . $ELEMENT["FIRE"] ." DESC,
  116. element = " . $ELEMENT["WATER"] ." DESC,
  117. element = " . $ELEMENT["WIND"] ." DESC,
  118. element = " . $ELEMENT["LIGHT"] ." DESC,
  119. element = " . $ELEMENT["DARK"] ." DESC
  120. ";
  121. return $s;
  122. }
  123. /**
  124. * Calculates the stats gained from bulding upgrades and populates the
  125. * $building_stats array.
  126. */
  127. private function calculate_buildings(){
  128. global $STAT;
  129. $s = "
  130. SELECT
  131. affected_stat,
  132. bonus,
  133. element
  134. FROM
  135. k_decoration,
  136. k_decoration_level,
  137. decoration
  138. WHERE
  139. k_decoration.id = decoration.decoration AND
  140. decoration.decoration = k_decoration_level.decoration AND
  141. decoration.level = k_decoration_level.level AND
  142. area = 'Everywhere' AND
  143. affected_stat IN ('ATK', 'DEF', 'HP', 'SPD', 'CRI Dmg');
  144. ";
  145. $q = $this->db->query($s);
  146. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  147. switch($r["affected_stat"]){
  148. case $STAT["DEF"]:
  149. $this->building_stats["def"] += $r["bonus"];
  150. break;
  151. case $STAT["HP"]:
  152. $this->building_stats["hp"] += $r["bonus"];
  153. break;
  154. case $STAT["SPD"]:
  155. $this->building_stats["spd"] += $r["bonus"];
  156. break;
  157. case $STAT["CRIT_DMG"]:
  158. $this->building_stats["crd"] += $r["bonus"];
  159. break;
  160. case $STAT["ATK"]:
  161. if(strlen($r["element"]) > 0){
  162. $this->building_stats["elm"][$r["element"]] += $r["bonus"];
  163. }
  164. else{
  165. $this->building_stats["atk"] += $r["bonus"];
  166. }
  167. break;
  168. }
  169. }
  170. }
  171. }
  172. ?>