Monsters_Page.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. * @global resource Database connection.
  47. */
  48. public function __construct(){
  49. global $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 = $db->query($s_mon);
  55. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  56. array_push($this->monsters, new Unit($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. * @global resource Database connection.
  95. */
  96. private function build_query(){
  97. global $UID;
  98. global $db;
  99. $s = "
  100. SELECT unit.id AS id
  101. FROM
  102. unit,
  103. k_unit
  104. WHERE
  105. unit.uid = '$UID' AND
  106. unit.unit = k_unit.id AND
  107. stars >= " . $this->filters["MIN_STARS"] . " AND
  108. stars <= " . $this->filters["MAX_STARS"];
  109. if (APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
  110. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE element = " . $this->filters["ELEMENT"] . ") ";
  111. }
  112. if ($this->filters["NAME"] != ""){
  113. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%') ";
  114. }
  115. if ($this->filters["IN_STORAGE"]){
  116. $s = $s . " AND building = " . BUILDING_ID::MONSTER_STORAGE;
  117. }
  118. if (!$this->filters["WITHOUT_RUNES"]){
  119. $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
  120. }
  121. $s = $s . "
  122. ORDER BY
  123. stars DESC,
  124. level DESC,
  125. element = " . ELEMENT_ID::FIRE ." DESC,
  126. element = " . ELEMENT_ID::WATER ." DESC,
  127. element = " . ELEMENT_ID::WIND ." DESC,
  128. element = " . ELEMENT_ID::LIGHT ." DESC,
  129. element = " . ELEMENT_ID::DARK ." DESC
  130. ";
  131. return $s;
  132. }
  133. /**
  134. * Calculates the stats gained from bulding upgrades and populates the
  135. * $building_stats array.
  136. *
  137. * @global int Player ID.
  138. * @global resource Database connection.
  139. */
  140. private function calculate_buildings(){
  141. global $UID;
  142. global $db;
  143. $s = "
  144. SELECT
  145. affected_stat,
  146. bonus,
  147. element
  148. FROM
  149. k_decoration,
  150. k_decoration_level,
  151. decoration
  152. WHERE
  153. decoration.uid = '$UID' AND
  154. k_decoration.id = decoration.decoration AND
  155. decoration.decoration = k_decoration_level.decoration AND
  156. decoration.level = k_decoration_level.level AND
  157. area = " . EFFECT_AREA_ID::GENERAL . " AND
  158. affected_stat IN (" . STAT_ID::ATK . ", " . STAT_ID::DEF . ", " . STAT_ID::HP . ", " . STAT_ID::SPD . ", " . STAT_ID::CRIT_DMG . ");
  159. ";
  160. $q = $db->query($s);
  161. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  162. switch($r["affected_stat"]){
  163. case STAT_ID::DEF:
  164. $this->building_stats["def"] += $r["bonus"];
  165. break;
  166. case STAT_ID::HP:
  167. $this->building_stats["hp"] += $r["bonus"];
  168. break;
  169. case STAT_ID::SPD:
  170. $this->building_stats["spd"] += $r["bonus"];
  171. break;
  172. case STAT_ID::CRIT_DMG:
  173. $this->building_stats["crd"] += $r["bonus"];
  174. break;
  175. case STAT_ID::ATK:
  176. if(strlen($r["element"]) > 0){
  177. $this->building_stats["elm"][$r["element"]] += $r["bonus"];
  178. }
  179. else{
  180. $this->building_stats["atk"] += $r["bonus"];
  181. }
  182. break;
  183. }
  184. }
  185. }
  186. }
  187. ?>