Report_Skillup_Page.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. require_once($path["page"] . "Page.php");
  3. require_once($path["entity"] . "Monster.php");
  4. /**
  5. * Urgent Skillup report page.
  6. */
  7. class Report_Skillup_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. "min_stars" => 5,
  17. "max_stars" => 6,
  18. "min_natural_stars" => 5,
  19. "max_natural_stars" => 5,
  20. "in_storage" => true,
  21. "without_runes" => true,
  22. "family" => false,
  23. "maxed" => false,
  24. "2a" => false
  25. ];
  26. /**
  27. * Constructor.
  28. *
  29. * Retrieves the data and initializes the variables.
  30. *
  31. * @param SQLite3 $db Connection to the database.
  32. */
  33. public function __construct($db){
  34. global $path;
  35. global $root;
  36. parent::__construct($db);
  37. $this->view = $path["view"] . "report_skillup.php";
  38. $this->parse_filters();
  39. $s_mon = $this->build_query();
  40. error_log($s_mon);
  41. $q_mon = $this->db->query($s_mon);
  42. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  43. array_push($this->monsters, new Monster($db, $r_mon["id"]));
  44. }
  45. $this->title = "Urgent skill-ups - SWDB";
  46. $this->description = "Monster in need os skilling up";
  47. $this->canonical = $root . "report/skillups";
  48. }
  49. /**
  50. * Parses the request looking for the selected filters, validates them
  51. * and adds them to the $filters array.
  52. */
  53. private function parse_filters(){
  54. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  55. $this->filters["min_stars"] = $_GET["min_stars"];
  56. }
  57. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  58. $this->filters["max_stars"] = $_GET["max_stars"];
  59. }
  60. if (isset($_GET["min_natural_stars"]) && intval($_GET["min_natural_stars"]) > 0 && intval($_GET["min_natural_stars"]) < 6){
  61. $this->filters["min_natural_stars"] = $_GET["min_natural_stars"];
  62. }
  63. if (isset($_GET["max_natural_stars"]) && intval($_GET["max_natural_stars"]) > 0 && intval($_GET["max_natural_stars"]) < 6){
  64. $this->filters["max_natural_stars"] = $_GET["max_natural_stars"];
  65. }
  66. if (isset($_GET["in_storage"]) && $_GET["in_storage"] != "on"){
  67. $this->filters["in_storage"] = false;
  68. }
  69. if (isset($_GET["without_runes"]) && $_GET["without_runes"] != "on"){
  70. $this->filters["without_runes"] = false;
  71. }
  72. if (isset($_GET["family"]) && $_GET["family"] == "on"){
  73. $this->filters["family"] = true;
  74. }
  75. if (isset($_GET["maxed"]) && $_GET["maxed"] == "on"){
  76. $this->filters["maxed"] = true;
  77. }
  78. if (isset($_GET["2a"]) && $_GET["2a"] == "on"){
  79. $this->filters["2a"] = 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 DISTINCT " .
  92. " monster.id AS id, " .
  93. " sum( " .
  94. " CASE k_skill.slot " .
  95. " WHEN 1 THEN (monster.skill_1_level - 1) * 1 " .
  96. " WHEN 2 THEN (monster.skill_2_level - 1) * 1.5 " .
  97. " WHEN 3 THEN (monster.skill_3_level - 1) * 2 " .
  98. " WHEN 4 THEN (monster.skill_4_level - 1) * 2.5 " .
  99. " END " .
  100. " ) AS ponderated_level, " .
  101. " sum( " .
  102. " CASE k_skill.slot " .
  103. " WHEN 1 THEN (monster.skill_1_level - 1) " .
  104. " WHEN 2 THEN (monster.skill_2_level - 1) " .
  105. " WHEN 3 THEN (monster.skill_3_level - 1) " .
  106. " WHEN 4 THEN (monster.skill_4_level - 1) " .
  107. " END " .
  108. " ) AS level, " .
  109. " sum( " .
  110. " CASE k_skill.slot " .
  111. " WHEN 1 THEN (k_skill.max_level - 1) * 1 " .
  112. " WHEN 2 THEN (k_skill.max_level - 1) * 1.5 " .
  113. " WHEN 3 THEN (k_skill.max_level - 1) * 2 " .
  114. " WHEN 4 THEN (k_skill.max_level - 1) * 2.5 " .
  115. " END " .
  116. " ) AS ponderated_max_level, " .
  117. " sum( " .
  118. " CASE k_skill.slot " .
  119. " WHEN 1 THEN (k_skill.max_level - 1) " .
  120. " WHEN 2 THEN (k_skill.max_level - 1) " .
  121. " WHEN 3 THEN (k_skill.max_level - 1) " .
  122. " WHEN 4 THEN (k_skill.max_level - 1) " .
  123. " END " .
  124. " ) AS max_level " .
  125. "FROM " .
  126. " monster, " .
  127. " k_monster_skill, " .
  128. " k_skill, " .
  129. " k_monster " .
  130. "WHERE " .
  131. " monster.monster = k_monster_skill.monster AND " .
  132. " k_skill.id = k_monster_skill.skill AND " .
  133. " k_monster.id = monster.monster AND " .
  134. " k_skill.max_level > 1 AND " .
  135. " monster.stars >= " . $this->filters["min_stars"] . " AND " .
  136. " monster.stars <= " . $this->filters["max_stars"] . " AND " .
  137. " k_monster.natural_stars >= " . $this->filters["min_natural_stars"] . " AND " .
  138. " k_monster.natural_stars <= " . $this->filters["max_natural_stars"] . " ";
  139. if (!$this->filters["in_storage"]){
  140. $s = $s . " AND monster.in_storage = 0 ";
  141. }
  142. if (!$this->filters["without_runes"]){
  143. $s = $s . " AND monster.id IN (SELECT DISTINCT monster FROM monster_rune) ";
  144. }
  145. if ($this->filters["family"]){
  146. $s = $s . " AND monster.monster NOT IN (SELECT id FROM k_monster m WHERE family_id IN (SELECT family_id FROM k_monster k WHERE k.family_id = m.family_id AND k.natural_stars < m.natural_stars AND k.natural_stars < " . $this->filters["min_natural_stars"] . ")) ";
  147. // Special case: Eirgar (Dark Vampire lord). Nat5 can be powered with 4 star Vampires.
  148. if ($this->filters["min_natural_stars"] >= 5){
  149. $s = $s . " AND k_monster.id <> 1279 AND k_monster.id <> 1280 ";
  150. }
  151. // Special case: Fran (Light Fairy Queen). Nat3 can be powered with 2 star Fairies.
  152. if ($this->filters["min_natural_stars"] >= 3){
  153. $s = $s . " AND k_monster.id <> 417 AND k_monster.id <> 418 ";
  154. }
  155. }
  156. if (!$this->filters["2a"]){
  157. $s = $s . " AND (k_monster.awakens_from IS NULL OR k_monster.awakens_to IS NOT NULL OR k_monster.base_stars - k_monster.natural_stars = 1) ";
  158. }
  159. $s = $s .
  160. "GROUP BY monster.id ";
  161. if (!$this->filters["maxed"]){
  162. $s = $s . " HAVING ponderated_level < ponderated_max_level ";
  163. }
  164. $s = $s .
  165. "ORDER BY " .
  166. " ponderated_level * 100 / ponderated_max_level, " .
  167. " monster.stars DESC, " .
  168. " monster.level DESC, " .
  169. " level * 100 / max_level, " .
  170. " max_level - level DESC; ";
  171. return $s;
  172. }
  173. }
  174. ?>