Report_Skillup_Page.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. $q_mon = $this->db->query($s_mon);
  42. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  43. array_push($this->units, new Unit($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"] = true;
  68. }
  69. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  70. $this->filters["without_runes"] = true;
  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. if (isset($_GET["homunculus"]) && $_GET["homunculus"] == "on"){
  82. $this->filters["homunculus"] = true;
  83. }
  84. return;
  85. }
  86. /**
  87. * Builds the query to the rune table using the selected or default
  88. * filters.
  89. *
  90. * @return The query to be executed.
  91. */
  92. private function build_query(){
  93. global $UID;
  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.uid = '$UID' AND " .
  109. " unit.unit = k_unit.id AND " .
  110. " unit_skill.unit = unit.id AND " .
  111. " unit_skill.skill = k_unit_skill.skill AND " .
  112. " k_unit.id = k_unit_skill.unit AND " .
  113. " k_skill.id = k_unit_skill.skill AND " .
  114. " unit.stars >= " . $this->filters["min_stars"] . " AND " .
  115. " unit.stars <= " . $this->filters["max_stars"] . " ";
  116. if ($this->filters["2a"]){
  117. $s = $s .
  118. " AND (( " .
  119. " k_unit.natural_stars >= " . $this->filters["min_natural_stars"] . " AND " .
  120. " k_unit.natural_stars <= " . $this->filters["max_natural_stars"] . " " .
  121. " ) OR ( " .
  122. " k_unit.base_stars - k_unit.natural_stars > 1 " .
  123. " )) ";
  124. }
  125. else{
  126. $s = $s .
  127. " AND k_unit.natural_stars >= " . $this->filters["min_natural_stars"] . " AND " .
  128. " k_unit.natural_stars <= " . $this->filters["max_natural_stars"] . " ";
  129. }
  130. if (!$this->filters["in_storage"]){
  131. // TODO
  132. //$s = $s . " AND unit.in_storage = 0 ";
  133. }
  134. if (!$this->filters["without_runes"]){
  135. $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
  136. }
  137. if (!$this->filters["family"]){
  138. $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"] . ")) ";
  139. // Special case: Eirgar (Dark Vampire lord). Nat5 can be powered with 4 star Vampires.
  140. if ($this->filters["min_natural_stars"] >= 5){
  141. $s = $s . " AND k_unit.id <> 19114 AND k_unit.id <> 19104 ";
  142. }
  143. // Special case: Fran (Light Fairy Queen). Nat3 can be powered with 2 star Fairies.
  144. if ($this->filters["min_natural_stars"] >= 3){
  145. $s = $s . " AND k_unit.id <> 23015 AND k_unit.id <> 23005 ";
  146. }
  147. }
  148. //if (!$this->filters["2a"]){
  149. // $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) ";
  150. //}
  151. if (!$this->filters["homunculus"]){
  152. $s = $s . " AND k_unit.homunculus <> 1 ";
  153. }
  154. $s = $s .
  155. "GROUP BY unit.id ";
  156. if (!$this->filters["maxed"]){
  157. $s = $s . " HAVING ponderated_lvl < ponderated_max_lvl ";
  158. }
  159. $s = $s .
  160. "ORDER BY " .
  161. " ponderated_lvl * 100 / ponderated_max_lvl, " .
  162. " unit.stars DESC; " .
  163. " unit.level DESC, " .
  164. " lvl * 100 / max_lvl, " .
  165. " max_lvl - lvl DESC; ";
  166. return $s;
  167. }
  168. }
  169. ?>