Report_Skillup_Page.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Urgent Skillup report 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. * Urgent Skillup report page model.
  16. *
  17. * @category Page
  18. */
  19. class Report_Skillup_Page extends Page{
  20. /**
  21. * @var \Unit[] Sorted list of units.
  22. */
  23. public $units = [];
  24. /**
  25. * Constructor.
  26. *
  27. * Retrieves the data and initializes the variables.
  28. *
  29. * @param resource $db Connection to the database.
  30. */
  31. public function __construct($db){
  32. parent::__construct($db);
  33. $this->view = PATH::VIEW . "report_skillup.php";
  34. $this->parse_filters();
  35. $s_mon = $this->build_query();
  36. $q_mon = $this->db->query($s_mon);
  37. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  38. array_push($this->units, new Unit($db, $r_mon["id"]));
  39. }
  40. $this->title = "Urgent skill-ups - SWDB";
  41. $this->description = "Monster in need of skilling up";
  42. $this->canonical = URL::BASE . "report/skillups";
  43. }
  44. /**
  45. * Parses the request looking for the selected filters, validates them
  46. * and adds them to the $filters array.
  47. */
  48. private function parse_filters(){
  49. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  50. $this->filters["min_stars"] = $_GET["min_stars"];
  51. }
  52. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  53. $this->filters["max_stars"] = $_GET["max_stars"];
  54. }
  55. if (isset($_GET["min_natural_stars"]) && intval($_GET["min_natural_stars"]) > 0 && intval($_GET["min_natural_stars"]) < 6){
  56. $this->filters["min_natural_stars"] = $_GET["min_natural_stars"];
  57. }
  58. if (isset($_GET["max_natural_stars"]) && intval($_GET["max_natural_stars"]) > 0 && intval($_GET["max_natural_stars"]) < 6){
  59. $this->filters["max_natural_stars"] = $_GET["max_natural_stars"];
  60. }
  61. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  62. $this->filters["in_storage"] = true;
  63. }
  64. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  65. $this->filters["without_runes"] = true;
  66. }
  67. if (isset($_GET["family"]) && $_GET["family"] == "on"){
  68. $this->filters["family"] = true;
  69. }
  70. if (isset($_GET["maxed"]) && $_GET["maxed"] == "on"){
  71. $this->filters["maxed"] = true;
  72. }
  73. if (isset($_GET["2a"]) && $_GET["2a"] == "on"){
  74. $this->filters["2a"] = true;
  75. }
  76. if (isset($_GET["homunculus"]) && $_GET["homunculus"] == "on"){
  77. $this->filters["homunculus"] = true;
  78. }
  79. return;
  80. }
  81. /**
  82. * Builds the query to the rune table using the selected or default
  83. * filters.
  84. *
  85. * @return string The query to be executed.
  86. * @global int Player ID.
  87. */
  88. private function build_query(){
  89. global $UID;
  90. $s = "
  91. SELECT DISTINCT
  92. unit.id AS id,
  93. sum(unit_skill.level * (CAST(k_skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_lvl,
  94. sum(unit_skill.level) AS lvl,
  95. sum(k_skill.max_level * (CAST(k_skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_max_lvl,
  96. sum(k_skill.max_level) AS max_lvl
  97. FROM
  98. unit,
  99. k_unit,
  100. unit_skill,
  101. k_unit_skill,
  102. k_skill
  103. WHERE
  104. unit.uid = '$UID' AND
  105. unit.unit = k_unit.id AND
  106. unit_skill.unit = unit.id AND
  107. unit_skill.skill = k_unit_skill.skill AND
  108. k_unit.id = k_unit_skill.unit AND
  109. k_skill.id = k_unit_skill.skill AND
  110. k_skill.max_level > 1 AND
  111. unit.stars >= " . $this->filters["min_stars"] . " AND
  112. unit.stars <= " . $this->filters["max_stars"]
  113. ;
  114. if ($this->filters["2a"]){
  115. $s = $s . "
  116. AND ((
  117. k_unit.natural_stars >= " . $this->filters["min_natural_stars"] . " AND
  118. k_unit.natural_stars <= " . $this->filters["max_natural_stars"] . "
  119. ) OR (
  120. k_unit.base_stars - k_unit.natural_stars > 1
  121. ))
  122. ";
  123. }
  124. else{
  125. $s = $s . "
  126. AND k_unit.natural_stars >= " . $this->filters["min_natural_stars"] . " AND
  127. k_unit.natural_stars <= " . $this->filters["max_natural_stars"]
  128. ;
  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["homunculus"]){
  149. $s = $s . " AND k_unit.homunculus <> 1 ";
  150. }
  151. $s = $s .
  152. "GROUP BY unit.id ";
  153. if (!$this->filters["maxed"]){
  154. $s = $s . " HAVING ponderated_lvl < ponderated_max_lvl ";
  155. }
  156. $s = $s . "
  157. ORDER BY
  158. CAST(ponderated_lvl AS FLOAT) / CAST(ponderated_max_lvl AS FLOAT),
  159. unit.stars DESC,
  160. unit.level DESC,
  161. CAST(lvl AS FLOAT) / CAST(max_lvl AS FLOAT),
  162. max_lvl - lvl DESC;
  163. ";
  164. return $s;
  165. }
  166. }
  167. ?>