Report_Runeupgrade_Page.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. * @global int Player ID.
  49. * @global resource Database connection.
  50. */
  51. public function __construct(){
  52. global $UID;
  53. global $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 = $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($r["unit"]),
  79. "upgrade" => [],
  80. ];
  81. $rupgrade = [
  82. "rune" => new Rune($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($r["id"]),
  93. "alternative" => [],
  94. ];
  95. }
  96. $rune_alt = new Rune($r["alternative"]);
  97. // If the alternative rune isassigned
  98. if (strlen($rune_alt->assigned_to) > 0){
  99. $rune_alt->assigned_to = new Unit($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 = $db->query($s);
  115. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  116. array_push($this->custom_filters, new Custom_Filter($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. * @global resource Database connection.
  221. */
  222. private function build_custom_query($filter){
  223. global $UID;
  224. global $db;
  225. $s = "
  226. SELECT condition
  227. FROM filter
  228. WHERE
  229. uid = '$UID' AND
  230. id = $filter;
  231. ";
  232. $q = $db->query($s);
  233. $r = $q->fetchArray(SQLITE3_ASSOC);
  234. $cond = $r["condition"];
  235. $s = "
  236. SELECT
  237. unit.id AS unit,
  238. rune.id AS id,
  239. rune_alt.id AS alternative
  240. FROM
  241. rune,
  242. rune rune_alt,
  243. unit,
  244. k_unit
  245. WHERE
  246. unit.uid = '$UID' AND
  247. rune.uid = '$UID' AND
  248. rune_alt.uid = '$UID' AND
  249. unit.id = rune.assigned_to AND
  250. unit.unit = k_unit.id AND
  251. rune.type = rune_alt.type AND
  252. rune.slot = rune_alt.slot AND
  253. rune.main_stat = rune_alt.main_stat AND
  254. rune.id <> rune_alt.id AND
  255. (
  256. $cond
  257. )
  258. ORDER BY
  259. (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,
  260. unit.stars DESC,
  261. unit.level DESC,
  262. unit.id,
  263. rune.slot,
  264. id,
  265. rune_alt.stars DESC,
  266. rune_alt.original_quality DESC,
  267. rune_alt.main_stat_value DESC;
  268. ";
  269. return $s;
  270. }
  271. /**
  272. * Builds the query for the page, using the providded or default filters.
  273. *
  274. * @return string The query to be executed.
  275. * @global int Player ID.
  276. */
  277. private function build_query(){
  278. global $UID;
  279. $s = "
  280. SELECT
  281. unit.id AS unit,
  282. rune.id AS id,
  283. rune_alt.id AS alternative
  284. FROM
  285. rune,
  286. rune rune_alt,
  287. unit,
  288. k_unit
  289. WHERE
  290. unit.uid = '$UID' AND
  291. rune.uid = '$UID' AND
  292. rune_alt.uid = '$UID' AND
  293. unit.id = rune.assigned_to AND
  294. unit.unit = k_unit.id AND
  295. unit.stars >= " . $this->filters["MONSTER_MIN_STARS"] . " AND
  296. unit.stars <= " . $this->filters["MONSTER_MAX_STARS"] . " AND
  297. rune.stars >= " . $this->filters["RUNE_MIN_STARS"] . " AND
  298. rune.stars <= " . $this->filters["RUNE_MAX_STARS"] . " AND
  299. rune.level >= " . $this->filters["RUNE_MIN_LEVEL"] . " AND
  300. rune.level <= " . $this->filters["RUNE_MAX_LEVEL"] . " AND
  301. rune.quality >= " . $this->filters["RUNE_MIN_QUALITY"] . " AND
  302. rune.quality <= " . $this->filters["RUNE_MAX_QUALITY"] . " AND
  303. rune.original_quality >= " . $this->filters["RUNE_MIN_ORIGINAL_QUALITY"] . " AND
  304. rune.original_quality <= " . $this->filters["RUNE_MAX_ORIGINAL_QUALITY"] . " AND
  305. rune.efficiency >= " . $this->filters["RUNE_MIN_EFFICIENCY"] . " AND
  306. rune.efficiency <= " . $this->filters["RUNE_MAX_EFFICIENCY"] . " AND
  307. rune.max_efficiency >= " . $this->filters["RUNE_MIN_MAX_EFFICIENCY"] . " AND
  308. rune.max_efficiency <= " . $this->filters["RUNE_MAX_MAX_EFFICIENCY"] . " AND
  309. rune.type = rune_alt.type AND
  310. rune.slot = rune_alt.slot AND
  311. rune.main_stat = rune_alt.main_stat AND
  312. rune.id <> rune_alt.id AND
  313. rune_alt.stars >= " . $this->filters["ALTERNATIVE_MIN_STARS"] . " AND
  314. rune_alt.level >= " . $this->filters["ALTERNATIVE_MIN_LEVEL"] . " AND
  315. rune_alt.original_quality >= " . $this->filters["ALTERNATIVE_MIN_ORIGINAL_QUALITY"] . " AND
  316. ";
  317. switch ($this->filters["ALTERNATIVE_SOURCE"]){
  318. case 0: // Unassigned
  319. $s = $s . " rune_alt.assigned_to IS NULL AND ";
  320. break;
  321. case 1: // Unassigned or assigned to lower star monsters
  322. $s = $s . "
  323. (
  324. rune_alt.assigned_to IS NULL OR
  325. rune_alt.assigned_to IN (SELECT id FROM unit m WHERE m.stars < unit.stars)
  326. ) AND
  327. ";
  328. break;
  329. // Case 2: Assigned or unassigned.
  330. }
  331. switch ($this->filters["ALTERNATIVE_CONDITION"]){
  332. case 0: // Higher grade, any quality
  333. $s = $s .
  334. " rune_alt.stars >= rune.stars ";
  335. break;
  336. case 1: // Higher grade or higher quality
  337. $s = $s .
  338. " ( " .
  339. " rune_alt.stars > rune.stars OR " .
  340. " rune_alt.original_quality > rune.original_quality " .
  341. " ) ";
  342. break;
  343. case 2: // Equal or higher grade and equal or higher quality
  344. $s = $s .
  345. " rune_alt.stars >= rune.stars AND " .
  346. " rune_alt.original_quality >= rune.original_quality ";
  347. break;
  348. case 3: // Equal or higher grade and higher quality
  349. $s = $s .
  350. " rune_alt.stars >= rune.stars AND " .
  351. " rune_alt.original_quality > rune.original_quality ";
  352. break;
  353. case 4: // Equal grade and equal or higher quality
  354. $s = $s .
  355. " rune_alt.stars > rune.stars AND " .
  356. " rune_alt.original_quality >= rune.original_quality ";
  357. break;
  358. default: // Case 5: Higher grade and higher quality
  359. $s = $s .
  360. " rune_alt.stars > rune.stars AND " .
  361. " rune_alt.original_quality > rune.original_quality ";
  362. break;
  363. }
  364. if (strlen($this->filters["MONSTER_NAME"]) > 0){
  365. $s = $s . " AND upper(k_unit.name) LIKE = upper('%" . $this->filters["MONSTER_NAME"] . "%') ";
  366. }
  367. if (!$this->filters["MONSTER_IN_STORAGE"]){
  368. $s = $s . " AND unit.building <> " . BUILDING_ID::MONSTER_STORAGE;
  369. }
  370. if (!$this->filters["MONSTER_TEAMS"]){
  371. $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') ";
  372. }
  373. if (count($this->filters["RUNE_SLOT"]) > 0){
  374. $s = $s . " AND rune.slot IN ( ";
  375. foreach($this->filters["RUNE_SLOT"] as $t){
  376. $s = $s . "$t, ";
  377. }
  378. $s = $s . "-1) ";
  379. }
  380. $s = $s . "
  381. ORDER BY
  382. (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,
  383. unit.stars DESC,
  384. unit.level DESC,
  385. unit.id,
  386. rune.slot,
  387. id,
  388. rune_alt.stars DESC,
  389. rune_alt.original_quality DESC,
  390. rune_alt.main_stat_value DESC;
  391. ";
  392. return $s;
  393. }
  394. }
  395. ?>