Report_Skillup_Page.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(PATH::PAGE . "Page.php");
  12. require_once(PATH::ENTITY . "Unit.php");
  13. /**
  14. * Urgent Skillup report page model.
  15. *
  16. * @category Page
  17. */
  18. class Report_Skillup_Page extends Page{
  19. /**
  20. * @var Unit[] Sorted list of units.
  21. */
  22. private $units = [];
  23. /**
  24. * @var mixed[] Default filter values.
  25. */
  26. private $filters = [
  27. "MIN_STARS" => 5,
  28. "MAX_STARS" => 6,
  29. "MIN_NATURAL_STARS" => 5,
  30. "MAX_NATURAL_STARS" => 5,
  31. "IN_STORAGE" => false,
  32. "WITHOUT_RUNES" => false,
  33. "FAMILY" => false,
  34. "MAXED" => false,
  35. "2A" => false,
  36. "HOMUNCULUS" => true,
  37. "UNIT" => 0, // 0: All, 1: In teams, 2: In teams (score > 0)
  38. "PONDERATION" => [1, 1.5, 2, 2.5]
  39. ];
  40. /**
  41. * Constructor.
  42. *
  43. * Retrieves the data and initializes the variables.
  44. */
  45. public function __construct(){
  46. $this->view = PATH::VIEW . "report_skillup.php";
  47. $this->parse_filters();
  48. $result_set = $this->prepare_statement()->execute();
  49. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  50. array_push($this->units, new Unit($result["id"]));
  51. }
  52. $this->title = "Urgent skill-ups - SWDB";
  53. $this->description = "Monster in need of skilling up";
  54. $this->canonical = URL::BASE . "report/skillups";
  55. }
  56. /**
  57. * Parses the request looking for the selected filters, validates them
  58. * and adds them to the $filters array.
  59. */
  60. private function parse_filters(){
  61. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 7){
  62. $this->filters["MIN_STARS"] = intval($_GET["min_stars"]);
  63. }
  64. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  65. $this->filters["MAX_STARS"] = intval($_GET["max_stars"]);
  66. }
  67. if (isset($_GET["min_natural_stars"]) && intval($_GET["min_natural_stars"]) > 0 && intval($_GET["min_natural_stars"]) < 6){
  68. $this->filters["MIN_NATURAL_STARS"] = intval($_GET["min_natural_stars"]);
  69. }
  70. if (isset($_GET["max_natural_stars"]) && intval($_GET["max_natural_stars"]) > 0 && intval($_GET["max_natural_stars"]) < 6){
  71. $this->filters["MAX_NATURAL_STARS"] = intval($_GET["max_natural_stars"]);
  72. }
  73. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  74. $this->filters["IN_STORAGE"] = true;
  75. }
  76. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  77. $this->filters["WITHOUT_RUNES"] = true;
  78. }
  79. if (isset($_GET["family"]) && $_GET["family"] == "on"){
  80. $this->filters["FAMILY"] = true;
  81. }
  82. if (isset($_GET["maxed"]) && $_GET["maxed"] == "on"){
  83. $this->filters["MAXED"] = true;
  84. }
  85. if (!isset($_GET["2a"]) || $_GET["2a"] != "on"){
  86. $this->filters["2A"] = false;
  87. }
  88. else{
  89. $this->filters["2A"] = true;
  90. }
  91. if (isset($_GET["homunculus"]) && $_GET["homunculus"] == "on"){
  92. $this->filters["HOMUNCULUS"] = true;
  93. }
  94. if (isset($_GET["unit"]) && intval($_GET["unit"]) >= 0 && intval($_GET["unit"]) <= 2){
  95. $this->filters["UNIT"] = intval($_GET["unit"]);
  96. }
  97. if (isset($_GET["pond_1"]) && floatval($_GET["pond_1"]) >= 0 && floatval($_GET["pond_1"]) <= 4){
  98. $this->filters["PONDERATION"][0] = floatval($_GET["pond_1"]);
  99. }
  100. if (isset($_GET["pond_2"]) && floatval($_GET["pond_2"]) >= 0 && floatval($_GET["pond_2"]) <= 4){
  101. $this->filters["PONDERATION"][1] = floatval($_GET["pond_2"]);
  102. }
  103. if (isset($_GET["pond_3"]) && floatval($_GET["pond_3"]) >= 0 && floatval($_GET["pond_3"]) <= 4){
  104. $this->filters["PONDERATION"][2] = floatval($_GET["pond_3"]);
  105. }
  106. if (isset($_GET["pond_4"]) && floatval($_GET["pond_4"]) >= 0 && floatval($_GET["pond_4"]) <= 4){
  107. $this->filters["PONDERATION"][3] = floatval($_GET["pond_4"]);
  108. }
  109. return;
  110. }
  111. /**
  112. * Prepares the statement to execute against the database using the filter values.
  113. *
  114. * @return SQLite3Stmt prepared statement.
  115. */
  116. private function prepare_statement(){
  117. // Common items
  118. $query = "
  119. SELECT DISTINCT
  120. unit.id AS id,
  121. --sum(unit_skill.level * (CAST(skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_lvl,
  122. sum(unit_skill.level *
  123. CASE
  124. WHEN skill.slot = 1 THEN :ponderation_0
  125. WHEN skill.slot = 2 THEN :ponderation_1
  126. WHEN skill.slot = 3 THEN :ponderation_2
  127. WHEN skill.slot = 4 THEN :ponderation_3
  128. ELSE 1
  129. END
  130. ) AS ponderated_lvl,
  131. sum(unit_skill.level) AS lvl,
  132. --sum(skill.max_level * (CAST(skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_max_lvl,
  133. sum(skill.max_level *
  134. CASE
  135. WHEN skill.slot = 1 THEN :ponderation_0
  136. WHEN skill.slot = 2 THEN :ponderation_1
  137. WHEN skill.slot = 3 THEN :ponderation_2
  138. WHEN skill.slot = 4 THEN :ponderation_3
  139. ELSE 1
  140. END
  141. ) AS ponderated_max_lvl,
  142. sum(skill.max_level) AS max_lvl
  143. FROM
  144. unit,
  145. monster,
  146. unit_skill,
  147. monster_skill,
  148. skill
  149. WHERE
  150. unit.player = :player AND
  151. unit.monster = monster.id AND
  152. unit_skill.unit = unit.id ";
  153. switch ($this->filters["UNIT"]){
  154. case 0: // All units, do not fiter.
  155. break;
  156. case 1: // Units in teams.
  157. $query .= "
  158. AND (
  159. unit.id IN (
  160. SELECT DISTINCT unit
  161. FROM
  162. team,
  163. team_unit
  164. WHERE
  165. team.id = team_unit.team AND
  166. team.player = unit.player
  167. )
  168. ) AND ";
  169. break;
  170. case 2: // Units in teams with score.
  171. $query .= "
  172. AND (
  173. unit.id IN (
  174. SELECT DISTINCT unit
  175. FROM
  176. team,
  177. team_unit
  178. WHERE
  179. team.id = team_unit.team AND
  180. team.player = unit.player AND
  181. team.score > 0
  182. )
  183. ) ";
  184. break;
  185. }
  186. if (!$this->filters["HOMUNCULUS"]){
  187. $query .= "
  188. -- TODO: filter by units id
  189. --monster.homunculus <> 1 AND
  190. AND unit_skill.skill = monster_skill.skill AND
  191. monster.id = monster_skill.monster AND
  192. skill.id = monster_skill.monster
  193. ";
  194. }
  195. else{
  196. $query .= "
  197. AND (
  198. (
  199. monster.homunculus <> 1 AND
  200. unit_skill.skill = monster_skill.skill AND
  201. monster.id = monster_skill.monster AND
  202. skill.id = monster_skill.skill
  203. ) OR
  204. (
  205. -- TODO: Flter by unit IDs
  206. --unit.homunculus = 1 AND
  207. unit.monster = monster.id AND
  208. unit_skill.unit = unit.id AND
  209. unit_skill.skill = skill.id AND
  210. unit.id = unit_skill.unit
  211. )
  212. )
  213. ";
  214. }
  215. $query .= "
  216. AND skill.max_level > 1 AND
  217. unit.stars BETWEEN :min_stars AND :max_stars
  218. ";
  219. if ($this->filters["2A"]){
  220. $query .= "
  221. AND ((
  222. monster.natural_stars BETWEEN :min_natural_stars AND :max_natural_stars
  223. ) OR (
  224. monster.base_stars - monster.natural_stars > 1
  225. ))
  226. ";
  227. }
  228. else{
  229. $query .= "
  230. AND monster.natural_stars BETWEEN :min_natural_stars AND :max_natural_stars
  231. ";
  232. }
  233. if (!$this->filters["IN_STORAGE"]){
  234. $query .= "
  235. AND unit.building <> :storage_building
  236. ";
  237. }
  238. if (! $this->filters["WITHOUT_RUNES"]){
  239. $query .= "
  240. AND unit.id IN (SELECT DISTINCT unit FROM unit_rune WHERE rta = :rta)
  241. ";
  242. }
  243. if (! $this->filters["FAMILY"]){
  244. $query .= "
  245. 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 < :min_natural_stars))
  246. ";
  247. // Special case: Eirgar (Dark Vampire lord). Nat5 can be powered with 4 star Vampires.
  248. if ($this->filters["MIN_NATURAL_STARS"] >= 5){
  249. $query .= " AND monster.id <> 19114 AND monster.id <> 19104 ";
  250. }
  251. // Special case: Fran (Light Fairy Queen). Nat3 can be powered with 2 star Fairies.
  252. if ($this->filters["MIN_NATURAL_STARS"] >= 3){
  253. $query .= " AND monster.id <> 23015 AND monster.id <> 23005 ";
  254. }
  255. }
  256. $query .= " GROUP BY unit.id ";
  257. if (! $this->filters["MAXED"]){
  258. $query .= " HAVING ponderated_lvl < ponderated_max_lvl ";
  259. }
  260. $query .= "
  261. ORDER BY
  262. CAST(ponderated_lvl * 100 AS FLOAT) / CAST(ponderated_max_lvl * 100 AS FLOAT),
  263. unit.stars DESC,
  264. unit.level DESC,
  265. CAST(lvl AS FLOAT) / CAST(max_lvl AS FLOAT),
  266. max_lvl - lvl DESC
  267. ";
  268. $statement = get_context()->get_db()->prepare($query);
  269. for ($i = 0; $i < 4; $i ++){
  270. $statement->bindValue(":ponderation_$i", $this->filters["PONDERATION"][$i], SQLITE3_FLOAT);
  271. }
  272. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  273. $statement->bindValue(":min_stars", $this->filters["MIN_STARS"], SQLITE3_INTEGER);
  274. $statement->bindValue(":max_stars", $this->filters["MAX_STARS"], SQLITE3_INTEGER);
  275. $statement->bindValue(":min_natural_stars", $this->filters["MIN_NATURAL_STARS"], SQLITE3_INTEGER);
  276. $statement->bindValue(":max_natural_stars", $this->filters["MAX_NATURAL_STARS"], SQLITE3_INTEGER);
  277. $statement->bindValue(":storage_building", BUILDING_ID::MONSTER_STORAGE, SQLITE3_INTEGER);
  278. $statement->bindValue(":rta", get_context()->get_game_mode(), SQLITE3_INTEGER);
  279. return $statement;
  280. }
  281. /**
  282. * Retrieves the selected units, sorted by urgency.
  283. *
  284. * @return Unit[] Selected units.
  285. */
  286. public function get_units(){
  287. return $this->units;
  288. }
  289. /**
  290. * Retrieves the page filters.
  291. *
  292. * @return mixed[] Page filtes
  293. */
  294. public function get_filters(){
  295. return $this->filters;
  296. }
  297. /**
  298. * Retrieves one filter.
  299. *
  300. * @param string $key
  301. * @return mixed Filter value, null if if doesn't exist.
  302. */
  303. public function get_filter($key){
  304. if (array_key_exists($key, $this->filters)){
  305. return $this->filters[$key];
  306. }
  307. else{
  308. return null;
  309. }
  310. }
  311. }
  312. ?>