Monsters_Page.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Monster list page file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @category Page
  8. */
  9. /**
  10. * Require dependent files if not present.
  11. */
  12. require_once(PATH::PAGE . "Page.php");
  13. require_once(PATH::ENTITY . "Unit.php");
  14. /**
  15. * Monster list page model.
  16. *
  17. * @category Page
  18. */
  19. class Monsters_Page extends Page{
  20. /**
  21. * @var \Unit[] Units to show.
  22. */
  23. public $monsters = [];
  24. /**
  25. * @var mixed[] Stats gained by buildings.
  26. */
  27. public $building_stats = [
  28. "atk" => 0,
  29. "def" => 0,
  30. "hp" => 0,
  31. "spd" => 0,
  32. "crd" => 0,
  33. "elm" => [
  34. 1 => 0,
  35. 2 => 0,
  36. 3 => 0,
  37. 4 => 0,
  38. 5 => 0,
  39. ]
  40. ];
  41. /**
  42. * Constructor.
  43. *
  44. * Retrieves the data and initializes the variables.
  45. *
  46. * @param resource $db Connection to the database.
  47. */
  48. public function __construct($db){
  49. parent::__construct($db);
  50. $this->view = PATH::VIEW . "monsters.php";
  51. $this->parse_filters();
  52. $this->calculate_buildings();
  53. $s_mon = $this->build_query();
  54. $q_mon = $this->db->query($s_mon);
  55. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  56. array_push($this->monsters, new Unit($db, $r_mon["id"]));
  57. }
  58. $this->title = "Monsters - SWDB";
  59. $this->description = "Owned monsters";
  60. $this->canonical = URL::BASE . "monsters/";
  61. }
  62. /**
  63. * Parses the request looking for the selected filters, validates them
  64. * and adds them to the $filters array.
  65. */
  66. private function parse_filters(){
  67. $this->filters = FILTER::MONSTER;
  68. if (isset($_GET["element"]) && APPLICATION::valid_id("ELEMENT_ID", strtoupper($_GET["element"]))){
  69. $this->filters["ELEMENT"] = $_GET["element"];
  70. }
  71. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  72. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  73. }
  74. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 6){
  75. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  76. }
  77. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 6){
  78. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  79. }
  80. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  81. $this->filters["IN_STORAGE"] = true;
  82. }
  83. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  84. $this->filters["WITHOUT_RUNES"] = true;
  85. }
  86. return;
  87. }
  88. /**
  89. * Builds the query to the rune table using the selected or default
  90. * filters.
  91. *
  92. * @return string The query to be executed.
  93. * @global int Player ID.
  94. */
  95. private function build_query(){
  96. global $UID;
  97. $s = "
  98. SELECT unit.id AS id
  99. FROM
  100. unit,
  101. k_unit
  102. WHERE
  103. unit.uid = '$UID' AND
  104. unit.unit = k_unit.id AND
  105. stars >= " . $this->filters["MIN_STARS"] . " AND
  106. stars <= " . $this->filters["MAX_STARS"];
  107. if (APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
  108. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE element = " . $this->filters["ELEMENT"] . ") ";
  109. }
  110. if ($this->filters["NAME"] != ""){
  111. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%') ";
  112. }
  113. if ($this->filters["IN_STORAGE"]){
  114. $s = $s . " AND building = " . BUILDING_ID::MONSTER_STORAGE;
  115. }
  116. if (!$this->filters["WITHOUT_RUNES"]){
  117. $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
  118. }
  119. $s = $s . "
  120. ORDER BY
  121. stars DESC,
  122. level DESC,
  123. element = " . ELEMENT_ID::FIRE ." DESC,
  124. element = " . ELEMENT_ID::WATER ." DESC,
  125. element = " . ELEMENT_ID::WIND ." DESC,
  126. element = " . ELEMENT_ID::LIGHT ." DESC,
  127. element = " . ELEMENT_ID::DARK ." DESC
  128. ";
  129. return $s;
  130. }
  131. /**
  132. * Calculates the stats gained from bulding upgrades and populates the
  133. * $building_stats array.
  134. */
  135. private function calculate_buildings(){
  136. global $UID;
  137. $s = "
  138. SELECT
  139. affected_stat,
  140. bonus,
  141. element
  142. FROM
  143. k_decoration,
  144. k_decoration_level,
  145. decoration
  146. WHERE
  147. decoration.uid = '$UID' AND
  148. k_decoration.id = decoration.decoration AND
  149. decoration.decoration = k_decoration_level.decoration AND
  150. decoration.level = k_decoration_level.level AND
  151. area = " . EFFECT_AREA_ID::GENERAL . " AND
  152. affected_stat IN (" . STAT_ID::ATK . ", " . STAT_ID::DEF . ", " . STAT_ID::HP . ", " . STAT_ID::SPD . ", " . STAT_ID::CRIT_DMG . ");
  153. ";
  154. $q = $this->db->query($s);
  155. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  156. switch($r["affected_stat"]){
  157. case STAT_ID::DEF:
  158. $this->building_stats["def"] += $r["bonus"];
  159. break;
  160. case STAT_ID::HP:
  161. $this->building_stats["hp"] += $r["bonus"];
  162. break;
  163. case STAT_ID::SPD:
  164. $this->building_stats["spd"] += $r["bonus"];
  165. break;
  166. case STAT_ID::CRIT_DMG:
  167. $this->building_stats["crd"] += $r["bonus"];
  168. break;
  169. case STAT_ID::ATK:
  170. if(strlen($r["element"]) > 0){
  171. $this->building_stats["elm"][$r["element"]] += $r["bonus"];
  172. }
  173. else{
  174. $this->building_stats["atk"] += $r["bonus"];
  175. }
  176. break;
  177. }
  178. }
  179. }
  180. }
  181. ?>