Monsters_Page.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. error_log($s_mon);
  49. $q_mon = $this->db->query($s_mon);
  50. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  51. array_push($this->monsters, new Unit($db, $r_mon["id"]));
  52. }
  53. $this->title = "Monsters - SWDB";
  54. $this->description = "Owned monsters";
  55. $this->canonical = $root . "monsters/";
  56. }
  57. /**
  58. * Parses the request looking for the selected filters, validates them
  59. * and adds them to the $filters array.
  60. */
  61. private function parse_filters(){
  62. global $FILTER_MONSTER;
  63. $this->filters = $FILTER_MONSTER;
  64. if (isset($_GET["element"]) && in_array($_GET["element"], $ELEMENT)){
  65. $this->filters["element"] = $_GET["element"];
  66. }
  67. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  68. $this->filters["name"] = SQLite3::escapeString($_GET["name"]);
  69. }
  70. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 6){
  71. $this->filters["min_stars"] = $_GET["min_stars"];
  72. }
  73. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 6){
  74. $this->filters["max_stars"] = $_GET["max_stars"];
  75. }
  76. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  77. $this->filters["in_storage"] = true;
  78. }
  79. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  80. $this->filters["without_runes"] = true;
  81. }
  82. return;
  83. }
  84. /**
  85. * Builds the query to the rune table using the selected or default
  86. * filters.
  87. *
  88. * @return The query to be executed.
  89. */
  90. private function build_query(){
  91. global $ELEMENT;
  92. $s = "
  93. SELECT unit.id AS id
  94. FROM
  95. unit,
  96. k_unit
  97. WHERE
  98. unit.unit = k_unit.id AND
  99. stars >= " . $this->filters["min_stars"] . " AND
  100. stars <= " . $this->filters["max_stars"];
  101. if (in_array($this->filters["element"], $ELEMENT)){
  102. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(element) = '" . strtoupper($this->filters["element"]) . "') ";
  103. }
  104. if ($this->filters["name"] != ""){
  105. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%') ";
  106. }
  107. if ($this->filters["in_storage"]){
  108. // TODO $s = $s . " AND in_storage = 0 ";
  109. }
  110. if (!$this->filters["without_runes"]){
  111. $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
  112. }
  113. $s = $s . "
  114. ORDER BY
  115. stars DESC,
  116. level DESC,
  117. element = " . $ELEMENT["FIRE"] ." DESC,
  118. element = " . $ELEMENT["WATER"] ." DESC,
  119. element = " . $ELEMENT["WIND"] ." DESC,
  120. element = " . $ELEMENT["LIGHT"] ." DESC,
  121. element = " . $ELEMENT["DARK"] ." DESC
  122. ";
  123. return $s;
  124. }
  125. /**
  126. * Calculates the stats gained from bulding upgrades and populates the
  127. * $building_stats array.
  128. */
  129. private function calculate_buildings(){
  130. global $STAT;
  131. global $EFFECT_AREA;
  132. $s = "
  133. SELECT
  134. affected_stat,
  135. bonus,
  136. element
  137. FROM
  138. k_decoration,
  139. k_decoration_level,
  140. decoration
  141. WHERE
  142. k_decoration.id = decoration.decoration AND
  143. decoration.decoration = k_decoration_level.decoration AND
  144. decoration.level = k_decoration_level.level AND
  145. area = " . $EFFECT_AREA["GENERAL"] . " AND
  146. affected_stat IN (" . $STAT["ATK"] . ", " . $STAT["DEF"] . ", " . $STAT["HP"] . ", " . $STAT["SPD"] . ", " . $STAT["CRIT_DMG"] . ");
  147. ";
  148. $q = $this->db->query($s);
  149. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  150. switch($r["affected_stat"]){
  151. case $STAT["DEF"]:
  152. $this->building_stats["def"] += $r["bonus"];
  153. break;
  154. case $STAT["HP"]:
  155. $this->building_stats["hp"] += $r["bonus"];
  156. break;
  157. case $STAT["SPD"]:
  158. $this->building_stats["spd"] += $r["bonus"];
  159. break;
  160. case $STAT["CRIT_DMG"]:
  161. $this->building_stats["crd"] += $r["bonus"];
  162. break;
  163. case $STAT["ATK"]:
  164. if(strlen($r["element"]) > 0){
  165. $this->building_stats["elm"][$r["element"]] += $r["bonus"];
  166. }
  167. else{
  168. $this->building_stats["atk"] += $r["bonus"];
  169. }
  170. break;
  171. }
  172. }
  173. }
  174. }
  175. ?>