Report_Runeupgrade_Page.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * Upgradeable runes report page model.
  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 . "Custom_Filter.php");
  14. require_once(PATH::ENTITY . "Unit.php");
  15. require_once(PATH::ENTITY . "Rune.php");
  16. /**
  17. * Upgradeable runes report page model.
  18. *
  19. * @category Page
  20. */
  21. class Report_Runeupgrade_Page extends Page{
  22. /**
  23. * @var \Filter[] Custom filters for this page.
  24. */
  25. public $custom_filters = [];
  26. /**
  27. * @var mixed[] List of available upgrades.
  28. *
  29. * Can be described as:
  30. * $upgrades = [
  31. * [
  32. * unit (Unit),
  33. * upgrade = [
  34. * [
  35. * rune (Rune),
  36. * alternative = [(Rune)]
  37. * ]
  38. * ]
  39. * ]
  40. * ]
  41. */
  42. public $upgrades = [];
  43. /**
  44. * Constructor.
  45. *
  46. * Retrieves the data and initializes the variables.
  47. *
  48. * @param resource $db Connection to the database.
  49. * @global int Player ID.
  50. */
  51. public function __construct($db){
  52. global $UID;
  53. parent::__construct($db);
  54. $this->view = PATH::VIEW . "report_runeupgrade.php";
  55. $this->parse_filters();
  56. if (!isset($_GET["custom_filter"])){
  57. $s = $this->build_query();
  58. }
  59. else{
  60. $s = $this->build_custom_query($_GET["custom_filter"]);
  61. }
  62. $q = $this->db->query($s);
  63. $prev_monster = "";
  64. $prev_rune = "";
  65. $upgrade = null;
  66. $rupgrade = null;
  67. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  68. if ($r["unit"] != $prev_monster){
  69. if ($prev_rune != ""){
  70. array_push($upgrade["upgrade"], $rupgrade);
  71. }
  72. if ($prev_monster != ""){
  73. array_push($this->upgrades, $upgrade);
  74. }
  75. $prev_monster = $r["unit"];
  76. $prev_rune = "";
  77. $upgrade = [
  78. "unit" => new Unit($this->db, $r["unit"]),
  79. "upgrade" => [],
  80. ];
  81. $rupgrade = [
  82. "rune" => new Rune($this->db, $r["id"]),
  83. "alternative" => [],
  84. ];
  85. }
  86. if ($r["id"] != $prev_rune){
  87. if ($prev_rune != ""){
  88. array_push($upgrade["upgrade"], $rupgrade);
  89. }
  90. $prev_rune = $r["id"];
  91. $rupgrade = [
  92. "rune" => new Rune($this->db, $r["id"]),
  93. "alternative" => [],
  94. ];
  95. }
  96. $rune_alt = new Rune($this->db, $r["alternative"]);
  97. // If the alternative rune isassigned
  98. if (strlen($rune_alt->assigned_to) > 0){
  99. $rune_alt->assigned_to = new Unit($this->db, $rune_alt->assigned_to);
  100. }
  101. array_push($rupgrade["alternative"], $rune_alt);
  102. }
  103. // Last entries
  104. array_push($upgrade["upgrade"], $rupgrade);
  105. array_push($this->upgrades, $upgrade);
  106. // Get custom filters for the form
  107. $s = "
  108. SELECT id
  109. FROM filter
  110. WHERE
  111. uid = '$UID' AND
  112. page = 'REPORT_RUNEUPGRADE';
  113. ";
  114. $q = $this->db->query($s);
  115. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  116. array_push($this->custom_filters, new Custom_Filter($this->db, $r["id"]));
  117. }
  118. $this->title = "Replaceable runes - SWDB";
  119. $this->description = "Find equiped runes that can be substituted for better ones.";
  120. $this->canonical = URL::BASE . "report/runeupgrade/";
  121. }
  122. /**
  123. * Parses the filters passed in the request and sets $filters.
  124. * Filters must be in $_GET, nd the available ones are max_stars (1-6),
  125. * min_stars (1-6) and in_storage (on/off).
  126. */
  127. private function parse_filters(){
  128. $this->filters = FILTER::RUNEUPGRADE;
  129. // Monster
  130. if (isset($_GET["monster_min_stars"]) && intval($_GET["monster_min_stars"]) > 0 && intval($_GET["monster_min_stars"]) < 7){
  131. $this->filters["MONSTER_MIN_STARS"] = $_GET["monster_min_stars"];
  132. }
  133. if (isset($_GET["monster_max_stars"]) && intval($_GET["monster_max_stars"]) > 0 && intval($_GET["monster_max_stars"]) < 7){
  134. $this->filters["MONSTER_MAX_STARS"] = $_GET["monster_max_stars"];
  135. }
  136. if (isset($_GET["monster_in_storage"]) && $_GET["monster_in_storage"] == "on"){
  137. $this->filters["MONSTER_IN_STORAGE"] = true;
  138. }
  139. else{
  140. $this->filters["MONSTER_IN_STORAGE"] = false;
  141. }
  142. if (isset($_GET["monster_teams"]) && $_GET["monster_teams"] == "on"){
  143. $this->filters["MONSTER_TEAMS"] = true;
  144. }
  145. else{
  146. $this->filters["MONSTER_TEAMS"] = false;
  147. }
  148. if (isset($_GET["monster_name"]) && strlen($_GET["monster_name"]) > 0){
  149. $this->filters["MONSTER_NAME"] = SQLite3::escapeString($_GET["monster_name"]);
  150. }
  151. // Runes
  152. if (isset($_GET["rune_min_stars"]) && intval($_GET["rune_min_stars"]) > 0 && intval($_GET["rune_min_stars"]) < 7){
  153. $this->filters["RUNE_MIN_STARS"] = $_GET["rune_min_stars"];
  154. }
  155. if (isset($_GET["rune_max_stars"]) && intval($_GET["rune_max_stars"]) > 0 && intval($_GET["rune_max_stars"]) < 7){
  156. $this->filters["RUNE_MAX_STARS"] = $_GET["rune_max_stars"];
  157. }
  158. if (isset($_GET["rune_min_level"]) && intval($_GET["rune_min_level"]) >= 0 && intval($_GET["rune_min_level"]) <= 15){
  159. $this->filters["RUNE_MIN_LEVEL"] = $_GET["rune_min_level"];
  160. }
  161. if (isset($_GET["rune_max_level"]) && intval($_GET["rune_max_level"]) >= 0 && intval($_GET["rune_max_level"]) <= 15){
  162. $this->filters["RUNE_MAX_LEVEL"] = $_GET["rune_max_level"];
  163. }
  164. if (isset($_GET["rune_min_quality"]) && intval($_GET["rune_min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_min_quality"]) <= QUALITY_ID::LEGEND){
  165. $this->filters["RUNE_MIN_QUALITY"] = $_GET["rune_min_quality"];
  166. }
  167. if (isset($_GET["rune_max_quality"]) && intval($_GET["rune_max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_max_quality"]) <= QUALITY_ID::LEGEND){
  168. $this->filters["RUNE_MAX_QUALITY"] = $_GET["rune_max_quality"];
  169. }
  170. if (isset($_GET["rune_min_original_quality"]) && intval($_GET["rune_min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_min_original_quality"]) <= QUALITY_ID::LEGEND){
  171. $this->filters["RUNE_MIN_ORIGINAL_QUALITY"] = $_GET["rune_min_original_quality"];
  172. }
  173. if (isset($_GET["rune_max_original_quality"]) && intval($_GET["rune_max_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["rune_max_original_quality"]) <= QUALITY_ID::LEGEND){
  174. $this->filters["RUNE_MAX_ORIGINAL_QUALITY"] = $_GET["rune_max_original_quality"];
  175. }
  176. if (isset($_GET["rune_min_efficiency"]) && intval($_GET["rune_min_efficiency"]) >= 0 && intval($_GET["rune_min_efficiency"]) <= 100){
  177. $this->filters["RUNE_MIN_EFFICIENCY"] = $_GET["rune_min_efficiency"];
  178. }
  179. if (isset($_GET["rune_max_efficiency"]) && intval($_GET["rune_max_efficiency"]) >= 0 && intval($_GET["rune_max_efficiency"]) <= 100){
  180. $this->filters["RUNE_MAX_EFFICIENCY"] = $_GET["rune_max_efficiency"];
  181. }
  182. if (isset($_GET["rune_min_max_efficiency"]) && intval($_GET["rune_min_max_efficiency"]) >= 0 && intval($_GET["rune_min_max_efficiency"]) <= 100){
  183. $this->filters["RUNE_MIN_MAX_EFFICIENCY"] = $_GET["rune_min_max_efficiency"];
  184. }
  185. if (isset($_GET["rune_max_max_efficiency"]) && intval($_GET["rune_max_max_efficiency"]) >= 0 && intval($_GET["rune_max_max_efficiency"]) <= 100){
  186. $this->filters["RUNE_MAX_MAX_EFFICIENCY"] = $_GET["rune_max_max_efficiency"];
  187. }
  188. if (isset($_GET["rune_slot"])){
  189. $slots = $_GET["rune_slot"];
  190. foreach ($slots as $slot){
  191. if (intval($slot) >= 1 && intval($slot) < 7){
  192. array_push($this->filters["RUNE_SLOT"], intval($slot));
  193. }
  194. }
  195. }
  196. // Alternatives
  197. if (isset($_GET["alternative_min_stars"]) && intval($_GET["alternative_min_stars"]) > 0 && intval($_GET["alternative_min_stars"]) < 7){
  198. $this->filters["ALTERNATIVE_MIN_STARS"] = $_GET["alternative_min_stars"];
  199. }
  200. if (isset($_GET["alternative_min_level"]) && intval($_GET["alternative_min_level"]) >= 0 && intval($_GET["alternative_min_level"]) <= 15){
  201. $this->filters["ALTERNATIVE_MIN_LEVEL"] = $_GET["alternative_min_level"];
  202. }
  203. if (isset($_GET["alternative_min_original_quality"]) && intval($_GET["alternative_min_original_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["alternative_min_original_quality"]) <= QUALITY_ID::LEGEND){
  204. $this->filters["ALTERNATIVE_MIN_ORIGINAL_QUALITY"] = $_GET["alternative_min_original_quality"];
  205. }
  206. if (isset($_GET["alternative_condition"]) && intval($_GET["alternative_condition"]) >= 0 && intval($_GET["alternative_condition"]) <= QUALITY_ID::LEGEND){
  207. $this->filters["ALTERNATIVE_CONDITION"] = $_GET["alternative_condition"];
  208. }
  209. if (isset($_GET["alternative_source"]) && intval($_GET["alternative_source"]) >= 0 && intval($_GET["alternative_source"]) <= 2){
  210. $this->filters["ALTERNATIVE_SOURCE"] = $_GET["alternative_source"];
  211. }
  212. return;
  213. }
  214. /**
  215. * Builds the query to the rune table using the a custom filter.
  216. *
  217. * @param int $filter Custom filter ID.
  218. * @return string The query to be executed.
  219. * @global int Player ID.
  220. */
  221. private function build_custom_query($filter){
  222. global $UID;
  223. $s = "
  224. SELECT condition
  225. FROM filter
  226. WHERE
  227. uid = '$UID' AND
  228. id = $filter;
  229. ";
  230. $q = $this->db->query($s);
  231. $r = $q->fetchArray(SQLITE3_ASSOC);
  232. $cond = $r["condition"];
  233. $s = "
  234. SELECT
  235. unit.id AS unit,
  236. rune.id AS id,
  237. rune_alt.id AS alternative
  238. FROM
  239. rune,
  240. rune rune_alt,
  241. unit,
  242. k_unit
  243. WHERE
  244. unit.uid = '$UID' AND
  245. rune.uid = '$UID' AND
  246. rune_alt.uid = '$UID' AND
  247. unit.id = rune.assigned_to AND
  248. unit.unit = k_unit.id AND
  249. rune.type = rune_alt.type AND
  250. rune.slot = rune_alt.slot AND
  251. rune.main_stat = rune_alt.main_stat AND
  252. rune.id <> rune_alt.id AND
  253. (
  254. $cond
  255. )
  256. ORDER BY
  257. (SELECT COUNT(DISTINCT(area_type || area)) FROM team, team_unit WHERE team.id = team_unit.team AND team_unit.unit = unit.id AND team.uid = '$UID') DESC,
  258. unit.stars DESC,
  259. unit.level DESC,
  260. unit.id,
  261. rune.slot,
  262. id,
  263. rune_alt.stars DESC,
  264. rune_alt.original_quality DESC,
  265. rune_alt.main_stat_value DESC;
  266. ";
  267. return $s;
  268. }
  269. /**
  270. * Builds the query for the page, using the providded or default filters.
  271. *
  272. * @return string The query to be executed.
  273. * @global int Player ID.
  274. */
  275. private function build_query(){
  276. global $UID;
  277. $s = "
  278. SELECT
  279. unit.id AS unit,
  280. rune.id AS id,
  281. rune_alt.id AS alternative
  282. FROM
  283. rune,
  284. rune rune_alt,
  285. unit,
  286. k_unit
  287. WHERE
  288. unit.uid = '$UID' AND
  289. rune.uid = '$UID' AND
  290. rune_alt.uid = '$UID' AND
  291. unit.id = rune.assigned_to AND
  292. unit.unit = k_unit.id AND
  293. unit.stars >= " . $this->filters["MONSTER_MIN_STARS"] . " AND
  294. unit.stars <= " . $this->filters["MONSTER_MAX_STARS"] . " AND
  295. rune.stars >= " . $this->filters["RUNE_MIN_STARS"] . " AND
  296. rune.stars <= " . $this->filters["RUNE_MAX_STARS"] . " AND
  297. rune.level >= " . $this->filters["RUNE_MIN_LEVEL"] . " AND
  298. rune.level <= " . $this->filters["RUNE_MAX_LEVEL"] . " AND
  299. rune.quality >= " . $this->filters["RUNE_MIN_QUALITY"] . " AND
  300. rune.quality <= " . $this->filters["RUNE_MAX_QUALITY"] . " AND
  301. rune.original_quality >= " . $this->filters["RUNE_MIN_ORIGINAL_QUALITY"] . " AND
  302. rune.original_quality <= " . $this->filters["RUNE_MAX_ORIGINAL_QUALITY"] . " AND
  303. rune.efficiency >= " . $this->filters["RUNE_MIN_EFFICIENCY"] . " AND
  304. rune.efficiency <= " . $this->filters["RUNE_MAX_EFFICIENCY"] . " AND
  305. rune.max_efficiency >= " . $this->filters["RUNE_MIN_MAX_EFFICIENCY"] . " AND
  306. rune.max_efficiency <= " . $this->filters["RUNE_MAX_MAX_EFFICIENCY"] . " AND
  307. rune.type = rune_alt.type AND
  308. rune.slot = rune_alt.slot AND
  309. rune.main_stat = rune_alt.main_stat AND
  310. rune.id <> rune_alt.id AND
  311. rune_alt.stars >= " . $this->filters["ALTERNATIVE_MIN_STARS"] . " AND
  312. rune_alt.level >= " . $this->filters["ALTERNATIVE_MIN_LEVEL"] . " AND
  313. rune_alt.original_quality >= " . $this->filters["ALTERNATIVE_MIN_ORIGINAL_QUALITY"] . " AND
  314. ";
  315. switch ($this->filters["ALTERNATIVE_SOURCE"]){
  316. case 0: // Unassigned
  317. $s = $s . " rune_alt.assigned_to IS NULL AND ";
  318. break;
  319. case 1: // Unassigned or assigned to lower star monsters
  320. $s = $s . "
  321. (
  322. rune_alt.assigned_to IS NULL OR
  323. rune_alt.assigned_to IN (SELECT id FROM unit m WHERE m.stars < unit.stars)
  324. ) AND
  325. ";
  326. break;
  327. // Case 2: Assigned or unassigned.
  328. }
  329. switch ($this->filters["ALTERNATIVE_CONDITION"]){
  330. case 0: // Higher grade, any quality
  331. $s = $s .
  332. " rune_alt.stars >= rune.stars ";
  333. break;
  334. case 1: // Higher grade or higher quality
  335. $s = $s .
  336. " ( " .
  337. " rune_alt.stars > rune.stars OR " .
  338. " rune_alt.original_quality > rune.original_quality " .
  339. " ) ";
  340. break;
  341. case 2: // Equal or higher grade and equal or higher quality
  342. $s = $s .
  343. " rune_alt.stars >= rune.stars AND " .
  344. " rune_alt.original_quality >= rune.original_quality ";
  345. break;
  346. case 3: // Equal or higher grade and higher quality
  347. $s = $s .
  348. " rune_alt.stars >= rune.stars AND " .
  349. " rune_alt.original_quality > rune.original_quality ";
  350. break;
  351. case 4: // Equal grade and equal or higher quality
  352. $s = $s .
  353. " rune_alt.stars > rune.stars AND " .
  354. " rune_alt.original_quality >= rune.original_quality ";
  355. break;
  356. default: // Case 5: Higher grade and higher quality
  357. $s = $s .
  358. " rune_alt.stars > rune.stars AND " .
  359. " rune_alt.original_quality > rune.original_quality ";
  360. break;
  361. }
  362. if (strlen($this->filters["MONSTER_NAME"]) > 0){
  363. $s = $s . " AND upper(k_unit.name) LIKE = upper('%" . $this->filters["MONSTER_NAME"] . "%') ";
  364. }
  365. if (!$this->filters["MONSTER_IN_STORAGE"]){
  366. $s = $s . " AND unit.building <> " . BUILDING_ID::MONSTER_STORAGE;
  367. }
  368. if (!$this->filters["MONSTER_TEAMS"]){
  369. $s = $s . " AND unit.id in (SELECT unit FROM team, team_unit WHERE team.id = team_unit.team AND team_unit.unit = unit.id AND team.uid = '$UID') ";
  370. }
  371. if (count($this->filters["RUNE_SLOT"]) > 0){
  372. $s = $s . " AND rune.slot IN ( ";
  373. foreach($this->filters["RUNE_SLOT"] as $t){
  374. $s = $s . "$t, ";
  375. }
  376. $s = $s . "-1) ";
  377. }
  378. $s = $s . "
  379. ORDER BY
  380. (SELECT COUNT(DISTINCT(area_type || area)) FROM team, team_unit WHERE team.id = team_unit.team AND team_unit.unit = unit.id AND team.uid = '$UID') DESC,
  381. unit.stars DESC,
  382. unit.level DESC,
  383. unit.id,
  384. rune.slot,
  385. id,
  386. rune_alt.stars DESC,
  387. rune_alt.original_quality DESC,
  388. rune_alt.main_stat_value DESC;
  389. ";
  390. return $s;
  391. }
  392. }
  393. ?>