Report_Skillup_Page.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * @global resource Database connection.
  30. */
  31. public function __construct(){
  32. global $db;
  33. $this->view = PATH::VIEW . "report_skillup.php";
  34. $this->parse_filters();
  35. $s = $this->build_query();
  36. $q = $db->query($s);
  37. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  38. array_push($this->units, new Unit($r["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. $this->filters = FILTER::SKILLUP;
  50. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  51. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  52. }
  53. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  54. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  55. }
  56. if (isset($_GET["min_natural_stars"]) && intval($_GET["min_natural_stars"]) > 0 && intval($_GET["min_natural_stars"]) < 6){
  57. $this->filters["MIN_NATURAL_STARS"] = $_GET["min_natural_stars"];
  58. }
  59. if (isset($_GET["max_natural_stars"]) && intval($_GET["max_natural_stars"]) > 0 && intval($_GET["max_natural_stars"]) < 6){
  60. $this->filters["MAX_NATURAL_STARS"] = $_GET["max_natural_stars"];
  61. }
  62. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  63. $this->filters["IN_STORAGE"] = true;
  64. }
  65. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  66. $this->filters["WITHOUT_RUNES"] = true;
  67. }
  68. if (isset($_GET["family"]) && $_GET["family"] == "on"){
  69. $this->filters["FAMILY"] = true;
  70. }
  71. if (isset($_GET["maxed"]) && $_GET["maxed"] == "on"){
  72. $this->filters["MAXED"] = true;
  73. }
  74. if (isset($_GET["2a"]) && $_GET["2a"] == "on"){
  75. $this->filters["2A"] = true;
  76. }
  77. if (isset($_GET["homunculus"]) && $_GET["homunculus"] == "on"){
  78. $this->filters["HOMUNCULUS"] = true;
  79. }
  80. return;
  81. }
  82. /**
  83. * Builds the query to the rune table using the selected or default
  84. * filters.
  85. *
  86. * @return string The query to be executed.
  87. * @global int Player ID.
  88. */
  89. private function build_query(){
  90. global $UID;
  91. $s = "
  92. SELECT DISTINCT
  93. unit.id AS id,
  94. sum(unit_skill.level * (CAST(k_skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_lvl,
  95. sum(unit_skill.level) AS lvl,
  96. sum(k_skill.max_level * (CAST(k_skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_max_lvl,
  97. sum(k_skill.max_level) AS max_lvl
  98. FROM
  99. unit,
  100. k_unit,
  101. unit_skill,
  102. k_unit_skill,
  103. k_skill
  104. WHERE
  105. unit.uid = '$UID' AND
  106. unit.unit = k_unit.id AND
  107. unit_skill.unit = unit.id AND ";
  108. if (!$this->filters["HOMUNCULUS"]){
  109. $s .= "
  110. k_unit.homunculus <> 1 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. ";
  115. }
  116. else{
  117. $s .= "
  118. (
  119. (
  120. k_unit.homunculus <> 1 AND
  121. unit_skill.skill = k_unit_skill.skill AND
  122. k_unit.id = k_unit_skill.unit AND
  123. k_skill.id = k_unit_skill.skill
  124. ) OR
  125. (
  126. unit.homunculus = 1 AND
  127. unit.unit = k_unit.id AND
  128. unit_skill.unit = unit.id AND
  129. unit_skill.skill = k_skill.id AND
  130. unit.id = unit_skill.unit
  131. )
  132. ) AND
  133. ";
  134. }
  135. $s .= "
  136. k_skill.max_level > 1 AND
  137. unit.stars >= " . $this->filters["MIN_STARS"] . " AND
  138. unit.stars <= " . $this->filters["MAX_STARS"]
  139. ;
  140. if ($this->filters["2A"]){
  141. $s = $s . "
  142. AND ((
  143. k_unit.natural_stars >= " . $this->filters["MIN_NATURAL_STARS"] . " AND
  144. k_unit.natural_stars <= " . $this->filters["MAX_NATURAL_STARS"] . "
  145. ) OR (
  146. k_unit.base_stars - k_unit.natural_stars > 1
  147. ))
  148. ";
  149. }
  150. else{
  151. $s = $s . "
  152. AND k_unit.natural_stars >= " . $this->filters["MIN_NATURAL_STARS"] . " AND
  153. k_unit.natural_stars <= " . $this->filters["MAX_NATURAL_STARS"]
  154. ;
  155. }
  156. if (!$this->filters["IN_STORAGE"]){
  157. $s = $s . " AND unit.building <> " . BUILDING_ID::MONSTER_STORAGE;
  158. }
  159. if (!$this->filters["WITHOUT_RUNES"]){
  160. $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
  161. }
  162. if (!$this->filters["FAMILY"]){
  163. $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"] . ")) ";
  164. // Special case: Eirgar (Dark Vampire lord). Nat5 can be powered with 4 star Vampires.
  165. if ($this->filters["MIN_NATURAL_STARS"] >= 5){
  166. $s = $s . " AND k_unit.id <> 19114 AND k_unit.id <> 19104 ";
  167. }
  168. // Special case: Fran (Light Fairy Queen). Nat3 can be powered with 2 star Fairies.
  169. if ($this->filters["MIN_NATURAL_STARS"] >= 3){
  170. $s = $s . " AND k_unit.id <> 23015 AND k_unit.id <> 23005 ";
  171. }
  172. }
  173. $s = $s .
  174. "GROUP BY unit.id ";
  175. if (!$this->filters["MAXED"]){
  176. $s = $s . " HAVING ponderated_lvl < ponderated_max_lvl ";
  177. }
  178. $s = $s . "
  179. ORDER BY
  180. CAST(ponderated_lvl * 100 AS FLOAT) / CAST(ponderated_max_lvl * 100 AS FLOAT),
  181. unit.stars DESC,
  182. unit.level DESC,
  183. CAST(lvl AS FLOAT) / CAST(max_lvl AS FLOAT),
  184. max_lvl - lvl DESC
  185. ";
  186. return $s;
  187. }
  188. }
  189. ?>