Report_Skillup_Page.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. * @var mixed[] Default filter values.
  26. */
  27. public $filters = [
  28. "MIN_STARS" => 5,
  29. "MAX_STARS" => 6,
  30. "MIN_NATURAL_STARS" => 5,
  31. "MAX_NATURAL_STARS" => 5,
  32. "IN_STORAGE" => false,
  33. "WITHOUT_RUNES" => false,
  34. "FAMILY" => false,
  35. "MAXED" => false,
  36. "2A" => true,
  37. "HOMUNCULUS" => true,
  38. "UNIT" => 0, // 0: All, 1: In teams, 2: In teams (score > 0)
  39. "PONDERATION" => [1, 1.5, 2, 2.5]
  40. ];
  41. /**
  42. * Constructor.
  43. *
  44. * Retrieves the data and initializes the variables.
  45. *
  46. * @global resource Database connection.
  47. */
  48. public function __construct(){
  49. global $db;
  50. $this->view = PATH::VIEW . "report_skillup.php";
  51. $this->parse_filters();
  52. $s = $this->build_query();
  53. $q = $db->query($s);
  54. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  55. array_push($this->units, new Unit($r["id"]));
  56. }
  57. $this->title = "Urgent skill-ups - SWDB";
  58. $this->description = "Monster in need of skilling up";
  59. $this->canonical = URL::BASE . "report/skillups";
  60. }
  61. /**
  62. * Parses the request looking for the selected filters, validates them
  63. * and adds them to the $filters array.
  64. */
  65. private function parse_filters(){
  66. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  67. $this->filters["MIN_STARS"] = intval($_GET["min_stars"]);
  68. }
  69. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  70. $this->filters["MAX_STARS"] = intval($_GET["max_stars"]);
  71. }
  72. if (isset($_GET["min_natural_stars"]) && intval($_GET["min_natural_stars"]) > 0 && intval($_GET["min_natural_stars"]) < 6){
  73. $this->filters["MIN_NATURAL_STARS"] = intval($_GET["min_natural_stars"]);
  74. }
  75. if (isset($_GET["max_natural_stars"]) && intval($_GET["max_natural_stars"]) > 0 && intval($_GET["max_natural_stars"]) < 6){
  76. $this->filters["MAX_NATURAL_STARS"] = intval($_GET["max_natural_stars"]);
  77. }
  78. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  79. $this->filters["IN_STORAGE"] = true;
  80. }
  81. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  82. $this->filters["WITHOUT_RUNES"] = true;
  83. }
  84. if (isset($_GET["family"]) && $_GET["family"] == "on"){
  85. $this->filters["FAMILY"] = true;
  86. }
  87. if (isset($_GET["maxed"]) && $_GET["maxed"] == "on"){
  88. $this->filters["MAXED"] = true;
  89. }
  90. if (!isset($_GET["2a"]) || $_GET["2a"] != "on"){
  91. $this->filters["2A"] = false;
  92. }
  93. if (isset($_GET["homunculus"]) && $_GET["homunculus"] == "on"){
  94. $this->filters["HOMUNCULUS"] = true;
  95. }
  96. if (isset($_GET["unit"]) && intval($_GET["unit"]) >= 0 && intval($_GET["unit"]) <= 2){
  97. $this->filters["UNIT"] = intval($_GET["unit"]);
  98. }
  99. if (isset($_GET["pond_1"]) && floatval($_GET["pond_1"]) >= 0 && floatval($_GET["pond_1"]) <= 4){
  100. $this->filters["PONDERATION"][0] = floatval($_GET["pond_1"]);
  101. }
  102. if (isset($_GET["pond_2"]) && floatval($_GET["pond_2"]) >= 0 && floatval($_GET["pond_2"]) <= 4){
  103. $this->filters["PONDERATION"][1] = floatval($_GET["pond_2"]);
  104. }
  105. if (isset($_GET["pond_3"]) && floatval($_GET["pond_3"]) >= 0 && floatval($_GET["pond_3"]) <= 4){
  106. $this->filters["PONDERATION"][2] = floatval($_GET["pond_3"]);
  107. }
  108. if (isset($_GET["pond_4"]) && floatval($_GET["pond_4"]) >= 0 && floatval($_GET["pond_4"]) <= 4){
  109. $this->filters["PONDERATION"][3] = floatval($_GET["pond_4"]);
  110. }
  111. return;
  112. }
  113. /**
  114. * Builds the query to the rune table using the selected or default
  115. * filters.
  116. *
  117. * @return string The query to be executed.
  118. * @global int Player ID.
  119. * @global int Game mode.
  120. */
  121. private function build_query(){
  122. global $UID;
  123. global $MODE;
  124. $s = "
  125. SELECT DISTINCT
  126. unit.id AS id,
  127. --sum(unit_skill.level * (CAST(k_skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_lvl,
  128. sum(unit_skill.level *
  129. CASE
  130. WHEN k_skill.slot = 1 THEN " . $this->filters["PONDERATION"][0] . "
  131. WHEN k_skill.slot = 2 THEN " . $this->filters["PONDERATION"][1] . "
  132. WHEN k_skill.slot = 3 THEN " . $this->filters["PONDERATION"][2] . "
  133. WHEN k_skill.slot = 4 THEN " . $this->filters["PONDERATION"][3] . "
  134. ELSE 1
  135. END
  136. ) AS ponderated_lvl,
  137. sum(unit_skill.level) AS lvl,
  138. --sum(k_skill.max_level * (CAST(k_skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_max_lvl,
  139. sum(k_skill.max_level *
  140. CASE
  141. WHEN k_skill.slot = 1 THEN " . $this->filters["PONDERATION"][0] . "
  142. WHEN k_skill.slot = 2 THEN " . $this->filters["PONDERATION"][1] . "
  143. WHEN k_skill.slot = 3 THEN " . $this->filters["PONDERATION"][2] . "
  144. WHEN k_skill.slot = 4 THEN " . $this->filters["PONDERATION"][3] . "
  145. ELSE 1
  146. END
  147. ) AS ponderated_max_lvl,
  148. sum(k_skill.max_level) AS max_lvl
  149. FROM
  150. unit,
  151. k_unit,
  152. unit_skill,
  153. k_unit_skill,
  154. k_skill
  155. WHERE
  156. unit.uid = '$UID' AND
  157. unit.unit = k_unit.id AND
  158. unit_skill.unit = unit.id AND ";
  159. switch ($this->filters["UNIT"]){
  160. case 0: // All units, do ot fiter.
  161. break;
  162. case 1: // Units in teams.
  163. $s .= "
  164. (
  165. unit.id IN (
  166. SELECT DISTINCT unit
  167. FROM
  168. team,
  169. team_unit
  170. WHERE
  171. team.id = team_unit.team AND
  172. team.uid = '$UID'
  173. )
  174. ) AND ";
  175. break;
  176. case 2: // Units in teams with score.
  177. $s .= "
  178. (
  179. unit.id IN (
  180. SELECT DISTINCT unit
  181. FROM
  182. team,
  183. team_unit
  184. WHERE
  185. team.id = team_unit.team AND
  186. team.uid = '$UID' AND
  187. team.score > 0
  188. )
  189. ) AND ";
  190. break;
  191. }
  192. if (!$this->filters["HOMUNCULUS"]){
  193. $s .= "
  194. k_unit.homunculus <> 1 AND
  195. unit_skill.skill = k_unit_skill.skill AND
  196. k_unit.id = k_unit_skill.unit AND
  197. k_skill.id = k_unit_skill.skill AND
  198. ";
  199. }
  200. else{
  201. $s .= "
  202. (
  203. (
  204. k_unit.homunculus <> 1 AND
  205. unit_skill.skill = k_unit_skill.skill AND
  206. k_unit.id = k_unit_skill.unit AND
  207. k_skill.id = k_unit_skill.skill
  208. ) OR
  209. (
  210. unit.homunculus = 1 AND
  211. unit.unit = k_unit.id AND
  212. unit_skill.unit = unit.id AND
  213. unit_skill.skill = k_skill.id AND
  214. unit.id = unit_skill.unit
  215. )
  216. ) AND
  217. ";
  218. }
  219. $s .= "
  220. k_skill.max_level > 1 AND
  221. unit.stars >= " . $this->filters["MIN_STARS"] . " AND
  222. unit.stars <= " . $this->filters["MAX_STARS"]
  223. ;
  224. if ($this->filters["2A"]){
  225. $s = $s . "
  226. AND ((
  227. k_unit.natural_stars >= " . $this->filters["MIN_NATURAL_STARS"] . " AND
  228. k_unit.natural_stars <= " . $this->filters["MAX_NATURAL_STARS"] . "
  229. ) OR (
  230. k_unit.base_stars - k_unit.natural_stars > 1
  231. ))
  232. ";
  233. }
  234. else{
  235. $s = $s . "
  236. AND k_unit.natural_stars >= " . $this->filters["MIN_NATURAL_STARS"] . " AND
  237. k_unit.natural_stars <= " . $this->filters["MAX_NATURAL_STARS"]
  238. ;
  239. }
  240. if (!$this->filters["IN_STORAGE"]){
  241. $s = $s . " AND unit.building <> " . BUILDING_ID::MONSTER_STORAGE;
  242. }
  243. if (!$this->filters["WITHOUT_RUNES"]){
  244. $s = $s . " AND unit.id IN (SELECT DISTINCT unit FROM unit_rune WHERE rta = $MODE) ";
  245. }
  246. if (!$this->filters["FAMILY"]){
  247. $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"] . ")) ";
  248. // Special case: Eirgar (Dark Vampire lord). Nat5 can be powered with 4 star Vampires.
  249. if ($this->filters["MIN_NATURAL_STARS"] >= 5){
  250. $s = $s . " AND k_unit.id <> 19114 AND k_unit.id <> 19104 ";
  251. }
  252. // Special case: Fran (Light Fairy Queen). Nat3 can be powered with 2 star Fairies.
  253. if ($this->filters["MIN_NATURAL_STARS"] >= 3){
  254. $s = $s . " AND k_unit.id <> 23015 AND k_unit.id <> 23005 ";
  255. }
  256. }
  257. $s = $s .
  258. "GROUP BY unit.id ";
  259. if (!$this->filters["MAXED"]){
  260. $s = $s . " HAVING ponderated_lvl < ponderated_max_lvl ";
  261. }
  262. $s = $s . "
  263. ORDER BY
  264. CAST(ponderated_lvl * 100 AS FLOAT) / CAST(ponderated_max_lvl * 100 AS FLOAT),
  265. unit.stars DESC,
  266. unit.level DESC,
  267. CAST(lvl AS FLOAT) / CAST(max_lvl AS FLOAT),
  268. max_lvl - lvl DESC
  269. ";
  270. return $s;
  271. }
  272. }
  273. ?>