Report_Skillup_Page.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. parent::__construct();
  47. $this->set_public(true);
  48. $this->set_view("report_skillup.php");
  49. $this->set_title("Urgent skill-ups");
  50. $this->set_description("FMonster in need of skilling up");
  51. $this->set_canonical("player/" . get_context()->get_player()->get_id() . "/report/report_skillup/");
  52. $this->add_css("report_skillup.css");
  53. $this->parse_filters();
  54. $result_set = $this->prepare_statement()->execute();
  55. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  56. array_push($this->units, new Unit($result["id"]));
  57. }
  58. $this->set_code(200);
  59. $this->set_message("OK");
  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. else{
  94. $this->filters["2A"] = true;
  95. }
  96. if (isset($_GET["homunculus"]) && $_GET["homunculus"] == "on"){
  97. $this->filters["HOMUNCULUS"] = true;
  98. }
  99. if (isset($_GET["unit"]) && intval($_GET["unit"]) >= 0 && intval($_GET["unit"]) <= 2){
  100. $this->filters["UNIT"] = intval($_GET["unit"]);
  101. }
  102. if (isset($_GET["pond_1"]) && floatval($_GET["pond_1"]) >= 0 && floatval($_GET["pond_1"]) <= 4){
  103. $this->filters["PONDERATION"][0] = floatval($_GET["pond_1"]);
  104. }
  105. if (isset($_GET["pond_2"]) && floatval($_GET["pond_2"]) >= 0 && floatval($_GET["pond_2"]) <= 4){
  106. $this->filters["PONDERATION"][1] = floatval($_GET["pond_2"]);
  107. }
  108. if (isset($_GET["pond_3"]) && floatval($_GET["pond_3"]) >= 0 && floatval($_GET["pond_3"]) <= 4){
  109. $this->filters["PONDERATION"][2] = floatval($_GET["pond_3"]);
  110. }
  111. if (isset($_GET["pond_4"]) && floatval($_GET["pond_4"]) >= 0 && floatval($_GET["pond_4"]) <= 4){
  112. $this->filters["PONDERATION"][3] = floatval($_GET["pond_4"]);
  113. }
  114. return;
  115. }
  116. /**
  117. * Prepares the statement to execute against the database using the filter values.
  118. *
  119. * @return SQLite3Stmt prepared statement.
  120. */
  121. private function prepare_statement(){
  122. // Common items
  123. $query = "
  124. SELECT DISTINCT
  125. unit.id AS id,
  126. --sum(unit_skill.level * (CAST(skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_lvl,
  127. sum((unit_skill.level - 1)*
  128. CASE
  129. WHEN skill.slot = 1 THEN :ponderation_0
  130. WHEN skill.slot = 2 THEN :ponderation_1
  131. WHEN skill.slot = 3 THEN :ponderation_2
  132. WHEN skill.slot = 4 THEN :ponderation_3
  133. ELSE 1
  134. END
  135. ) AS ponderated_lvl,
  136. sum(unit_skill.level) AS lvl,
  137. --sum(skill.max_level * (CAST(skill.slot + 1 AS FLOAT) / 2.0)) AS ponderated_max_lvl,
  138. sum((skill.max_level - 1) *
  139. CASE
  140. WHEN skill.slot = 1 THEN :ponderation_0
  141. WHEN skill.slot = 2 THEN :ponderation_1
  142. WHEN skill.slot = 3 THEN :ponderation_2
  143. WHEN skill.slot = 4 THEN :ponderation_3
  144. ELSE 1
  145. END
  146. ) AS ponderated_max_lvl,
  147. sum(skill.max_level) AS max_lvl
  148. FROM
  149. unit,
  150. monster,
  151. unit_skill,
  152. monster_skill,
  153. skill
  154. WHERE
  155. unit.player = :player AND
  156. unit.monster = monster.id AND
  157. unit_skill.unit = unit.id ";
  158. switch ($this->filters["UNIT"]){
  159. case 0: // All units, do not fiter.
  160. break;
  161. case 1: // Units in teams.
  162. $query .= "
  163. AND (
  164. unit.id IN (
  165. SELECT DISTINCT unit
  166. FROM
  167. team,
  168. team_unit
  169. WHERE
  170. team.id = team_unit.team AND
  171. team.player = unit.player
  172. )
  173. ) AND ";
  174. break;
  175. case 2: // Units in teams with score.
  176. $query .= "
  177. AND (
  178. unit.id IN (
  179. SELECT DISTINCT unit
  180. FROM
  181. team,
  182. team_unit
  183. WHERE
  184. team.id = team_unit.team AND
  185. team.player = unit.player AND
  186. team.score > 0
  187. )
  188. ) ";
  189. break;
  190. }
  191. if (!$this->filters["HOMUNCULUS"]){
  192. $query .= "
  193. -- TODO: filter by units id
  194. --monster.homunculus <> 1 AND
  195. AND unit_skill.skill = monster_skill.skill AND
  196. monster.id = monster_skill.monster AND
  197. skill.id = monster_skill.monster
  198. ";
  199. }
  200. else{
  201. $query .= "
  202. AND (
  203. (
  204. monster.homunculus <> 1 AND
  205. unit_skill.skill = monster_skill.skill AND
  206. monster.id = monster_skill.monster AND
  207. skill.id = monster_skill.skill
  208. ) OR
  209. (
  210. -- TODO: Flter by unit IDs
  211. --unit.homunculus = 1 AND
  212. unit.monster = monster.id AND
  213. unit_skill.unit = unit.id AND
  214. unit_skill.skill = skill.id AND
  215. unit.id = unit_skill.unit
  216. )
  217. )
  218. ";
  219. }
  220. $query .= "
  221. AND skill.max_level > 1 AND
  222. unit.stars BETWEEN :min_stars AND :max_stars
  223. ";
  224. if ($this->filters["2A"]){
  225. $query .= "
  226. AND ((
  227. monster.natural_stars BETWEEN :min_natural_stars AND :max_natural_stars
  228. ) OR (
  229. monster.base_stars - monster.natural_stars > 1
  230. ))
  231. ";
  232. }
  233. else{
  234. $query .= "
  235. AND monster.natural_stars BETWEEN :min_natural_stars AND :max_natural_stars
  236. ";
  237. }
  238. if (!$this->filters["IN_STORAGE"]){
  239. $query .= "
  240. AND unit.building <> :storage_building
  241. ";
  242. }
  243. if (! $this->filters["WITHOUT_RUNES"]){
  244. $query .= "
  245. AND unit.id IN (SELECT DISTINCT unit FROM unit_rune WHERE rta = :rta)
  246. ";
  247. }
  248. if (! $this->filters["FAMILY"]){
  249. $query .= "
  250. 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))
  251. ";
  252. // Special case: Eirgar (Dark Vampire lord). Nat5 can be powered with 4 star Vampires.
  253. if ($this->filters["MIN_NATURAL_STARS"] >= 5){
  254. $query .= " AND monster.id <> 19114 AND monster.id <> 19104 ";
  255. }
  256. // Special case: Fran (Light Fairy Queen). Nat3 can be powered with 2 star Fairies.
  257. if ($this->filters["MIN_NATURAL_STARS"] >= 3){
  258. $query .= " AND monster.id <> 23015 AND monster.id <> 23005 ";
  259. }
  260. }
  261. $query .= " GROUP BY unit.id ";
  262. if (! $this->filters["MAXED"]){
  263. $query .= " HAVING ponderated_lvl < ponderated_max_lvl ";
  264. }
  265. $query .= "
  266. ORDER BY
  267. CAST(ponderated_lvl * 100 AS FLOAT) / CAST(ponderated_max_lvl * 100 AS FLOAT),
  268. unit.stars DESC,
  269. unit.level DESC,
  270. CAST(lvl AS FLOAT) / CAST(max_lvl AS FLOAT),
  271. max_lvl - lvl DESC
  272. ";
  273. $statement = get_context()->get_db()->prepare($query);
  274. for ($i = 0; $i < 4; $i ++){
  275. $statement->bindValue(":ponderation_$i", $this->filters["PONDERATION"][$i], SQLITE3_FLOAT);
  276. }
  277. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  278. $statement->bindValue(":min_stars", $this->filters["MIN_STARS"], SQLITE3_INTEGER);
  279. $statement->bindValue(":max_stars", $this->filters["MAX_STARS"], SQLITE3_INTEGER);
  280. $statement->bindValue(":min_natural_stars", $this->filters["MIN_NATURAL_STARS"], SQLITE3_INTEGER);
  281. $statement->bindValue(":max_natural_stars", $this->filters["MAX_NATURAL_STARS"], SQLITE3_INTEGER);
  282. $statement->bindValue(":storage_building", BUILDING_ID::MONSTER_STORAGE, SQLITE3_INTEGER);
  283. $statement->bindValue(":rta", get_context()->get_game_mode(), SQLITE3_INTEGER);
  284. return $statement;
  285. }
  286. /**
  287. * Retrieves the selected units, sorted by urgency.
  288. *
  289. * @return Unit[] Selected units.
  290. */
  291. public function get_units(){
  292. return $this->units;
  293. }
  294. /**
  295. * Retrieves the page filters.
  296. *
  297. * @return mixed[] Page filtes
  298. */
  299. public function get_filters(){
  300. return $this->filters;
  301. }
  302. /**
  303. * Retrieves one filter.
  304. *
  305. * @param string $key
  306. * @return mixed Filter value, null if if doesn't exist.
  307. */
  308. public function get_filter($key){
  309. if (array_key_exists($key, $this->filters)){
  310. return $this->filters[$key];
  311. }
  312. else{
  313. return null;
  314. }
  315. }
  316. }
  317. ?>