Report_Skillup_Page.php 11 KB

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