Report_Skillup_Page.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. require_once($path["page"] . "Page.php");
  3. require_once($path["entity"] . "Unit.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 $units = [];
  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" => false,
  21. "without_runes" => false,
  22. "family" => false,
  23. "maxed" => false,
  24. "2a" => false,
  25. "homunculus" => false
  26. ];
  27. /**
  28. * Constructor.
  29. *
  30. * Retrieves the data and initializes the variables.
  31. *
  32. * @param SQLite3 $db Connection to the database.
  33. */
  34. public function __construct($db){
  35. global $path;
  36. global $root;
  37. parent::__construct($db);
  38. $this->view = $path["view"] . "report_skillup.php";
  39. $this->parse_filters();
  40. $s_mon = $this->build_query();
  41. error_log($s_mon);
  42. $q_mon = $this->db->query($s_mon);
  43. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  44. array_push($this->units, new Unit($db, $r_mon["id"]));
  45. }
  46. $this->title = "Urgent skill-ups - SWDB";
  47. $this->description = "Monster in need os skilling up";
  48. $this->canonical = $root . "report/skillups";
  49. }
  50. /**
  51. * Parses the request looking for the selected filters, validates them
  52. * and adds them to the $filters array.
  53. */
  54. private function parse_filters(){
  55. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  56. $this->filters["min_stars"] = $_GET["min_stars"];
  57. }
  58. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  59. $this->filters["max_stars"] = $_GET["max_stars"];
  60. }
  61. if (isset($_GET["min_natural_stars"]) && intval($_GET["min_natural_stars"]) > 0 && intval($_GET["min_natural_stars"]) < 6){
  62. $this->filters["min_natural_stars"] = $_GET["min_natural_stars"];
  63. }
  64. if (isset($_GET["max_natural_stars"]) && intval($_GET["max_natural_stars"]) > 0 && intval($_GET["max_natural_stars"]) < 6){
  65. $this->filters["max_natural_stars"] = $_GET["max_natural_stars"];
  66. }
  67. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  68. $this->filters["in_storage"] = true;
  69. }
  70. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  71. $this->filters["without_runes"] = true;
  72. }
  73. if (isset($_GET["family"]) && $_GET["family"] == "on"){
  74. $this->filters["family"] = true;
  75. }
  76. if (isset($_GET["maxed"]) && $_GET["maxed"] == "on"){
  77. $this->filters["maxed"] = true;
  78. }
  79. if (isset($_GET["2a"]) && $_GET["2a"] == "on"){
  80. $this->filters["2a"] = true;
  81. }
  82. if (isset($_GET["homunculus"]) && $_GET["homunculus"] == "on"){
  83. $this->filters["homunculus"] = true;
  84. }
  85. return;
  86. }
  87. /**
  88. * Builds the query to the rune table using the selected or default
  89. * filters.
  90. *
  91. * @return The query to be executed.
  92. */
  93. private function build_query(){
  94. $s =
  95. "SELECT DISTINCT " .
  96. " unit.id AS id, " .
  97. " sum(unit_skill.level * ((k_skill.slot + 1) / 2)) AS ponderated_lvl, " .
  98. " sum(unit_skill.level) AS lvl, " .
  99. " sum(k_skill.max_level * ((k_skill.slot + 1) / 2)) AS ponderated_max_lvl, " .
  100. " sum(k_skill.max_level) AS max_lvl " .
  101. "FROM " .
  102. " unit, " .
  103. " k_unit, " .
  104. " unit_skill, " .
  105. " k_unit_skill, " .
  106. " k_skill " .
  107. "WHERE " .
  108. " unit.unit = k_unit.id AND " .
  109. " unit_skill.unit = unit.id AND " .
  110. " unit_skill.skill = k_unit_skill.skill AND " .
  111. " k_unit.id = k_unit_skill.unit AND " .
  112. " k_skill.id = k_unit_skill.skill AND " .
  113. " unit.stars >= " . $this->filters["min_stars"] . " AND " .
  114. " unit.stars <= " . $this->filters["max_stars"] . " AND " .
  115. " k_unit.natural_stars >= " . $this->filters["min_natural_stars"] . " AND " .
  116. " k_unit.natural_stars <= " . $this->filters["max_natural_stars"] . " ";
  117. if (!$this->filters["in_storage"]){
  118. // TODO
  119. //$s = $s . " AND unit.in_storage = 0 ";
  120. }
  121. if (!$this->filters["without_runes"]){
  122. $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
  123. }
  124. if ($this->filters["family"]){
  125. $s = $s . " AND unit.unit NOT IN (SELECT id FROM k_unit m WHERE family IN (SELECT family FROM k_unit k WHERE k.family = m.family AND k.natural_stars < m.natural_stars AND k.natural_stars < " . $this->filters["min_natural_stars"] . ")) ";
  126. // Special case: Eirgar (Dark Vampire lord). Nat5 can be powered with 4 star Vampires.
  127. if ($this->filters["min_natural_stars"] >= 5){
  128. $s = $s . " AND k_unit.id <> 19114 AND k_unit.id <> 19104 ";
  129. }
  130. // Special case: Fran (Light Fairy Queen). Nat3 can be powered with 2 star Fairies.
  131. if ($this->filters["min_natural_stars"] >= 3){
  132. $s = $s . " AND k_unit.id <> 23015 AND k_unit.id <> 23005 ";
  133. }
  134. }
  135. if (!$this->filters["2a"]){
  136. $s = $s . " AND (k_unit.awakens_from IS NULL OR k_unit.awakens_to IS NOT NULL OR k_unit.base_stars - k_unit.natural_stars = 1) ";
  137. }
  138. if (!$this->filters["homunculus"]){
  139. $s = $s . " AND k_unit.homunculus <> 1 ";
  140. }
  141. $s = $s .
  142. "GROUP BY unit.id ";
  143. if (!$this->filters["maxed"]){
  144. $s = $s . " HAVING ponderated_lvl < ponderated_max_lvl ";
  145. }
  146. $s = $s .
  147. "ORDER BY " .
  148. " ponderated_lvl * 100 / ponderated_max_lvl, " .
  149. " unit.stars DESC; " .
  150. " unit.level DESC, " .
  151. " lvl * 100 / max_lvl, " .
  152. " max_lvl - lvl DESC; ";
  153. return $s;
  154. }
  155. }
  156. ?>