Monsters_Page.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. global $UID;
  93. $s = "
  94. SELECT unit.id AS id
  95. FROM
  96. unit,
  97. k_unit
  98. WHERE
  99. unit.uid = '$UID' AND
  100. unit.unit = k_unit.id AND
  101. stars >= " . $this->filters["min_stars"] . " AND
  102. stars <= " . $this->filters["max_stars"];
  103. if (in_array($this->filters["element"], $ELEMENT)){
  104. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(element) = '" . strtoupper($this->filters["element"]) . "') ";
  105. }
  106. if ($this->filters["name"] != ""){
  107. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%') ";
  108. }
  109. if ($this->filters["in_storage"]){
  110. // TODO $s = $s . " AND in_storage = 0 ";
  111. }
  112. if (!$this->filters["without_runes"]){
  113. $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
  114. }
  115. $s = $s . "
  116. ORDER BY
  117. stars DESC,
  118. level DESC,
  119. element = " . $ELEMENT["FIRE"] ." DESC,
  120. element = " . $ELEMENT["WATER"] ." DESC,
  121. element = " . $ELEMENT["WIND"] ." DESC,
  122. element = " . $ELEMENT["LIGHT"] ." DESC,
  123. element = " . $ELEMENT["DARK"] ." DESC
  124. ";
  125. return $s;
  126. }
  127. /**
  128. * Calculates the stats gained from bulding upgrades and populates the
  129. * $building_stats array.
  130. */
  131. private function calculate_buildings(){
  132. global $STAT;
  133. global $EFFECT_AREA;
  134. global $UID;
  135. $s = "
  136. SELECT
  137. affected_stat,
  138. bonus,
  139. element
  140. FROM
  141. k_decoration,
  142. k_decoration_level,
  143. decoration
  144. WHERE
  145. decoration.uid = '$UID' AND
  146. k_decoration.id = decoration.decoration AND
  147. decoration.decoration = k_decoration_level.decoration AND
  148. decoration.level = k_decoration_level.level AND
  149. area = " . $EFFECT_AREA["GENERAL"] . " AND
  150. affected_stat IN (" . $STAT["ATK"] . ", " . $STAT["DEF"] . ", " . $STAT["HP"] . ", " . $STAT["SPD"] . ", " . $STAT["CRIT_DMG"] . ");
  151. ";
  152. $q = $this->db->query($s);
  153. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  154. switch($r["affected_stat"]){
  155. case $STAT["DEF"]:
  156. $this->building_stats["def"] += $r["bonus"];
  157. break;
  158. case $STAT["HP"]:
  159. $this->building_stats["hp"] += $r["bonus"];
  160. break;
  161. case $STAT["SPD"]:
  162. $this->building_stats["spd"] += $r["bonus"];
  163. break;
  164. case $STAT["CRIT_DMG"]:
  165. $this->building_stats["crd"] += $r["bonus"];
  166. break;
  167. case $STAT["ATK"]:
  168. if(strlen($r["element"]) > 0){
  169. $this->building_stats["elm"][$r["element"]] += $r["bonus"];
  170. }
  171. else{
  172. $this->building_stats["atk"] += $r["bonus"];
  173. }
  174. break;
  175. }
  176. }
  177. }
  178. }
  179. ?>